1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
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.
8 Written by Philip Hazel
9 Copyright (c) 1997-2020 University of Cambridge
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
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.
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.
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 -----------------------------------------------------------------------------
41 /* This module contains the external function pcre_compile(), along with
42 supporting internal functions that are not used by other modules. */
49 #define NLBLOCK cd /* Block containing newline information */
50 #define PSSTART start_pattern /* Field containing pattern start */
51 #define PSEND end_pattern /* Field containing pattern end */
53 #include "pcre_internal.h"
56 /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
57 is also used by pcretest. PCRE_DEBUG is not defined when building a production
58 library. We do not need to select pcre16_printint.c specially, because the
59 COMPILE_PCREx macro will already be appropriately set. */
62 /* pcre_printint.c should not include any headers */
64 #include "pcre_printint.c"
69 /* Macro for setting individual bits in class bitmaps. */
71 #define SETBIT(a,b) a[(b)/8] |= (1U << ((b)&7))
73 /* Maximum length value to check against when making sure that the integer that
74 holds the compiled pattern length does not overflow. We make it a bit less than
75 INT_MAX to allow for adding in group terminating bytes, so that we don't have
76 to check them every time. */
78 #define OFLOW_MAX (INT_MAX - 20)
80 /* Definitions to allow mutual recursion */
83 add_list_to_class(pcre_uint8
*, pcre_uchar
**, int, compile_data
*,
84 const pcre_uint32
*, unsigned int);
87 compile_regex(int, pcre_uchar
**, const pcre_uchar
**, int *, BOOL
, BOOL
, int, int,
88 pcre_uint32
*, pcre_int32
*, pcre_uint32
*, pcre_int32
*, branch_chain
*,
89 compile_data
*, int *);
93 /*************************************************
94 * Code parameters and static tables *
95 *************************************************/
97 /* This value specifies the size of stack workspace that is used during the
98 first pre-compile phase that determines how much memory is required. The regex
99 is partly compiled into this space, but the compiled parts are discarded as
100 soon as they can be, so that hopefully there will never be an overrun. The code
101 does, however, check for an overrun. The largest amount I've seen used is 218,
102 so this number is very generous.
104 The same workspace is used during the second, actual compile phase for
105 remembering forward references to groups so that they can be filled in at the
106 end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107 is 4 there is plenty of room for most patterns. However, the memory can get
108 filled up by repetitions of forward references, for example patterns like
109 /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110 that the workspace is expanded using malloc() in this situation. The value
111 below is therefore a minimum, and we put a maximum on it for safety. The
112 minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113 kicks in at the same number of forward references in all cases. */
115 #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116 #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
118 /* This value determines the size of the initial vector that is used for
119 remembering named groups during the pre-compile. It is allocated on the stack,
120 but if it is too small, it is expanded using malloc(), in a similar way to the
121 workspace. The value is the number of slots in the list. */
123 #define NAMED_GROUP_LIST_SIZE 20
125 /* The overrun tests check for a slightly smaller size so that they detect the
126 overrun before it actually does run off the end of the data block. */
128 #define WORK_SIZE_SAFETY_MARGIN (100)
130 /* Private flags added to firstchar and reqchar. */
132 #define REQ_CASELESS (1U << 0) /* Indicates caselessness */
133 #define REQ_VARY (1U << 1) /* Reqchar followed non-literal item */
134 /* Negative values for the firstchar and reqchar flags */
135 #define REQ_UNSET (-2)
136 #define REQ_NONE (-1)
138 /* Repeated character flags. */
140 #define UTF_LENGTH 0x10000000l /* The char contains its length. */
142 /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143 are simple data values; negative values are for special things like \d and so
144 on. Zero means further processing is needed (for things like \x), or the escape
149 /* This is the "normal" table for ASCII systems or for EBCDIC systems running
152 static const short int escapes
[] = {
158 CHAR_COLON
, CHAR_SEMICOLON
,
159 CHAR_LESS_THAN_SIGN
, CHAR_EQUALS_SIGN
,
160 CHAR_GREATER_THAN_SIGN
, CHAR_QUESTION_MARK
,
161 CHAR_COMMERCIAL_AT
, -ESC_A
,
174 -ESC_Z
, CHAR_LEFT_SQUARE_BRACKET
,
175 CHAR_BACKSLASH
, CHAR_RIGHT_SQUARE_BRACKET
,
176 CHAR_CIRCUMFLEX_ACCENT
, CHAR_UNDERSCORE
,
177 CHAR_GRAVE_ACCENT
, ESC_a
,
195 /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
197 static const short int escapes
[] = {
198 /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
199 /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
200 /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
201 /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
202 /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
203 /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
204 /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
205 /* 80 */ 0, ESC_a
, -ESC_b
, 0, -ESC_d
, ESC_e
, ESC_f
, 0,
206 /* 88 */-ESC_h
, 0, 0, '{', 0, 0, 0, 0,
207 /* 90 */ 0, 0, -ESC_k
, 0, 0, ESC_n
, 0, -ESC_p
,
208 /* 98 */ 0, ESC_r
, 0, '}', 0, 0, 0, 0,
209 /* A0 */ 0, '~', -ESC_s
, ESC_tee
, 0,-ESC_v
, -ESC_w
, 0,
210 /* A8 */ 0,-ESC_z
, 0, 0, 0, '[', 0, 0,
211 /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
212 /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
213 /* C0 */ '{',-ESC_A
, -ESC_B
, -ESC_C
, -ESC_D
,-ESC_E
, 0, -ESC_G
,
214 /* C8 */-ESC_H
, 0, 0, 0, 0, 0, 0, 0,
215 /* D0 */ '}', 0, -ESC_K
, 0, 0,-ESC_N
, 0, -ESC_P
,
216 /* D8 */-ESC_Q
,-ESC_R
, 0, 0, 0, 0, 0, 0,
217 /* E0 */ '\\', 0, -ESC_S
, 0, 0,-ESC_V
, -ESC_W
, -ESC_X
,
218 /* E8 */ 0,-ESC_Z
, 0, 0, 0, 0, 0, 0,
219 /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
220 /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
223 /* We also need a table of characters that may follow \c in an EBCDIC
224 environment for characters 0-31. */
226 static unsigned char ebcdic_escape_c
[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
231 /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
232 searched linearly. Put all the names into a single string, in order to reduce
233 the number of relocations when a shared library is dynamically linked. The
234 string is built from string macros so that it works in UTF-8 mode on EBCDIC
237 typedef struct verbitem
{
238 int len
; /* Length of verb name */
239 int op
; /* Op when no arg, or -1 if arg mandatory */
240 int op_arg
; /* Op when arg present, or -1 if not allowed */
243 static const char verbnames
[] =
244 "\0" /* Empty name is a shorthand for MARK */
254 static const verbitem verbs
[] = {
257 { 6, OP_ACCEPT
, -1 },
258 { 6, OP_COMMIT
, -1 },
261 { 5, OP_PRUNE
, OP_PRUNE_ARG
},
262 { 4, OP_SKIP
, OP_SKIP_ARG
},
263 { 4, OP_THEN
, OP_THEN_ARG
}
266 static const int verbcount
= sizeof(verbs
)/sizeof(verbitem
);
269 /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
270 another regex library. */
272 static const pcre_uchar sub_start_of_word
[] = {
273 CHAR_BACKSLASH
, CHAR_b
, CHAR_LEFT_PARENTHESIS
, CHAR_QUESTION_MARK
,
274 CHAR_EQUALS_SIGN
, CHAR_BACKSLASH
, CHAR_w
, CHAR_RIGHT_PARENTHESIS
, '\0' };
276 static const pcre_uchar sub_end_of_word
[] = {
277 CHAR_BACKSLASH
, CHAR_b
, CHAR_LEFT_PARENTHESIS
, CHAR_QUESTION_MARK
,
278 CHAR_LESS_THAN_SIGN
, CHAR_EQUALS_SIGN
, CHAR_BACKSLASH
, CHAR_w
,
279 CHAR_RIGHT_PARENTHESIS
, '\0' };
282 /* Substitute for implicit newline. */
284 static const pcre_uchar sub_implicit_rsb_newline
[] = {
285 CHAR_RIGHT_SQUARE_BRACKET
, CHAR_BACKSLASH
, CHAR_n
, '\0' };
288 /* Substitute for embedded literal NUL. */
290 static const pcre_uchar sub_embedded_nul
[] = {
291 CHAR_BACKSLASH
, CHAR_0
, CHAR_0
, CHAR_0
, '\0' };
294 /* Tables of names of POSIX character classes and their lengths. The names are
295 now all in a single string, to reduce the number of relocations when a shared
296 library is dynamically loaded. The list of lengths is terminated by a zero
297 length entry. The first three must be alpha, lower, upper, as this is assumed
298 for handling case independence. The indices for graph, print, and punct are
299 needed, so identify them. */
301 static const char posix_names
[] =
302 STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
303 STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
304 STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
305 STRING_word0 STRING_xdigit
;
307 static const pcre_uint8 posix_name_lengths
[] = {
308 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
315 /* Table of class bit maps for each POSIX class. Each class is formed from a
316 base map, with an optional addition or removal of another map. Then, for some
317 classes, there is some additional tweaking: for [:blank:] the vertical space
318 characters are removed, and for [:alpha:] and [:alnum:] the underscore
319 character is removed. The triples in the table consist of the base map offset,
320 second map offset or -1 if no second map, and a non-negative value for map
321 addition or a negative value for map subtraction (if there are two maps). The
322 absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
323 remove vertical space characters, 2 => remove underscore. */
325 static const int posix_class_maps
[] = {
326 cbit_word
, cbit_digit
, -2, /* alpha */
327 cbit_lower
, -1, 0, /* lower */
328 cbit_upper
, -1, 0, /* upper */
329 cbit_word
, -1, 2, /* alnum - word without underscore */
330 cbit_print
, cbit_cntrl
, 0, /* ascii */
331 cbit_space
, -1, 1, /* blank - a GNU extension */
332 cbit_cntrl
, -1, 0, /* cntrl */
333 cbit_digit
, -1, 0, /* digit */
334 cbit_graph
, -1, 0, /* graph */
335 cbit_print
, -1, 0, /* print */
336 cbit_punct
, -1, 0, /* punct */
337 cbit_space
, -1, 0, /* space */
338 cbit_word
, -1, 0, /* word - a Perl extension */
339 cbit_xdigit
,-1, 0 /* xdigit */
342 /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
343 Unicode property escapes. */
346 static const pcre_uchar string_PNd
[] = {
347 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
348 CHAR_N
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
349 static const pcre_uchar string_pNd
[] = {
350 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
351 CHAR_N
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
352 static const pcre_uchar string_PXsp
[] = {
353 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
354 CHAR_X
, CHAR_s
, CHAR_p
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
355 static const pcre_uchar string_pXsp
[] = {
356 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
357 CHAR_X
, CHAR_s
, CHAR_p
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
358 static const pcre_uchar string_PXwd
[] = {
359 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
360 CHAR_X
, CHAR_w
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
361 static const pcre_uchar string_pXwd
[] = {
362 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
363 CHAR_X
, CHAR_w
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
365 static const pcre_uchar
*substitutes
[] = {
368 string_PXsp
, /* \S */ /* Xsp is Perl space, but from 8.34, Perl */
369 string_pXsp
, /* \s */ /* space and POSIX space are the same. */
370 string_PXwd
, /* \W */
374 /* The POSIX class substitutes must be in the order of the POSIX class names,
375 defined above, and there are both positive and negative cases. NULL means no
376 general substitute of a Unicode property escape (\p or \P). However, for some
377 POSIX classes (e.g. graph, print, punct) a special property code is compiled
380 static const pcre_uchar string_pL
[] = {
381 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
382 CHAR_L
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
383 static const pcre_uchar string_pLl
[] = {
384 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
385 CHAR_L
, CHAR_l
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
386 static const pcre_uchar string_pLu
[] = {
387 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
388 CHAR_L
, CHAR_u
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
389 static const pcre_uchar string_pXan
[] = {
390 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
391 CHAR_X
, CHAR_a
, CHAR_n
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
392 static const pcre_uchar string_h
[] = {
393 CHAR_BACKSLASH
, CHAR_h
, '\0' };
394 static const pcre_uchar string_pXps
[] = {
395 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
396 CHAR_X
, CHAR_p
, CHAR_s
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
397 static const pcre_uchar string_PL
[] = {
398 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
399 CHAR_L
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
400 static const pcre_uchar string_PLl
[] = {
401 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
402 CHAR_L
, CHAR_l
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
403 static const pcre_uchar string_PLu
[] = {
404 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
405 CHAR_L
, CHAR_u
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
406 static const pcre_uchar string_PXan
[] = {
407 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
408 CHAR_X
, CHAR_a
, CHAR_n
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
409 static const pcre_uchar string_H
[] = {
410 CHAR_BACKSLASH
, CHAR_H
, '\0' };
411 static const pcre_uchar string_PXps
[] = {
412 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
413 CHAR_X
, CHAR_p
, CHAR_s
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
415 static const pcre_uchar
*posix_substitutes
[] = {
416 string_pL
, /* alpha */
417 string_pLl
, /* lower */
418 string_pLu
, /* upper */
419 string_pXan
, /* alnum */
421 string_h
, /* blank */
423 string_pNd
, /* digit */
427 string_pXps
, /* space */ /* Xps is POSIX space, but from 8.34 */
428 string_pXwd
, /* word */ /* Perl and POSIX space are the same */
431 string_PL
, /* ^alpha */
432 string_PLl
, /* ^lower */
433 string_PLu
, /* ^upper */
434 string_PXan
, /* ^alnum */
436 string_H
, /* ^blank */
438 string_PNd
, /* ^digit */
442 string_PXps
, /* ^space */ /* Xps is POSIX space, but from 8.34 */
443 string_PXwd
, /* ^word */ /* Perl and POSIX space are the same */
446 #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
449 #define STRING(a) # a
450 #define XSTRING(s) STRING(s)
452 /* The texts of compile-time error messages. These are "char *" because they
453 are passed to the outside world. Do not ever re-use any error number, because
454 they are documented. Always add a new error instead. Messages marked DEAD below
455 are no longer used. This used to be a table of strings, but in order to reduce
456 the number of relocations needed when a shared library is loaded dynamically,
457 it is now one long string. We cannot use a table of offsets, because the
458 lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
459 simply count through to the one we want - this isn't a performance issue
460 because these strings are used only when there is a compilation error.
462 Each substring ends with \0 to insert a null character. This includes the final
463 substring, so that the whole string ends with \0\0, which can be detected when
466 static const char error_texts
[] =
468 "\\ at end of pattern\0"
469 "\\c at end of pattern\0"
470 "unrecognized character follows \\\0"
471 "numbers out of order in {} quantifier\0"
473 "number too big in {} quantifier\0"
474 "missing terminating ] for character class\0"
475 "invalid escape sequence in character class\0"
476 "range out of order in character class\0"
477 "nothing to repeat\0"
479 "internal error: invalid forward reference offset\0"
480 "internal error: unexpected repeat\0"
481 "unrecognized character after (? or (?-\0"
482 "POSIX named classes are supported only within a class\0"
485 "reference to non-existent subpattern\0"
486 "erroffset passed as NULL\0"
487 "unknown option bit(s) set\0"
488 "missing ) after comment\0"
489 "parentheses nested too deeply\0" /** DEAD **/
491 "regular expression is too large\0"
492 "failed to get memory\0"
493 "unmatched parentheses\0"
494 "internal error: code overflow\0"
495 "unrecognized character after (?<\0"
497 "lookbehind assertion is not fixed length\0"
498 "malformed number or name after (?(\0"
499 "conditional group contains more than two branches\0"
500 "assertion expected after (?( or (?(?C)\0"
501 "(?R or (?[+-]digits must be followed by )\0"
503 "unknown POSIX class name\0"
504 "POSIX collating elements are not supported\0"
505 "this version of PCRE is compiled without UTF support\0"
506 "spare error\0" /** DEAD **/
507 "character value in \\x{} or \\o{} is too large\0"
509 "invalid condition (?(0)\0"
510 "\\C not allowed in lookbehind assertion\0"
511 "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
512 "number after (?C is > 255\0"
513 "closing ) for (?C expected\0"
515 "recursive call could loop indefinitely\0"
516 "unrecognized character after (?P\0"
517 "syntax error in subpattern name (missing terminator)\0"
518 "two named subpatterns have the same name\0"
519 "invalid UTF-8 string\0"
521 "support for \\P, \\p, and \\X has not been compiled\0"
522 "malformed \\P or \\p sequence\0"
523 "unknown property name after \\P or \\p\0"
524 "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE
) " characters)\0"
525 "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT
) ")\0"
527 "repeated subpattern is too long\0" /** DEAD **/
528 "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
529 "internal error: overran compiling workspace\0"
530 "internal error: previously-checked referenced subpattern not found\0"
531 "DEFINE group contains more than one branch\0"
533 "repeating a DEFINE group is not allowed\0" /** DEAD **/
534 "inconsistent NEWLINE options\0"
535 "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
536 "a numbered reference must not be zero\0"
537 "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
539 "(*VERB) not recognized or malformed\0"
540 "number is too big\0"
541 "subpattern name expected\0"
542 "digit expected after (?+\0"
543 "] is an invalid data character in JavaScript compatibility mode\0"
545 "different names for subpatterns of the same number are not allowed\0"
546 "(*MARK) must have an argument\0"
547 "this version of PCRE is not compiled with Unicode property support\0"
549 "\\c must be followed by an ASCII character\0"
551 "\\c must be followed by a letter or one of [\\]^_?\0"
553 "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
555 "internal error: unknown opcode in find_fixedlength()\0"
556 "\\N is not supported in a class\0"
557 "too many forward references\0"
558 "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
559 "invalid UTF-16 string\0"
561 "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
562 "character value in \\u.... sequence is too large\0"
563 "invalid UTF-32 string\0"
564 "setting UTF is disabled by the application\0"
565 "non-hex character in \\x{} (closing brace missing?)\0"
567 "non-octal character in \\o{} (closing brace missing?)\0"
568 "missing opening brace after \\o\0"
569 "parentheses are too deeply nested\0"
570 "invalid range in character class\0"
571 "group name must start with a non-digit\0"
573 "parentheses are too deeply nested (stack check)\0"
574 "digits missing in \\x{} or \\o{}\0"
575 "regular expression is too complicated\0"
578 /* Table to identify digits and hex digits. This is used when compiling
579 patterns. Note that the tables in chartables are dependent on the locale, and
580 may mark arbitrary characters as digits - but the PCRE compiling code expects
581 to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
582 a private table here. It costs 256 bytes, but it is a lot faster than doing
583 character value tests (at least in some simple cases I timed), and in some
584 applications one wants PCRE to compile efficiently as well as match
587 For convenience, we use the same bit definitions as in chartables:
590 0x08 hexadecimal digit
592 Then we can use ctype_digit and ctype_xdigit in the code. */
594 /* Using a simple comparison for decimal numbers rather than a memory read
595 is much faster, and the resulting code is simpler (the compiler turns it
596 into a subtraction and unsigned comparison). */
598 #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
602 /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
605 static const pcre_uint8 digitab
[] =
607 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
608 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
609 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
610 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
611 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
612 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
613 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
614 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
615 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
616 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
617 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
618 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
619 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
620 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
621 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
622 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
623 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
624 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
625 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
626 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
627 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
628 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
629 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
630 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
631 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
632 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
633 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
634 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
635 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
636 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
637 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
638 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
642 /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
644 static const pcre_uint8 digitab
[] =
646 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
647 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
648 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
649 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
650 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
651 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
652 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
653 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
654 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
655 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
656 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
657 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
658 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
659 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
660 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
661 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
662 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
663 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
664 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
665 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
666 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
667 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
668 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
669 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
670 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
671 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
672 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
673 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
674 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
675 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
676 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
677 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
679 static const pcre_uint8 ebcdic_chartab
[] = { /* chartable partial dup */
680 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
681 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
682 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
683 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
684 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
685 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
686 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
687 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
688 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
689 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
690 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
691 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
692 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
693 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
694 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
695 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
696 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
697 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
698 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
699 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
700 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
701 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
702 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
703 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
704 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
705 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
706 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
707 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
708 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
709 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
710 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
711 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
715 /* This table is used to check whether auto-possessification is possible
716 between adjacent character-type opcodes. The left-hand (repeated) opcode is
717 used to select the row, and the right-hand opcode is use to select the column.
718 A value of 1 means that auto-possessification is OK. For example, the second
719 value in the first row means that \D+\d can be turned into \D++\d.
721 The Unicode property types (\P and \p) have to be present to fill out the table
722 because of what their opcode values are, but the table values should always be
723 zero because property types are handled separately in the code. The last four
724 columns apply to items that cannot be repeated, so there is no need to have
725 rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
726 *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
728 #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
729 #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
731 static const pcre_uint8 autoposstab
[APTROWS
][APTCOLS
] = {
732 /* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */
733 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */
734 { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */
735 { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */
736 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */
737 { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */
738 { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */
739 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */
740 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */
741 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */
742 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */
743 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */
744 { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */
745 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */
746 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */
747 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */
748 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */
749 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */
753 /* This table is used to check whether auto-possessification is possible
754 between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
755 left-hand (repeated) opcode is used to select the row, and the right-hand
756 opcode is used to select the column. The values are as follows:
758 0 Always return FALSE (never auto-possessify)
759 1 Character groups are distinct (possessify if both are OP_PROP)
760 2 Check character categories in the same group (general or particular)
761 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP)
763 4 Check left general category vs right particular category
764 5 Check right general category vs left particular category
766 6 Left alphanum vs right general category
767 7 Left space vs right general category
768 8 Left word vs right general category
770 9 Right alphanum vs left general category
771 10 Right space vs left general category
772 11 Right word vs left general category
774 12 Left alphanum vs right particular category
775 13 Left space vs right particular category
776 14 Left word vs right particular category
778 15 Right alphanum vs left particular category
779 16 Right space vs left particular category
780 17 Right word vs left particular category
783 static const pcre_uint8 propposstab
[PT_TABSIZE
][PT_TABSIZE
] = {
784 /* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
785 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */
786 { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */
787 { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */
788 { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */
789 { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */
790 { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */
791 { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */
792 { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */
793 { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */
794 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */
795 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */
798 /* This table is used to check whether auto-possessification is possible
799 between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
800 specifies a general category and the other specifies a particular category. The
801 row is selected by the general category and the column by the particular
802 category. The value is 1 if the particular category is not part of the general
805 static const pcre_uint8 catposstab
[7][30] = {
806 /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
807 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */
808 { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */
809 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */
810 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */
811 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */
812 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */
813 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */
816 /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
817 a general or particular category. The properties in each row are those
818 that apply to the character set in question. Duplication means that a little
819 unnecessary work is done when checking, but this keeps things much simpler
820 because they can all use the same code. For more details see the comment where
823 Note: SPACE and PXSPACE used to be different because Perl excluded VT from
824 "space", but from Perl 5.18 it's included, so both categories are treated the
827 static const pcre_uint8 posspropstab
[3][4] = {
828 { ucp_L
, ucp_N
, ucp_N
, ucp_Nl
}, /* ALNUM, 3rd and 4th values redundant */
829 { ucp_Z
, ucp_Z
, ucp_C
, ucp_Cc
}, /* SPACE and PXSPACE, 2nd value redundant */
830 { ucp_L
, ucp_N
, ucp_P
, ucp_Po
} /* WORD */
833 /* This table is used when converting repeating opcodes into possessified
834 versions as a result of an explicit possessive quantifier such as ++. A zero
835 value means there is no possessified version - in those cases the item in
836 question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
837 because all relevant opcodes are less than that. */
839 static const pcre_uint8 opcode_possessify
[] = {
840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */
841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */
844 OP_POSSTAR
, 0, /* STAR, MINSTAR */
845 OP_POSPLUS
, 0, /* PLUS, MINPLUS */
846 OP_POSQUERY
, 0, /* QUERY, MINQUERY */
847 OP_POSUPTO
, 0, /* UPTO, MINUPTO */
849 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */
851 OP_POSSTARI
, 0, /* STARI, MINSTARI */
852 OP_POSPLUSI
, 0, /* PLUSI, MINPLUSI */
853 OP_POSQUERYI
, 0, /* QUERYI, MINQUERYI */
854 OP_POSUPTOI
, 0, /* UPTOI, MINUPTOI */
856 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */
858 OP_NOTPOSSTAR
, 0, /* NOTSTAR, NOTMINSTAR */
859 OP_NOTPOSPLUS
, 0, /* NOTPLUS, NOTMINPLUS */
860 OP_NOTPOSQUERY
, 0, /* NOTQUERY, NOTMINQUERY */
861 OP_NOTPOSUPTO
, 0, /* NOTUPTO, NOTMINUPTO */
863 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
865 OP_NOTPOSSTARI
, 0, /* NOTSTARI, NOTMINSTARI */
866 OP_NOTPOSPLUSI
, 0, /* NOTPLUSI, NOTMINPLUSI */
867 OP_NOTPOSQUERYI
, 0, /* NOTQUERYI, NOTMINQUERYI */
868 OP_NOTPOSUPTOI
, 0, /* NOTUPTOI, NOTMINUPTOI */
870 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
872 OP_TYPEPOSSTAR
, 0, /* TYPESTAR, TYPEMINSTAR */
873 OP_TYPEPOSPLUS
, 0, /* TYPEPLUS, TYPEMINPLUS */
874 OP_TYPEPOSQUERY
, 0, /* TYPEQUERY, TYPEMINQUERY */
875 OP_TYPEPOSUPTO
, 0, /* TYPEUPTO, TYPEMINUPTO */
877 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
879 OP_CRPOSSTAR
, 0, /* CRSTAR, CRMINSTAR */
880 OP_CRPOSPLUS
, 0, /* CRPLUS, CRMINPLUS */
881 OP_CRPOSQUERY
, 0, /* CRQUERY, CRMINQUERY */
882 OP_CRPOSRANGE
, 0, /* CRRANGE, CRMINRANGE */
883 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */
885 0, 0, 0, /* CLASS, NCLASS, XCLASS */
886 0, 0, /* REF, REFI */
887 0, 0, /* DNREF, DNREFI */
888 0, 0 /* RECURSE, CALLOUT */
893 /*************************************************
894 * Find an error text *
895 *************************************************/
897 /* The error texts are now all in one long string, to save on relocations. As
898 some of the text is of unknown length, we can't use a table of offsets.
899 Instead, just count through the strings. This is not a performance issue
900 because it happens only when there has been a compilation error.
902 Argument: the error number
903 Returns: pointer to the error string
907 find_error_text(int n
)
909 const char *s
= error_texts
;
912 while (*s
++ != CHAR_NULL
) {};
913 if (*s
== CHAR_NULL
) return "Error text not found (please report)";
920 /*************************************************
921 * Expand the workspace *
922 *************************************************/
924 /* This function is called during the second compiling phase, if the number of
925 forward references fills the existing workspace, which is originally a block on
926 the stack. A larger block is obtained from malloc() unless the ultimate limit
927 has been reached or the increase will be rather small.
929 Argument: pointer to the compile data block
930 Returns: 0 if all went well, else an error number
934 expand_workspace(compile_data
*cd
)
936 pcre_uchar
*newspace
;
937 int newsize
= cd
->workspace_size
* 2;
939 if (newsize
> COMPILE_WORK_SIZE_MAX
) newsize
= COMPILE_WORK_SIZE_MAX
;
940 if (cd
->workspace_size
>= COMPILE_WORK_SIZE_MAX
||
941 newsize
- cd
->workspace_size
< WORK_SIZE_SAFETY_MARGIN
)
944 newspace
= (PUBL(malloc
))(IN_UCHARS(newsize
));
945 if (newspace
== NULL
) return ERR21
;
946 memcpy(newspace
, cd
->start_workspace
, cd
->workspace_size
* sizeof(pcre_uchar
));
947 cd
->hwm
= (pcre_uchar
*)newspace
+ (cd
->hwm
- cd
->start_workspace
);
948 if (cd
->workspace_size
> COMPILE_WORK_SIZE
)
949 (PUBL(free
))((void *)cd
->start_workspace
);
950 cd
->start_workspace
= newspace
;
951 cd
->workspace_size
= newsize
;
957 /*************************************************
958 * Check for counted repeat *
959 *************************************************/
961 /* This function is called when a '{' is encountered in a place where it might
962 start a quantifier. It looks ahead to see if it really is a quantifier or not.
963 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
964 where the ddds are digits.
967 p pointer to the first char after '{'
969 Returns: TRUE or FALSE
973 is_counted_repeat(const pcre_uchar
*p
, BOOL basicre
)
975 if (!IS_DIGIT(*p
)) return FALSE
;
977 while (IS_DIGIT(*p
)) p
++;
978 if ((*p
== CHAR_RIGHT_CURLY_BRACKET
&& !basicre
) ||
979 (basicre
&& *p
== CHAR_BACKSLASH
&& p
[1] == CHAR_RIGHT_CURLY_BRACKET
))
982 if (*p
++ != CHAR_COMMA
) return FALSE
;
983 if ((*p
== CHAR_RIGHT_CURLY_BRACKET
&& !basicre
) ||
984 (basicre
&& *p
== CHAR_BACKSLASH
&& p
[1] == CHAR_RIGHT_CURLY_BRACKET
))
987 if (!IS_DIGIT(*p
)) return FALSE
;
989 while (IS_DIGIT(*p
)) p
++;
991 return ((*p
== CHAR_RIGHT_CURLY_BRACKET
&& !basicre
) ||
992 (basicre
&& *p
== CHAR_BACKSLASH
&& p
[1] == CHAR_RIGHT_CURLY_BRACKET
));
997 /*************************************************
999 *************************************************/
1001 /* This function is called when a \ has been encountered. It either returns a
1002 positive value for a simple escape such as \n, or 0 for a data character which
1003 will be placed in chptr. A backreference to group n is returned as negative n.
1004 When UTF-8 is enabled, a positive value greater than 255 may be returned in
1005 chptr. On entry, ptr is pointing at the \. On exit, it is on the final
1006 character of the escape sequence.
1009 ptrptr points to the pattern position pointer
1010 chptr points to a returned data character
1011 errorcodeptr points to the errorcode variable
1012 bracount number of previous extracting brackets
1013 options the options bits
1014 isclass TRUE if inside a character class
1016 Returns: zero => a data character
1017 positive => a special escape sequence
1018 negative => a back reference
1019 on error, errorcodeptr is set
1023 check_escape(const pcre_uchar
**ptrptr
, pcre_uint32
*chptr
, int *errorcodeptr
,
1024 int bracount
, int options
, BOOL isclass
)
1026 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
1027 BOOL utf
= (options
& PCRE_UTF8
) != 0;
1028 const pcre_uchar
*ptr
= *ptrptr
+ 1;
1033 GETCHARINCTEST(c
, ptr
); /* Get character value, increment pointer */
1034 ptr
--; /* Set pointer back to the last byte */
1036 /* If backslash is at the end of the pattern, it's an error. */
1038 if (c
== CHAR_NULL
) *errorcodeptr
= ERR1
;
1040 /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
1041 in a table. A non-zero result is something that can be returned immediately.
1042 Otherwise further processing may be required. */
1044 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1045 /* Not alphanumeric */
1046 else if (c
< CHAR_0
|| c
> CHAR_z
) {}
1047 else if ((i
= escapes
[c
- CHAR_0
]) != 0)
1048 { if (i
> 0) c
= (pcre_uint32
)i
; else escape
= -i
; }
1050 #else /* EBCDIC coding */
1051 /* Not alphanumeric */
1052 else if (c
< CHAR_a
|| (!MAX_255(c
) || (ebcdic_chartab
[c
] & 0x0E) == 0)) {}
1053 else if ((i
= escapes
[c
- 0x48]) != 0) { if (i
> 0) c
= (pcre_uint32
)i
; else escape
= -i
; }
1056 /* Escapes that need further processing, or are illegal. */
1060 const pcre_uchar
*oldptr
;
1061 BOOL braced
, negated
, overflow
;
1066 /* A number of Perl escapes are not handled by PCRE. We give an explicit
1071 *errorcodeptr
= ERR37
;
1075 if ((options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
1077 /* In JavaScript, \u must be followed by four hexadecimal numbers.
1078 Otherwise it is a lowercase u letter. */
1079 if (MAX_255(ptr
[1]) && (digitab
[ptr
[1]] & ctype_xdigit
) != 0
1080 && MAX_255(ptr
[2]) && (digitab
[ptr
[2]] & ctype_xdigit
) != 0
1081 && MAX_255(ptr
[3]) && (digitab
[ptr
[3]] & ctype_xdigit
) != 0
1082 && MAX_255(ptr
[4]) && (digitab
[ptr
[4]] & ctype_xdigit
) != 0)
1085 for (i
= 0; i
< 4; ++i
)
1087 register pcre_uint32 cc
= *(++ptr
);
1088 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1089 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1090 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1091 #else /* EBCDIC coding */
1092 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1093 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1097 #if defined COMPILE_PCRE8
1098 if (c
> (utf
? 0x10ffffU
: 0xffU
))
1099 #elif defined COMPILE_PCRE16
1100 if (c
> (utf
? 0x10ffffU
: 0xffffU
))
1101 #elif defined COMPILE_PCRE32
1102 if (utf
&& c
> 0x10ffffU
)
1105 *errorcodeptr
= ERR76
;
1107 else if (utf
&& c
>= 0xd800 && c
<= 0xdfff) *errorcodeptr
= ERR73
;
1111 *errorcodeptr
= ERR37
;
1115 /* In JavaScript, \U is an uppercase U letter. */
1116 if ((options
& PCRE_JAVASCRIPT_COMPAT
) == 0) *errorcodeptr
= ERR37
;
1119 /* In a character class, \g is just a literal "g". Outside a character
1120 class, \g must be followed by one of a number of specific things:
1122 (1) A number, either plain or braced. If positive, it is an absolute
1123 backreference. If negative, it is a relative backreference. This is a Perl
1126 (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
1127 is part of Perl's movement towards a unified syntax for back references. As
1128 this is synonymous with \k{name}, we fudge it up by pretending it really
1131 (3) For Oniguruma compatibility we also support \g followed by a name or a
1132 number either in angle brackets or in single quotes. However, these are
1133 (possibly recursive) subroutine calls, _not_ backreferences. Just return
1134 the ESC_g code (cf \k). */
1138 if (ptr
[1] == CHAR_LESS_THAN_SIGN
|| ptr
[1] == CHAR_APOSTROPHE
)
1144 /* Handle the Perl-compatible cases */
1146 if (ptr
[1] == CHAR_LEFT_CURLY_BRACKET
)
1148 const pcre_uchar
*p
;
1149 for (p
= ptr
+2; *p
!= CHAR_NULL
&& *p
!= CHAR_RIGHT_CURLY_BRACKET
; p
++)
1150 if (*p
!= CHAR_MINUS
&& !IS_DIGIT(*p
)) break;
1151 if (*p
!= CHAR_NULL
&& *p
!= CHAR_RIGHT_CURLY_BRACKET
)
1159 else braced
= FALSE
;
1161 if (ptr
[1] == CHAR_MINUS
)
1166 else negated
= FALSE
;
1168 /* The integer range is limited by the machine's int representation. */
1171 while (IS_DIGIT(ptr
[1]))
1173 if (s
> INT_MAX
/ 10 - 1) /* Integer overflow */
1178 s
= s
* 10 + (int)(*(++ptr
) - CHAR_0
);
1180 if (overflow
) /* Integer overflow */
1182 while (IS_DIGIT(ptr
[1]))
1184 *errorcodeptr
= ERR61
;
1188 if (braced
&& *(++ptr
) != CHAR_RIGHT_CURLY_BRACKET
)
1190 *errorcodeptr
= ERR57
;
1196 *errorcodeptr
= ERR58
;
1204 *errorcodeptr
= ERR15
;
1207 s
= bracount
- (s
- 1);
1213 /* The handling of escape sequences consisting of a string of digits
1214 starting with one that is not zero is not straightforward. Perl has changed
1215 over the years. Nowadays \g{} for backreferences and \o{} for octal are
1216 recommended to avoid the ambiguities in the old syntax.
1218 Outside a character class, the digits are read as a decimal number. If the
1219 number is less than 8 (used to be 10), or if there are that many previous
1220 extracting left brackets, then it is a back reference. Otherwise, up to
1221 three octal digits are read to form an escaped byte. Thus \123 is likely to
1222 be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1223 the octal value is greater than 377, the least significant 8 bits are
1224 taken. \8 and \9 are treated as the literal characters 8 and 9.
1226 Inside a character class, \ followed by a digit is always either a literal
1227 8 or 9 or an octal number. */
1229 case CHAR_1
: case CHAR_2
: case CHAR_3
: case CHAR_4
: case CHAR_5
:
1230 case CHAR_6
: case CHAR_7
: case CHAR_8
: case CHAR_9
:
1235 /* The integer range is limited by the machine's int representation. */
1236 s
= (int)(c
-CHAR_0
);
1238 while (IS_DIGIT(ptr
[1]))
1240 if (s
> INT_MAX
/ 10 - 1) /* Integer overflow */
1245 s
= s
* 10 + (int)(*(++ptr
) - CHAR_0
);
1247 if (overflow
) /* Integer overflow */
1249 while (IS_DIGIT(ptr
[1]))
1251 *errorcodeptr
= ERR61
;
1254 if (s
< 8 || s
<= bracount
) /* Check for back reference */
1259 ptr
= oldptr
; /* Put the pointer back and fall through */
1262 /* Handle a digit following \ when the number is not a back reference. If
1263 the first digit is 8 or 9, Perl used to generate a binary zero byte and
1264 then treat the digit as a following literal. At least by Perl 5.18 this
1265 changed so as not to insert the binary zero. */
1267 if ((c
= *ptr
) >= CHAR_8
) break;
1269 /* Fall through with a digit less than 8 */
1271 /* \0 always starts an octal number, but we may drop through to here with a
1272 larger first octal digit. The original code used just to take the least
1273 significant 8 bits of octal numbers (I think this is what early Perls used
1274 to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1275 but no more than 3 octal digits. */
1279 while(i
++ < 2 && ptr
[1] >= CHAR_0
&& ptr
[1] <= CHAR_7
)
1280 c
= c
* 8 + *(++ptr
) - CHAR_0
;
1281 #ifdef COMPILE_PCRE8
1282 if (!utf
&& c
> 0xff) *errorcodeptr
= ERR51
;
1286 /* \o is a relatively new Perl feature, supporting a more general way of
1287 specifying character codes in octal. The only supported form is \o{ddd}. */
1290 if (ptr
[1] != CHAR_LEFT_CURLY_BRACKET
) *errorcodeptr
= ERR81
; else
1291 if (ptr
[2] == CHAR_RIGHT_CURLY_BRACKET
) *errorcodeptr
= ERR86
; else
1296 while (*ptr
>= CHAR_0
&& *ptr
<= CHAR_7
)
1298 register pcre_uint32 cc
= *ptr
++;
1299 if (c
== 0 && cc
== CHAR_0
) continue; /* Leading zeroes */
1300 #ifdef COMPILE_PCRE32
1301 if (c
>= 0x20000000l
) { overflow
= TRUE
; break; }
1303 c
= (c
<< 3) + cc
- CHAR_0
;
1304 #if defined COMPILE_PCRE8
1305 if (c
> (utf
? 0x10ffffU
: 0xffU
)) { overflow
= TRUE
; break; }
1306 #elif defined COMPILE_PCRE16
1307 if (c
> (utf
? 0x10ffffU
: 0xffffU
)) { overflow
= TRUE
; break; }
1308 #elif defined COMPILE_PCRE32
1309 if (utf
&& c
> 0x10ffffU
) { overflow
= TRUE
; break; }
1314 while (*ptr
>= CHAR_0
&& *ptr
<= CHAR_7
) ptr
++;
1315 *errorcodeptr
= ERR34
;
1317 else if (*ptr
== CHAR_RIGHT_CURLY_BRACKET
)
1319 if (utf
&& c
>= 0xd800 && c
<= 0xdfff) *errorcodeptr
= ERR73
;
1321 else *errorcodeptr
= ERR80
;
1325 /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1326 numbers. Otherwise it is a lowercase x letter. */
1329 if ((options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
1331 if (MAX_255(ptr
[1]) && (digitab
[ptr
[1]] & ctype_xdigit
) != 0
1332 && MAX_255(ptr
[2]) && (digitab
[ptr
[2]] & ctype_xdigit
) != 0)
1335 for (i
= 0; i
< 2; ++i
)
1337 register pcre_uint32 cc
= *(++ptr
);
1338 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1339 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1340 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1341 #else /* EBCDIC coding */
1342 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1343 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1347 } /* End JavaScript handling */
1349 /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1350 greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1351 digits. If not, { used to be treated as a data character. However, Perl
1352 seems to read hex digits up to the first non-such, and ignore the rest, so
1353 that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1354 now gives an error. */
1358 if (ptr
[1] == CHAR_LEFT_CURLY_BRACKET
)
1361 if (*ptr
== CHAR_RIGHT_CURLY_BRACKET
)
1363 *errorcodeptr
= ERR86
;
1368 while (MAX_255(*ptr
) && (digitab
[*ptr
] & ctype_xdigit
) != 0)
1370 register pcre_uint32 cc
= *ptr
++;
1371 if (c
== 0 && cc
== CHAR_0
) continue; /* Leading zeroes */
1373 #ifdef COMPILE_PCRE32
1374 if (c
>= 0x10000000l
) { overflow
= TRUE
; break; }
1377 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1378 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1379 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1380 #else /* EBCDIC coding */
1381 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1382 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1385 #if defined COMPILE_PCRE8
1386 if (c
> (utf
? 0x10ffffU
: 0xffU
)) { overflow
= TRUE
; break; }
1387 #elif defined COMPILE_PCRE16
1388 if (c
> (utf
? 0x10ffffU
: 0xffffU
)) { overflow
= TRUE
; break; }
1389 #elif defined COMPILE_PCRE32
1390 if (utf
&& c
> 0x10ffffU
) { overflow
= TRUE
; break; }
1396 while (MAX_255(*ptr
) && (digitab
[*ptr
] & ctype_xdigit
) != 0) ptr
++;
1397 *errorcodeptr
= ERR34
;
1400 else if (*ptr
== CHAR_RIGHT_CURLY_BRACKET
)
1402 if (utf
&& c
>= 0xd800 && c
<= 0xdfff) *errorcodeptr
= ERR73
;
1405 /* If the sequence of hex digits does not end with '}', give an error.
1406 We used just to recognize this construct and fall through to the normal
1407 \x handling, but nowadays Perl gives an error, which seems much more
1408 sensible, so we do too. */
1410 else *errorcodeptr
= ERR79
;
1411 } /* End of \x{} processing */
1413 /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1418 while (i
++ < 2 && MAX_255(ptr
[1]) && (digitab
[ptr
[1]] & ctype_xdigit
) != 0)
1420 pcre_uint32 cc
; /* Some compilers don't like */
1421 cc
= *(++ptr
); /* ++ in initializers */
1422 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1423 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1424 c
= c
* 16 + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1425 #else /* EBCDIC coding */
1426 if (cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1427 c
= c
* 16 + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1430 } /* End of \xdd handling */
1431 } /* End of Perl-style \x handling */
1434 /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1435 An error is given if the byte following \c is not an ASCII character. This
1436 coding is ASCII-specific, but then the whole concept of \cx is
1437 ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1443 *errorcodeptr
= ERR2
;
1446 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1447 if (c
> 127) /* Excludes all non-ASCII in either mode */
1449 *errorcodeptr
= ERR68
;
1452 if (c
>= CHAR_a
&& c
<= CHAR_z
) c
-= 32;
1454 #else /* EBCDIC coding */
1455 if (c
>= CHAR_a
&& c
<= CHAR_z
) c
+= 64;
1456 if (c
== CHAR_QUESTION_MARK
)
1457 c
= ('\\' == 188 && '`' == 74)? 0x5f : 0xff;
1460 for (i
= 0; i
< 32; i
++)
1462 if (c
== ebcdic_escape_c
[i
]) break;
1464 if (i
< 32) c
= i
; else *errorcodeptr
= ERR68
;
1469 /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1470 other alphanumeric following \ is an error if PCRE_EXTRA was set;
1471 otherwise, for Perl compatibility, it is a literal. This code looks a bit
1472 odd, but there used to be some cases other than the default, and there may
1473 be again in future, so I haven't "optimized" it. */
1476 if ((options
& PCRE_EXTRA
) != 0) switch(c
)
1479 *errorcodeptr
= ERR3
;
1486 /* Perl supports \N{name} for character names, as well as plain \N for "not
1487 newline". PCRE does not support \N{name}. However, it does support
1488 quantification such as \N{2,3}. */
1490 if (escape
== ESC_N
&& ptr
[1] == CHAR_LEFT_CURLY_BRACKET
&&
1491 !is_counted_repeat(ptr
+2, FALSE
))
1492 *errorcodeptr
= ERR37
;
1494 /* If PCRE_UCP is set, we change the values for \d etc. */
1496 if ((options
& PCRE_UCP
) != 0 && escape
>= ESC_D
&& escape
<= ESC_w
)
1497 escape
+= (ESC_DU
- ESC_D
);
1499 /* Set the pointer to the final character before returning. */
1509 /*************************************************
1510 * Handle \P and \p *
1511 *************************************************/
1513 /* This function is called after \P or \p has been encountered, provided that
1514 PCRE is compiled with support for Unicode properties. On entry, ptrptr is
1515 pointing at the P or p. On exit, it is pointing at the final character of the
1519 ptrptr points to the pattern position pointer
1520 negptr points to a boolean that is set TRUE for negation else FALSE
1521 ptypeptr points to an unsigned int that is set to the type value
1522 pdataptr points to an unsigned int that is set to the detailed property value
1523 errorcodeptr points to the error code variable
1525 Returns: TRUE if the type value was found, or FALSE for an invalid type
1529 get_ucp(const pcre_uchar
**ptrptr
, BOOL
*negptr
, unsigned int *ptypeptr
,
1530 unsigned int *pdataptr
, int *errorcodeptr
)
1534 const pcre_uchar
*ptr
= *ptrptr
;
1535 pcre_uchar name
[32];
1538 if (c
== CHAR_NULL
) goto ERROR_RETURN
;
1542 /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1545 if (c
== CHAR_LEFT_CURLY_BRACKET
)
1547 if (ptr
[1] == CHAR_CIRCUMFLEX_ACCENT
)
1552 for (i
= 0; i
< (int)(sizeof(name
) / sizeof(pcre_uchar
)) - 1; i
++)
1555 if (c
== CHAR_NULL
) goto ERROR_RETURN
;
1556 if (c
== CHAR_RIGHT_CURLY_BRACKET
) break;
1559 if (c
!= CHAR_RIGHT_CURLY_BRACKET
) goto ERROR_RETURN
;
1563 /* Otherwise there is just one following character */
1573 /* Search for a recognized property name using binary chop */
1576 top
= PRIV(utt_size
);
1581 i
= (bot
+ top
) >> 1;
1582 r
= STRCMP_UC_C8(name
, PRIV(utt_names
) + PRIV(utt
)[i
].name_offset
);
1585 *ptypeptr
= PRIV(utt
)[i
].type
;
1586 *pdataptr
= PRIV(utt
)[i
].value
;
1589 if (r
> 0) bot
= i
+ 1; else top
= i
;
1592 *errorcodeptr
= ERR47
;
1597 *errorcodeptr
= ERR46
;
1605 /*************************************************
1606 * Read repeat counts *
1607 *************************************************/
1609 /* Read an item of the form {n,m} and return the values. This is called only
1610 after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
1611 so the syntax is guaranteed to be correct, but we need to check the values.
1614 p pointer to first char after '{'
1615 minp pointer to int for min
1616 maxp pointer to int for max
1617 returned as -1 if no max
1618 errorcodeptr points to error code variable
1620 Returns: pointer to '}' on success;
1621 current ptr on error, with errorcodeptr set non-zero
1624 static const pcre_uchar
*
1625 read_repeat_counts(const pcre_uchar
*p
, int *minp
, int *maxp
, int *errorcodeptr
)
1630 while (IS_DIGIT(*p
))
1632 min
= min
* 10 + (int)(*p
++ - CHAR_0
);
1635 *errorcodeptr
= ERR5
;
1640 if (*p
== CHAR_BACKSLASH
) ++p
;
1641 if (*p
== CHAR_RIGHT_CURLY_BRACKET
) max
= min
; else
1643 if (*(++p
) != CHAR_RIGHT_CURLY_BRACKET
&& *p
!= CHAR_BACKSLASH
)
1648 max
= max
* 10 + (int)(*p
++ - CHAR_0
);
1651 *errorcodeptr
= ERR5
;
1657 *errorcodeptr
= ERR4
;
1661 if (*p
== CHAR_BACKSLASH
) ++p
;
1671 /*************************************************
1672 * Find first significant op code *
1673 *************************************************/
1675 /* This is called by several functions that scan a compiled expression looking
1676 for a fixed first character, or an anchoring op code etc. It skips over things
1677 that do not influence this. For some calls, it makes sense to skip negative
1678 forward and all backward assertions, and also the \b assertion; for others it
1682 code pointer to the start of the group
1683 skipassert TRUE if certain assertions are to be skipped
1685 Returns: pointer to the first significant opcode
1688 static const pcre_uchar
*
1689 first_significant_code(const pcre_uchar
*code
, BOOL skipassert
)
1697 case OP_ASSERTBACK_NOT
:
1698 if (!skipassert
) return code
;
1699 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
1700 code
+= PRIV(OP_lengths
)[*code
];
1703 case OP_WORD_BOUNDARY
:
1704 case OP_NOT_WORD_BOUNDARY
:
1705 if (!skipassert
) return code
;
1714 code
+= PRIV(OP_lengths
)[*code
];
1721 /* Control never reaches here */
1726 /*************************************************
1727 * Find the fixed length of a branch *
1728 *************************************************/
1730 /* Scan a branch and compute the fixed length of subject that will match it,
1731 if the length is fixed. This is needed for dealing with backward assertions.
1732 In UTF8 mode, the result is in characters rather than bytes. The branch is
1733 temporarily terminated with OP_END when this function is called.
1735 This function is called when a backward assertion is encountered, so that if it
1736 fails, the error message can point to the correct place in the pattern.
1737 However, we cannot do this when the assertion contains subroutine calls,
1738 because they can be forward references. We solve this by remembering this case
1739 and doing the check at the end; a flag specifies which mode we are running in.
1742 code points to the start of the pattern (the bracket)
1743 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1744 atend TRUE if called when the pattern is complete
1745 cd the "compile data" structure
1746 recurses chain of recurse_check to catch mutual recursion
1748 Returns: the fixed length,
1749 or -1 if there is no fixed length,
1750 or -2 if \C was encountered (in UTF-8 mode only)
1751 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1752 or -4 if an unknown opcode was encountered (internal error)
1756 find_fixedlength(pcre_uchar
*code
, BOOL utf
, BOOL atend
, compile_data
*cd
,
1757 recurse_check
*recurses
)
1760 recurse_check this_recurse
;
1761 register int branchlength
= 0;
1762 register pcre_uchar
*cc
= code
+ 1 + LINK_SIZE
;
1764 /* Scan along the opcodes for this branch. If we get to the end of the
1765 branch, check the length against that of the other branches. */
1770 pcre_uchar
*ce
, *cs
;
1771 register pcre_uchar op
= *cc
;
1775 /* We only need to continue for OP_CBRA (normal capturing bracket) and
1776 OP_BRA (normal non-capturing bracket) because the other variants of these
1777 opcodes are all concerned with unlimited repeated groups, which of course
1778 are not of fixed length. */
1785 d
= find_fixedlength(cc
+ ((op
== OP_CBRA
)? IMM2_SIZE
: 0), utf
, atend
, cd
,
1787 if (d
< 0) return d
;
1789 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
1790 cc
+= 1 + LINK_SIZE
;
1793 /* Reached end of a branch; if it's a ket it is the end of a nested call.
1794 If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1795 an ALT. If it is END it's the end of the outer call. All can be handled by
1796 the same code. Note that we must not include the OP_KETRxxx opcodes here,
1797 because they all imply an unlimited repeat. */
1803 case OP_ASSERT_ACCEPT
:
1804 if (length
< 0) length
= branchlength
;
1805 else if (length
!= branchlength
) return -1;
1806 if (*cc
!= OP_ALT
) return length
;
1807 cc
+= 1 + LINK_SIZE
;
1811 /* A true recursion implies not fixed length, but a subroutine call may
1812 be OK. If the subroutine is a forward reference, we can't deal with
1813 it until the end of the pattern, so return -3. */
1816 if (!atend
) return -3;
1817 cs
= ce
= (pcre_uchar
*)cd
->start_code
+ GET(cc
, 1); /* Start subpattern */
1818 do ce
+= GET(ce
, 1); while (*ce
== OP_ALT
); /* End subpattern */
1819 if (cc
> cs
&& cc
< ce
) return -1; /* Recursion */
1820 else /* Check for mutual recursion */
1822 recurse_check
*r
= recurses
;
1823 for (r
= recurses
; r
!= NULL
; r
= r
->prev
) if (r
->group
== cs
) break;
1824 if (r
!= NULL
) return -1; /* Mutual recursion */
1826 this_recurse
.prev
= recurses
;
1827 this_recurse
.group
= cs
;
1828 d
= find_fixedlength(cs
+ IMM2_SIZE
, utf
, atend
, cd
, &this_recurse
);
1829 if (d
< 0) return d
;
1831 cc
+= 1 + LINK_SIZE
;
1834 /* Skip over assertive subpatterns */
1839 case OP_ASSERTBACK_NOT
:
1840 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
1841 cc
+= 1 + LINK_SIZE
;
1844 /* Skip over things that don't match chars */
1850 cc
+= cc
[1] + PRIV(OP_lengths
)[*cc
];
1867 case OP_NOT_WORD_BOUNDARY
:
1876 case OP_WORD_BOUNDARY
:
1877 cc
+= PRIV(OP_lengths
)[*cc
];
1880 /* Handle literal characters */
1889 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
1893 /* Handle exact repetitions. The count is already in characters, but we
1894 need to skip over a multibyte character in UTF8 mode. */
1900 branchlength
+= (int)GET2(cc
,1);
1901 cc
+= 2 + IMM2_SIZE
;
1903 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
1908 branchlength
+= GET2(cc
,1);
1909 if (cc
[1 + IMM2_SIZE
] == OP_PROP
|| cc
[1 + IMM2_SIZE
] == OP_NOTPROP
)
1911 cc
+= 1 + IMM2_SIZE
+ 1;
1914 /* Handle single-char matchers */
1927 case OP_NOT_WHITESPACE
:
1929 case OP_NOT_WORDCHAR
:
1937 /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1938 otherwise \C is coded as OP_ALLANY. */
1943 /* Check a class for variable quantification */
1947 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1949 /* The original code caused an unsigned overflow in 64 bit systems,
1950 so now we use a conditional statement. */
1951 if (op
== OP_XCLASS
)
1954 cc
+= PRIV(OP_lengths
)[OP_CLASS
];
1956 cc
+= PRIV(OP_lengths
)[OP_CLASS
];
1975 if (GET2(cc
,1) != GET2(cc
,1+IMM2_SIZE
)) return -1;
1976 branchlength
+= (int)GET2(cc
,1);
1977 cc
+= 1 + 2 * IMM2_SIZE
;
1985 /* Anything else is variable length */
2006 case OP_NOTMINPLUSI
:
2007 case OP_NOTMINQUERY
:
2008 case OP_NOTMINQUERYI
:
2010 case OP_NOTMINSTARI
:
2012 case OP_NOTMINUPTOI
:
2016 case OP_NOTPOSPLUSI
:
2017 case OP_NOTPOSQUERY
:
2018 case OP_NOTPOSQUERYI
:
2020 case OP_NOTPOSSTARI
:
2022 case OP_NOTPOSUPTOI
:
2053 case OP_TYPEMINPLUS
:
2054 case OP_TYPEMINQUERY
:
2055 case OP_TYPEMINSTAR
:
2056 case OP_TYPEMINUPTO
:
2058 case OP_TYPEPOSPLUS
:
2059 case OP_TYPEPOSQUERY
:
2060 case OP_TYPEPOSSTAR
:
2061 case OP_TYPEPOSUPTO
:
2069 /* Catch unrecognized opcodes so that when new ones are added they
2070 are not forgotten, as has happened in the past. */
2076 /* Control never gets here */
2081 /*************************************************
2082 * Scan compiled regex for specific bracket *
2083 *************************************************/
2085 /* This little function scans through a compiled pattern until it finds a
2086 capturing bracket with the given number, or, if the number is negative, an
2087 instance of OP_REVERSE for a lookbehind. The function is global in the C sense
2088 so that it can be called from pcre_study() when finding the minimum matching
2092 code points to start of expression
2093 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
2094 number the required bracket number or negative to find a lookbehind
2096 Returns: pointer to the opcode for the bracket, or NULL if not found
2100 PRIV(find_bracket
)(const pcre_uchar
*code
, BOOL utf
, int number
)
2104 register pcre_uchar c
= *code
;
2106 if (c
== OP_END
) return NULL
;
2108 /* XCLASS is used for classes that cannot be represented just by a bit
2109 map. This includes negated single high-valued characters. The length in
2110 the table is zero; the actual length is stored in the compiled code. */
2112 if (c
== OP_XCLASS
) code
+= GET(code
, 1);
2114 /* Handle recursion */
2116 else if (c
== OP_REVERSE
)
2118 if (number
< 0) return (pcre_uchar
*)code
;
2119 code
+= PRIV(OP_lengths
)[c
];
2122 /* Handle capturing bracket */
2124 else if (c
== OP_CBRA
|| c
== OP_SCBRA
||
2125 c
== OP_CBRAPOS
|| c
== OP_SCBRAPOS
)
2127 int n
= (int)GET2(code
, 1+LINK_SIZE
);
2128 if (n
== number
) return (pcre_uchar
*)code
;
2129 code
+= PRIV(OP_lengths
)[c
];
2132 /* Otherwise, we can get the item's length from the table, except that for
2133 repeated character types, we have to test for \p and \P, which have an extra
2134 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2135 must add in its length. */
2142 case OP_TYPEMINSTAR
:
2144 case OP_TYPEMINPLUS
:
2146 case OP_TYPEMINQUERY
:
2147 case OP_TYPEPOSSTAR
:
2148 case OP_TYPEPOSPLUS
:
2149 case OP_TYPEPOSQUERY
:
2150 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2154 case OP_TYPEMINUPTO
:
2156 case OP_TYPEPOSUPTO
:
2157 if (code
[1 + IMM2_SIZE
] == OP_PROP
|| code
[1 + IMM2_SIZE
] == OP_NOTPROP
)
2169 /* Add in the fixed length from the table */
2171 code
+= PRIV(OP_lengths
)[c
];
2173 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2174 a multi-byte character. The length in the table is a minimum, so we have to
2175 arrange to skip the extra bytes. */
2177 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2195 case OP_NOTMINUPTOI
:
2199 case OP_NOTPOSUPTOI
:
2207 case OP_NOTMINSTARI
:
2211 case OP_NOTPOSSTARI
:
2219 case OP_NOTMINPLUSI
:
2223 case OP_NOTPOSPLUSI
:
2230 case OP_NOTMINQUERY
:
2231 case OP_NOTMINQUERYI
:
2234 case OP_NOTPOSQUERY
:
2235 case OP_NOTPOSQUERYI
:
2236 if (HAS_EXTRALEN(code
[-1])) code
+= GET_EXTRALEN(code
[-1]);
2240 (void)(utf
); /* Keep compiler happy by referencing function argument */
2248 /*************************************************
2249 * Scan compiled regex for recursion reference *
2250 *************************************************/
2252 /* This little function scans through a compiled pattern until it finds an
2253 instance of OP_RECURSE.
2256 code points to start of expression
2257 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
2259 Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
2262 static const pcre_uchar
*
2263 find_recurse(const pcre_uchar
*code
, BOOL utf
)
2267 register pcre_uchar c
= *code
;
2268 if (c
== OP_END
) return NULL
;
2269 if (c
== OP_RECURSE
) return code
;
2271 /* XCLASS is used for classes that cannot be represented just by a bit
2272 map. This includes negated single high-valued characters. The length in
2273 the table is zero; the actual length is stored in the compiled code. */
2275 if (c
== OP_XCLASS
) code
+= GET(code
, 1);
2277 /* Otherwise, we can get the item's length from the table, except that for
2278 repeated character types, we have to test for \p and \P, which have an extra
2279 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2280 must add in its length. */
2287 case OP_TYPEMINSTAR
:
2289 case OP_TYPEMINPLUS
:
2291 case OP_TYPEMINQUERY
:
2292 case OP_TYPEPOSSTAR
:
2293 case OP_TYPEPOSPLUS
:
2294 case OP_TYPEPOSQUERY
:
2295 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2298 case OP_TYPEPOSUPTO
:
2300 case OP_TYPEMINUPTO
:
2302 if (code
[1 + IMM2_SIZE
] == OP_PROP
|| code
[1 + IMM2_SIZE
] == OP_NOTPROP
)
2314 /* Add in the fixed length from the table */
2316 code
+= PRIV(OP_lengths
)[c
];
2318 /* In UTF-8 mode, opcodes that are followed by a character may be followed
2319 by a multi-byte character. The length in the table is a minimum, so we have
2320 to arrange to skip the extra bytes. */
2322 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2340 case OP_NOTMINUPTOI
:
2344 case OP_NOTPOSUPTOI
:
2352 case OP_NOTMINSTARI
:
2356 case OP_NOTPOSSTARI
:
2364 case OP_NOTMINPLUSI
:
2368 case OP_NOTPOSPLUSI
:
2375 case OP_NOTMINQUERY
:
2376 case OP_NOTMINQUERYI
:
2379 case OP_NOTPOSQUERY
:
2380 case OP_NOTPOSQUERYI
:
2381 if (HAS_EXTRALEN(code
[-1])) code
+= GET_EXTRALEN(code
[-1]);
2385 (void)(utf
); /* Keep compiler happy by referencing function argument */
2393 /*************************************************
2394 * Scan compiled branch for non-emptiness *
2395 *************************************************/
2397 /* This function scans through a branch of a compiled pattern to see whether it
2398 can match the empty string or not. It is called from could_be_empty()
2399 below and from compile_branch() when checking for an unlimited repeat of a
2400 group that can match nothing. Note that first_significant_code() skips over
2401 backward and negative forward assertions when its final argument is TRUE. If we
2402 hit an unclosed bracket, we return "empty" - this means we've struck an inner
2403 bracket whose current branch will already have been scanned.
2406 code points to start of search
2407 endcode points to where to stop
2408 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2409 cd contains pointers to tables etc.
2410 recurses chain of recurse_check to catch mutual recursion
2412 Returns: TRUE if what is matched could be empty
2416 could_be_empty_branch(const pcre_uchar
*code
, const pcre_uchar
*endcode
,
2417 BOOL utf
, compile_data
*cd
, recurse_check
*recurses
)
2419 register pcre_uchar c
;
2420 recurse_check this_recurse
;
2422 for (code
= first_significant_code(code
+ PRIV(OP_lengths
)[*code
], TRUE
);
2424 code
= first_significant_code(code
+ PRIV(OP_lengths
)[c
], TRUE
))
2426 const pcre_uchar
*ccode
;
2430 /* Skip over forward assertions; the other assertions are skipped by
2431 first_significant_code() with a TRUE final argument. */
2435 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2440 /* For a recursion/subroutine call, if its end has been reached, which
2441 implies a backward reference subroutine call, we can scan it. If it's a
2442 forward reference subroutine call, we can't. To detect forward reference
2443 we have to scan up the list that is kept in the workspace. This function is
2444 called only when doing the real compile, not during the pre-compile that
2445 measures the size of the compiled pattern. */
2447 if (c
== OP_RECURSE
)
2449 const pcre_uchar
*scode
= cd
->start_code
+ GET(code
, 1);
2450 const pcre_uchar
*endgroup
= scode
;
2453 /* Test for forward reference or uncompleted reference. This is disabled
2454 when called to scan a completed pattern by setting cd->start_workspace to
2457 if (cd
->start_workspace
!= NULL
)
2459 const pcre_uchar
*tcode
;
2460 for (tcode
= cd
->start_workspace
; tcode
< cd
->hwm
; tcode
+= LINK_SIZE
)
2461 if ((int)GET(tcode
, 0) == (int)(code
+ 1 - cd
->start_code
)) return TRUE
;
2462 if (GET(scode
, 1) == 0) return TRUE
; /* Unclosed */
2465 /* If the reference is to a completed group, we need to detect whether this
2466 is a recursive call, as otherwise there will be an infinite loop. If it is
2467 a recursion, just skip over it. Simple recursions are easily detected. For
2468 mutual recursions we keep a chain on the stack. */
2470 do endgroup
+= GET(endgroup
, 1); while (*endgroup
== OP_ALT
);
2471 if (code
>= scode
&& code
<= endgroup
) continue; /* Simple recursion */
2474 recurse_check
*r
= recurses
;
2475 for (r
= recurses
; r
!= NULL
; r
= r
->prev
)
2476 if (r
->group
== scode
) break;
2477 if (r
!= NULL
) continue; /* Mutual recursion */
2480 /* Completed reference; scan the referenced group, remembering it on the
2481 stack chain to detect mutual recursions. */
2483 empty_branch
= FALSE
;
2484 this_recurse
.prev
= recurses
;
2485 this_recurse
.group
= scode
;
2489 if (could_be_empty_branch(scode
, endcode
, utf
, cd
, &this_recurse
))
2491 empty_branch
= TRUE
;
2494 scode
+= GET(scode
, 1);
2496 while (*scode
== OP_ALT
);
2498 if (!empty_branch
) return FALSE
; /* All branches are non-empty */
2502 /* Groups with zero repeats can of course be empty; skip them. */
2504 if (c
== OP_BRAZERO
|| c
== OP_BRAMINZERO
|| c
== OP_SKIPZERO
||
2507 code
+= PRIV(OP_lengths
)[c
];
2508 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2513 /* A nested group that is already marked as "could be empty" can just be
2516 if (c
== OP_SBRA
|| c
== OP_SBRAPOS
||
2517 c
== OP_SCBRA
|| c
== OP_SCBRAPOS
)
2519 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2524 /* For other groups, scan the branches. */
2526 if (c
== OP_BRA
|| c
== OP_BRAPOS
||
2527 c
== OP_CBRA
|| c
== OP_CBRAPOS
||
2528 c
== OP_ONCE
|| c
== OP_ONCE_NC
||
2529 c
== OP_COND
|| c
== OP_SCOND
)
2532 if (GET(code
, 1) == 0) return TRUE
; /* Hit unclosed bracket */
2534 /* If a conditional group has only one branch, there is a second, implied,
2535 empty branch, so just skip over the conditional, because it could be empty.
2536 Otherwise, scan the individual branches of the group. */
2538 if (c
== OP_COND
&& code
[GET(code
, 1)] != OP_ALT
)
2539 code
+= GET(code
, 1);
2542 empty_branch
= FALSE
;
2545 if (!empty_branch
&& could_be_empty_branch(code
, endcode
, utf
, cd
,
2546 recurses
)) empty_branch
= TRUE
;
2547 code
+= GET(code
, 1);
2549 while (*code
== OP_ALT
);
2550 if (!empty_branch
) return FALSE
; /* All branches are non-empty */
2557 /* Handle the other opcodes */
2561 /* Check for quantifiers after a class. XCLASS is used for classes that
2562 cannot be represented just by a bit map. This includes negated single
2563 high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2564 actual length is stored in the compiled code, so we must update "code"
2567 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2569 ccode
= code
+= GET(code
, 1);
2570 goto CHECK_CLASS_REPEAT
;
2575 ccode
= code
+ PRIV(OP_lengths
)[OP_CLASS
];
2577 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2583 case OP_CRSTAR
: /* These could be empty; continue */
2591 default: /* Non-repeat => class must match */
2592 case OP_CRPLUS
: /* These repeats aren't empty */
2600 if (GET2(ccode
, 1) > 0) return FALSE
; /* Minimum > 0 */
2605 /* Opcodes that must match a character */
2623 case OP_NOT_WHITESPACE
:
2625 case OP_NOT_WORDCHAR
:
2641 case OP_NOTMINPLUSI
:
2646 case OP_NOTPOSPLUSI
:
2654 case OP_TYPEMINPLUS
:
2655 case OP_TYPEPOSPLUS
:
2660 /* These are going to continue, as they may be empty, but we have to
2661 fudge the length for the \p and \P cases. */
2664 case OP_TYPEMINSTAR
:
2665 case OP_TYPEPOSSTAR
:
2667 case OP_TYPEMINQUERY
:
2668 case OP_TYPEPOSQUERY
:
2669 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2672 /* Same for these */
2675 case OP_TYPEMINUPTO
:
2676 case OP_TYPEPOSUPTO
:
2677 if (code
[1 + IMM2_SIZE
] == OP_PROP
|| code
[1 + IMM2_SIZE
] == OP_NOTPROP
)
2690 /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2691 MINUPTO, and POSUPTO and their caseless and negative versions may be
2692 followed by a multibyte character. */
2694 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2703 case OP_NOTMINSTARI
:
2708 case OP_NOTPOSSTARI
:
2717 case OP_NOTMINQUERY
:
2718 case OP_NOTMINQUERYI
:
2722 case OP_NOTPOSQUERY
:
2723 case OP_NOTPOSQUERYI
:
2725 if (utf
&& HAS_EXTRALEN(code
[1])) code
+= GET_EXTRALEN(code
[1]);
2736 case OP_NOTMINUPTOI
:
2741 case OP_NOTPOSUPTOI
:
2743 if (utf
&& HAS_EXTRALEN(code
[1 + IMM2_SIZE
])) code
+= GET_EXTRALEN(code
[1 + IMM2_SIZE
]);
2747 /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2757 /* None of the remaining opcodes are required to match a character. */
2769 /*************************************************
2770 * Scan compiled regex for non-emptiness *
2771 *************************************************/
2773 /* This function is called to check for left recursive calls. We want to check
2774 the current branch of the current pattern to see if it could match the empty
2775 string. If it could, we must look outwards for branches at other levels,
2776 stopping when we pass beyond the bracket which is the subject of the recursion.
2777 This function is called only during the real compile, not during the
2781 code points to start of the recursion
2782 endcode points to where to stop (current RECURSE item)
2783 bcptr points to the chain of current (unclosed) branch starts
2784 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2785 cd pointers to tables etc
2787 Returns: TRUE if what is matched could be empty
2791 could_be_empty(const pcre_uchar
*code
, const pcre_uchar
*endcode
,
2792 branch_chain
*bcptr
, BOOL utf
, compile_data
*cd
)
2794 while (bcptr
!= NULL
&& bcptr
->current_branch
>= code
)
2796 if (!could_be_empty_branch(bcptr
->current_branch
, endcode
, utf
, cd
, NULL
))
2798 bcptr
= bcptr
->outer
;
2805 /*************************************************
2806 * Base opcode of repeated opcodes *
2807 *************************************************/
2809 /* Returns the base opcode for repeated single character type opcodes. If the
2810 opcode is not a repeated character type, it returns with the original value.
2813 Returns: base opcode for the type
2817 get_repeat_base(pcre_uchar c
)
2819 return (c
> OP_TYPEPOSUPTO
)? c
:
2820 (c
>= OP_TYPESTAR
)? OP_TYPESTAR
:
2821 (c
>= OP_NOTSTARI
)? OP_NOTSTARI
:
2822 (c
>= OP_NOTSTAR
)? OP_NOTSTAR
:
2823 (c
>= OP_STARI
)? OP_STARI
:
2830 /*************************************************
2831 * Check a character and a property *
2832 *************************************************/
2834 /* This function is called by check_auto_possessive() when a property item
2835 is adjacent to a fixed character.
2839 ptype the property type
2840 pdata the data for the type
2841 negated TRUE if it's a negated property (\P or \p{^)
2843 Returns: TRUE if auto-possessifying is OK
2847 check_char_prop(pcre_uint32 c
, unsigned int ptype
, unsigned int pdata
,
2850 const pcre_uint32
*p
;
2851 const ucd_record
*prop
= GET_UCD(c
);
2856 return (prop
->chartype
== ucp_Lu
||
2857 prop
->chartype
== ucp_Ll
||
2858 prop
->chartype
== ucp_Lt
) == negated
;
2861 return (pdata
== PRIV(ucp_gentype
)[prop
->chartype
]) == negated
;
2864 return (pdata
== prop
->chartype
) == negated
;
2867 return (pdata
== prop
->script
) == negated
;
2869 /* These are specials */
2872 return (PRIV(ucp_gentype
)[prop
->chartype
] == ucp_L
||
2873 PRIV(ucp_gentype
)[prop
->chartype
] == ucp_N
) == negated
;
2875 /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2876 means that Perl space and POSIX space are now identical. PCRE was changed
2879 case PT_SPACE
: /* Perl space */
2880 case PT_PXSPACE
: /* POSIX space */
2888 return (PRIV(ucp_gentype
)[prop
->chartype
] == ucp_Z
) == negated
;
2890 break; /* Control never reaches here */
2893 return (PRIV(ucp_gentype
)[prop
->chartype
] == ucp_L
||
2894 PRIV(ucp_gentype
)[prop
->chartype
] == ucp_N
||
2895 c
== CHAR_UNDERSCORE
) == negated
;
2898 p
= PRIV(ucd_caseless_sets
) + prop
->caseset
;
2901 if (c
< *p
) return !negated
;
2902 if (c
== *p
++) return negated
;
2904 break; /* Control never reaches here */
2909 #endif /* SUPPORT_UCP */
2913 /*************************************************
2914 * Fill the character property list *
2915 *************************************************/
2917 /* Checks whether the code points to an opcode that can take part in auto-
2918 possessification, and if so, fills a list with its properties.
2921 code points to start of expression
2922 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2923 fcc points to case-flipping table
2924 list points to output list
2925 list[0] will be filled with the opcode
2926 list[1] will be non-zero if this opcode
2927 can match an empty character string
2928 list[2..7] depends on the opcode
2930 Returns: points to the start of the next opcode if *code is accepted
2931 NULL if *code is not accepted
2934 static const pcre_uchar
*
2935 get_chr_property_list(const pcre_uchar
*code
, BOOL utf
,
2936 const pcre_uint8
*fcc
, pcre_uint32
*list
)
2938 pcre_uchar c
= *code
;
2940 const pcre_uchar
*end
;
2944 pcre_uint32
*clist_dest
;
2945 const pcre_uint32
*clist_src
;
2947 utf
= utf
; /* Suppress "unused parameter" compiler warning */
2954 if (c
>= OP_STAR
&& c
<= OP_TYPEPOSUPTO
)
2956 base
= get_repeat_base(c
);
2957 c
-= (base
- OP_STAR
);
2959 if (c
== OP_UPTO
|| c
== OP_MINUPTO
|| c
== OP_EXACT
|| c
== OP_POSUPTO
)
2962 list
[1] = (c
!= OP_PLUS
&& c
!= OP_MINPLUS
&& c
!= OP_EXACT
&& c
!= OP_POSPLUS
);
2994 case OP_NOT_WHITESPACE
:
2996 case OP_NOT_WORDCHAR
:
3014 GETCHARINCTEST(chr
, code
);
3021 list
[0] = (c
== OP_CHARI
) ? OP_CHAR
: OP_NOT
;
3022 GETCHARINCTEST(chr
, code
);
3026 if (chr
< 128 || (chr
< 256 && !utf
))
3029 list
[3] = UCD_OTHERCASE(chr
);
3030 #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
3031 list
[3] = (chr
< 256) ? fcc
[chr
] : chr
;
3036 /* The othercase might be the same value. */
3047 if (code
[0] != PT_CLIST
)
3054 /* Convert only if we have enough space. */
3056 clist_src
= PRIV(ucd_caseless_sets
) + code
[1];
3057 clist_dest
= list
+ 2;
3061 if (clist_dest
>= list
+ 8)
3063 /* Early return if there is not enough space. This should never
3064 happen, since all clists are shorter than 5 character now. */
3069 *clist_dest
++ = *clist_src
;
3071 while(*clist_src
++ != NOTACHAR
);
3073 /* All characters are stored. The terminating NOTACHAR
3074 is copied form the clist itself. */
3076 list
[0] = (c
== OP_PROP
) ? OP_CHAR
: OP_NOT
;
3082 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3085 end
= code
+ GET(code
, 0) - 1;
3088 end
= code
+ 32 / sizeof(pcre_uchar
);
3111 list
[1] = (GET2(end
, 1) == 0);
3112 end
+= 1 + 2 * IMM2_SIZE
;
3115 list
[2] = (pcre_uint32
)(end
- code
);
3118 return NULL
; /* Opcode not accepted */
3123 /*************************************************
3124 * Scan further character sets for match *
3125 *************************************************/
3127 /* Checks whether the base and the current opcode have a common character, in
3128 which case the base cannot be possessified.
3131 code points to the byte code
3132 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
3133 cd static compile data
3134 base_list the data list of the base opcode
3136 Returns: TRUE if the auto-possessification is possible
3140 compare_opcodes(const pcre_uchar
*code
, BOOL utf
, const compile_data
*cd
,
3141 const pcre_uint32
*base_list
, const pcre_uchar
*base_end
, int *rec_limit
)
3144 pcre_uint32 list
[8];
3145 const pcre_uint32
*chr_ptr
;
3146 const pcre_uint32
*ochr_ptr
;
3147 const pcre_uint32
*list_ptr
;
3148 const pcre_uchar
*next_code
;
3149 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3150 const pcre_uchar
*xclass_flags
;
3152 const pcre_uint8
*class_bitset
;
3153 const pcre_uint8
*set1
, *set2
, *set_end
;
3155 BOOL accepted
, invert_bits
;
3156 BOOL entered_a_group
= FALSE
;
3158 if (*rec_limit
== 0) return FALSE
;
3161 /* Note: the base_list[1] contains whether the current opcode has greedy
3162 (represented by a non-zero value) quantifier. This is a different from
3163 other character type lists, which stores here that the character iterator
3164 matches to an empty string (also represented by a non-zero value). */
3168 /* All operations move the code pointer forward.
3169 Therefore infinite recursions are not possible. */
3173 /* Skip over callouts */
3175 if (c
== OP_CALLOUT
)
3177 code
+= PRIV(OP_lengths
)[c
];
3183 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
3191 /* TRUE only in greedy case. The non-greedy case could be replaced by
3192 an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3193 uses more memory, which we cannot get at this stage.) */
3195 return base_list
[1] != 0;
3198 /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3199 it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3200 cannot be converted to a possessive form. */
3202 if (base_list
[1] == 0) return FALSE
;
3204 switch(*(code
- GET(code
, 1)))
3209 case OP_ASSERTBACK_NOT
:
3212 /* Atomic sub-patterns and assertions can always auto-possessify their
3213 last iterator. However, if the group was entered as a result of checking
3214 a previous iterator, this is not possible. */
3216 return !entered_a_group
;
3219 code
+= PRIV(OP_lengths
)[c
];
3226 next_code
= code
+ GET(code
, 1);
3227 code
+= PRIV(OP_lengths
)[c
];
3229 while (*next_code
== OP_ALT
)
3231 if (!compare_opcodes(code
, utf
, cd
, base_list
, base_end
, rec_limit
))
3233 code
= next_code
+ 1 + LINK_SIZE
;
3234 next_code
+= GET(next_code
, 1);
3237 entered_a_group
= TRUE
;
3243 next_code
= code
+ 1;
3244 if (*next_code
!= OP_BRA
&& *next_code
!= OP_CBRA
3245 && *next_code
!= OP_ONCE
&& *next_code
!= OP_ONCE_NC
) return FALSE
;
3247 do next_code
+= GET(next_code
, 1); while (*next_code
== OP_ALT
);
3249 /* The bracket content will be checked by the
3250 OP_BRA/OP_CBRA case above. */
3251 next_code
+= 1 + LINK_SIZE
;
3252 if (!compare_opcodes(next_code
, utf
, cd
, base_list
, base_end
, rec_limit
))
3255 code
+= PRIV(OP_lengths
)[c
];
3262 /* Check for a supported opcode, and load its properties. */
3264 code
= get_chr_property_list(code
, utf
, cd
->fcc
, list
);
3265 if (code
== NULL
) return FALSE
; /* Unsupported */
3267 /* If either opcode is a small character list, set pointers for comparing
3268 characters from that list with another list, or with a property. */
3270 if (base_list
[0] == OP_CHAR
)
3272 chr_ptr
= base_list
+ 2;
3275 else if (list
[0] == OP_CHAR
)
3278 list_ptr
= base_list
;
3281 /* Character bitsets can also be compared to certain opcodes. */
3283 else if (base_list
[0] == OP_CLASS
|| list
[0] == OP_CLASS
3284 #ifdef COMPILE_PCRE8
3285 /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
3286 || (!utf
&& (base_list
[0] == OP_NCLASS
|| list
[0] == OP_NCLASS
))
3290 #ifdef COMPILE_PCRE8
3291 if (base_list
[0] == OP_CLASS
|| (!utf
&& base_list
[0] == OP_NCLASS
))
3293 if (base_list
[0] == OP_CLASS
)
3296 set1
= (pcre_uint8
*)(base_end
- base_list
[2]);
3301 set1
= (pcre_uint8
*)(code
- list
[2]);
3302 list_ptr
= base_list
;
3305 invert_bits
= FALSE
;
3310 set2
= (pcre_uint8
*)
3311 ((list_ptr
== list
? code
: base_end
) - list_ptr
[2]);
3314 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3316 xclass_flags
= (list_ptr
== list
? code
: base_end
) - list_ptr
[2] + LINK_SIZE
;
3317 if ((*xclass_flags
& XCL_HASPROP
) != 0) return FALSE
;
3318 if ((*xclass_flags
& XCL_MAP
) == 0)
3320 /* No bits are set for characters < 256. */
3321 if (list
[1] == 0) return (*xclass_flags
& XCL_NOT
) == 0;
3322 /* Might be an empty repeat. */
3325 set2
= (pcre_uint8
*)(xclass_flags
+ 1);
3333 set2
= (pcre_uint8
*)(cd
->cbits
+ cbit_digit
);
3336 case OP_NOT_WHITESPACE
:
3340 set2
= (pcre_uint8
*)(cd
->cbits
+ cbit_space
);
3343 case OP_NOT_WORDCHAR
:
3347 set2
= (pcre_uint8
*)(cd
->cbits
+ cbit_word
);
3354 /* Because the sets are unaligned, we need
3355 to perform byte comparison here. */
3356 set_end
= set1
+ 32;
3361 if ((*set1
++ & ~(*set2
++)) != 0) return FALSE
;
3363 while (set1
< set_end
);
3369 if ((*set1
++ & *set2
++) != 0) return FALSE
;
3371 while (set1
< set_end
);
3374 if (list
[1] == 0) return TRUE
;
3375 /* Might be an empty repeat. */
3379 /* Some property combinations also acceptable. Unicode property opcodes are
3380 processed specially; the rest can be handled with a lookup table. */
3384 pcre_uint32 leftop
, rightop
;
3386 leftop
= base_list
[0];
3390 accepted
= FALSE
; /* Always set in non-unicode case. */
3391 if (leftop
== OP_PROP
|| leftop
== OP_NOTPROP
)
3393 if (rightop
== OP_EOD
)
3395 else if (rightop
== OP_PROP
|| rightop
== OP_NOTPROP
)
3398 const pcre_uint8
*p
;
3399 BOOL same
= leftop
== rightop
;
3400 BOOL lisprop
= leftop
== OP_PROP
;
3401 BOOL risprop
= rightop
== OP_PROP
;
3402 BOOL bothprop
= lisprop
&& risprop
;
3404 /* There's a table that specifies how each combination is to be
3406 0 Always return FALSE (never auto-possessify)
3407 1 Character groups are distinct (possessify if both are OP_PROP)
3408 2 Check character categories in the same group (general or particular)
3409 3 Return TRUE if the two opcodes are not the same
3410 ... see comments below
3413 n
= propposstab
[base_list
[2]][list
[2]];
3417 case 1: accepted
= bothprop
; break;
3418 case 2: accepted
= (base_list
[3] == list
[3]) != same
; break;
3419 case 3: accepted
= !same
; break;
3421 case 4: /* Left general category, right particular category */
3422 accepted
= risprop
&& catposstab
[base_list
[3]][list
[3]] == same
;
3425 case 5: /* Right general category, left particular category */
3426 accepted
= lisprop
&& catposstab
[list
[3]][base_list
[3]] == same
;
3429 /* This code is logically tricky. Think hard before fiddling with it.
3430 The posspropstab table has four entries per row. Each row relates to
3431 one of PCRE's special properties such as ALNUM or SPACE or WORD.
3432 Only WORD actually needs all four entries, but using repeats for the
3433 others means they can all use the same code below.
3435 The first two entries in each row are Unicode general categories, and
3436 apply always, because all the characters they include are part of the
3437 PCRE character set. The third and fourth entries are a general and a
3438 particular category, respectively, that include one or more relevant
3439 characters. One or the other is used, depending on whether the check
3440 is for a general or a particular category. However, in both cases the
3441 category contains more characters than the specials that are defined
3442 for the property being tested against. Therefore, it cannot be used
3445 Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3446 Underscore is covered by ucp_P or ucp_Po. */
3448 case 6: /* Left alphanum vs right general category */
3449 case 7: /* Left space vs right general category */
3450 case 8: /* Left word vs right general category */
3451 p
= posspropstab
[n
-6];
3452 accepted
= risprop
&& lisprop
==
3455 (list
[3] != p
[2] || !lisprop
));
3458 case 9: /* Right alphanum vs left general category */
3459 case 10: /* Right space vs left general category */
3460 case 11: /* Right word vs left general category */
3461 p
= posspropstab
[n
-9];
3462 accepted
= lisprop
&& risprop
==
3463 (base_list
[3] != p
[0] &&
3464 base_list
[3] != p
[1] &&
3465 (base_list
[3] != p
[2] || !risprop
));
3468 case 12: /* Left alphanum vs right particular category */
3469 case 13: /* Left space vs right particular category */
3470 case 14: /* Left word vs right particular category */
3471 p
= posspropstab
[n
-12];
3472 accepted
= risprop
&& lisprop
==
3473 (catposstab
[p
[0]][list
[3]] &&
3474 catposstab
[p
[1]][list
[3]] &&
3475 (list
[3] != p
[3] || !lisprop
));
3478 case 15: /* Right alphanum vs left particular category */
3479 case 16: /* Right space vs left particular category */
3480 case 17: /* Right word vs left particular category */
3481 p
= posspropstab
[n
-15];
3482 accepted
= lisprop
&& risprop
==
3483 (catposstab
[p
[0]][base_list
[3]] &&
3484 catposstab
[p
[1]][base_list
[3]] &&
3485 (base_list
[3] != p
[3] || !risprop
));
3492 #endif /* SUPPORT_UCP */
3494 accepted
= leftop
>= FIRST_AUTOTAB_OP
&& leftop
<= LAST_AUTOTAB_LEFT_OP
&&
3495 rightop
>= FIRST_AUTOTAB_OP
&& rightop
<= LAST_AUTOTAB_RIGHT_OP
&&
3496 autoposstab
[leftop
- FIRST_AUTOTAB_OP
][rightop
- FIRST_AUTOTAB_OP
];
3498 if (!accepted
) return FALSE
;
3500 if (list
[1] == 0) return TRUE
;
3501 /* Might be an empty repeat. */
3505 /* Control reaches here only if one of the items is a small character list.
3506 All characters are checked against the other side. */
3515 ochr_ptr
= list_ptr
+ 2;
3518 if (chr
== *ochr_ptr
) return FALSE
;
3521 while(*ochr_ptr
!= NOTACHAR
);
3525 ochr_ptr
= list_ptr
+ 2;
3528 if (chr
== *ochr_ptr
)
3532 while(*ochr_ptr
!= NOTACHAR
);
3533 if (*ochr_ptr
== NOTACHAR
) return FALSE
; /* Not found */
3536 /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3537 set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3540 if (chr
< 256 && (cd
->ctypes
[chr
] & ctype_digit
) != 0) return FALSE
;
3544 if (chr
> 255 || (cd
->ctypes
[chr
] & ctype_digit
) == 0) return FALSE
;
3548 if (chr
< 256 && (cd
->ctypes
[chr
] & ctype_space
) != 0) return FALSE
;
3551 case OP_NOT_WHITESPACE
:
3552 if (chr
> 255 || (cd
->ctypes
[chr
] & ctype_space
) == 0) return FALSE
;
3556 if (chr
< 255 && (cd
->ctypes
[chr
] & ctype_word
) != 0) return FALSE
;
3559 case OP_NOT_WORDCHAR
:
3560 if (chr
> 255 || (cd
->ctypes
[chr
] & ctype_word
) == 0) return FALSE
;
3566 HSPACE_CASES
: return FALSE
;
3574 HSPACE_CASES
: break;
3575 default: return FALSE
;
3583 VSPACE_CASES
: return FALSE
;
3591 VSPACE_CASES
: break;
3592 default: return FALSE
;
3608 #endif /* Not EBCDIC */
3613 case OP_EOD
: /* Can always possessify before \z */
3619 if (!check_char_prop(chr
, list_ptr
[2], list_ptr
[3],
3620 list_ptr
[0] == OP_NOTPROP
))
3626 if (chr
> 255) return FALSE
;
3630 if (chr
> 255) break;
3631 class_bitset
= (pcre_uint8
*)
3632 ((list_ptr
== list
? code
: base_end
) - list_ptr
[2]);
3633 if ((class_bitset
[chr
>> 3] & (1U << (chr
& 7))) != 0) return FALSE
;
3636 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3638 if (PRIV(xclass
)(chr
, (list_ptr
== list
? code
: base_end
) -
3639 list_ptr
[2] + LINK_SIZE
, utf
)) return FALSE
;
3649 while(*chr_ptr
!= NOTACHAR
);
3651 /* At least one character must be matched from this opcode. */
3653 if (list
[1] == 0) return TRUE
;
3656 /* Control never reaches here. There used to be a fail-save return FALSE; here,
3657 but some compilers complain about an unreachable statement. */
3663 /*************************************************
3664 * Scan compiled regex for auto-possession *
3665 *************************************************/
3667 /* Replaces single character iterations with their possessive alternatives
3668 if appropriate. This function modifies the compiled opcode!
3671 code points to start of the byte code
3672 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
3673 cd static compile data
3679 auto_possessify(pcre_uchar
*code
, BOOL utf
, const compile_data
*cd
)
3681 register pcre_uchar c
;
3682 const pcre_uchar
*end
;
3683 pcre_uchar
*repeat_opcode
;
3684 pcre_uint32 list
[8];
3691 /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK,
3692 it may compile without complaining, but may get into a loop here if the code
3693 pointer points to a bad value. This is, of course a documentated possibility,
3694 when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and
3695 just give up on this optimization. */
3697 if (c
>= OP_TABLE_LENGTH
) return;
3699 if (c
>= OP_STAR
&& c
<= OP_TYPEPOSUPTO
)
3701 c
-= get_repeat_base(c
) - OP_STAR
;
3702 end
= (c
<= OP_MINUPTO
) ?
3703 get_chr_property_list(code
, utf
, cd
->fcc
, list
) : NULL
;
3704 list
[1] = c
== OP_STAR
|| c
== OP_PLUS
|| c
== OP_QUERY
|| c
== OP_UPTO
;
3707 if (end
!= NULL
&& compare_opcodes(end
, utf
, cd
, list
, end
, &rec_limit
))
3712 *code
+= OP_POSSTAR
- OP_STAR
;
3716 *code
+= OP_POSSTAR
- OP_MINSTAR
;
3720 *code
+= OP_POSPLUS
- OP_PLUS
;
3724 *code
+= OP_POSPLUS
- OP_MINPLUS
;
3728 *code
+= OP_POSQUERY
- OP_QUERY
;
3732 *code
+= OP_POSQUERY
- OP_MINQUERY
;
3736 *code
+= OP_POSUPTO
- OP_UPTO
;
3740 *code
+= OP_POSUPTO
- OP_MINUPTO
;
3746 else if (c
== OP_CLASS
|| c
== OP_NCLASS
|| c
== OP_XCLASS
)
3748 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3750 repeat_opcode
= code
+ GET(code
, 1);
3753 repeat_opcode
= code
+ 1 + (32 / sizeof(pcre_uchar
));
3756 if (c
>= OP_CRSTAR
&& c
<= OP_CRMINRANGE
)
3758 /* end must not be NULL. */
3759 end
= get_chr_property_list(code
, utf
, cd
->fcc
, list
);
3761 list
[1] = (c
& 1) == 0;
3764 if (compare_opcodes(end
, utf
, cd
, list
, end
, &rec_limit
))
3770 *repeat_opcode
= OP_CRPOSSTAR
;
3775 *repeat_opcode
= OP_CRPOSPLUS
;
3780 *repeat_opcode
= OP_CRPOSQUERY
;
3785 *repeat_opcode
= OP_CRPOSRANGE
;
3799 case OP_TYPEMINSTAR
:
3801 case OP_TYPEMINPLUS
:
3803 case OP_TYPEMINQUERY
:
3804 case OP_TYPEPOSSTAR
:
3805 case OP_TYPEPOSPLUS
:
3806 case OP_TYPEPOSQUERY
:
3807 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
3811 case OP_TYPEMINUPTO
:
3813 case OP_TYPEPOSUPTO
:
3814 if (code
[1 + IMM2_SIZE
] == OP_PROP
|| code
[1 + IMM2_SIZE
] == OP_NOTPROP
)
3818 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3820 code
+= GET(code
, 1);
3832 /* Add in the fixed length from the table */
3834 code
+= PRIV(OP_lengths
)[c
];
3836 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3837 a multi-byte character. The length in the table is a minimum, so we have to
3838 arrange to skip the extra bytes. */
3840 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3878 case OP_NOTMINQUERY
:
3884 case OP_NOTPOSQUERY
:
3887 case OP_NOTMINSTARI
:
3889 case OP_NOTMINPLUSI
:
3891 case OP_NOTMINQUERYI
:
3893 case OP_NOTMINUPTOI
:
3895 case OP_NOTPOSSTARI
:
3896 case OP_NOTPOSPLUSI
:
3897 case OP_NOTPOSQUERYI
:
3898 case OP_NOTPOSUPTOI
:
3899 if (HAS_EXTRALEN(code
[-1])) code
+= GET_EXTRALEN(code
[-1]);
3903 (void)(utf
); /* Keep compiler happy by referencing function argument */
3910 /*************************************************
3911 * Check for POSIX class syntax *
3912 *************************************************/
3914 /* This function is called when the sequence "[:" or "[." or "[=" is
3915 encountered in a character class. It checks whether this is followed by a
3916 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3917 reach an unescaped ']' without the special preceding character, return FALSE.
3919 Originally, this function only recognized a sequence of letters between the
3920 terminators, but it seems that Perl recognizes any sequence of characters,
3921 though of course unknown POSIX names are subsequently rejected. Perl gives an
3922 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3923 didn't consider this to be a POSIX class. Likewise for [:1234:].
3925 The problem in trying to be exactly like Perl is in the handling of escapes. We
3926 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3927 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3928 below handles the special cases \\ and \], but does not try to do any other
3929 escape processing. This makes it different from Perl for cases such as
3930 [:l\ower:] where Perl recognizes it as the POSIX class "lower" but PCRE does
3931 not recognize "l\ower". This is a lesser evil than not diagnosing bad classes
3932 when Perl does, I think.
3934 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3935 It seems that the appearance of a nested POSIX class supersedes an apparent
3936 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3939 In Perl, unescaped square brackets may also appear as part of class names. For
3940 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3941 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3942 seem right at all. PCRE does not allow closing square brackets in POSIX class
3946 ptr pointer to the initial [
3947 endptr where to return the end pointer
3949 Returns: TRUE or FALSE
3953 check_posix_syntax(const pcre_uchar
*ptr
, const pcre_uchar
**endptr
)
3955 pcre_uchar terminator
; /* Don't combine these lines; the Solaris cc */
3956 terminator
= *(++ptr
); /* compiler warns about "non-constant" initializer. */
3957 for (++ptr
; *ptr
!= CHAR_NULL
; ptr
++)
3959 if (*ptr
== CHAR_BACKSLASH
&&
3960 (ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
||
3961 ptr
[1] == CHAR_BACKSLASH
))
3963 else if ((*ptr
== CHAR_LEFT_SQUARE_BRACKET
&& ptr
[1] == terminator
) ||
3964 *ptr
== CHAR_RIGHT_SQUARE_BRACKET
) return FALSE
;
3965 else if (*ptr
== terminator
&& ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
)
3977 /*************************************************
3978 * Check POSIX class name *
3979 *************************************************/
3981 /* This function is called to check the name given in a POSIX-style class entry
3985 ptr points to the first letter
3986 len the length of the name
3988 Returns: a value representing the name, or -1 if unknown
3992 check_posix_name(const pcre_uchar
*ptr
, int len
)
3994 const char *pn
= posix_names
;
3995 register int yield
= 0;
3996 while (posix_name_lengths
[yield
] != 0)
3998 if (len
== posix_name_lengths
[yield
] &&
3999 STRNCMP_UC_C8(ptr
, pn
, (unsigned int)len
) == 0) return yield
;
4000 pn
+= posix_name_lengths
[yield
] + 1;
4007 /*************************************************
4008 * Adjust OP_RECURSE items in repeated group *
4009 *************************************************/
4011 /* OP_RECURSE items contain an offset from the start of the regex to the group
4012 that is referenced. This means that groups can be replicated for fixed
4013 repetition simply by copying (because the recursion is allowed to refer to
4014 earlier groups that are outside the current group). However, when a group is
4015 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
4016 inserted before it, after it has been compiled. This means that any OP_RECURSE
4017 items within it that refer to the group itself or any contained groups have to
4018 have their offsets adjusted. That one of the jobs of this function. Before it
4019 is called, the partially compiled regex must be temporarily terminated with
4022 This function has been extended to cope with forward references for recursions
4023 and subroutine calls. It must check the list of such references for the
4024 group we are dealing with. If it finds that one of the recursions in the
4025 current group is on this list, it does not adjust the value in the reference
4026 (which is a group number). After the group has been scanned, all the offsets in
4027 the forward reference list for the group are adjusted.
4030 group points to the start of the group
4031 adjust the amount by which the group is to be moved
4032 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
4033 cd contains pointers to tables etc.
4034 save_hwm_offset the hwm forward reference offset at the start of the group
4040 adjust_recurse(pcre_uchar
*group
, int adjust
, BOOL utf
, compile_data
*cd
,
4041 size_t save_hwm_offset
)
4045 pcre_uchar
*ptr
= group
;
4047 while ((ptr
= (pcre_uchar
*)find_recurse(ptr
, utf
)) != NULL
)
4049 for (hc
= (pcre_uchar
*)cd
->start_workspace
+ save_hwm_offset
; hc
< cd
->hwm
;
4052 offset
= (int)GET(hc
, 0);
4053 if (cd
->start_code
+ offset
== ptr
+ 1) break;
4056 /* If we have not found this recursion on the forward reference list, adjust
4057 the recursion's offset if it's after the start of this group. */
4061 offset
= (int)GET(ptr
, 1);
4062 if (cd
->start_code
+ offset
>= group
) PUT(ptr
, 1, offset
+ adjust
);
4065 ptr
+= 1 + LINK_SIZE
;
4068 /* Now adjust all forward reference offsets for the group. */
4070 for (hc
= (pcre_uchar
*)cd
->start_workspace
+ save_hwm_offset
; hc
< cd
->hwm
;
4073 offset
= (int)GET(hc
, 0);
4074 PUT(hc
, 0, offset
+ adjust
);
4080 /*************************************************
4081 * Insert an automatic callout point *
4082 *************************************************/
4084 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
4085 callout points before each pattern item.
4088 code current code pointer
4089 ptr current pattern pointer
4090 cd pointers to tables etc
4092 Returns: new code pointer
4096 auto_callout(pcre_uchar
*code
, const pcre_uchar
*ptr
, compile_data
*cd
)
4098 *code
++ = OP_CALLOUT
;
4100 PUT(code
, 0, (int)(ptr
- cd
->start_pattern
)); /* Pattern offset */
4101 PUT(code
, LINK_SIZE
, 0); /* Default length */
4102 return code
+ 2 * LINK_SIZE
;
4107 /*************************************************
4108 * Complete a callout item *
4109 *************************************************/
4111 /* A callout item contains the length of the next item in the pattern, which
4112 we can't fill in till after we have reached the relevant point. This is used
4113 for both automatic and manual callouts.
4116 previous_callout points to previous callout item
4117 ptr current pattern pointer
4118 cd pointers to tables etc
4124 complete_callout(pcre_uchar
*previous_callout
, const pcre_uchar
*ptr
, compile_data
*cd
)
4126 int length
= (int)(ptr
- cd
->start_pattern
- GET(previous_callout
, 2));
4127 PUT(previous_callout
, 2 + LINK_SIZE
, length
);
4133 /*************************************************
4134 * Get othercase range *
4135 *************************************************/
4137 /* This function is passed the start and end of a class range, in UTF-8 mode
4138 with UCP support. It searches up the characters, looking for ranges of
4139 characters in the "other" case. Each call returns the next one, updating the
4140 start address. A character with multiple other cases is returned on its own
4141 with a special return value.
4144 cptr points to starting character value; updated
4146 ocptr where to put start of othercase range
4147 odptr where to put end of othercase range
4149 Yield: -1 when no more
4150 0 when a range is returned
4151 >0 the CASESET offset for char with multiple other cases
4152 in this case, ocptr contains the original
4156 get_othercase_range(pcre_uint32
*cptr
, pcre_uint32 d
, pcre_uint32
*ocptr
,
4159 pcre_uint32 c
, othercase
, next
;
4162 /* Find the first character that has an other case. If it has multiple other
4163 cases, return its case offset value. */
4165 for (c
= *cptr
; c
<= d
; c
++)
4167 if ((co
= UCD_CASESET(c
)) != 0)
4169 *ocptr
= c
++; /* Character that has the set */
4170 *cptr
= c
; /* Rest of input range */
4173 if ((othercase
= UCD_OTHERCASE(c
)) != c
) break;
4176 if (c
> d
) return -1; /* Reached end of range */
4178 /* Found a character that has a single other case. Search for the end of the
4179 range, which is either the end of the input range, or a character that has zero
4180 or more than one other cases. */
4183 next
= othercase
+ 1;
4185 for (++c
; c
<= d
; c
++)
4187 if ((co
= UCD_CASESET(c
)) != 0 || UCD_OTHERCASE(c
) != next
) break;
4191 *odptr
= next
- 1; /* End of othercase range */
4192 *cptr
= c
; /* Rest of input range */
4195 #endif /* SUPPORT_UCP */
4199 /*************************************************
4200 * Add a character or range to a class *
4201 *************************************************/
4203 /* This function packages up the logic of adding a character or range of
4204 characters to a class. The character values in the arguments will be within the
4205 valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
4206 mutually recursive with the function immediately below.
4209 classbits the bit map for characters < 256
4210 uchardptr points to the pointer for extra data
4211 options the options word
4212 cd contains pointers to tables etc.
4213 start start of range character
4214 end end of range character
4216 Returns: the number of < 256 characters added
4217 the pointer to extra data is updated
4221 add_to_class(pcre_uint8
*classbits
, pcre_uchar
**uchardptr
, int options
,
4222 compile_data
*cd
, pcre_uint32 start
, pcre_uint32 end
)
4225 pcre_uint32 classbits_end
= (end
<= 0xff ? end
: 0xff);
4228 /* If caseless matching is required, scan the range and process alternate
4229 cases. In Unicode, there are 8-bit characters that have alternate cases that
4230 are greater than 255 and vice-versa. Sometimes we can just extend the original
4233 if ((options
& PCRE_CASELESS
) != 0)
4236 if ((options
& PCRE_UTF8
) != 0)
4241 options
&= ~PCRE_CASELESS
; /* Remove for recursive calls */
4244 while ((rc
= get_othercase_range(&c
, end
, &oc
, &od
)) >= 0)
4246 /* Handle a single character that has more than one other case. */
4248 if (rc
> 0) n8
+= add_list_to_class(classbits
, uchardptr
, options
, cd
,
4249 PRIV(ucd_caseless_sets
) + rc
, oc
);
4251 /* Do nothing if the other case range is within the original range. */
4253 else if (oc
>= start
&& od
<= end
) continue;
4255 /* Extend the original range if there is overlap, noting that if oc < c, we
4256 can't have od > end because a subrange is always shorter than the basic
4257 range. Otherwise, use a recursive call to add the additional range. */
4259 else if (oc
< start
&& od
>= start
- 1) start
= oc
; /* Extend downwards */
4260 else if (od
> end
&& oc
<= end
+ 1)
4262 end
= od
; /* Extend upwards */
4263 if (end
> classbits_end
) classbits_end
= (end
<= 0xff ? end
: 0xff);
4265 else n8
+= add_to_class(classbits
, uchardptr
, options
, cd
, oc
, od
);
4269 #endif /* SUPPORT_UCP */
4271 /* Not UTF-mode, or no UCP */
4273 for (c
= start
; c
<= classbits_end
; c
++)
4275 SETBIT(classbits
, cd
->fcc
[c
]);
4280 /* Now handle the original range. Adjust the final value according to the bit
4281 length - this means that the same lists of (e.g.) horizontal spaces can be used
4284 #if defined COMPILE_PCRE8
4286 if ((options
& PCRE_UTF8
) == 0)
4288 if (end
> 0xff) end
= 0xff;
4290 #elif defined COMPILE_PCRE16
4292 if ((options
& PCRE_UTF16
) == 0)
4294 if (end
> 0xffff) end
= 0xffff;
4296 #endif /* COMPILE_PCRE[8|16] */
4298 /* Use the bitmap for characters < 256. Otherwise use extra data.*/
4300 for (c
= start
; c
<= classbits_end
; c
++)
4302 /* Regardless of start, c will always be <= 255. */
4303 SETBIT(classbits
, c
);
4307 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4308 if (start
<= 0xff) start
= 0xff + 1;
4312 pcre_uchar
*uchardata
= *uchardptr
;
4314 if ((options
& PCRE_UTF8
) != 0) /* All UTFs use the same flag bit */
4318 *uchardata
++ = XCL_RANGE
;
4319 uchardata
+= PRIV(ord2utf
)(start
, uchardata
);
4320 uchardata
+= PRIV(ord2utf
)(end
, uchardata
);
4322 else if (start
== end
)
4324 *uchardata
++ = XCL_SINGLE
;
4325 uchardata
+= PRIV(ord2utf
)(start
, uchardata
);
4329 #endif /* SUPPORT_UTF */
4331 /* Without UTF support, character values are constrained by the bit length,
4332 and can only be > 256 for 16-bit and 32-bit libraries. */
4334 #ifdef COMPILE_PCRE8
4339 *uchardata
++ = XCL_RANGE
;
4340 *uchardata
++ = start
;
4343 else if (start
== end
)
4345 *uchardata
++ = XCL_SINGLE
;
4346 *uchardata
++ = start
;
4350 *uchardptr
= uchardata
; /* Updata extra data pointer */
4352 #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
4354 return n8
; /* Number of 8-bit characters */
4360 /*************************************************
4361 * Add a list of characters to a class *
4362 *************************************************/
4364 /* This function is used for adding a list of case-equivalent characters to a
4365 class, and also for adding a list of horizontal or vertical whitespace. If the
4366 list is in order (which it should be), ranges of characters are detected and
4367 handled appropriately. This function is mutually recursive with the function
4371 classbits the bit map for characters < 256
4372 uchardptr points to the pointer for extra data
4373 options the options word
4374 cd contains pointers to tables etc.
4375 p points to row of 32-bit values, terminated by NOTACHAR
4376 except character to omit; this is used when adding lists of
4377 case-equivalent characters to avoid including the one we
4380 Returns: the number of < 256 characters added
4381 the pointer to extra data is updated
4385 add_list_to_class(pcre_uint8
*classbits
, pcre_uchar
**uchardptr
, int options
,
4386 compile_data
*cd
, const pcre_uint32
*p
, unsigned int except
)
4389 while (p
[0] < NOTACHAR
)
4394 while(p
[n
+1] == p
[0] + n
+ 1) n
++;
4395 n8
+= add_to_class(classbits
, uchardptr
, options
, cd
, p
[0], p
[n
]);
4404 /*************************************************
4405 * Add characters not in a list to a class *
4406 *************************************************/
4408 /* This function is used for adding the complement of a list of horizontal or
4409 vertical whitespace to a class. The list must be in order.
4412 classbits the bit map for characters < 256
4413 uchardptr points to the pointer for extra data
4414 options the options word
4415 cd contains pointers to tables etc.
4416 p points to row of 32-bit values, terminated by NOTACHAR
4418 Returns: the number of < 256 characters added
4419 the pointer to extra data is updated
4423 add_not_list_to_class(pcre_uint8
*classbits
, pcre_uchar
**uchardptr
,
4424 int options
, compile_data
*cd
, const pcre_uint32
*p
)
4426 BOOL utf
= (options
& PCRE_UTF8
) != 0;
4429 n8
+= add_to_class(classbits
, uchardptr
, options
, cd
, 0, p
[0] - 1);
4430 while (p
[0] < NOTACHAR
)
4432 while (p
[1] == p
[0] + 1) p
++;
4433 n8
+= add_to_class(classbits
, uchardptr
, options
, cd
, p
[0] + 1,
4434 (p
[1] == NOTACHAR
) ? (utf
? 0x10ffffu
: 0xffffffffu
) : p
[1] - 1);
4442 /*************************************************
4443 * Compile one branch *
4444 *************************************************/
4446 /* Scan the pattern, compiling it into the a vector. If the options are
4447 changed during the branch, the pointer is used to change the external options
4448 bits. This function is used during the pre-compile phase when we are trying
4449 to find out the amount of memory needed, as well as during the real compile
4450 phase. The value of lengthptr distinguishes the two phases.
4453 optionsptr pointer to the option bits
4454 codeptr points to the pointer to the current code point
4455 ptrptr points to the current pattern pointer
4456 errorcodeptr points to error code variable
4457 firstcharptr place to put the first required character
4458 firstcharflagsptr place to put the first character flags, or a negative number
4459 reqcharptr place to put the last required character
4460 reqcharflagsptr place to put the last required character flags, or a negative number
4461 bcptr points to current branch chain
4462 cond_depth conditional nesting depth
4463 cd contains pointers to tables etc.
4464 lengthptr NULL during the real compile phase
4465 points to length accumulator during pre-compile phase
4467 Returns: TRUE on success
4468 FALSE, with *errorcodeptr set non-zero on error
4472 compile_branch(int *optionsptr
, pcre_uchar
**codeptr
,
4473 const pcre_uchar
**ptrptr
, int *errorcodeptr
,
4474 pcre_uint32
*firstcharptr
, pcre_int32
*firstcharflagsptr
,
4475 pcre_uint32
*reqcharptr
, pcre_int32
*reqcharflagsptr
,
4476 branch_chain
*bcptr
, int cond_depth
,
4477 compile_data
*cd
, int *lengthptr
)
4479 int repeat_type
, op_type
;
4480 int repeat_min
= 0, repeat_max
= 0; /* To please picky compilers */
4482 int greedy_default
, greedy_non_default
;
4483 pcre_uint32 firstchar
, reqchar
;
4484 pcre_int32 firstcharflags
, reqcharflags
;
4485 pcre_uint32 zeroreqchar
, zerofirstchar
;
4486 pcre_int32 zeroreqcharflags
, zerofirstcharflags
;
4487 pcre_int32 req_caseopt
, reqvary
, tempreqvary
;
4488 int options
= *optionsptr
; /* May change dynamically */
4489 int after_manual_callout
= 0;
4490 int length_prevgroup
= 0;
4491 register pcre_uint32 c
;
4493 register pcre_uchar
*code
= *codeptr
;
4494 pcre_uchar
*last_code
= code
;
4495 pcre_uchar
*orig_code
= code
;
4496 pcre_uchar
*tempcode
;
4497 BOOL inescq
= (cd
->extended_options
& PCRE_VERBATIM_BIT
) != 0;
4498 BOOL basicre
= (cd
->extended_options
& PCRE_POSIX_BASIC_ESC_BIT
) != 0;
4499 BOOL posixre
= (cd
->extended_options
& PCRE_POSIX_RE_BITS
) != 0;
4500 BOOL embednul
= (cd
->extended_options
& PCRE_ALLOW_EMBEDDED_NUL_BIT
) != 0;
4501 BOOL groupsetfirstchar
= FALSE
;
4502 const pcre_uchar
*ptr
= *ptrptr
;
4503 const pcre_uchar
*tempptr
;
4504 const pcre_uchar
*nestptr
= NULL
;
4505 pcre_uchar
*previous
= NULL
;
4506 pcre_uchar
*previous_callout
= NULL
;
4507 size_t item_hwm_offset
= 0;
4508 pcre_uint8 classbits
[32];
4509 pcre_uchar sub_basic_esc
[3];
4511 /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
4512 must not do this for other options (e.g. PCRE_EXTENDED) because they may change
4513 dynamically as we process the pattern. */
4516 /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
4517 BOOL utf
= (options
& PCRE_UTF8
) != 0;
4518 #ifndef COMPILE_PCRE32
4519 pcre_uchar utf_chars
[6];
4525 /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4526 class_uchardata always so that it can be passed to add_to_class() always,
4527 though it will not be used in non-UTF 8-bit cases. This avoids having to supply
4528 alternative calls for the different cases. */
4530 pcre_uchar
*class_uchardata
;
4531 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4533 pcre_uchar
*class_uchardata_base
;
4537 if (lengthptr
!= NULL
) DPRINTF((">> start branch\n"));
4540 /* Set up the default and non-default settings for greediness */
4542 greedy_default
= ((options
& PCRE_UNGREEDY
) != 0);
4543 greedy_non_default
= greedy_default
^ 1;
4545 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
4546 matching encountered yet". It gets changed to REQ_NONE if we hit something that
4547 matches a non-fixed char first char; reqchar just remains unset if we never
4550 When we hit a repeat whose minimum is zero, we may have to adjust these values
4551 to take the zero repeat into account. This is implemented by setting them to
4552 zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
4553 item types that can be repeated set these backoff variables appropriately. */
4555 firstchar
= reqchar
= zerofirstchar
= zeroreqchar
= 0;
4556 firstcharflags
= reqcharflags
= zerofirstcharflags
= zeroreqcharflags
= REQ_UNSET
;
4558 /* The variable req_caseopt contains either the REQ_CASELESS value
4559 or zero, according to the current setting of the caseless flag. The
4560 REQ_CASELESS leaves the lower 28 bit empty. It is added into the
4561 firstchar or reqchar variables to record the case status of the
4562 value. This is used only for ASCII characters. */
4564 req_caseopt
= ((options
& PCRE_CASELESS
) != 0)? REQ_CASELESS
:0;
4566 /* Switch on next character until the end of the branch */
4571 BOOL should_flip_negation
;
4572 BOOL possessive_quantifier
;
4575 BOOL reset_bracount
;
4576 int class_has_8bitchar
;
4578 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4579 BOOL xclass_has_prop
;
4585 pcre_uint32 subreqchar
, subfirstchar
;
4586 pcre_int32 subreqcharflags
, subfirstcharflags
;
4588 unsigned int mclength
;
4589 unsigned int tempbracount
;
4591 pcre_uchar mcbuffer
[8];
4593 /* Come here to restart the loop without advancing the pointer. */
4597 /* Get next character in the pattern */
4601 /* If we are at the end of a nested substitution, revert to the outer level
4602 string. Nesting only happens one level deep. */
4604 if (c
== CHAR_NULL
&& nestptr
!= NULL
)
4611 if (c
== CHAR_NULL
&& !inescq
&& embednul
&& ptr
< cd
->end_pattern
)
4614 ptr
= sub_embedded_nul
;
4618 if (nestptr
== NULL
&& !inescq
&& basicre
)
4620 if (c
== CHAR_LEFT_PARENTHESIS
|| c
== CHAR_RIGHT_PARENTHESIS
||
4621 c
== CHAR_LEFT_CURLY_BRACKET
|| c
== CHAR_RIGHT_CURLY_BRACKET
||
4622 c
== CHAR_QUESTION_MARK
|| c
== CHAR_PLUS
|| c
== CHAR_VERTICAL_LINE
)
4624 sub_basic_esc
[2] = CHAR_NULL
;
4625 sub_basic_esc
[1] = c
;
4626 sub_basic_esc
[0] = c
= CHAR_BACKSLASH
;
4628 ptr
= sub_basic_esc
;
4630 else if (c
== CHAR_BACKSLASH
&&
4631 !((CHAR_1
<= ptr
[1] && ptr
[1] <= CHAR_9
) || ptr
[1] == CHAR_DOT
||
4632 ptr
[1] == CHAR_BACKSLASH
||
4633 ptr
[1] == CHAR_LEFT_SQUARE_BRACKET
|| ptr
[1] == CHAR_ASTERISK
||
4634 ptr
[1] == CHAR_CIRCUMFLEX_ACCENT
|| ptr
[1] == CHAR_DOLLAR_SIGN
))
4640 /* If we are in the pre-compile phase, accumulate the length used for the
4641 previous cycle of this loop. */
4643 if (lengthptr
!= NULL
)
4646 if (code
> cd
->hwm
) cd
->hwm
= code
; /* High water info */
4648 if (code
> cd
->start_workspace
+ cd
->workspace_size
-
4649 WORK_SIZE_SAFETY_MARGIN
) /* Check for overrun */
4651 *errorcodeptr
= (code
>= cd
->start_workspace
+ cd
->workspace_size
)?
4656 /* There is at least one situation where code goes backwards: this is the
4657 case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
4658 the class is simply eliminated. However, it is created first, so we have to
4659 allow memory for it. Therefore, don't ever reduce the length at this point.
4662 if (code
< last_code
) code
= last_code
;
4664 /* Paranoid check for integer overflow */
4666 if (OFLOW_MAX
- *lengthptr
< code
- last_code
)
4668 *errorcodeptr
= ERR20
;
4672 *lengthptr
+= (int)(code
- last_code
);
4673 DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr
,
4674 (int)(code
- last_code
), c
, c
));
4676 /* If "previous" is set and it is not at the start of the work space, move
4677 it back to there, in order to avoid filling up the work space. Otherwise,
4678 if "previous" is NULL, reset the current code pointer to the start. */
4680 if (previous
!= NULL
)
4682 if (previous
> orig_code
)
4684 memmove(orig_code
, previous
, IN_UCHARS(code
- previous
));
4685 code
-= previous
- orig_code
;
4686 previous
= orig_code
;
4689 else code
= orig_code
;
4691 /* Remember where this code item starts so we can pick up the length
4697 /* In the real compile phase, just check the workspace used by the forward
4700 else if (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
)
4702 *errorcodeptr
= ERR52
;
4706 /* If in \Q...\E, check for the end; if not, we have a literal. Otherwise an
4707 isolated \E is ignored. */
4709 if (c
!= CHAR_NULL
||
4710 (inescq
&& nestptr
== NULL
&& embednul
&& ptr
< cd
->end_pattern
))
4712 if (c
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
&&
4713 (cd
->extended_options
& PCRE_VERBATIM_BIT
) == 0)
4721 if (previous_callout
!= NULL
)
4723 if (lengthptr
== NULL
) /* Don't attempt in pre-compile phase */
4724 complete_callout(previous_callout
, ptr
, cd
);
4725 previous_callout
= NULL
;
4727 if ((options
& PCRE_AUTO_CALLOUT
) != 0)
4729 previous_callout
= code
;
4730 code
= auto_callout(code
, ptr
, cd
);
4735 /* Check for the start of a \Q...\E sequence. We must do this here rather
4736 than later in case it is immediately followed by \E, which turns it into a
4737 "do nothing" sequence. */
4739 if (c
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_Q
)
4747 /* In extended mode, skip white space and comments. */
4749 if ((options
& PCRE_EXTENDED
) != 0)
4751 const pcre_uchar
*wscptr
= ptr
;
4752 while (MAX_255(c
) && (cd
->ctypes
[c
] & ctype_space
) != 0) c
= *(++ptr
);
4753 if (c
== CHAR_NUMBER_SIGN
)
4756 while (*ptr
!= CHAR_NULL
|| (nestptr
== NULL
&& embednul
&& ptr
< cd
->end_pattern
))
4758 if (IS_NEWLINE(ptr
)) /* For non-fixed-length newline cases, */
4759 { /* IS_NEWLINE sets cd->nllen. */
4765 if (utf
) FORWARDCHAR(ptr
);
4770 /* If we skipped any characters, restart the loop. Otherwise, we didn't see
4773 if (ptr
> wscptr
) goto REDO_LOOP
;
4776 /* Skip over (?# comments. We need to do this here because we want to know if
4777 the next thing is a quantifier, and these comments may come between an item
4778 and its quantifier. */
4780 if (c
== CHAR_LEFT_PARENTHESIS
&& ptr
[1] == CHAR_QUESTION_MARK
&&
4781 ptr
[2] == CHAR_NUMBER_SIGN
&& (!basicre
|| nestptr
))
4784 while ((*ptr
!= CHAR_NULL
||
4785 (nestptr
== NULL
&& embednul
&& ptr
< cd
->end_pattern
)) &&
4786 *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
4787 if (*ptr
== CHAR_NULL
)
4789 *errorcodeptr
= ERR18
;
4795 /* See if the next thing is a quantifier. */
4798 c
== CHAR_ASTERISK
|| c
== CHAR_PLUS
|| c
== CHAR_QUESTION_MARK
||
4799 (c
== CHAR_LEFT_CURLY_BRACKET
&& is_counted_repeat(ptr
+1, basicre
&& !nestptr
));
4801 /* Fill in length of a previous callout, except when the next thing is a
4802 quantifier or when processing a property substitution string in UCP mode. */
4804 if (!is_quantifier
&& previous_callout
!= NULL
&& nestptr
== NULL
&&
4805 after_manual_callout
-- <= 0)
4807 if (lengthptr
== NULL
) /* Don't attempt in pre-compile phase */
4808 complete_callout(previous_callout
, ptr
, cd
);
4809 previous_callout
= NULL
;
4812 /* Create auto callout, except for quantifiers, or while processing property
4813 strings that are substituted for \w etc in UCP mode. */
4815 if ((options
& PCRE_AUTO_CALLOUT
) != 0 && !is_quantifier
&& nestptr
== NULL
)
4817 previous_callout
= code
;
4818 code
= auto_callout(code
, ptr
, cd
);
4821 /* Process the next pattern item. */
4825 /* ===================================================================*/
4826 case CHAR_NULL
: /* The branch terminates at string end */
4827 case CHAR_VERTICAL_LINE
: /* or | or ) */
4828 case CHAR_RIGHT_PARENTHESIS
:
4829 *firstcharptr
= firstchar
;
4830 *firstcharflagsptr
= firstcharflags
;
4831 *reqcharptr
= reqchar
;
4832 *reqcharflagsptr
= reqcharflags
;
4835 if (lengthptr
!= NULL
)
4837 if (OFLOW_MAX
- *lengthptr
< code
- last_code
)
4839 *errorcodeptr
= ERR20
;
4842 *lengthptr
+= (int)(code
- last_code
); /* To include callout length */
4843 DPRINTF((">> end branch\n"));
4848 /* ===================================================================*/
4849 /* Handle single-character metacharacters. In multiline mode, ^ disables
4850 the setting of any following char as a first character. */
4852 case CHAR_CIRCUMFLEX_ACCENT
:
4854 if ((options
& PCRE_MULTILINE
) != 0)
4856 if (firstcharflags
== REQ_UNSET
)
4857 zerofirstcharflags
= firstcharflags
= REQ_NONE
;
4860 else *code
++ = OP_CIRC
;
4863 case CHAR_DOLLAR_SIGN
:
4865 *code
++ = ((options
& PCRE_MULTILINE
) != 0)? OP_DOLLM
: OP_DOLL
;
4868 /* There can never be a first char if '.' is first, whatever happens about
4869 repeats. The value of reqchar doesn't change either. */
4872 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
4873 zerofirstchar
= firstchar
;
4874 zerofirstcharflags
= firstcharflags
;
4875 zeroreqchar
= reqchar
;
4876 zeroreqcharflags
= reqcharflags
;
4878 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
4879 *code
++ = ((options
& PCRE_DOTALL
) != 0)? OP_ALLANY
: OP_ANY
;
4883 /* ===================================================================*/
4884 /* Character classes. If the included characters are all < 256, we build a
4885 32-byte bitmap of the permitted characters, except in the special case
4886 where there is only one such character. For negated classes, we build the
4887 map as usual, then invert it at the end. However, we use a different opcode
4888 so that data characters > 255 can be handled correctly.
4890 If the class contains characters outside the 0-255 range, a different
4891 opcode is compiled. It may optionally have a bit map for characters < 256,
4892 but those above are are explicitly listed afterwards. A flag byte tells
4893 whether the bitmap is present, and whether this is a negated class or not.
4895 In JavaScript compatibility mode, an isolated ']' causes an error. In
4896 default (Perl) mode, it is treated as a data character. */
4898 case CHAR_RIGHT_SQUARE_BRACKET
:
4899 if ((cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
4901 *errorcodeptr
= ERR64
;
4906 /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
4907 used for "start of word" and "end of word". As these are otherwise illegal
4908 sequences, we don't break anything by recognizing them. They are replaced
4909 by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
4910 erroneous and are handled by the normal code below. */
4912 case CHAR_LEFT_SQUARE_BRACKET
:
4913 if (STRNCMP_UC_C8(ptr
+1, STRING_WEIRD_STARTWORD
, 6) == 0)
4916 ptr
= sub_start_of_word
;
4920 if (STRNCMP_UC_C8(ptr
+1, STRING_WEIRD_ENDWORD
, 6) == 0)
4923 ptr
= sub_end_of_word
;
4927 /* Handle a real character class. */
4930 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
4932 /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
4933 they are encountered at the top level, so we'll do that too. */
4935 if ((ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
4936 ptr
[1] == CHAR_EQUALS_SIGN
) &&
4937 check_posix_syntax(ptr
, &tempptr
))
4939 *errorcodeptr
= (ptr
[1] == CHAR_COLON
)? ERR13
: ERR31
;
4943 /* If the first character is '^', set the negation flag and skip it. Also,
4944 if the first few characters (either before or after ^) are \Q\E or \E we
4945 skip them too. This makes for compatibility with Perl. */
4947 negate_class
= FALSE
;
4951 if (c
== CHAR_BACKSLASH
&& (!posixre
|| nestptr
))
4953 if (ptr
[1] == CHAR_E
)
4955 else if (STRNCMP_UC_C8(ptr
+ 1, STR_Q STR_BACKSLASH STR_E
, 3) == 0)
4960 else if (!negate_class
&& c
== CHAR_CIRCUMFLEX_ACCENT
)
4961 negate_class
= TRUE
;
4965 /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
4966 an initial ']' is taken as a data character -- the code below handles
4967 that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
4968 [^] must match any character, so generate OP_ALLANY. */
4970 if (c
== CHAR_RIGHT_SQUARE_BRACKET
&&
4971 (cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
4973 *code
++ = negate_class
? OP_ALLANY
: OP_FAIL
;
4974 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
4975 zerofirstchar
= firstchar
;
4976 zerofirstcharflags
= firstcharflags
;
4980 /* If a class contains a negative special such as \S, we need to flip the
4981 negation flag at the end, so that support for characters > 255 works
4982 correctly (they are all included in the class). */
4984 should_flip_negation
= FALSE
;
4986 /* Extended class (xclass) will be used when characters > 255
4989 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4991 class_uchardata
= code
+ LINK_SIZE
+ 2; /* For XCLASS items */
4992 class_uchardata_base
= class_uchardata
; /* Save the start */
4995 /* For optimization purposes, we track some properties of the class:
4996 class_has_8bitchar will be non-zero if the class contains at least one <
4997 256 character; class_one_char will be 1 if the class contains just one
4998 character; xclass_has_prop will be TRUE if unicode property checks
4999 are present in the class. */
5001 class_has_8bitchar
= 0;
5003 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5004 xclass_has_prop
= FALSE
;
5007 /* Initialize the 32-char bit map to all zeros. We build the map in a
5008 temporary bit of memory, in case the class contains fewer than two
5009 8-bit characters because in that case the compiled code doesn't use the bit
5012 memset(classbits
, 0, 32 * sizeof(pcre_uint8
));
5014 /* If the PCRE_NOT_EXCLUDES_NL option is set AND the class started with '^',
5015 stuff an ESC_n into the character class then resume. However, [^] means
5016 match any character in JS but not in Perl/PCRE so for JS [^] still becomes
5017 [^\n] but [^]] in Perl/PCRE must become [^]\n] instead. */
5019 if (negate_class
&& nestptr
== NULL
&& (cd
->extended_options
& PCRE_NOT_EXCLUDES_NL_BIT
) != 0)
5021 if (c
!= CHAR_RIGHT_SQUARE_BRACKET
||
5022 (cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
5025 ptr
= sub_implicit_rsb_newline
+ 1;
5030 ptr
= sub_implicit_rsb_newline
;
5035 if (c
== CHAR_NULL
&& nestptr
== NULL
&& embednul
&& ptr
< cd
->end_pattern
)
5038 ptr
= sub_embedded_nul
;
5042 /* Process characters until ] is reached. By writing this as a "do" it
5043 means that an initial ] is taken as a data character. At the start of the
5044 loop, c contains the first byte of the character. */
5046 if (c
!= CHAR_NULL
) do
5048 const pcre_uchar
*oldptr
;
5051 if (utf
&& HAS_EXTRALEN(c
))
5052 { /* Braces are required because the */
5053 GETCHARLEN(c
, ptr
, ptr
); /* macro generates multiple statements */
5057 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5058 /* In the pre-compile phase, accumulate the length of any extra
5059 data and reset the pointer. This is so that very large classes that
5060 contain a zillion > 255 characters no longer overwrite the work space
5061 (which is on the stack). We have to remember that there was XCLASS data,
5064 if (class_uchardata
> class_uchardata_base
) xclass
= TRUE
;
5066 if (lengthptr
!= NULL
&& class_uchardata
> class_uchardata_base
)
5068 *lengthptr
+= (int)(class_uchardata
- class_uchardata_base
);
5069 class_uchardata
= class_uchardata_base
;
5073 /* Inside \Q...\E everything is literal except \E */
5077 if (c
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
) /* If we are at \E */
5079 inescq
= FALSE
; /* Reset literal state */
5080 ptr
++; /* Skip the 'E' */
5081 continue; /* Carry on with next */
5083 goto CHECK_RANGE
; /* Could be range if \E follows */
5086 /* Handle POSIX class names. Perl allows a negation extension of the
5087 form [:^name:]. A square bracket that doesn't match the syntax is
5088 treated as a literal. We also recognize the POSIX constructions
5089 [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
5092 if (c
== CHAR_LEFT_SQUARE_BRACKET
&&
5093 (ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
5094 ptr
[1] == CHAR_EQUALS_SIGN
) && check_posix_syntax(ptr
, &tempptr
))
5096 BOOL local_negate
= FALSE
;
5097 int posix_class
, taboffset
, tabopt
;
5098 register const pcre_uint8
*cbits
= cd
->cbits
;
5099 pcre_uint8 pbits
[32];
5101 if (ptr
[1] != CHAR_COLON
)
5103 *errorcodeptr
= ERR31
;
5108 if (*ptr
== CHAR_CIRCUMFLEX_ACCENT
)
5110 local_negate
= TRUE
;
5111 should_flip_negation
= TRUE
; /* Note negative special */
5115 posix_class
= check_posix_name(ptr
, (int)(tempptr
- ptr
));
5116 if (posix_class
< 0)
5118 *errorcodeptr
= ERR30
;
5122 /* If matching is caseless, upper and lower are converted to
5123 alpha. This relies on the fact that the class table starts with
5124 alpha, lower, upper as the first 3 entries. */
5126 if ((options
& PCRE_CASELESS
) != 0 && posix_class
<= 2)
5129 /* When PCRE_UCP is set, some of the POSIX classes are converted to
5130 different escape sequences that use Unicode properties \p or \P. Others
5131 that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP
5135 if ((options
& PCRE_UCP
) != 0)
5137 unsigned int ptype
= 0;
5138 int pc
= posix_class
+ ((local_negate
)? POSIX_SUBSIZE
/2 : 0);
5140 /* The posix_substitutes table specifies which POSIX classes can be
5141 converted to \p or \P items. */
5143 if (posix_substitutes
[pc
] != NULL
)
5145 nestptr
= tempptr
+ 1;
5146 ptr
= posix_substitutes
[pc
] - 1;
5150 /* There are three other classes that generate special property calls
5151 that are recognized only in an XCLASS. */
5153 else switch(posix_class
)
5159 if (ptype
== 0) ptype
= PT_PXPRINT
;
5162 if (ptype
== 0) ptype
= PT_PXPUNCT
;
5163 *class_uchardata
++ = local_negate
? XCL_NOTPROP
: XCL_PROP
;
5164 *class_uchardata
++ = ptype
;
5165 *class_uchardata
++ = 0;
5166 xclass_has_prop
= TRUE
;
5170 /* For the other POSIX classes (ascii, cntrl, xdigit) we are going
5171 to fall through to the non-UCP case and build a bit map for
5172 characters with code points less than 256. If we are in a negated
5173 POSIX class, characters with code points greater than 255 must
5174 either all match or all not match. In the special case where we
5175 have not yet generated any xclass data, and this is the final item
5176 in the overall class, we need do nothing: later on, the opcode
5177 OP_NCLASS will be used to indicate that characters greater than 255
5178 are acceptable. If we have already seen an xclass item or one may
5179 follow (we have to assume that it might if this is not the end of
5180 the class), explicitly list all wide codepoints, which will then
5181 either not match or match, depending on whether the class is or is
5186 (xclass
|| tempptr
[2] != CHAR_RIGHT_SQUARE_BRACKET
))
5188 *class_uchardata
++ = XCL_RANGE
;
5189 class_uchardata
+= PRIV(ord2utf
)(0x100, class_uchardata
);
5190 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
5196 /* In the non-UCP case, or when UCP makes no difference, we build the
5197 bit map for the POSIX class in a chunk of local store because we may be
5198 adding and subtracting from it, and we don't want to subtract bits that
5199 may be in the main map already. At the end we or the result into the
5200 bit map that is being built. */
5204 /* Copy in the first table (always present) */
5206 memcpy(pbits
, cbits
+ posix_class_maps
[posix_class
],
5207 32 * sizeof(pcre_uint8
));
5209 /* If there is a second table, add or remove it as required. */
5211 taboffset
= posix_class_maps
[posix_class
+ 1];
5212 tabopt
= posix_class_maps
[posix_class
+ 2];
5217 for (c
= 0; c
< 32; c
++) pbits
[c
] |= cbits
[c
+ taboffset
];
5219 for (c
= 0; c
< 32; c
++) pbits
[c
] &= ~cbits
[c
+ taboffset
];
5222 /* Now see if we need to remove any special characters. An option
5223 value of 1 removes vertical space and 2 removes underscore. */
5225 if (tabopt
< 0) tabopt
= -tabopt
;
5226 if (tabopt
== 1) pbits
[1] &= ~0x3c;
5227 else if (tabopt
== 2) pbits
[11] &= 0x7f;
5229 /* Add the POSIX table or its complement into the main table that is
5230 being built and we are done. */
5233 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~pbits
[c
];
5235 for (c
= 0; c
< 32; c
++) classbits
[c
] |= pbits
[c
];
5238 /* Every class contains at least one < 256 character. */
5239 class_has_8bitchar
= 1;
5240 /* Every class contains at least two characters. */
5242 continue; /* End of POSIX syntax handling */
5245 /* Backslash may introduce a single character, or it may introduce one
5246 of the specials, which just set a flag. The sequence \b is a special
5247 case. Inside a class (and only there) it is treated as backspace. We
5248 assume that other escapes have more than one character in them, so
5249 speculatively set both class_has_8bitchar and class_one_char bigger
5250 than one. Unrecognized escapes fall through and are either treated
5251 as literal characters (by default), or are faulted if
5252 PCRE_EXTRA is set. */
5254 if (c
== CHAR_BACKSLASH
&& (!posixre
|| nestptr
))
5256 escape
= check_escape(&ptr
, &ec
, errorcodeptr
, cd
->bracount
, options
,
5258 if (*errorcodeptr
!= 0) goto FAILED
;
5259 if (escape
== 0) c
= ec
;
5260 else if (escape
== ESC_b
) c
= CHAR_BS
; /* \b is backspace in a class */
5261 else if (escape
== ESC_N
) /* \N is not supported in a class */
5263 *errorcodeptr
= ERR71
;
5266 else if (escape
== ESC_Q
) /* Handle start of quoted string */
5268 if (ptr
[1] == CHAR_BACKSLASH
&& ptr
[2] == CHAR_E
)
5270 ptr
+= 2; /* avoid empty string */
5275 else if (escape
== ESC_E
) continue; /* Ignore orphan \E */
5279 register const pcre_uint8
*cbits
= cd
->cbits
;
5280 /* Every class contains at least two < 256 characters. */
5281 class_has_8bitchar
++;
5282 /* Every class contains at least two characters. */
5283 class_one_char
+= 2;
5288 case ESC_du
: /* These are the values given for \d etc */
5289 case ESC_DU
: /* when PCRE_UCP is set. We replace the */
5290 case ESC_wu
: /* escape sequence with an appropriate \p */
5291 case ESC_WU
: /* or \P to test Unicode properties instead */
5292 case ESC_su
: /* of the default ASCII testing. */
5295 ptr
= substitutes
[escape
- ESC_DU
] - 1; /* Just before substitute */
5296 class_has_8bitchar
--; /* Undo! */
5300 for (c
= 0; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_digit
];
5304 should_flip_negation
= TRUE
;
5305 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_digit
];
5309 for (c
= 0; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_word
];
5313 should_flip_negation
= TRUE
;
5314 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_word
];
5317 /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
5318 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
5319 previously set by something earlier in the character class.
5320 Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
5321 we could just adjust the appropriate bit. From PCRE 8.34 we no
5322 longer treat \s and \S specially. */
5325 for (c
= 0; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_space
];
5329 should_flip_negation
= TRUE
;
5330 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_space
];
5333 /* The rest apply in both UCP and non-UCP cases. */
5336 (void)add_list_to_class(classbits
, &class_uchardata
, options
, cd
,
5337 PRIV(hspace_list
), NOTACHAR
);
5341 (void)add_not_list_to_class(classbits
, &class_uchardata
, options
,
5342 cd
, PRIV(hspace_list
));
5346 (void)add_list_to_class(classbits
, &class_uchardata
, options
, cd
,
5347 PRIV(vspace_list
), NOTACHAR
);
5351 (void)add_not_list_to_class(classbits
, &class_uchardata
, options
,
5352 cd
, PRIV(vspace_list
));
5360 unsigned int ptype
= 0, pdata
= 0;
5361 if (!get_ucp(&ptr
, &negated
, &ptype
, &pdata
, errorcodeptr
))
5363 *class_uchardata
++ = ((escape
== ESC_p
) != negated
)?
5364 XCL_PROP
: XCL_NOTPROP
;
5365 *class_uchardata
++ = ptype
;
5366 *class_uchardata
++ = pdata
;
5367 xclass_has_prop
= TRUE
;
5368 class_has_8bitchar
--; /* Undo! */
5372 *errorcodeptr
= ERR45
;
5375 /* Unrecognized escapes are faulted if PCRE is running in its
5376 strict mode. By default, for compatibility with Perl, they are
5377 treated as literals. */
5380 if ((options
& PCRE_EXTRA
) != 0)
5382 *errorcodeptr
= ERR7
;
5385 class_has_8bitchar
--; /* Undo the speculative increase. */
5386 class_one_char
-= 2; /* Undo the speculative increase. */
5387 c
= *ptr
; /* Get the final character and fall through */
5392 /* Fall through if the escape just defined a single character (c >= 0).
5393 This may be greater than 256. */
5397 } /* End of backslash handling */
5399 /* A character may be followed by '-' to form a range. However, Perl does
5400 not permit ']' to be the end of the range. A '-' character at the end is
5401 treated as a literal. Perl ignores orphaned \E sequences entirely. The
5402 code for handling \Q and \E is messy. */
5405 while (ptr
[1] == CHAR_BACKSLASH
&& ptr
[2] == CHAR_E
&& (!posixre
|| nestptr
))
5412 /* Remember if \r or \n were explicitly used */
5414 if (c
== CHAR_CR
|| c
== CHAR_NL
) cd
->external_flags
|= PCRE_HASCRORLF
;
5416 /* Check for range */
5418 if (!inescq
&& ptr
[1] == CHAR_MINUS
)
5422 while (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
&& (!posixre
|| nestptr
)) ptr
+= 2;
5424 /* If we hit \Q (not followed by \E) at this point, go into escaped
5427 while (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_Q
&& (!posixre
|| nestptr
))
5430 if (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
)
5431 { ptr
+= 2; continue; }
5436 /* Minus (hyphen) at the end of a class is treated as a literal, so put
5437 back the pointer and jump to handle the character that preceded it. */
5439 if (*ptr
== CHAR_NULL
|| (!inescq
&& *ptr
== CHAR_RIGHT_SQUARE_BRACKET
))
5442 goto CLASS_SINGLE_CHARACTER
;
5445 /* Otherwise, we have a potential range; pick up the next character */
5449 { /* Braces are required because the */
5450 GETCHARLEN(d
, ptr
, ptr
); /* macro generates multiple statements */
5454 d
= *ptr
; /* Not UTF-8 mode */
5456 /* The second part of a range can be a single-character escape
5457 sequence, but not any of the other escapes. Perl treats a hyphen as a
5458 literal in such circumstances. However, in Perl's warning mode, a
5459 warning is given, so PCRE now faults it as it is almost certainly a
5460 mistake on the user's part. */
5464 if (d
== CHAR_BACKSLASH
&& (!posixre
|| nestptr
))
5467 descape
= check_escape(&ptr
, &d
, errorcodeptr
, cd
->bracount
, options
, TRUE
);
5468 if (*errorcodeptr
!= 0) goto FAILED
;
5470 /* 0 means a character was put into d; \b is backspace; any other
5471 special causes an error. */
5475 if (descape
== ESC_b
) d
= CHAR_BS
; else
5477 *errorcodeptr
= ERR83
;
5483 /* A hyphen followed by a POSIX class is treated in the same way. */
5485 else if (d
== CHAR_LEFT_SQUARE_BRACKET
&&
5486 (ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
5487 ptr
[1] == CHAR_EQUALS_SIGN
) &&
5488 check_posix_syntax(ptr
, &tempptr
))
5490 *errorcodeptr
= ERR83
;
5495 /* Check that the two values are in the correct order. Optimize
5496 one-character ranges. */
5500 *errorcodeptr
= ERR8
;
5503 if (d
== c
) goto CLASS_SINGLE_CHARACTER
; /* A few lines below */
5505 /* We have found a character range, so single character optimizations
5506 cannot be done anymore. Any value greater than 1 indicates that there
5507 is more than one character. */
5511 /* Remember an explicit \r or \n, and add the range to the class. */
5513 if (d
== CHAR_CR
|| d
== CHAR_NL
) cd
->external_flags
|= PCRE_HASCRORLF
;
5515 class_has_8bitchar
+=
5516 add_to_class(classbits
, &class_uchardata
, options
, cd
, c
, d
);
5518 continue; /* Go get the next char in the class */
5521 /* Handle a single character - we can get here for a normal non-escape
5522 char, or after \ that introduces a single character or for an apparent
5523 range that isn't. Only the value 1 matters for class_one_char, so don't
5524 increase it if it is already 2 or more ... just in case there's a class
5525 with a zillion characters in it. */
5527 CLASS_SINGLE_CHARACTER
:
5528 if (class_one_char
< 2) class_one_char
++;
5530 /* If xclass_has_prop is false and class_one_char is 1, we have the first
5531 single character in the class, and there have been no prior ranges, or
5532 XCLASS items generated by escapes. If this is the final character in the
5533 class, we can optimize by turning the item into a 1-character OP_CHAR[I]
5534 if it's positive, or OP_NOT[I] if it's negative. In the positive case, it
5535 can cause firstchar to be set. Otherwise, there can be no first char if
5536 this item is first, whatever repeat count may follow. In the case of
5537 reqchar, save the previous value for reinstating. */
5543 class_one_char
== 1 && ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
)
5546 zeroreqchar
= reqchar
;
5547 zeroreqcharflags
= reqcharflags
;
5554 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
5555 zerofirstchar
= firstchar
;
5556 zerofirstcharflags
= firstcharflags
;
5558 /* For caseless UTF-8 mode when UCP support is available, check
5559 whether this character has more than one other case. If so, generate
5560 a special OP_NOTPROP item instead of OP_NOTI. */
5563 if (utf
&& (options
& PCRE_CASELESS
) != 0 &&
5564 (d
= UCD_CASESET(c
)) != 0)
5566 *code
++ = OP_NOTPROP
;
5572 /* Char has only one other case, or UCP not available */
5575 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_NOTI
: OP_NOT
;
5576 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5577 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
5578 code
+= PRIV(ord2utf
)(c
, code
);
5584 /* We are finished with this character class */
5589 /* For a single, positive character, get the value into mcbuffer, and
5590 then we can handle this with the normal one-character code. */
5592 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5593 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
5594 mclength
= PRIV(ord2utf
)(c
, mcbuffer
);
5602 } /* End of 1-char optimization */
5604 /* There is more than one character in the class, or an XCLASS item
5605 has been generated. Add this character to the class. */
5607 class_has_8bitchar
+=
5608 add_to_class(classbits
, &class_uchardata
, options
, cd
, c
, c
);
5611 /* Loop until ']' reached. This "while" is the end of the "do" far above.
5612 If we are at the end of an internal nested string, revert to the outer
5615 while (((c
= *(++ptr
)) != CHAR_NULL
||
5617 (ptr
= nestptr
, nestptr
= NULL
, c
= *(++ptr
)) != CHAR_NULL
) ||
5618 (inescq
&& embednul
&& ptr
< cd
->end_pattern
) ||
5619 (embednul
&& ptr
< cd
->end_pattern
&&
5620 (nestptr
= ptr
+ 1, ptr
= sub_embedded_nul
, c
= *(++ptr
)) != CHAR_NULL
)) &&
5621 (c
!= CHAR_RIGHT_SQUARE_BRACKET
|| inescq
));
5623 /* Check for missing terminating ']' */
5627 *errorcodeptr
= ERR6
;
5631 /* We will need an XCLASS if data has been placed in class_uchardata. In
5632 the second phase this is a sufficient test. However, in the pre-compile
5633 phase, class_uchardata gets emptied to prevent workspace overflow, so it
5634 only if the very last character in the class needs XCLASS will it contain
5635 anything at this point. For this reason, xclass gets set TRUE above when
5636 uchar_classdata is emptied, and that's why this code is the way it is here
5637 instead of just doing a test on class_uchardata below. */
5639 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5640 if (class_uchardata
> class_uchardata_base
) xclass
= TRUE
;
5643 /* If this is the first thing in the branch, there can be no first char
5644 setting, whatever the repeat count. Any reqchar setting must remain
5645 unchanged after any kind of repeat. */
5647 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
5648 zerofirstchar
= firstchar
;
5649 zerofirstcharflags
= firstcharflags
;
5650 zeroreqchar
= reqchar
;
5651 zeroreqcharflags
= reqcharflags
;
5653 /* If there are characters with values > 255, we have to compile an
5654 extended class, with its own opcode, unless there was a negated special
5655 such as \S in the class, and PCRE_UCP is not set, because in that case all
5656 characters > 255 are in the class, so any that were explicitly given as
5657 well can be ignored. If (when there are explicit characters > 255 that must
5658 be listed) there are no characters < 256, we can omit the bitmap in the
5659 actual compiled code. */
5662 if (xclass
&& (xclass_has_prop
|| !should_flip_negation
||
5663 (options
& PCRE_UCP
) != 0))
5664 #elif !defined COMPILE_PCRE8
5665 if (xclass
&& (xclass_has_prop
|| !should_flip_negation
))
5667 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5669 /* For non-UCP wide characters, in a non-negative class containing \S or
5670 similar (should_flip_negation is set), all characters greater than 255
5671 must be in the class. */
5674 #if defined COMPILE_PCRE8
5677 should_flip_negation
&& !negate_class
&& (options
& PCRE_UCP
) == 0)
5679 *class_uchardata
++ = XCL_RANGE
;
5680 if (utf
) /* Will always be utf in the 8-bit library */
5682 class_uchardata
+= PRIV(ord2utf
)(0x100, class_uchardata
);
5683 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
5685 else /* Can only happen for the 16-bit & 32-bit libraries */
5687 #if defined COMPILE_PCRE16
5688 *class_uchardata
++ = 0x100;
5689 *class_uchardata
++ = 0xffffu
;
5690 #elif defined COMPILE_PCRE32
5691 *class_uchardata
++ = 0x100;
5692 *class_uchardata
++ = 0xffffffffu
;
5697 *class_uchardata
++ = XCL_END
; /* Marks the end of extra data */
5698 *code
++ = OP_XCLASS
;
5700 *code
= negate_class
? XCL_NOT
:0;
5701 if (xclass_has_prop
) *code
|= XCL_HASPROP
;
5703 /* If the map is required, move up the extra data to make room for it;
5704 otherwise just move the code pointer to the end of the extra data. */
5706 if (class_has_8bitchar
> 0)
5709 memmove(code
+ (32 / sizeof(pcre_uchar
)), code
,
5710 IN_UCHARS(class_uchardata
- code
));
5711 if (negate_class
&& !xclass_has_prop
)
5712 for (c
= 0; c
< 32; c
++) classbits
[c
] = ~classbits
[c
];
5713 memcpy(code
, classbits
, 32);
5714 code
= class_uchardata
+ (32 / sizeof(pcre_uchar
));
5716 else code
= class_uchardata
;
5718 /* Now fill in the complete length of the item */
5720 PUT(previous
, 1, (int)(code
- previous
));
5721 break; /* End of class handling */
5724 /* Even though any XCLASS list is now discarded, we must allow for
5727 if (lengthptr
!= NULL
)
5728 *lengthptr
+= (int)(class_uchardata
- class_uchardata_base
);
5731 /* If there are no characters > 255, or they are all to be included or
5732 excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
5733 whole class was negated and whether there were negative specials such as \S
5734 (non-UCP) in the class. Then copy the 32-byte map into the code vector,
5735 negating it if necessary. */
5737 *code
++ = (negate_class
== should_flip_negation
) ? OP_CLASS
: OP_NCLASS
;
5738 if (lengthptr
== NULL
) /* Save time in the pre-compile phase */
5741 for (c
= 0; c
< 32; c
++) classbits
[c
] = ~classbits
[c
];
5742 memcpy(code
, classbits
, 32);
5744 code
+= 32 / sizeof(pcre_uchar
);
5750 /* ===================================================================*/
5751 /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
5752 has been tested above. */
5754 case CHAR_LEFT_CURLY_BRACKET
:
5755 if (!is_quantifier
) goto NORMAL_CHAR
;
5756 ptr
= read_repeat_counts(ptr
+1, &repeat_min
, &repeat_max
, errorcodeptr
);
5757 if (*errorcodeptr
!= 0) goto FAILED
;
5770 case CHAR_QUESTION_MARK
:
5775 if (previous
== NULL
)
5777 *errorcodeptr
= ERR9
;
5781 if (repeat_min
== 0)
5783 firstchar
= zerofirstchar
; /* Adjust for zero repeat */
5784 firstcharflags
= zerofirstcharflags
;
5785 reqchar
= zeroreqchar
; /* Ditto */
5786 reqcharflags
= zeroreqcharflags
;
5789 /* Remember whether this is a variable length repeat */
5791 reqvary
= (repeat_min
== repeat_max
)? 0 : REQ_VARY
;
5793 op_type
= 0; /* Default single-char op codes */
5794 possessive_quantifier
= FALSE
; /* Default not possessive quantifier */
5796 /* Save start of previous item, in case we have to move it up in order to
5797 insert something before it. */
5799 tempcode
= previous
;
5801 /* Before checking for a possessive quantifier, we must skip over
5802 whitespace and comments in extended mode because Perl allows white space at
5805 if ((options
& PCRE_EXTENDED
) != 0)
5807 const pcre_uchar
*p
= ptr
+ 1;
5810 while (MAX_255(*p
) && (cd
->ctypes
[*p
] & ctype_space
) != 0) p
++;
5811 if (*p
!= CHAR_NUMBER_SIGN
) break;
5813 while (*p
!= CHAR_NULL
|| (nestptr
== NULL
&& embednul
&& p
< cd
->end_pattern
))
5815 if (IS_NEWLINE(p
)) /* For non-fixed-length newline cases, */
5816 { /* IS_NEWLINE sets cd->nllen. */
5822 if (utf
) FORWARDCHAR(p
);
5824 } /* Loop for comment characters */
5825 } /* Loop for multiple comments */
5826 ptr
= p
- 1; /* Character before the next significant one. */
5829 /* We also need to skip over (?# comments, which are not dependent on
5832 if (ptr
[1] == CHAR_LEFT_PARENTHESIS
&& ptr
[2] == CHAR_QUESTION_MARK
&&
5833 ptr
[3] == CHAR_NUMBER_SIGN
&& (!basicre
|| nestptr
))
5836 while (*ptr
!= CHAR_NULL
&& *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
5837 if (*ptr
== CHAR_NULL
)
5839 *errorcodeptr
= ERR18
;
5844 /* If the next character is '+', we have a possessive quantifier. This
5845 implies greediness, whatever the setting of the PCRE_UNGREEDY option.
5846 If the next character is '?' this is a minimizing repeat, by default,
5847 but if PCRE_UNGREEDY is set, it works the other way round. We change the
5848 repeat type to the non-default. */
5850 if (ptr
[1] == CHAR_PLUS
&& (!basicre
|| nestptr
))
5852 repeat_type
= 0; /* Force greedy */
5853 possessive_quantifier
= TRUE
;
5856 else if (ptr
[1] == CHAR_QUESTION_MARK
&& (!basicre
|| nestptr
))
5858 repeat_type
= greedy_non_default
;
5861 else repeat_type
= greedy_default
;
5863 /* If previous was a recursion call, wrap it in atomic brackets so that
5864 previous becomes the atomic group. All recursions were so wrapped in the
5865 past, but it no longer happens for non-repeated recursions. In fact, the
5866 repeated ones could be re-implemented independently so as not to need this,
5867 but for the moment we rely on the code for repeating groups. */
5869 if (*previous
== OP_RECURSE
)
5871 memmove(previous
+ 1 + LINK_SIZE
, previous
, IN_UCHARS(1 + LINK_SIZE
));
5872 *previous
= OP_ONCE
;
5873 PUT(previous
, 1, 2 + 2*LINK_SIZE
);
5874 previous
[2 + 2*LINK_SIZE
] = OP_KET
;
5875 PUT(previous
, 3 + 2*LINK_SIZE
, 2 + 2*LINK_SIZE
);
5876 code
+= 2 + 2 * LINK_SIZE
;
5877 length_prevgroup
= 3 + 3*LINK_SIZE
;
5879 /* When actually compiling, we need to check whether this was a forward
5880 reference, and if so, adjust the offset. */
5882 if (lengthptr
== NULL
&& cd
->hwm
>= cd
->start_workspace
+ LINK_SIZE
)
5884 int offset
= GET(cd
->hwm
, -LINK_SIZE
);
5885 if (offset
== previous
+ 1 - cd
->start_code
)
5886 PUT(cd
->hwm
, -LINK_SIZE
, offset
+ 1 + LINK_SIZE
);
5890 /* Now handle repetition for the different types of item. */
5892 /* If previous was a character or negated character match, abolish the item
5893 and generate a repeat item instead. If a char item has a minimum of more
5894 than one, ensure that it is set in reqchar - it might not be if a sequence
5895 such as x{3} is the first thing in a branch because the x will have gone
5896 into firstchar instead. */
5898 if (*previous
== OP_CHAR
|| *previous
== OP_CHARI
5899 || *previous
== OP_NOT
|| *previous
== OP_NOTI
)
5903 default: /* Make compiler happy. */
5904 case OP_CHAR
: op_type
= OP_STAR
- OP_STAR
; break;
5905 case OP_CHARI
: op_type
= OP_STARI
- OP_STAR
; break;
5906 case OP_NOT
: op_type
= OP_NOTSTAR
- OP_STAR
; break;
5907 case OP_NOTI
: op_type
= OP_NOTSTARI
- OP_STAR
; break;
5910 /* Deal with UTF characters that take up more than one character. It's
5911 easier to write this out separately than try to macrify it. Use c to
5912 hold the length of the character in bytes, plus UTF_LENGTH to flag that
5913 it's a length rather than a small character. */
5915 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5916 if (utf
&& NOT_FIRSTCHAR(code
[-1]))
5918 pcre_uchar
*lastchar
= code
- 1;
5920 c
= (int)(code
- lastchar
); /* Length of UTF-8 character */
5921 memcpy(utf_chars
, lastchar
, IN_UCHARS(c
)); /* Save the char */
5922 c
|= UTF_LENGTH
; /* Flag c as a length */
5925 #endif /* SUPPORT_UTF */
5927 /* Handle the case of a single charater - either with no UTF support, or
5928 with UTF disabled, or for a single character UTF character. */
5931 if (*previous
<= OP_CHARI
&& repeat_min
> 1)
5934 reqcharflags
= req_caseopt
| cd
->req_varyopt
;
5938 goto OUTPUT_SINGLE_REPEAT
; /* Code shared with single character types */
5941 /* If previous was a character type match (\d or similar), abolish it and
5942 create a suitable repeat item. The code is shared with single-character
5943 repeats by setting op_type to add a suitable offset into repeat_type. Note
5944 the the Unicode property types will be present only when SUPPORT_UCP is
5945 defined, but we don't wrap the little bits of code here because it just
5946 makes it horribly messy. */
5948 else if (*previous
< OP_EODN
)
5950 pcre_uchar
*oldcode
;
5951 int prop_type
, prop_value
;
5952 op_type
= OP_TYPESTAR
- OP_STAR
; /* Use type opcodes */
5955 OUTPUT_SINGLE_REPEAT
:
5956 if (*previous
== OP_PROP
|| *previous
== OP_NOTPROP
)
5958 prop_type
= previous
[1];
5959 prop_value
= previous
[2];
5961 else prop_type
= prop_value
= -1;
5964 code
= previous
; /* Usually overwrite previous item */
5966 /* If the maximum is zero then the minimum must also be zero; Perl allows
5967 this case, so we do too - by simply omitting the item altogether. */
5969 if (repeat_max
== 0) goto END_REPEAT
;
5971 /* Combine the op_type with the repeat_type */
5973 repeat_type
+= op_type
;
5975 /* A minimum of zero is handled either as the special case * or ?, or as
5976 an UPTO, with the maximum given. */
5978 if (repeat_min
== 0)
5980 if (repeat_max
== -1) *code
++ = OP_STAR
+ repeat_type
;
5981 else if (repeat_max
== 1) *code
++ = OP_QUERY
+ repeat_type
;
5984 *code
++ = OP_UPTO
+ repeat_type
;
5985 PUT2INC(code
, 0, repeat_max
);
5989 /* A repeat minimum of 1 is optimized into some special cases. If the
5990 maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
5991 left in place and, if the maximum is greater than 1, we use OP_UPTO with
5992 one less than the maximum. */
5994 else if (repeat_min
== 1)
5996 if (repeat_max
== -1)
5997 *code
++ = OP_PLUS
+ repeat_type
;
6000 code
= oldcode
; /* leave previous item in place */
6001 if (repeat_max
== 1) goto END_REPEAT
;
6002 *code
++ = OP_UPTO
+ repeat_type
;
6003 PUT2INC(code
, 0, repeat_max
- 1);
6007 /* The case {n,n} is just an EXACT, while the general case {n,m} is
6008 handled as an EXACT followed by an UPTO. */
6012 *code
++ = OP_EXACT
+ op_type
; /* NB EXACT doesn't have repeat_type */
6013 PUT2INC(code
, 0, repeat_min
);
6015 /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
6016 we have to insert the character for the previous code. For a repeated
6017 Unicode property match, there are two extra bytes that define the
6018 required property. In UTF-8 mode, long characters have their length in
6019 c, with the UTF_LENGTH bit as a flag. */
6023 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6024 if (utf
&& (c
& UTF_LENGTH
) != 0)
6026 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
6035 *code
++ = prop_type
;
6036 *code
++ = prop_value
;
6039 *code
++ = OP_STAR
+ repeat_type
;
6042 /* Else insert an UPTO if the max is greater than the min, again
6043 preceded by the character, for the previously inserted code. If the
6044 UPTO is just for 1 instance, we can use QUERY instead. */
6046 else if (repeat_max
!= repeat_min
)
6048 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6049 if (utf
&& (c
& UTF_LENGTH
) != 0)
6051 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
6059 *code
++ = prop_type
;
6060 *code
++ = prop_value
;
6062 repeat_max
-= repeat_min
;
6064 if (repeat_max
== 1)
6066 *code
++ = OP_QUERY
+ repeat_type
;
6070 *code
++ = OP_UPTO
+ repeat_type
;
6071 PUT2INC(code
, 0, repeat_max
);
6076 /* The character or character type itself comes last in all cases. */
6078 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6079 if (utf
&& (c
& UTF_LENGTH
) != 0)
6081 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
6088 /* For a repeated Unicode property match, there are two extra bytes that
6089 define the required property. */
6094 *code
++ = prop_type
;
6095 *code
++ = prop_value
;
6100 /* If previous was a character class or a back reference, we put the repeat
6101 stuff after it, but just skip the item if the repeat was {0,0}. */
6103 else if (*previous
== OP_CLASS
|| *previous
== OP_NCLASS
||
6104 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6105 *previous
== OP_XCLASS
||
6107 *previous
== OP_REF
|| *previous
== OP_REFI
||
6108 *previous
== OP_DNREF
|| *previous
== OP_DNREFI
)
6110 if (repeat_max
== 0)
6116 if (repeat_min
== 0 && repeat_max
== -1)
6117 *code
++ = OP_CRSTAR
+ repeat_type
;
6118 else if (repeat_min
== 1 && repeat_max
== -1)
6119 *code
++ = OP_CRPLUS
+ repeat_type
;
6120 else if (repeat_min
== 0 && repeat_max
== 1)
6121 *code
++ = OP_CRQUERY
+ repeat_type
;
6124 *code
++ = OP_CRRANGE
+ repeat_type
;
6125 PUT2INC(code
, 0, repeat_min
);
6126 if (repeat_max
== -1) repeat_max
= 0; /* 2-byte encoding for max */
6127 PUT2INC(code
, 0, repeat_max
);
6131 /* If previous was a bracket group, we may have to replicate it in certain
6132 cases. Note that at this point we can encounter only the "basic" bracket
6133 opcodes such as BRA and CBRA, as this is the place where they get converted
6134 into the more special varieties such as BRAPOS and SBRA. A test for >=
6135 OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
6136 ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND.
6137 Originally, PCRE did not allow repetition of assertions, but now it does,
6138 for Perl compatibility. */
6140 else if (*previous
>= OP_ASSERT
&& *previous
<= OP_COND
)
6143 int len
= (int)(code
- previous
);
6144 size_t base_hwm_offset
= item_hwm_offset
;
6145 pcre_uchar
*bralink
= NULL
;
6146 pcre_uchar
*brazeroptr
= NULL
;
6148 /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
6149 we just ignore the repeat. */
6151 if (*previous
== OP_COND
&& previous
[LINK_SIZE
+1] == OP_DEF
)
6154 /* There is no sense in actually repeating assertions. The only potential
6155 use of repetition is in cases when the assertion is optional. Therefore,
6156 if the minimum is greater than zero, just ignore the repeat. If the
6157 maximum is not zero or one, set it to 1. */
6159 if (*previous
< OP_ONCE
) /* Assertion */
6161 if (repeat_min
> 0) goto END_REPEAT
;
6162 if (repeat_max
< 0 || repeat_max
> 1) repeat_max
= 1;
6165 /* The case of a zero minimum is special because of the need to stick
6166 OP_BRAZERO in front of it, and because the group appears once in the
6167 data, whereas in other cases it appears the minimum number of times. For
6168 this reason, it is simplest to treat this case separately, as otherwise
6169 the code gets far too messy. There are several special subcases when the
6172 if (repeat_min
== 0)
6174 /* If the maximum is also zero, we used to just omit the group from the
6175 output altogether, like this:
6177 ** if (repeat_max == 0)
6183 However, that fails when a group or a subgroup within it is referenced
6184 as a subroutine from elsewhere in the pattern, so now we stick in
6185 OP_SKIPZERO in front of it so that it is skipped on execution. As we
6186 don't have a list of which groups are referenced, we cannot do this
6189 If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
6190 and do no more at this point. However, we do need to adjust any
6191 OP_RECURSE calls inside the group that refer to the group itself or any
6192 internal or forward referenced group, because the offset is from the
6193 start of the whole regex. Temporarily terminate the pattern while doing
6196 if (repeat_max
<= 1) /* Covers 0, 1, and unlimited */
6199 adjust_recurse(previous
, 1, utf
, cd
, item_hwm_offset
);
6200 memmove(previous
+ 1, previous
, IN_UCHARS(len
));
6202 if (repeat_max
== 0)
6204 *previous
++ = OP_SKIPZERO
;
6207 brazeroptr
= previous
; /* Save for possessive optimizing */
6208 *previous
++ = OP_BRAZERO
+ repeat_type
;
6211 /* If the maximum is greater than 1 and limited, we have to replicate
6212 in a nested fashion, sticking OP_BRAZERO before each set of brackets.
6213 The first one has to be handled carefully because it's the original
6214 copy, which has to be moved up. The remainder can be handled by code
6215 that is common with the non-zero minimum case below. We have to
6216 adjust the value or repeat_max, since one less copy is required. Once
6217 again, we may have to adjust any OP_RECURSE calls inside the group. */
6223 adjust_recurse(previous
, 2 + LINK_SIZE
, utf
, cd
, item_hwm_offset
);
6224 memmove(previous
+ 2 + LINK_SIZE
, previous
, IN_UCHARS(len
));
6225 code
+= 2 + LINK_SIZE
;
6226 *previous
++ = OP_BRAZERO
+ repeat_type
;
6227 *previous
++ = OP_BRA
;
6229 /* We chain together the bracket offset fields that have to be
6230 filled in later when the ends of the brackets are reached. */
6232 offset
= (bralink
== NULL
)? 0 : (int)(previous
- bralink
);
6234 PUTINC(previous
, 0, offset
);
6240 /* If the minimum is greater than zero, replicate the group as many
6241 times as necessary, and adjust the maximum to the number of subsequent
6242 copies that we need. If we set a first char from the group, and didn't
6243 set a required char, copy the latter from the former. If there are any
6244 forward reference subroutine calls in the group, there will be entries on
6245 the workspace list; replicate these with an appropriate increment. */
6251 /* In the pre-compile phase, we don't actually do the replication. We
6252 just adjust the length as if we had. Do some paranoid checks for
6253 potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
6254 integer type when available, otherwise double. */
6256 if (lengthptr
!= NULL
)
6258 int delta
= (repeat_min
- 1)*length_prevgroup
;
6259 if ((INT64_OR_DOUBLE
)(repeat_min
- 1)*
6260 (INT64_OR_DOUBLE
)length_prevgroup
>
6261 (INT64_OR_DOUBLE
)INT_MAX
||
6262 OFLOW_MAX
- *lengthptr
< delta
)
6264 *errorcodeptr
= ERR20
;
6267 *lengthptr
+= delta
;
6270 /* This is compiling for real. If there is a set first byte for
6271 the group, and we have not yet set a "required byte", set it. Make
6272 sure there is enough workspace for copying forward references before
6277 if (groupsetfirstchar
&& reqcharflags
< 0)
6279 reqchar
= firstchar
;
6280 reqcharflags
= firstcharflags
;
6283 for (i
= 1; i
< repeat_min
; i
++)
6286 size_t this_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
6287 memcpy(code
, previous
, IN_UCHARS(len
));
6289 while (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
-
6290 WORK_SIZE_SAFETY_MARGIN
-
6291 (this_hwm_offset
- base_hwm_offset
))
6293 *errorcodeptr
= expand_workspace(cd
);
6294 if (*errorcodeptr
!= 0) goto FAILED
;
6297 for (hc
= (pcre_uchar
*)cd
->start_workspace
+ base_hwm_offset
;
6298 hc
< (pcre_uchar
*)cd
->start_workspace
+ this_hwm_offset
;
6301 PUT(cd
->hwm
, 0, GET(hc
, 0) + len
);
6302 cd
->hwm
+= LINK_SIZE
;
6304 base_hwm_offset
= this_hwm_offset
;
6310 if (repeat_max
> 0) repeat_max
-= repeat_min
;
6313 /* This code is common to both the zero and non-zero minimum cases. If
6314 the maximum is limited, it replicates the group in a nested fashion,
6315 remembering the bracket starts on a stack. In the case of a zero minimum,
6316 the first one was set up above. In all cases the repeat_max now specifies
6317 the number of additional copies needed. Again, we must remember to
6318 replicate entries on the forward reference list. */
6320 if (repeat_max
>= 0)
6322 /* In the pre-compile phase, we don't actually do the replication. We
6323 just adjust the length as if we had. For each repetition we must add 1
6324 to the length for BRAZERO and for all but the last repetition we must
6325 add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
6326 paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
6327 a 64-bit integer type when available, otherwise double. */
6329 if (lengthptr
!= NULL
&& repeat_max
> 0)
6331 int delta
= repeat_max
* (length_prevgroup
+ 1 + 2 + 2*LINK_SIZE
) -
6332 2 - 2*LINK_SIZE
; /* Last one doesn't nest */
6333 if ((INT64_OR_DOUBLE
)repeat_max
*
6334 (INT64_OR_DOUBLE
)(length_prevgroup
+ 1 + 2 + 2*LINK_SIZE
)
6335 > (INT64_OR_DOUBLE
)INT_MAX
||
6336 OFLOW_MAX
- *lengthptr
< delta
)
6338 *errorcodeptr
= ERR20
;
6341 *lengthptr
+= delta
;
6344 /* This is compiling for real */
6346 else for (i
= repeat_max
- 1; i
>= 0; i
--)
6349 size_t this_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
6351 *code
++ = OP_BRAZERO
+ repeat_type
;
6353 /* All but the final copy start a new nesting, maintaining the
6354 chain of brackets outstanding. */
6360 offset
= (bralink
== NULL
)? 0 : (int)(code
- bralink
);
6362 PUTINC(code
, 0, offset
);
6365 memcpy(code
, previous
, IN_UCHARS(len
));
6367 /* Ensure there is enough workspace for forward references before
6370 while (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
-
6371 WORK_SIZE_SAFETY_MARGIN
-
6372 (this_hwm_offset
- base_hwm_offset
))
6374 *errorcodeptr
= expand_workspace(cd
);
6375 if (*errorcodeptr
!= 0) goto FAILED
;
6378 for (hc
= (pcre_uchar
*)cd
->start_workspace
+ base_hwm_offset
;
6379 hc
< (pcre_uchar
*)cd
->start_workspace
+ this_hwm_offset
;
6382 PUT(cd
->hwm
, 0, GET(hc
, 0) + len
+ ((i
!= 0)? 2+LINK_SIZE
: 1));
6383 cd
->hwm
+= LINK_SIZE
;
6385 base_hwm_offset
= this_hwm_offset
;
6389 /* Now chain through the pending brackets, and fill in their length
6390 fields (which are holding the chain links pro tem). */
6392 while (bralink
!= NULL
)
6395 int offset
= (int)(code
- bralink
+ 1);
6396 pcre_uchar
*bra
= code
- offset
;
6397 oldlinkoffset
= GET(bra
, 1);
6398 bralink
= (oldlinkoffset
== 0)? NULL
: bralink
- oldlinkoffset
;
6400 PUTINC(code
, 0, offset
);
6401 PUT(bra
, 1, offset
);
6405 /* If the maximum is unlimited, set a repeater in the final copy. For
6406 ONCE brackets, that's all we need to do. However, possessively repeated
6407 ONCE brackets can be converted into non-capturing brackets, as the
6408 behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
6409 deal with possessive ONCEs specially.
6411 Otherwise, when we are doing the actual compile phase, check to see
6412 whether this group is one that could match an empty string. If so,
6413 convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
6414 that runtime checking can be done. [This check is also applied to ONCE
6415 groups at runtime, but in a different way.]
6417 Then, if the quantifier was possessive and the bracket is not a
6418 conditional, we convert the BRA code to the POS form, and the KET code to
6419 KETRPOS. (It turns out to be convenient at runtime to detect this kind of
6420 subpattern at both the start and at the end.) The use of special opcodes
6421 makes it possible to reduce greatly the stack usage in pcre_exec(). If
6422 the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
6424 Then, if the minimum number of matches is 1 or 0, cancel the possessive
6425 flag so that the default action below, of wrapping everything inside
6426 atomic brackets, does not happen. When the minimum is greater than 1,
6427 there will be earlier copies of the group, and so we still have to wrap
6432 pcre_uchar
*ketcode
= code
- 1 - LINK_SIZE
;
6433 pcre_uchar
*bracode
= ketcode
- GET(ketcode
, 1);
6435 /* Convert possessive ONCE brackets to non-capturing */
6437 if ((*bracode
== OP_ONCE
|| *bracode
== OP_ONCE_NC
) &&
6438 possessive_quantifier
) *bracode
= OP_BRA
;
6440 /* For non-possessive ONCE brackets, all we need to do is to
6443 if (*bracode
== OP_ONCE
|| *bracode
== OP_ONCE_NC
)
6444 *ketcode
= OP_KETRMAX
+ repeat_type
;
6446 /* Handle non-ONCE brackets and possessive ONCEs (which have been
6447 converted to non-capturing above). */
6451 /* In the compile phase, check for empty string matching. */
6453 if (lengthptr
== NULL
)
6455 pcre_uchar
*scode
= bracode
;
6458 if (could_be_empty_branch(scode
, ketcode
, utf
, cd
, NULL
))
6460 *bracode
+= OP_SBRA
- OP_BRA
;
6463 scode
+= GET(scode
, 1);
6465 while (*scode
== OP_ALT
);
6468 /* A conditional group with only one branch has an implicit empty
6469 alternative branch. */
6471 if (*bracode
== OP_COND
&& bracode
[GET(bracode
,1)] != OP_ALT
)
6472 *bracode
= OP_SCOND
;
6474 /* Handle possessive quantifiers. */
6476 if (possessive_quantifier
)
6478 /* For COND brackets, we wrap the whole thing in a possessively
6479 repeated non-capturing bracket, because we have not invented POS
6480 versions of the COND opcodes. Because we are moving code along, we
6481 must ensure that any pending recursive references are updated. */
6483 if (*bracode
== OP_COND
|| *bracode
== OP_SCOND
)
6485 int nlen
= (int)(code
- bracode
);
6487 adjust_recurse(bracode
, 1 + LINK_SIZE
, utf
, cd
, item_hwm_offset
);
6488 memmove(bracode
+ 1 + LINK_SIZE
, bracode
, IN_UCHARS(nlen
));
6489 code
+= 1 + LINK_SIZE
;
6490 nlen
+= 1 + LINK_SIZE
;
6491 *bracode
= (*bracode
== OP_COND
)? OP_BRAPOS
: OP_SBRAPOS
;
6492 *code
++ = OP_KETRPOS
;
6493 PUTINC(code
, 0, nlen
);
6494 PUT(bracode
, 1, nlen
);
6497 /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
6501 *bracode
+= 1; /* Switch to xxxPOS opcodes */
6502 *ketcode
= OP_KETRPOS
;
6505 /* If the minimum is zero, mark it as possessive, then unset the
6506 possessive flag when the minimum is 0 or 1. */
6508 if (brazeroptr
!= NULL
) *brazeroptr
= OP_BRAPOSZERO
;
6509 if (repeat_min
< 2) possessive_quantifier
= FALSE
;
6512 /* Non-possessive quantifier */
6514 else *ketcode
= OP_KETRMAX
+ repeat_type
;
6519 /* If previous is OP_FAIL, it was generated by an empty class [] in
6520 JavaScript mode. The other ways in which OP_FAIL can be generated, that is
6521 by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
6522 error above. We can just ignore the repeat in JS case. */
6524 else if (*previous
== OP_FAIL
) goto END_REPEAT
;
6526 /* Else there's some kind of shambles */
6530 *errorcodeptr
= ERR11
;
6534 /* If the character following a repeat is '+', possessive_quantifier is
6535 TRUE. For some opcodes, there are special alternative opcodes for this
6536 case. For anything else, we wrap the entire repeated item inside OP_ONCE
6537 brackets. Logically, the '+' notation is just syntactic sugar, taken from
6538 Sun's Java package, but the special opcodes can optimize it.
6540 Some (but not all) possessively repeated subpatterns have already been
6541 completely handled in the code just above. For them, possessive_quantifier
6542 is always FALSE at this stage. Note that the repeated item starts at
6543 tempcode, not at previous, which might be the first part of a string whose
6544 (former) last char we repeated. */
6546 if (possessive_quantifier
)
6550 /* Possessifying an EXACT quantifier has no effect, so we can ignore it.
6551 However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6},
6552 {5,}, or {5,10}). We skip over an EXACT item; if the length of what
6553 remains is greater than zero, there's a further opcode that can be
6554 handled. If not, do nothing, leaving the EXACT alone. */
6559 tempcode
+= PRIV(OP_lengths
)[*tempcode
] +
6560 ((tempcode
[1 + IMM2_SIZE
] == OP_PROP
6561 || tempcode
[1 + IMM2_SIZE
] == OP_NOTPROP
)? 2 : 0);
6564 /* CHAR opcodes are used for exacts whose count is 1. */
6574 tempcode
+= PRIV(OP_lengths
)[*tempcode
];
6576 if (utf
&& HAS_EXTRALEN(tempcode
[-1]))
6577 tempcode
+= GET_EXTRALEN(tempcode
[-1]);
6581 /* For the class opcodes, the repeat operator appears at the end;
6582 adjust tempcode to point to it. */
6586 tempcode
+= 1 + 32/sizeof(pcre_uchar
);
6589 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6591 tempcode
+= GET(tempcode
, 1);
6596 /* If tempcode is equal to code (which points to the end of the repeated
6597 item), it means we have skipped an EXACT item but there is no following
6598 QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In
6599 all other cases, tempcode will be pointing to the repeat opcode, and will
6600 be less than code, so the value of len will be greater than 0. */
6602 len
= (int)(code
- tempcode
);
6605 unsigned int repcode
= *tempcode
;
6607 /* There is a table for possessifying opcodes, all of which are less
6608 than OP_CALLOUT. A zero entry means there is no possessified version.
6611 if (repcode
< OP_CALLOUT
&& opcode_possessify
[repcode
] > 0)
6612 *tempcode
= opcode_possessify
[repcode
];
6614 /* For opcode without a special possessified version, wrap the item in
6615 ONCE brackets. Because we are moving code along, we must ensure that any
6616 pending recursive references are updated. */
6621 adjust_recurse(tempcode
, 1 + LINK_SIZE
, utf
, cd
, item_hwm_offset
);
6622 memmove(tempcode
+ 1 + LINK_SIZE
, tempcode
, IN_UCHARS(len
));
6623 code
+= 1 + LINK_SIZE
;
6624 len
+= 1 + LINK_SIZE
;
6625 tempcode
[0] = OP_ONCE
;
6627 PUTINC(code
, 0, len
);
6628 PUT(tempcode
, 1, len
);
6633 if (len
> 0) switch (*tempcode
)
6635 case OP_STAR
: *tempcode
= OP_POSSTAR
; break;
6636 case OP_PLUS
: *tempcode
= OP_POSPLUS
; break;
6637 case OP_QUERY
: *tempcode
= OP_POSQUERY
; break;
6638 case OP_UPTO
: *tempcode
= OP_POSUPTO
; break;
6640 case OP_STARI
: *tempcode
= OP_POSSTARI
; break;
6641 case OP_PLUSI
: *tempcode
= OP_POSPLUSI
; break;
6642 case OP_QUERYI
: *tempcode
= OP_POSQUERYI
; break;
6643 case OP_UPTOI
: *tempcode
= OP_POSUPTOI
; break;
6645 case OP_NOTSTAR
: *tempcode
= OP_NOTPOSSTAR
; break;
6646 case OP_NOTPLUS
: *tempcode
= OP_NOTPOSPLUS
; break;
6647 case OP_NOTQUERY
: *tempcode
= OP_NOTPOSQUERY
; break;
6648 case OP_NOTUPTO
: *tempcode
= OP_NOTPOSUPTO
; break;
6650 case OP_NOTSTARI
: *tempcode
= OP_NOTPOSSTARI
; break;
6651 case OP_NOTPLUSI
: *tempcode
= OP_NOTPOSPLUSI
; break;
6652 case OP_NOTQUERYI
: *tempcode
= OP_NOTPOSQUERYI
; break;
6653 case OP_NOTUPTOI
: *tempcode
= OP_NOTPOSUPTOI
; break;
6655 case OP_TYPESTAR
: *tempcode
= OP_TYPEPOSSTAR
; break;
6656 case OP_TYPEPLUS
: *tempcode
= OP_TYPEPOSPLUS
; break;
6657 case OP_TYPEQUERY
: *tempcode
= OP_TYPEPOSQUERY
; break;
6658 case OP_TYPEUPTO
: *tempcode
= OP_TYPEPOSUPTO
; break;
6660 case OP_CRSTAR
: *tempcode
= OP_CRPOSSTAR
; break;
6661 case OP_CRPLUS
: *tempcode
= OP_CRPOSPLUS
; break;
6662 case OP_CRQUERY
: *tempcode
= OP_CRPOSQUERY
; break;
6663 case OP_CRRANGE
: *tempcode
= OP_CRPOSRANGE
; break;
6665 /* Because we are moving code along, we must ensure that any
6666 pending recursive references are updated. */
6670 adjust_recurse(tempcode
, 1 + LINK_SIZE
, utf
, cd
, item_hwm_offset
);
6671 memmove(tempcode
+ 1 + LINK_SIZE
, tempcode
, IN_UCHARS(len
));
6672 code
+= 1 + LINK_SIZE
;
6673 len
+= 1 + LINK_SIZE
;
6674 tempcode
[0] = OP_ONCE
;
6676 PUTINC(code
, 0, len
);
6677 PUT(tempcode
, 1, len
);
6683 /* In all case we no longer have a previous item. We also set the
6684 "follows varying string" flag for subsequently encountered reqchars if
6685 it isn't already set and we have just passed a varying length item. */
6689 cd
->req_varyopt
|= reqvary
;
6693 /* ===================================================================*/
6694 /* Start of nested parenthesized sub-expression, or comment or lookahead or
6695 lookbehind or option setting or condition or all the other extended
6696 parenthesis forms. */
6698 case CHAR_LEFT_PARENTHESIS
:
6701 /* Now deal with various "verbs" that can be introduced by '*'. */
6703 if (ptr
[0] == CHAR_ASTERISK
&& (!basicre
|| nestptr
) && (ptr
[1] == ':'
6704 || (MAX_255(ptr
[1]) && ((cd
->ctypes
[ptr
[1]] & ctype_letter
) != 0))))
6708 const char *vn
= verbnames
;
6709 const pcre_uchar
*name
= ptr
+ 1;
6710 const pcre_uchar
*arg
= NULL
;
6713 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_letter
) != 0) ptr
++;
6714 namelen
= (int)(ptr
- name
);
6716 /* It appears that Perl allows any characters whatsoever, other than
6717 a closing parenthesis, to appear in arguments, so we no longer insist on
6718 letters, digits, and underscores. */
6720 if (*ptr
== CHAR_COLON
)
6723 while (*ptr
!= CHAR_NULL
&& *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
6724 arglen
= (int)(ptr
- arg
);
6725 if ((unsigned int)arglen
> MAX_MARK
)
6727 *errorcodeptr
= ERR75
;
6732 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
6734 *errorcodeptr
= ERR60
;
6738 /* Scan the table of verb names */
6740 for (i
= 0; i
< verbcount
; i
++)
6742 if (namelen
== verbs
[i
].len
&&
6743 STRNCMP_UC_C8(name
, vn
, namelen
) == 0)
6747 /* Check for open captures before ACCEPT and convert it to
6748 ASSERT_ACCEPT if in an assertion. */
6750 if (verbs
[i
].op
== OP_ACCEPT
)
6755 *errorcodeptr
= ERR59
;
6758 cd
->had_accept
= TRUE
;
6759 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
6761 if (lengthptr
!= NULL
)
6763 #ifdef COMPILE_PCRE8
6764 *lengthptr
+= 1 + IMM2_SIZE
;
6765 #elif defined COMPILE_PCRE16
6766 *lengthptr
+= 2 + IMM2_SIZE
;
6767 #elif defined COMPILE_PCRE32
6768 *lengthptr
+= 4 + IMM2_SIZE
;
6774 PUT2INC(code
, 0, oc
->number
);
6778 (cd
->assert_depth
> 0)? OP_ASSERT_ACCEPT
: OP_ACCEPT
;
6780 /* Do not set firstchar after *ACCEPT */
6781 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
6784 /* Handle other cases with/without an argument */
6786 else if (arglen
== 0)
6788 if (verbs
[i
].op
< 0) /* Argument is mandatory */
6790 *errorcodeptr
= ERR66
;
6793 setverb
= *code
++ = verbs
[i
].op
;
6798 if (verbs
[i
].op_arg
< 0) /* Argument is forbidden */
6800 *errorcodeptr
= ERR59
;
6803 setverb
= *code
++ = verbs
[i
].op_arg
;
6804 if (lengthptr
!= NULL
) /* In pass 1 just add in the length */
6805 { /* to avoid potential workspace */
6806 *lengthptr
+= arglen
; /* overflow. */
6812 memcpy(code
, arg
, IN_UCHARS(arglen
));
6822 cd
->external_flags
|= PCRE_HASTHEN
;
6829 cd
->had_pruneorskip
= TRUE
;
6833 break; /* Found verb, exit loop */
6836 vn
+= verbs
[i
].len
+ 1;
6839 if (i
< verbcount
) continue; /* Successfully handled a verb */
6840 *errorcodeptr
= ERR60
; /* Verb not recognized */
6844 /* Initialize for "real" parentheses */
6846 newoptions
= options
;
6849 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
6850 reset_bracount
= FALSE
;
6852 /* Deal with the extended parentheses; all are introduced by '?', and the
6853 appearance of any of them means that this is not a capturing group. */
6855 if (*ptr
== CHAR_QUESTION_MARK
&& (!basicre
|| nestptr
))
6857 int i
, set
, unset
, namelen
;
6859 const pcre_uchar
*name
;
6864 /* ------------------------------------------------------------ */
6865 case CHAR_VERTICAL_LINE
: /* Reset capture count for each branch */
6866 reset_bracount
= TRUE
;
6867 cd
->dupgroups
= TRUE
; /* Record (?| encountered */
6870 /* ------------------------------------------------------------ */
6871 case CHAR_COLON
: /* Non-capturing bracket */
6877 /* ------------------------------------------------------------ */
6878 case CHAR_LEFT_PARENTHESIS
:
6879 bravalue
= OP_COND
; /* Conditional group */
6882 /* A condition can be an assertion, a number (referring to a numbered
6883 group's having been set), a name (referring to a named group), or 'R',
6884 referring to recursion. R<digits> and R&name are also permitted for
6887 There are ways of testing a named group: (?(name)) is used by Python;
6888 Perl 5.10 onwards uses (?(<name>) or (?('name')).
6890 There is one unfortunate ambiguity, caused by history. 'R' can be the
6891 recursive thing or the name 'R' (and similarly for 'R' followed by
6892 digits). We look for a name first; if not found, we try the other case.
6894 For compatibility with auto-callouts, we allow a callout to be
6895 specified before a condition that is an assertion. First, check for the
6896 syntax of a callout; if found, adjust the temporary pointer that is
6897 used to check for an assertion condition. That's all that is needed! */
6899 if (ptr
[1] == CHAR_QUESTION_MARK
&& ptr
[2] == CHAR_C
)
6901 for (i
= 3;; i
++) if (!IS_DIGIT(ptr
[i
])) break;
6902 if (ptr
[i
] == CHAR_RIGHT_PARENTHESIS
)
6905 /* tempptr should now be pointing to the opening parenthesis of the
6906 assertion condition. */
6908 if (*tempptr
!= CHAR_LEFT_PARENTHESIS
)
6910 *errorcodeptr
= ERR28
;
6915 /* For conditions that are assertions, check the syntax, and then exit
6916 the switch. This will take control down to where bracketed groups,
6917 including assertions, are processed. */
6919 if (tempptr
[1] == CHAR_QUESTION_MARK
&&
6920 (tempptr
[2] == CHAR_EQUALS_SIGN
||
6921 tempptr
[2] == CHAR_EXCLAMATION_MARK
||
6922 (tempptr
[2] == CHAR_LESS_THAN_SIGN
&&
6923 (tempptr
[3] == CHAR_EQUALS_SIGN
||
6924 tempptr
[3] == CHAR_EXCLAMATION_MARK
))))
6926 cd
->iscondassert
= TRUE
;
6930 /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all
6931 need to skip at least 1+IMM2_SIZE bytes at the start of the group. */
6933 code
[1+LINK_SIZE
] = OP_CREF
;
6934 skipbytes
= 1+IMM2_SIZE
;
6935 refsign
= -1; /* => not a number */
6936 namelen
= -1; /* => not a name; must set to avoid warning */
6937 name
= NULL
; /* Always set to avoid warning */
6938 recno
= 0; /* Always set to avoid warning */
6940 /* Check for a test for recursion in a named group. */
6943 if (*ptr
== CHAR_R
&& ptr
[1] == CHAR_AMPERSAND
)
6947 code
[1+LINK_SIZE
] = OP_RREF
; /* Change the type of test */
6950 /* Check for a test for a named group's having been set, using the Perl
6951 syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6952 syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */
6954 else if (*ptr
== CHAR_LESS_THAN_SIGN
)
6956 terminator
= CHAR_GREATER_THAN_SIGN
;
6959 else if (*ptr
== CHAR_APOSTROPHE
)
6961 terminator
= CHAR_APOSTROPHE
;
6966 terminator
= CHAR_NULL
;
6967 if (*ptr
== CHAR_MINUS
|| *ptr
== CHAR_PLUS
) refsign
= *ptr
++;
6968 else if (IS_DIGIT(*ptr
)) refsign
= 0;
6971 /* Handle a number */
6975 while (IS_DIGIT(*ptr
))
6977 if (recno
> INT_MAX
/ 10 - 1) /* Integer overflow */
6979 while (IS_DIGIT(*ptr
)) ptr
++;
6980 *errorcodeptr
= ERR61
;
6983 recno
= recno
* 10 + (int)(*ptr
- CHAR_0
);
6988 /* Otherwise we expect to read a name; anything else is an error. When
6989 a name is one of a number of duplicates, a different opcode is used and
6990 it needs more memory. Unfortunately we cannot tell whether a name is a
6991 duplicate in the first pass, so we have to allow for more memory. */
6997 *errorcodeptr
= ERR84
;
7000 if (!MAX_255(*ptr
) || (cd
->ctypes
[*ptr
] & ctype_word
) == 0)
7002 *errorcodeptr
= ERR28
; /* Assertion expected */
7006 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0)
7010 namelen
= (int)(ptr
- name
);
7011 if (lengthptr
!= NULL
) skipbytes
+= IMM2_SIZE
;
7014 /* Check the terminator */
7016 if ((terminator
> 0 && *ptr
++ != (pcre_uchar
)terminator
) ||
7017 *ptr
++ != CHAR_RIGHT_PARENTHESIS
)
7019 ptr
--; /* Error offset */
7020 *errorcodeptr
= ERR26
; /* Malformed number or name */
7024 /* Do no further checking in the pre-compile phase. */
7026 if (lengthptr
!= NULL
) break;
7028 /* In the real compile we do the work of looking for the actual
7029 reference. If refsign is not negative, it means we have a number in
7036 *errorcodeptr
= ERR35
;
7039 if (refsign
!= 0) recno
= (refsign
== CHAR_MINUS
)?
7040 cd
->bracount
- recno
+ 1 : recno
+ cd
->bracount
;
7041 if (recno
<= 0 || recno
> cd
->final_bracount
)
7043 *errorcodeptr
= ERR15
;
7046 PUT2(code
, 2+LINK_SIZE
, recno
);
7047 if (recno
> cd
->top_backref
) cd
->top_backref
= recno
;
7051 /* Otherwise look for the name. */
7053 slot
= cd
->name_table
;
7054 for (i
= 0; i
< cd
->names_found
; i
++)
7056 if (STRNCMP_UC_UC(name
, slot
+IMM2_SIZE
, namelen
) == 0 &&
7057 slot
[IMM2_SIZE
+namelen
] == 0) break;
7058 slot
+= cd
->name_entry_size
;
7061 /* Found the named subpattern. If the name is duplicated, add one to
7062 the opcode to change CREF/RREF into DNCREF/DNRREF and insert
7063 appropriate data values. Otherwise, just insert the unique subpattern
7066 if (i
< cd
->names_found
)
7070 recno
= GET2(slot
, 0); /* Number from first found */
7071 if (recno
> cd
->top_backref
) cd
->top_backref
= recno
;
7072 for (; i
< cd
->names_found
; i
++)
7074 slot
+= cd
->name_entry_size
;
7075 if (STRNCMP_UC_UC(name
, slot
+IMM2_SIZE
, namelen
) != 0 ||
7076 (slot
+IMM2_SIZE
)[namelen
] != 0) break;
7082 PUT2(code
, 2+LINK_SIZE
, offset
);
7083 PUT2(code
, 2+LINK_SIZE
+IMM2_SIZE
, count
);
7084 skipbytes
+= IMM2_SIZE
;
7085 code
[1+LINK_SIZE
]++;
7087 else /* Not a duplicated name */
7089 PUT2(code
, 2+LINK_SIZE
, recno
);
7093 /* If terminator == CHAR_NULL it means that the name followed directly
7094 after the opening parenthesis [e.g. (?(abc)...] and in this case there
7095 are some further alternatives to try. For the cases where terminator !=
7096 CHAR_NULL [things like (?(<name>... or (?('name')... or (?(R&name)... ]
7097 we have now checked all the possibilities, so give an error. */
7099 else if (terminator
!= CHAR_NULL
)
7101 *errorcodeptr
= ERR15
;
7105 /* Check for (?(R) for recursion. Allow digits after R to specify a
7106 specific group number. */
7108 else if (*name
== CHAR_R
)
7111 for (i
= 1; i
< namelen
; i
++)
7113 if (!IS_DIGIT(name
[i
]))
7115 *errorcodeptr
= ERR15
;
7118 if (recno
> INT_MAX
/ 10 - 1) /* Integer overflow */
7120 *errorcodeptr
= ERR61
;
7123 recno
= recno
* 10 + name
[i
] - CHAR_0
;
7125 if (recno
== 0) recno
= RREF_ANY
;
7126 code
[1+LINK_SIZE
] = OP_RREF
; /* Change test type */
7127 PUT2(code
, 2+LINK_SIZE
, recno
);
7130 /* Similarly, check for the (?(DEFINE) "condition", which is always
7133 else if (namelen
== 6 && STRNCMP_UC_C8(name
, STRING_DEFINE
, 6) == 0)
7135 code
[1+LINK_SIZE
] = OP_DEF
;
7139 /* Reference to an unidentified subpattern. */
7143 *errorcodeptr
= ERR15
;
7149 /* ------------------------------------------------------------ */
7150 case CHAR_EQUALS_SIGN
: /* Positive lookahead */
7151 bravalue
= OP_ASSERT
;
7152 cd
->assert_depth
+= 1;
7156 /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird
7157 thing to do, but Perl allows all assertions to be quantified, and when
7158 they contain capturing parentheses there may be a potential use for
7159 this feature. Not that that applies to a quantified (?!) but we allow
7160 it for uniformity. */
7162 /* ------------------------------------------------------------ */
7163 case CHAR_EXCLAMATION_MARK
: /* Negative lookahead */
7165 if (*ptr
== CHAR_RIGHT_PARENTHESIS
&& ptr
[1] != CHAR_ASTERISK
&&
7166 ptr
[1] != CHAR_PLUS
&& ptr
[1] != CHAR_QUESTION_MARK
&&
7167 (ptr
[1] != CHAR_LEFT_CURLY_BRACKET
|| !is_counted_repeat(ptr
+2, FALSE
)))
7173 bravalue
= OP_ASSERT_NOT
;
7174 cd
->assert_depth
+= 1;
7178 /* ------------------------------------------------------------ */
7179 case CHAR_LESS_THAN_SIGN
: /* Lookbehind or named define */
7182 case CHAR_EQUALS_SIGN
: /* Positive lookbehind */
7183 bravalue
= OP_ASSERTBACK
;
7184 cd
->assert_depth
+= 1;
7188 case CHAR_EXCLAMATION_MARK
: /* Negative lookbehind */
7189 bravalue
= OP_ASSERTBACK_NOT
;
7190 cd
->assert_depth
+= 1;
7194 default: /* Could be name define, else bad */
7195 if (MAX_255(ptr
[1]) && (cd
->ctypes
[ptr
[1]] & ctype_word
) != 0)
7197 ptr
++; /* Correct offset for error */
7198 *errorcodeptr
= ERR24
;
7204 /* ------------------------------------------------------------ */
7205 case CHAR_GREATER_THAN_SIGN
: /* One-time brackets */
7211 /* ------------------------------------------------------------ */
7212 case CHAR_C
: /* Callout - may be followed by digits; */
7213 previous_callout
= code
; /* Save for later completion */
7214 after_manual_callout
= 1; /* Skip one item before completing */
7215 *code
++ = OP_CALLOUT
;
7219 while(IS_DIGIT(*ptr
))
7221 n
= n
* 10 + *ptr
++ - CHAR_0
;
7224 *errorcodeptr
= ERR38
;
7228 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
7230 *errorcodeptr
= ERR39
;
7234 PUT(code
, 0, (int)(ptr
- cd
->start_pattern
+ 1)); /* Pattern offset */
7235 PUT(code
, LINK_SIZE
, 0); /* Default length */
7236 code
+= 2 * LINK_SIZE
;
7242 /* ------------------------------------------------------------ */
7243 case CHAR_P
: /* Python-style named subpattern handling */
7244 if (*(++ptr
) == CHAR_EQUALS_SIGN
||
7245 *ptr
== CHAR_GREATER_THAN_SIGN
) /* Reference or recursion */
7247 is_recurse
= *ptr
== CHAR_GREATER_THAN_SIGN
;
7248 terminator
= CHAR_RIGHT_PARENTHESIS
;
7249 goto NAMED_REF_OR_RECURSE
;
7251 else if (*ptr
!= CHAR_LESS_THAN_SIGN
) /* Test for Python-style defn */
7253 *errorcodeptr
= ERR41
;
7256 /* Fall through to handle (?P< as (?< is handled */
7259 /* ------------------------------------------------------------ */
7260 DEFINE_NAME
: /* Come here from (?< handling */
7261 case CHAR_APOSTROPHE
:
7262 terminator
= (*ptr
== CHAR_LESS_THAN_SIGN
)?
7263 CHAR_GREATER_THAN_SIGN
: CHAR_APOSTROPHE
;
7267 *errorcodeptr
= ERR84
; /* Group name must start with non-digit */
7270 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0) ptr
++;
7271 namelen
= (int)(ptr
- name
);
7273 /* In the pre-compile phase, do a syntax check, remember the longest
7274 name, and then remember the group in a vector, expanding it if
7275 necessary. Duplicates for the same number are skipped; other duplicates
7276 are checked for validity. In the actual compile, there is nothing to
7279 if (lengthptr
!= NULL
)
7282 pcre_uint32 number
= cd
->bracount
+ 1;
7284 if (*ptr
!= (pcre_uchar
)terminator
)
7286 *errorcodeptr
= ERR42
;
7290 if (cd
->names_found
>= MAX_NAME_COUNT
)
7292 *errorcodeptr
= ERR49
;
7296 if (namelen
+ IMM2_SIZE
+ 1 > cd
->name_entry_size
)
7298 cd
->name_entry_size
= namelen
+ IMM2_SIZE
+ 1;
7299 if (namelen
> MAX_NAME_SIZE
)
7301 *errorcodeptr
= ERR48
;
7306 /* Scan the list to check for duplicates. For duplicate names, if the
7307 number is the same, break the loop, which causes the name to be
7308 discarded; otherwise, if DUPNAMES is not set, give an error.
7309 If it is set, allow the name with a different number, but continue
7310 scanning in case this is a duplicate with the same number. For
7311 non-duplicate names, give an error if the number is duplicated. */
7313 ng
= cd
->named_groups
;
7314 for (i
= 0; i
< cd
->names_found
; i
++, ng
++)
7316 if (namelen
== ng
->length
&&
7317 STRNCMP_UC_UC(name
, ng
->name
, namelen
) == 0)
7319 if (ng
->number
== number
) break;
7320 if ((options
& PCRE_DUPNAMES
) == 0)
7322 *errorcodeptr
= ERR43
;
7325 cd
->dupnames
= TRUE
; /* Duplicate names exist */
7327 else if (ng
->number
== number
)
7329 *errorcodeptr
= ERR65
;
7334 if (i
>= cd
->names_found
) /* Not a duplicate with same number */
7336 /* Increase the list size if necessary */
7338 if (cd
->names_found
>= cd
->named_group_list_size
)
7340 int newsize
= cd
->named_group_list_size
* 2;
7341 named_group
*newspace
= (PUBL(malloc
))
7342 (newsize
* sizeof(named_group
));
7344 if (newspace
== NULL
)
7346 *errorcodeptr
= ERR21
;
7350 memcpy(newspace
, cd
->named_groups
,
7351 cd
->named_group_list_size
* sizeof(named_group
));
7352 if (cd
->named_group_list_size
> NAMED_GROUP_LIST_SIZE
)
7353 (PUBL(free
))((void *)cd
->named_groups
);
7354 cd
->named_groups
= newspace
;
7355 cd
->named_group_list_size
= newsize
;
7358 cd
->named_groups
[cd
->names_found
].name
= name
;
7359 cd
->named_groups
[cd
->names_found
].length
= namelen
;
7360 cd
->named_groups
[cd
->names_found
].number
= number
;
7365 ptr
++; /* Move past > or ' in both passes. */
7366 goto NUMBERED_GROUP
;
7369 /* ------------------------------------------------------------ */
7370 case CHAR_AMPERSAND
: /* Perl recursion/subroutine syntax */
7371 terminator
= CHAR_RIGHT_PARENTHESIS
;
7375 /* We come here from the Python syntax above that handles both
7376 references (?P=name) and recursion (?P>name), as well as falling
7377 through from the Perl recursion syntax (?&name). We also come here from
7378 the Perl \k<name> or \k'name' back reference syntax and the \k{name}
7379 .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
7381 NAMED_REF_OR_RECURSE
:
7385 *errorcodeptr
= ERR84
; /* Group name must start with non-digit */
7388 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0) ptr
++;
7389 namelen
= (int)(ptr
- name
);
7391 /* In the pre-compile phase, do a syntax check. We used to just set
7392 a dummy reference number, because it was not used in the first pass.
7393 However, with the change of recursive back references to be atomic,
7394 we have to look for the number so that this state can be identified, as
7395 otherwise the incorrect length is computed. If it's not a backwards
7396 reference, the dummy number will do. */
7398 if (lengthptr
!= NULL
)
7405 *errorcodeptr
= ERR62
;
7408 if (*ptr
!= (pcre_uchar
)terminator
)
7410 *errorcodeptr
= ERR42
;
7413 if (namelen
> MAX_NAME_SIZE
)
7415 *errorcodeptr
= ERR48
;
7419 /* Count named back references. */
7421 if (!is_recurse
) cd
->namedrefcount
++;
7423 /* We have to allow for a named reference to a duplicated name (this
7424 cannot be determined until the second pass). This needs an extra
7425 16-bit data item. */
7427 *lengthptr
+= IMM2_SIZE
;
7429 /* If this is a forward reference and we are within a (?|...) group,
7430 the reference may end up as the number of a group which we are
7431 currently inside, that is, it could be a recursive reference. In the
7432 real compile this will be picked up and the reference wrapped with
7433 OP_ONCE to make it atomic, so we must space in case this occurs. */
7435 /* In fact, this can happen for a non-forward reference because
7436 another group with the same number might be created later. This
7437 issue is fixed "properly" in PCRE2. As PCRE1 is now in maintenance
7438 only mode, we finesse the bug by allowing more memory always. */
7440 *lengthptr
+= 4 + 4*LINK_SIZE
;
7442 /* It is even worse than that. The current reference may be to an
7443 existing named group with a different number (so apparently not
7444 recursive) but which later on is also attached to a group with the
7445 current number. This can only happen if $(| has been previous
7446 encountered. In that case, we allow yet more memory, just in case.
7447 (Again, this is fixed "properly" in PCRE2. */
7449 if (cd
->dupgroups
) *lengthptr
+= 4 + 4*LINK_SIZE
;
7451 /* Otherwise, check for recursion here. The name table does not exist
7452 in the first pass; instead we must scan the list of names encountered
7453 so far in order to get the number. If the name is not found, leave
7454 the value of recno as 0 for a forward reference. */
7456 /* This patch (removing "else") fixes a problem when a reference is
7457 to multiple identically named nested groups from within the nest.
7458 Once again, it is not the "proper" fix, and it results in an
7459 over-allocation of memory. */
7463 ng
= cd
->named_groups
;
7464 for (i
= 0; i
< cd
->names_found
; i
++, ng
++)
7466 if (namelen
== ng
->length
&&
7467 STRNCMP_UC_UC(name
, ng
->name
, namelen
) == 0)
7471 if (is_recurse
) break;
7472 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
7474 if (oc
->number
== recno
)
7485 /* In the real compile, search the name table. We check the name
7486 first, and then check that we have reached the end of the name in the
7487 table. That way, if the name is longer than any in the table, the
7488 comparison will fail without reading beyond the table entry. */
7492 slot
= cd
->name_table
;
7493 for (i
= 0; i
< cd
->names_found
; i
++)
7495 if (STRNCMP_UC_UC(name
, slot
+IMM2_SIZE
, namelen
) == 0 &&
7496 slot
[IMM2_SIZE
+namelen
] == 0)
7498 slot
+= cd
->name_entry_size
;
7501 if (i
< cd
->names_found
)
7503 recno
= GET2(slot
, 0);
7507 *errorcodeptr
= ERR15
;
7512 /* In both phases, for recursions, we can now go to the code than
7513 handles numerical recursion. */
7515 if (is_recurse
) goto HANDLE_RECURSION
;
7517 /* In the second pass we must see if the name is duplicated. If so, we
7518 generate a different opcode. */
7520 if (lengthptr
== NULL
&& cd
->dupnames
)
7523 unsigned int index
= i
;
7524 pcre_uchar
*cslot
= slot
+ cd
->name_entry_size
;
7526 for (i
++; i
< cd
->names_found
; i
++)
7528 if (STRCMP_UC_UC(slot
+ IMM2_SIZE
, cslot
+ IMM2_SIZE
) != 0) break;
7530 cslot
+= cd
->name_entry_size
;
7535 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
7537 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
7538 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_DNREFI
: OP_DNREF
;
7539 PUT2INC(code
, 0, index
);
7540 PUT2INC(code
, 0, count
);
7542 /* Process each potentially referenced group. */
7544 for (; slot
< cslot
; slot
+= cd
->name_entry_size
)
7547 recno
= GET2(slot
, 0);
7548 cd
->backref_map
|= (recno
< 32)? (1U << recno
) : 1;
7549 if (recno
> cd
->top_backref
) cd
->top_backref
= recno
;
7551 /* Check to see if this back reference is recursive, that it, it
7552 is inside the group that it references. A flag is set so that the
7553 group can be made atomic. */
7555 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
7557 if (oc
->number
== recno
)
7565 continue; /* End of back ref handling */
7569 /* First pass, or a non-duplicated name. */
7571 goto HANDLE_REFERENCE
;
7574 /* ------------------------------------------------------------ */
7575 case CHAR_R
: /* Recursion, same as (?0) */
7577 if (*(++ptr
) != CHAR_RIGHT_PARENTHESIS
)
7579 *errorcodeptr
= ERR29
;
7582 goto HANDLE_RECURSION
;
7585 /* ------------------------------------------------------------ */
7586 case CHAR_MINUS
: case CHAR_PLUS
: /* Recursion or subroutine */
7587 case CHAR_0
: case CHAR_1
: case CHAR_2
: case CHAR_3
: case CHAR_4
:
7588 case CHAR_5
: case CHAR_6
: case CHAR_7
: case CHAR_8
: case CHAR_9
:
7590 const pcre_uchar
*called
;
7591 terminator
= CHAR_RIGHT_PARENTHESIS
;
7593 /* Come here from the \g<...> and \g'...' code (Oniguruma
7594 compatibility). However, the syntax has been checked to ensure that
7595 the ... are a (signed) number, so that neither ERR63 nor ERR29 will
7596 be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
7599 HANDLE_NUMERICAL_RECURSION
:
7601 if ((refsign
= *ptr
) == CHAR_PLUS
)
7604 if (!IS_DIGIT(*ptr
))
7606 *errorcodeptr
= ERR63
;
7610 else if (refsign
== CHAR_MINUS
)
7612 if (!IS_DIGIT(ptr
[1]))
7613 goto OTHER_CHAR_AFTER_QUERY
;
7618 while(IS_DIGIT(*ptr
))
7620 if (recno
> INT_MAX
/ 10 - 1) /* Integer overflow */
7622 while (IS_DIGIT(*ptr
)) ptr
++;
7623 *errorcodeptr
= ERR61
;
7626 recno
= recno
* 10 + *ptr
++ - CHAR_0
;
7629 if (*ptr
!= (pcre_uchar
)terminator
)
7631 *errorcodeptr
= ERR29
;
7635 if (refsign
== CHAR_MINUS
)
7639 *errorcodeptr
= ERR58
;
7642 recno
= cd
->bracount
- recno
+ 1;
7645 *errorcodeptr
= ERR15
;
7649 else if (refsign
== CHAR_PLUS
)
7653 *errorcodeptr
= ERR58
;
7656 recno
+= cd
->bracount
;
7659 /* Come here from code above that handles a named recursion */
7664 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
7665 called
= cd
->start_code
;
7667 /* When we are actually compiling, find the bracket that is being
7668 referenced. Temporarily end the regex in case it doesn't exist before
7669 this point. If we end up with a forward reference, first check that
7670 the bracket does occur later so we can give the error (and position)
7671 now. Then remember this forward reference in the workspace so it can
7672 be filled in at the end. */
7674 if (lengthptr
== NULL
)
7678 called
= PRIV(find_bracket
)(cd
->start_code
, utf
, recno
);
7680 /* Forward reference */
7684 if (recno
> cd
->final_bracount
)
7686 *errorcodeptr
= ERR15
;
7690 /* Fudge the value of "called" so that when it is inserted as an
7691 offset below, what it actually inserted is the reference number
7692 of the group. Then remember the forward reference. */
7694 called
= cd
->start_code
+ recno
;
7695 if (cd
->hwm
>= cd
->start_workspace
+ cd
->workspace_size
-
7696 WORK_SIZE_SAFETY_MARGIN
)
7698 *errorcodeptr
= expand_workspace(cd
);
7699 if (*errorcodeptr
!= 0) goto FAILED
;
7701 PUTINC(cd
->hwm
, 0, (int)(code
+ 1 - cd
->start_code
));
7704 /* If not a forward reference, and the subpattern is still open,
7705 this is a recursive call. We check to see if this is a left
7706 recursion that could loop for ever, and diagnose that case. We
7707 must not, however, do this check if we are in a conditional
7708 subpattern because the condition might be testing for recursion in
7709 a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
7710 Forever loops are also detected at runtime, so those that occur in
7711 conditional subpatterns will be picked up then. */
7713 else if (GET(called
, 1) == 0 && cond_depth
<= 0 &&
7714 could_be_empty(called
, code
, bcptr
, utf
, cd
))
7716 *errorcodeptr
= ERR40
;
7721 /* Insert the recursion/subroutine item. It does not have a set first
7722 character (relevant if it is repeated, because it will then be
7723 wrapped with ONCE brackets). */
7726 PUT(code
, 1, (int)(called
- cd
->start_code
));
7727 code
+= 1 + LINK_SIZE
;
7728 groupsetfirstchar
= FALSE
;
7731 /* Can't determine a first byte now */
7733 if (firstcharflags
== REQ_UNSET
) firstcharflags
= REQ_NONE
;
7734 zerofirstchar
= firstchar
;
7735 zerofirstcharflags
= firstcharflags
;
7739 /* ------------------------------------------------------------ */
7740 default: /* Other characters: check option setting */
7741 OTHER_CHAR_AFTER_QUERY
:
7745 while (*ptr
!= CHAR_RIGHT_PARENTHESIS
&& *ptr
!= CHAR_COLON
)
7749 case CHAR_MINUS
: optset
= &unset
; break;
7751 case CHAR_J
: /* Record that it changed in the external options */
7752 *optset
|= PCRE_DUPNAMES
;
7753 cd
->external_flags
|= PCRE_JCHANGED
;
7756 case CHAR_i
: *optset
|= PCRE_CASELESS
; break;
7757 case CHAR_m
: *optset
|= PCRE_MULTILINE
; break;
7758 case CHAR_s
: *optset
|= PCRE_DOTALL
; break;
7759 case CHAR_x
: *optset
|= PCRE_EXTENDED
; break;
7760 case CHAR_U
: *optset
|= PCRE_UNGREEDY
; break;
7761 case CHAR_X
: *optset
|= PCRE_EXTRA
; break;
7763 default: *errorcodeptr
= ERR12
;
7764 ptr
--; /* Correct the offset */
7769 /* Set up the changed option bits, but don't change anything yet. */
7771 newoptions
= (options
| set
) & (~unset
);
7773 /* If the options ended with ')' this is not the start of a nested
7774 group with option changes, so the options change at this level.
7775 If we are not at the pattern start, reset the greedy defaults and the
7776 case value for firstchar and reqchar. */
7778 if (*ptr
== CHAR_RIGHT_PARENTHESIS
)
7780 greedy_default
= ((newoptions
& PCRE_UNGREEDY
) != 0);
7781 greedy_non_default
= greedy_default
^ 1;
7782 req_caseopt
= ((newoptions
& PCRE_CASELESS
) != 0)? REQ_CASELESS
:0;
7784 /* Change options at this level, and pass them back for use
7785 in subsequent branches. */
7787 *optionsptr
= options
= newoptions
;
7788 previous
= NULL
; /* This item can't be repeated */
7789 continue; /* It is complete */
7792 /* If the options ended with ':' we are heading into a nested group
7793 with possible change of options. Such groups are non-capturing and are
7794 not assertions of any kind. All we need to do is skip over the ':';
7795 the newoptions value is handled below. */
7799 } /* End of switch for character following (? */
7800 } /* End of (? handling */
7802 /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
7803 is set, all unadorned brackets become non-capturing and behave like (?:...)
7806 else if ((options
& PCRE_NO_AUTO_CAPTURE
) != 0)
7811 /* Else we have a capturing group. */
7817 PUT2(code
, 1+LINK_SIZE
, cd
->bracount
);
7818 skipbytes
= IMM2_SIZE
;
7821 /* Process nested bracketed regex. First check for parentheses nested too
7824 if ((cd
->parens_depth
+= 1) > PARENS_NEST_LIMIT
)
7826 *errorcodeptr
= ERR82
;
7830 /* All assertions used not to be repeatable, but this was changed for Perl
7831 compatibility. All kinds can now be repeated except for assertions that are
7832 conditions (Perl also forbids these to be repeated). We copy code into a
7833 non-register variable (tempcode) in order to be able to pass its address
7834 because some compilers complain otherwise. At the start of a conditional
7835 group whose condition is an assertion, cd->iscondassert is set. We unset it
7836 here so as to allow assertions later in the group to be quantified. */
7838 if (bravalue
>= OP_ASSERT
&& bravalue
<= OP_ASSERTBACK_NOT
&&
7842 cd
->iscondassert
= FALSE
;
7847 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
7852 tempreqvary
= cd
->req_varyopt
; /* Save value before bracket */
7853 tempbracount
= cd
->bracount
; /* Save value before bracket */
7854 length_prevgroup
= 0; /* Initialize for pre-compile phase */
7856 if (embednul
&& nestptr
!= NULL
)
7857 cd
->extended_options
&= ~PCRE_ALLOW_EMBEDDED_NUL_BIT
;
7859 newoptions
, /* The complete new option state */
7860 &tempcode
, /* Where to put code (updated) */
7861 &ptr
, /* Input pointer (updated) */
7862 errorcodeptr
, /* Where to put an error message */
7863 (bravalue
== OP_ASSERTBACK
||
7864 bravalue
== OP_ASSERTBACK_NOT
), /* TRUE if back assert */
7865 reset_bracount
, /* True if (?| group */
7866 skipbytes
, /* Skip over bracket number */
7868 ((bravalue
== OP_COND
)?1:0), /* Depth of condition subpatterns */
7869 &subfirstchar
, /* For possible first char */
7871 &subreqchar
, /* For possible last char */
7873 bcptr
, /* Current branch chain */
7874 cd
, /* Tables block */
7875 (lengthptr
== NULL
)? NULL
: /* Actual compile phase */
7876 &length_prevgroup
/* Pre-compile phase */
7879 if (embednul
&& nestptr
!= NULL
)
7880 cd
->extended_options
|= PCRE_ALLOW_EMBEDDED_NUL_BIT
;
7882 cd
->parens_depth
-= 1;
7884 /* If this was an atomic group and there are no capturing groups within it,
7885 generate OP_ONCE_NC instead of OP_ONCE. */
7887 if (bravalue
== OP_ONCE
&& cd
->bracount
<= tempbracount
)
7890 if (bravalue
>= OP_ASSERT
&& bravalue
<= OP_ASSERTBACK_NOT
)
7891 cd
->assert_depth
-= 1;
7893 /* At the end of compiling, code is still pointing to the start of the
7894 group, while tempcode has been updated to point past the end of the group.
7895 The pattern pointer (ptr) is on the bracket.
7897 If this is a conditional bracket, check that there are no more than
7898 two branches in the group, or just one if it's a DEFINE group. We do this
7899 in the real compile phase, not in the pre-pass, where the whole group may
7900 not be available. */
7902 if (bravalue
== OP_COND
&& lengthptr
== NULL
)
7904 pcre_uchar
*tc
= code
;
7911 while (*tc
!= OP_KET
);
7913 /* A DEFINE group is never obeyed inline (the "condition" is always
7914 false). It must have only one branch. */
7916 if (code
[LINK_SIZE
+1] == OP_DEF
)
7920 *errorcodeptr
= ERR54
;
7923 bravalue
= OP_DEF
; /* Just a flag to suppress char handling below */
7926 /* A "normal" conditional group. If there is just one branch, we must not
7927 make use of its firstchar or reqchar, because this is equivalent to an
7928 empty second branch. */
7934 *errorcodeptr
= ERR27
;
7937 if (condcount
== 1) subfirstcharflags
= subreqcharflags
= REQ_NONE
;
7941 /* Error if hit end of pattern */
7943 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
7945 *errorcodeptr
= ERR14
;
7949 /* In the pre-compile phase, update the length by the length of the group,
7950 less the brackets at either end. Then reduce the compiled code to just a
7951 set of non-capturing brackets so that it doesn't use much memory if it is
7952 duplicated by a quantifier.*/
7954 if (lengthptr
!= NULL
)
7956 if (OFLOW_MAX
- *lengthptr
< length_prevgroup
- 2 - 2*LINK_SIZE
)
7958 *errorcodeptr
= ERR20
;
7961 *lengthptr
+= length_prevgroup
- 2 - 2*LINK_SIZE
;
7962 code
++; /* This already contains bravalue */
7963 PUTINC(code
, 0, 1 + LINK_SIZE
);
7965 PUTINC(code
, 0, 1 + LINK_SIZE
);
7966 break; /* No need to waste time with special character handling */
7969 /* Otherwise update the main code pointer to the end of the group. */
7973 /* For a DEFINE group, required and first character settings are not
7976 if (bravalue
== OP_DEF
) break;
7978 /* Handle updating of the required and first characters for other types of
7979 group. Update for normal brackets of all kinds, and conditions with two
7980 branches (see code above). If the bracket is followed by a quantifier with
7981 zero repeat, we have to back off. Hence the definition of zeroreqchar and
7982 zerofirstchar outside the main loop so that they can be accessed for the
7985 zeroreqchar
= reqchar
;
7986 zeroreqcharflags
= reqcharflags
;
7987 zerofirstchar
= firstchar
;
7988 zerofirstcharflags
= firstcharflags
;
7989 groupsetfirstchar
= FALSE
;
7991 if (bravalue
>= OP_ONCE
)
7993 /* If we have not yet set a firstchar in this branch, take it from the
7994 subpattern, remembering that it was set here so that a repeat of more
7995 than one can replicate it as reqchar if necessary. If the subpattern has
7996 no firstchar, set "none" for the whole branch. In both cases, a zero
7997 repeat forces firstchar to "none". */
7999 if (firstcharflags
== REQ_UNSET
)
8001 if (subfirstcharflags
>= 0)
8003 firstchar
= subfirstchar
;
8004 firstcharflags
= subfirstcharflags
;
8005 groupsetfirstchar
= TRUE
;
8007 else firstcharflags
= REQ_NONE
;
8008 zerofirstcharflags
= REQ_NONE
;
8011 /* If firstchar was previously set, convert the subpattern's firstchar
8012 into reqchar if there wasn't one, using the vary flag that was in
8013 existence beforehand. */
8015 else if (subfirstcharflags
>= 0 && subreqcharflags
< 0)
8017 subreqchar
= subfirstchar
;
8018 subreqcharflags
= subfirstcharflags
| tempreqvary
;
8021 /* If the subpattern set a required byte (or set a first byte that isn't
8022 really the first byte - see above), set it. */
8024 if (subreqcharflags
>= 0)
8026 reqchar
= subreqchar
;
8027 reqcharflags
= subreqcharflags
;
8031 /* For a forward assertion, we take the reqchar, if set, provided that the
8032 group has also set a first char. This can be helpful if the pattern that
8033 follows the assertion doesn't set a different char. For example, it's
8034 useful for /(?=abcde).+/. We can't set firstchar for an assertion, however
8035 because it leads to incorrect effect for patterns such as /(?=a)a.+/ when
8036 the "real" "a" would then become a reqchar instead of a firstchar. This is
8037 overcome by a scan at the end if there's no firstchar, looking for an
8038 asserted first char. */
8040 else if (bravalue
== OP_ASSERT
&& subreqcharflags
>= 0 &&
8041 subfirstcharflags
>= 0)
8043 reqchar
= subreqchar
;
8044 reqcharflags
= subreqcharflags
;
8046 break; /* End of processing '(' */
8049 /* ===================================================================*/
8050 /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
8051 are arranged to be the negation of the corresponding OP_values in the
8052 default case when PCRE_UCP is not set. For the back references, the values
8053 are negative the reference number. Only back references and those types
8054 that consume a character may be repeated. We can test for values between
8055 ESC_b and ESC_Z for the latter; this may have to change if any new ones are
8058 case CHAR_BACKSLASH
:
8060 escape
= check_escape(&ptr
, &ec
, errorcodeptr
, cd
->bracount
, options
, FALSE
);
8061 if (*errorcodeptr
!= 0) goto FAILED
;
8063 if (escape
== 0) /* The escape coded a single character */
8067 /* For metasequences that actually match a character, we disable the
8068 setting of a first character if it hasn't already been set. */
8070 if (firstcharflags
== REQ_UNSET
&& escape
> ESC_b
&& escape
< ESC_Z
)
8071 firstcharflags
= REQ_NONE
;
8073 /* Set values to reset to if this is followed by a zero repeat. */
8075 zerofirstchar
= firstchar
;
8076 zerofirstcharflags
= firstcharflags
;
8077 zeroreqchar
= reqchar
;
8078 zeroreqcharflags
= reqcharflags
;
8080 /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
8081 is a subroutine call by number (Oniguruma syntax). In fact, the value
8082 ESC_g is returned only for these cases. So we don't need to check for <
8083 or ' if the value is ESC_g. For the Perl syntax \g{n} the value is
8084 -n, and for the Perl syntax \g{name} the result is ESC_k (as
8085 that is a synonym for a named back reference). */
8087 if (escape
== ESC_g
)
8089 const pcre_uchar
*p
;
8092 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
; /* Normally this is set when '(' is read */
8093 terminator
= (*(++ptr
) == CHAR_LESS_THAN_SIGN
)?
8094 CHAR_GREATER_THAN_SIGN
: CHAR_APOSTROPHE
;
8096 /* These two statements stop the compiler for warning about possibly
8097 unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
8098 fact, because we do the check for a number below, the paths that
8099 would actually be in error are never taken. */
8102 reset_bracount
= FALSE
;
8104 /* If it's not a signed or unsigned number, treat it as a name. */
8107 if (cf
!= CHAR_PLUS
&& cf
!= CHAR_MINUS
&& !IS_DIGIT(cf
))
8110 goto NAMED_REF_OR_RECURSE
;
8113 /* Signed or unsigned number (cf = ptr[1]) is known to be plus or minus
8117 while (IS_DIGIT(*p
)) p
++;
8118 if (*p
!= (pcre_uchar
)terminator
)
8120 *errorcodeptr
= ERR57
;
8124 goto HANDLE_NUMERICAL_RECURSION
;
8127 /* \k<name> or \k'name' is a back reference by name (Perl syntax).
8128 We also support \k{name} (.NET syntax). */
8130 if (escape
== ESC_k
)
8132 if ((ptr
[1] != CHAR_LESS_THAN_SIGN
&&
8133 ptr
[1] != CHAR_APOSTROPHE
&& ptr
[1] != CHAR_LEFT_CURLY_BRACKET
))
8135 *errorcodeptr
= ERR69
;
8139 terminator
= (*(++ptr
) == CHAR_LESS_THAN_SIGN
)?
8140 CHAR_GREATER_THAN_SIGN
: (*ptr
== CHAR_APOSTROPHE
)?
8141 CHAR_APOSTROPHE
: CHAR_RIGHT_CURLY_BRACKET
;
8142 goto NAMED_REF_OR_RECURSE
;
8145 /* Back references are handled specially; must disable firstchar if
8146 not set to cope with cases like (?=(\w+))\1: which would otherwise set
8154 /* Come here from named backref handling when the reference is to a
8155 single group (i.e. not to a duplicated name. */
8158 if (firstcharflags
== REQ_UNSET
) zerofirstcharflags
= firstcharflags
= REQ_NONE
;
8160 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
8161 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_REFI
: OP_REF
;
8162 PUT2INC(code
, 0, recno
);
8163 cd
->backref_map
|= (recno
< 32)? (1U << recno
) : 1;
8164 if (recno
> cd
->top_backref
) cd
->top_backref
= recno
;
8166 /* Check to see if this back reference is recursive, that it, it
8167 is inside the group that it references. A flag is set so that the
8168 group can be made atomic. */
8170 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
8172 if (oc
->number
== recno
)
8180 /* So are Unicode property matches, if supported. */
8183 else if (escape
== ESC_P
|| escape
== ESC_p
)
8186 unsigned int ptype
= 0, pdata
= 0;
8187 if (!get_ucp(&ptr
, &negated
, &ptype
, &pdata
, errorcodeptr
))
8190 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
8191 *code
++ = ((escape
== ESC_p
) != negated
)? OP_PROP
: OP_NOTPROP
;
8197 /* If Unicode properties are not supported, \X, \P, and \p are not
8200 else if (escape
== ESC_X
|| escape
== ESC_P
|| escape
== ESC_p
)
8202 *errorcodeptr
= ERR45
;
8207 /* For the rest (including \X when Unicode properties are supported), we
8208 can obtain the OP value by negating the escape value in the default
8209 situation when PCRE_UCP is not set. When it *is* set, we substitute
8210 Unicode property tests. Note that \b and \B do a one-character
8211 lookbehind, and \A also behaves as if it does. */
8215 if ((escape
== ESC_b
|| escape
== ESC_B
|| escape
== ESC_A
) &&
8216 cd
->max_lookbehind
== 0)
8217 cd
->max_lookbehind
= 1;
8219 if (escape
>= ESC_DU
&& escape
<= ESC_wu
)
8221 nestptr
= ptr
+ 1; /* Where to resume */
8222 ptr
= substitutes
[escape
- ESC_DU
] - 1; /* Just before substitute */
8226 /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
8227 so that it works in DFA mode and in lookbehinds. */
8230 previous
= (escape
> ESC_b
&& escape
< ESC_Z
)? code
: NULL
;
8231 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
8232 *code
++ = (!utf
&& escape
== ESC_C
)? OP_ALLANY
: escape
;
8238 /* We have a data character whose value is in c. In UTF-8 mode it may have
8239 a value > 127. We set its representation in the length/buffer, and then
8240 handle it as a data character. */
8242 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
8243 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
8244 mclength
= PRIV(ord2utf
)(c
, mcbuffer
);
8255 /* ===================================================================*/
8256 /* Handle a literal character. It is guaranteed not to be whitespace or #
8257 when the extended flag is set. If we are in a UTF mode, it may be a
8258 multi-unit literal character. */
8266 if (utf
&& HAS_EXTRALEN(c
))
8267 ACROSSCHAR(TRUE
, ptr
[1], mcbuffer
[mclength
++] = *(++ptr
));
8270 /* At this point we have the character's bytes in mcbuffer, and the length
8271 in mclength. When not in UTF-8 mode, the length is always 1. */
8275 item_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
8277 /* For caseless UTF-8 mode when UCP support is available, check whether
8278 this character has more than one other case. If so, generate a special
8279 OP_PROP item instead of OP_CHARI. */
8282 if (utf
&& (options
& PCRE_CASELESS
) != 0)
8284 GETCHAR(c
, mcbuffer
);
8285 if ((c
= UCD_CASESET(c
)) != 0)
8290 if (firstcharflags
== REQ_UNSET
)
8291 firstcharflags
= zerofirstcharflags
= REQ_NONE
;
8297 /* Caseful matches, or not one of the multicase characters. */
8299 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_CHARI
: OP_CHAR
;
8300 for (c
= 0; c
< mclength
; c
++) *code
++ = mcbuffer
[c
];
8302 /* Remember if \r or \n were seen */
8304 if (mcbuffer
[0] == CHAR_CR
|| mcbuffer
[0] == CHAR_NL
)
8305 cd
->external_flags
|= PCRE_HASCRORLF
;
8307 /* Set the first and required bytes appropriately. If no previous first
8308 byte, set it from this character, but revert to none on a zero repeat.
8309 Otherwise, leave the firstchar value alone, and don't change it on a zero
8312 if (firstcharflags
== REQ_UNSET
)
8314 zerofirstcharflags
= REQ_NONE
;
8315 zeroreqchar
= reqchar
;
8316 zeroreqcharflags
= reqcharflags
;
8318 /* If the character is more than one byte long, we can set firstchar
8319 only if it is not to be matched caselessly. */
8321 if (mclength
== 1 || req_caseopt
== 0)
8323 firstchar
= mcbuffer
[0];
8324 firstcharflags
= req_caseopt
;
8329 reqcharflags
= cd
->req_varyopt
;
8332 else firstcharflags
= reqcharflags
= REQ_NONE
;
8335 /* firstchar was previously set; we can set reqchar only if the length is
8336 1 or the matching is caseful. */
8340 zerofirstchar
= firstchar
;
8341 zerofirstcharflags
= firstcharflags
;
8342 zeroreqchar
= reqchar
;
8343 zeroreqcharflags
= reqcharflags
;
8344 if (mclength
== 1 || req_caseopt
== 0)
8347 reqcharflags
= req_caseopt
| cd
->req_varyopt
;
8351 break; /* End of literal character handling */
8353 } /* end of big loop */
8356 /* Control never reaches here by falling through, only by a goto for all the
8357 error states. Pass back the position in the pattern so that it can be displayed
8358 to the user for diagnosing the error. */
8367 /*************************************************
8368 * Compile sequence of alternatives *
8369 *************************************************/
8371 /* On entry, ptr is pointing past the bracket character, but on return it
8372 points to the closing bracket, or vertical bar, or end of string. The code
8373 variable is pointing at the byte into which the BRA operator has been stored.
8374 This function is used during the pre-compile phase when we are trying to find
8375 out the amount of memory needed, as well as during the real compile phase. The
8376 value of lengthptr distinguishes the two phases.
8379 options option bits, including any changes for this subpattern
8380 codeptr -> the address of the current code pointer
8381 ptrptr -> the address of the current pattern pointer
8382 errorcodeptr -> pointer to error code variable
8383 lookbehind TRUE if this is a lookbehind assertion
8384 reset_bracount TRUE to reset the count for each branch
8385 skipbytes skip this many bytes at start (for brackets and OP_COND)
8386 cond_depth depth of nesting for conditional subpatterns
8387 firstcharptr place to put the first required character
8388 firstcharflagsptr place to put the first character flags, or a negative number
8389 reqcharptr place to put the last required character
8390 reqcharflagsptr place to put the last required character flags, or a negative number
8391 bcptr pointer to the chain of currently open branches
8392 cd points to the data block with tables pointers etc.
8393 lengthptr NULL during the real compile phase
8394 points to length accumulator during pre-compile phase
8396 Returns: TRUE on success
8400 compile_regex(int options
, pcre_uchar
**codeptr
, const pcre_uchar
**ptrptr
,
8401 int *errorcodeptr
, BOOL lookbehind
, BOOL reset_bracount
, int skipbytes
,
8403 pcre_uint32
*firstcharptr
, pcre_int32
*firstcharflagsptr
,
8404 pcre_uint32
*reqcharptr
, pcre_int32
*reqcharflagsptr
,
8405 branch_chain
*bcptr
, compile_data
*cd
, int *lengthptr
)
8407 const pcre_uchar
*ptr
= *ptrptr
;
8408 pcre_uchar
*code
= *codeptr
;
8409 pcre_uchar
*last_branch
= code
;
8410 pcre_uchar
*start_bracket
= code
;
8411 pcre_uchar
*reverse_count
= NULL
;
8412 open_capitem capitem
;
8414 pcre_uint32 firstchar
, reqchar
;
8415 pcre_int32 firstcharflags
, reqcharflags
;
8416 pcre_uint32 branchfirstchar
, branchreqchar
;
8417 pcre_int32 branchfirstcharflags
, branchreqcharflags
;
8419 unsigned int orig_bracount
;
8420 unsigned int max_bracount
;
8422 size_t save_hwm_offset
;
8424 /* If set, call the external function that checks for stack availability. */
8426 if (PUBL(stack_guard
) != NULL
&& PUBL(stack_guard
)())
8428 *errorcodeptr
= ERR85
;
8432 /* Miscellaneous initialization */
8435 bc
.current_branch
= code
;
8437 firstchar
= reqchar
= 0;
8438 firstcharflags
= reqcharflags
= REQ_UNSET
;
8440 save_hwm_offset
= cd
->hwm
- cd
->start_workspace
;
8442 /* Accumulate the length for use in the pre-compile phase. Start with the
8443 length of the BRA and KET and any extra bytes that are required at the
8444 beginning. We accumulate in a local variable to save frequent testing of
8445 lenthptr for NULL. We cannot do this by looking at the value of code at the
8446 start and end of each alternative, because compiled items are discarded during
8447 the pre-compile phase so that the work space is not exceeded. */
8449 length
= 2 + 2*LINK_SIZE
+ skipbytes
;
8451 /* WARNING: If the above line is changed for any reason, you must also change
8452 the code that abstracts option settings at the start of the pattern and makes
8453 them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
8454 pre-compile phase to find out whether anything has yet been compiled or not. */
8456 /* If this is a capturing subpattern, add to the chain of open capturing items
8457 so that we can detect them if (*ACCEPT) is encountered. This is also used to
8458 detect groups that contain recursive back references to themselves. Note that
8459 only OP_CBRA need be tested here; changing this opcode to one of its variants,
8460 e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
8462 if (*code
== OP_CBRA
)
8464 capnumber
= GET2(code
, 1 + LINK_SIZE
);
8465 capitem
.number
= capnumber
;
8466 capitem
.next
= cd
->open_caps
;
8467 capitem
.flag
= FALSE
;
8468 cd
->open_caps
= &capitem
;
8471 /* Offset is set zero to mark that this bracket is still open */
8474 code
+= 1 + LINK_SIZE
+ skipbytes
;
8476 /* Loop for each alternative branch */
8478 orig_bracount
= max_bracount
= cd
->bracount
;
8481 /* For a (?| group, reset the capturing bracket count so that each branch
8482 uses the same numbers. */
8484 if (reset_bracount
) cd
->bracount
= orig_bracount
;
8486 /* Set up dummy OP_REVERSE if lookbehind assertion */
8490 *code
++ = OP_REVERSE
;
8491 reverse_count
= code
;
8493 length
+= 1 + LINK_SIZE
;
8496 /* Now compile the branch; in the pre-compile phase its length gets added
8499 if (!compile_branch(&options
, &code
, &ptr
, errorcodeptr
, &branchfirstchar
,
8500 &branchfirstcharflags
, &branchreqchar
, &branchreqcharflags
, &bc
,
8501 cond_depth
, cd
, (lengthptr
== NULL
)? NULL
: &length
))
8507 /* Keep the highest bracket count in case (?| was used and some branch
8508 has fewer than the rest. */
8510 if (cd
->bracount
> max_bracount
) max_bracount
= cd
->bracount
;
8512 /* In the real compile phase, there is some post-processing to be done. */
8514 if (lengthptr
== NULL
)
8516 /* If this is the first branch, the firstchar and reqchar values for the
8517 branch become the values for the regex. */
8519 if (*last_branch
!= OP_ALT
)
8521 firstchar
= branchfirstchar
;
8522 firstcharflags
= branchfirstcharflags
;
8523 reqchar
= branchreqchar
;
8524 reqcharflags
= branchreqcharflags
;
8527 /* If this is not the first branch, the first char and reqchar have to
8528 match the values from all the previous branches, except that if the
8529 previous value for reqchar didn't have REQ_VARY set, it can still match,
8530 and we set REQ_VARY for the regex. */
8534 /* If we previously had a firstchar, but it doesn't match the new branch,
8535 we have to abandon the firstchar for the regex, but if there was
8536 previously no reqchar, it takes on the value of the old firstchar. */
8538 if (firstcharflags
>= 0 &&
8539 (firstcharflags
!= branchfirstcharflags
|| firstchar
!= branchfirstchar
))
8541 if (reqcharflags
< 0)
8543 reqchar
= firstchar
;
8544 reqcharflags
= firstcharflags
;
8546 firstcharflags
= REQ_NONE
;
8549 /* If we (now or from before) have no firstchar, a firstchar from the
8550 branch becomes a reqchar if there isn't a branch reqchar. */
8552 if (firstcharflags
< 0 && branchfirstcharflags
>= 0 && branchreqcharflags
< 0)
8554 branchreqchar
= branchfirstchar
;
8555 branchreqcharflags
= branchfirstcharflags
;
8558 /* Now ensure that the reqchars match */
8560 if (((reqcharflags
& ~REQ_VARY
) != (branchreqcharflags
& ~REQ_VARY
)) ||
8561 reqchar
!= branchreqchar
)
8562 reqcharflags
= REQ_NONE
;
8565 reqchar
= branchreqchar
;
8566 reqcharflags
|= branchreqcharflags
; /* To "or" REQ_VARY */
8570 /* If lookbehind, check that this branch matches a fixed-length string, and
8571 put the length into the OP_REVERSE item. Temporarily mark the end of the
8572 branch with OP_END. If the branch contains OP_RECURSE, the result is -3
8573 because there may be forward references that we can't check here. Set a
8574 flag to cause another lookbehind check at the end. Why not do it all at the
8575 end? Because common, erroneous checks are picked up here and the offset of
8576 the problem can be shown. */
8582 fixed_length
= find_fixedlength(last_branch
, (options
& PCRE_UTF8
) != 0,
8584 DPRINTF(("fixed length = %d\n", fixed_length
));
8585 if (fixed_length
== -3)
8587 cd
->check_lookbehind
= TRUE
;
8589 else if (fixed_length
< 0)
8591 *errorcodeptr
= (fixed_length
== -2)? ERR36
:
8592 (fixed_length
== -4)? ERR70
: ERR25
;
8598 if (fixed_length
> cd
->max_lookbehind
)
8599 cd
->max_lookbehind
= fixed_length
;
8600 PUT(reverse_count
, 0, fixed_length
);
8605 /* Reached end of expression, either ')' or end of pattern. In the real
8606 compile phase, go back through the alternative branches and reverse the chain
8607 of offsets, with the field in the BRA item now becoming an offset to the
8608 first alternative. If there are no alternatives, it points to the end of the
8609 group. The length in the terminating ket is always the length of the whole
8610 bracketed item. Return leaving the pointer at the terminating char. */
8612 if (*ptr
!= CHAR_VERTICAL_LINE
)
8614 if (lengthptr
== NULL
)
8616 int branch_length
= (int)(code
- last_branch
);
8619 int prev_length
= GET(last_branch
, 1);
8620 PUT(last_branch
, 1, branch_length
);
8621 branch_length
= prev_length
;
8622 last_branch
-= branch_length
;
8624 while (branch_length
> 0);
8627 /* Fill in the ket */
8630 PUT(code
, 1, (int)(code
- start_bracket
));
8631 code
+= 1 + LINK_SIZE
;
8633 /* If it was a capturing subpattern, check to see if it contained any
8634 recursive back references. If so, we must wrap it in atomic brackets.
8635 Because we are moving code along, we must ensure that any pending recursive
8636 references are updated. In any event, remove the block from the chain. */
8640 if (cd
->open_caps
->flag
)
8643 adjust_recurse(start_bracket
, 1 + LINK_SIZE
,
8644 (options
& PCRE_UTF8
) != 0, cd
, save_hwm_offset
);
8645 memmove(start_bracket
+ 1 + LINK_SIZE
, start_bracket
,
8646 IN_UCHARS(code
- start_bracket
));
8647 *start_bracket
= OP_ONCE
;
8648 code
+= 1 + LINK_SIZE
;
8649 PUT(start_bracket
, 1, (int)(code
- start_bracket
));
8651 PUT(code
, 1, (int)(code
- start_bracket
));
8652 code
+= 1 + LINK_SIZE
;
8653 length
+= 2 + 2*LINK_SIZE
;
8655 cd
->open_caps
= cd
->open_caps
->next
;
8658 /* Retain the highest bracket number, in case resetting was used. */
8660 cd
->bracount
= max_bracount
;
8662 /* Set values to pass back */
8666 *firstcharptr
= firstchar
;
8667 *firstcharflagsptr
= firstcharflags
;
8668 *reqcharptr
= reqchar
;
8669 *reqcharflagsptr
= reqcharflags
;
8670 if (lengthptr
!= NULL
)
8672 if (OFLOW_MAX
- *lengthptr
< length
)
8674 *errorcodeptr
= ERR20
;
8677 *lengthptr
+= length
;
8682 /* Another branch follows. In the pre-compile phase, we can move the code
8683 pointer back to where it was for the start of the first branch. (That is,
8684 pretend that each branch is the only one.)
8686 In the real compile phase, insert an ALT node. Its length field points back
8687 to the previous branch while the bracket remains open. At the end the chain
8688 is reversed. It's done like this so that the start of the bracket has a
8689 zero offset until it is closed, making it possible to detect recursion. */
8691 if (lengthptr
!= NULL
)
8693 code
= *codeptr
+ 1 + LINK_SIZE
+ skipbytes
;
8694 length
+= 1 + LINK_SIZE
;
8699 PUT(code
, 1, (int)(code
- last_branch
));
8700 bc
.current_branch
= last_branch
= code
;
8701 code
+= 1 + LINK_SIZE
;
8706 /* Control never reaches here */
8712 /*************************************************
8713 * Check for anchored expression *
8714 *************************************************/
8716 /* Try to find out if this is an anchored regular expression. Consider each
8717 alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
8718 all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
8719 it's anchored. However, if this is a multiline pattern, then only OP_SOD will
8720 be found, because ^ generates OP_CIRCM in that mode.
8722 We can also consider a regex to be anchored if OP_SOM starts all its branches.
8723 This is the code for \G, which means "match at start of match position, taking
8724 into account the match offset".
8726 A branch is also implicitly anchored if it starts with .* and DOTALL is set,
8727 because that will try the rest of the pattern at all possible matching points,
8728 so there is no point trying again.... er ....
8730 .... except when the .* appears inside capturing parentheses, and there is a
8731 subsequent back reference to those parentheses. We haven't enough information
8732 to catch that case precisely.
8734 At first, the best we could do was to detect when .* was in capturing brackets
8735 and the highest back reference was greater than or equal to that level.
8736 However, by keeping a bitmap of the first 31 back references, we can catch some
8737 of the more common cases more precisely.
8739 ... A second exception is when the .* appears inside an atomic group, because
8740 this prevents the number of characters it matches from being adjusted.
8743 code points to start of expression (the bracket)
8744 bracket_map a bitmap of which brackets we are inside while testing; this
8745 handles up to substring 31; after that we just have to take
8746 the less precise approach
8747 cd points to the compile data block
8748 atomcount atomic group level
8750 Returns: TRUE or FALSE
8754 is_anchored(register const pcre_uchar
*code
, unsigned int bracket_map
,
8755 compile_data
*cd
, int atomcount
)
8758 const pcre_uchar
*scode
= first_significant_code(
8759 code
+ PRIV(OP_lengths
)[*code
], FALSE
);
8760 register int op
= *scode
;
8762 /* Non-capturing brackets */
8764 if (op
== OP_BRA
|| op
== OP_BRAPOS
||
8765 op
== OP_SBRA
|| op
== OP_SBRAPOS
)
8767 if (!is_anchored(scode
, bracket_map
, cd
, atomcount
)) return FALSE
;
8770 /* Capturing brackets */
8772 else if (op
== OP_CBRA
|| op
== OP_CBRAPOS
||
8773 op
== OP_SCBRA
|| op
== OP_SCBRAPOS
)
8775 int n
= GET2(scode
, 1+LINK_SIZE
);
8776 int new_map
= bracket_map
| ((n
< 32)? (1U << n
) : 1);
8777 if (!is_anchored(scode
, new_map
, cd
, atomcount
)) return FALSE
;
8780 /* Positive forward assertion */
8782 else if (op
== OP_ASSERT
)
8784 if (!is_anchored(scode
, bracket_map
, cd
, atomcount
)) return FALSE
;
8787 /* Condition; not anchored if no second branch */
8789 else if (op
== OP_COND
)
8791 if (scode
[GET(scode
,1)] != OP_ALT
) return FALSE
;
8792 if (!is_anchored(scode
, bracket_map
, cd
, atomcount
)) return FALSE
;
8797 else if (op
== OP_ONCE
|| op
== OP_ONCE_NC
)
8799 if (!is_anchored(scode
, bracket_map
, cd
, atomcount
+ 1))
8803 /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
8804 it isn't in brackets that are or may be referenced or inside an atomic
8807 else if ((op
== OP_TYPESTAR
|| op
== OP_TYPEMINSTAR
||
8808 op
== OP_TYPEPOSSTAR
))
8810 if (scode
[1] != OP_ALLANY
|| (bracket_map
& cd
->backref_map
) != 0 ||
8811 atomcount
> 0 || cd
->had_pruneorskip
)
8815 /* Check for explicit anchoring */
8817 else if (op
!= OP_SOD
&& op
!= OP_SOM
&& op
!= OP_CIRC
) return FALSE
;
8819 code
+= GET(code
, 1);
8821 while (*code
== OP_ALT
); /* Loop for each alternative */
8827 /*************************************************
8828 * Check for starting with ^ or .* *
8829 *************************************************/
8831 /* This is called to find out if every branch starts with ^ or .* so that
8832 "first char" processing can be done to speed things up in multiline
8833 matching and for non-DOTALL patterns that start with .* (which must start at
8834 the beginning or after \n). As in the case of is_anchored() (see above), we
8835 have to take account of back references to capturing brackets that contain .*
8836 because in that case we can't make the assumption. Also, the appearance of .*
8837 inside atomic brackets or in an assertion, or in a pattern that contains *PRUNE
8838 or *SKIP does not count, because once again the assumption no longer holds.
8841 code points to start of expression (the bracket)
8842 bracket_map a bitmap of which brackets we are inside while testing; this
8843 handles up to substring 31; after that we just have to take
8844 the less precise approach
8845 cd points to the compile data
8846 atomcount atomic group level
8847 inassert TRUE if in an assertion
8849 Returns: TRUE or FALSE
8853 is_startline(const pcre_uchar
*code
, unsigned int bracket_map
,
8854 compile_data
*cd
, int atomcount
, BOOL inassert
)
8857 const pcre_uchar
*scode
= first_significant_code(
8858 code
+ PRIV(OP_lengths
)[*code
], FALSE
);
8859 register int op
= *scode
;
8861 /* If we are at the start of a conditional assertion group, *both* the
8862 conditional assertion *and* what follows the condition must satisfy the test
8863 for start of line. Other kinds of condition fail. Note that there may be an
8864 auto-callout at the start of a condition. */
8868 scode
+= 1 + LINK_SIZE
;
8869 if (*scode
== OP_CALLOUT
) scode
+= PRIV(OP_lengths
)[OP_CALLOUT
];
8880 default: /* Assertion */
8881 if (!is_startline(scode
, bracket_map
, cd
, atomcount
, TRUE
)) return FALSE
;
8882 do scode
+= GET(scode
, 1); while (*scode
== OP_ALT
);
8883 scode
+= 1 + LINK_SIZE
;
8886 scode
= first_significant_code(scode
, FALSE
);
8890 /* Non-capturing brackets */
8892 if (op
== OP_BRA
|| op
== OP_BRAPOS
||
8893 op
== OP_SBRA
|| op
== OP_SBRAPOS
)
8895 if (!is_startline(scode
, bracket_map
, cd
, atomcount
, inassert
)) return FALSE
;
8898 /* Capturing brackets */
8900 else if (op
== OP_CBRA
|| op
== OP_CBRAPOS
||
8901 op
== OP_SCBRA
|| op
== OP_SCBRAPOS
)
8903 int n
= GET2(scode
, 1+LINK_SIZE
);
8904 int new_map
= bracket_map
| ((n
< 32)? (1U << n
) : 1);
8905 if (!is_startline(scode
, new_map
, cd
, atomcount
, inassert
)) return FALSE
;
8908 /* Positive forward assertions */
8910 else if (op
== OP_ASSERT
)
8912 if (!is_startline(scode
, bracket_map
, cd
, atomcount
, TRUE
)) return FALSE
;
8915 /* Atomic brackets */
8917 else if (op
== OP_ONCE
|| op
== OP_ONCE_NC
)
8919 if (!is_startline(scode
, bracket_map
, cd
, atomcount
+ 1, inassert
)) return FALSE
;
8922 /* .* means "start at start or after \n" if it isn't in atomic brackets or
8923 brackets that may be referenced or an assertion, as long as the pattern does
8924 not contain *PRUNE or *SKIP, because these break the feature. Consider, for
8925 example, /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e.
8926 not at the start of a line. */
8928 else if (op
== OP_TYPESTAR
|| op
== OP_TYPEMINSTAR
|| op
== OP_TYPEPOSSTAR
)
8930 if (scode
[1] != OP_ANY
|| (bracket_map
& cd
->backref_map
) != 0 ||
8931 atomcount
> 0 || cd
->had_pruneorskip
|| inassert
)
8935 /* Check for explicit circumflex; anything else gives a FALSE result. Note
8936 in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC
8937 because the number of characters matched by .* cannot be adjusted inside
8940 else if (op
!= OP_CIRC
&& op
!= OP_CIRCM
) return FALSE
;
8942 /* Move on to the next alternative */
8944 code
+= GET(code
, 1);
8946 while (*code
== OP_ALT
); /* Loop for each alternative */
8952 /*************************************************
8953 * Check for asserted fixed first char *
8954 *************************************************/
8956 /* During compilation, the "first char" settings from forward assertions are
8957 discarded, because they can cause conflicts with actual literals that follow.
8958 However, if we end up without a first char setting for an unanchored pattern,
8959 it is worth scanning the regex to see if there is an initial asserted first
8960 char. If all branches start with the same asserted char, or with a
8961 non-conditional bracket all of whose alternatives start with the same asserted
8962 char (recurse ad lib), then we return that char, with the flags set to zero or
8963 REQ_CASELESS; otherwise return zero with REQ_NONE in the flags.
8966 code points to start of expression (the bracket)
8967 flags points to the first char flags, or to REQ_NONE
8968 inassert TRUE if in an assertion
8970 Returns: the fixed first char, or 0 with REQ_NONE in flags
8974 find_firstassertedchar(const pcre_uchar
*code
, pcre_int32
*flags
,
8977 register pcre_uint32 c
= 0;
8978 int cflags
= REQ_NONE
;
8984 int xl
= (*code
== OP_CBRA
|| *code
== OP_SCBRA
||
8985 *code
== OP_CBRAPOS
|| *code
== OP_SCBRAPOS
)? IMM2_SIZE
:0;
8986 const pcre_uchar
*scode
= first_significant_code(code
+ 1+LINK_SIZE
+ xl
,
8988 register pcre_uchar op
= *scode
;
9004 d
= find_firstassertedchar(scode
, &dflags
, op
== OP_ASSERT
);
9007 if (cflags
< 0) { c
= d
; cflags
= dflags
; } else if (c
!= d
|| cflags
!= dflags
) return 0;
9018 if (!inassert
) return 0;
9019 if (cflags
< 0) { c
= scode
[1]; cflags
= 0; }
9020 else if (c
!= scode
[1]) return 0;
9031 if (!inassert
) return 0;
9032 if (cflags
< 0) { c
= scode
[1]; cflags
= REQ_CASELESS
; }
9033 else if (c
!= scode
[1]) return 0;
9037 code
+= GET(code
, 1);
9039 while (*code
== OP_ALT
);
9047 /*************************************************
9048 * Add an entry to the name/number table *
9049 *************************************************/
9051 /* This function is called between compiling passes to add an entry to the
9052 name/number table, maintaining alphabetical order. Checking for permitted
9053 and forbidden duplicates has already been done.
9056 cd the compile data block
9057 name the name to add
9058 length the length of the name
9059 groupno the group number
9065 add_name(compile_data
*cd
, const pcre_uchar
*name
, int length
,
9066 unsigned int groupno
)
9069 pcre_uchar
*slot
= cd
->name_table
;
9071 for (i
= 0; i
< cd
->names_found
; i
++)
9073 int crc
= memcmp(name
, slot
+IMM2_SIZE
, IN_UCHARS(length
));
9074 if (crc
== 0 && slot
[IMM2_SIZE
+length
] != 0)
9075 crc
= -1; /* Current name is a substring */
9077 /* Make space in the table and break the loop for an earlier name. For a
9078 duplicate or later name, carry on. We do this for duplicates so that in the
9079 simple case (when ?(| is not used) they are in order of their numbers. In all
9080 cases they are in the order in which they appear in the pattern. */
9084 memmove(slot
+ cd
->name_entry_size
, slot
,
9085 IN_UCHARS((cd
->names_found
- i
) * cd
->name_entry_size
));
9089 /* Continue the loop for a later or duplicate name */
9091 slot
+= cd
->name_entry_size
;
9094 PUT2(slot
, 0, groupno
);
9095 memcpy(slot
+ IMM2_SIZE
, name
, IN_UCHARS(length
));
9096 slot
[IMM2_SIZE
+ length
] = 0;
9102 /*************************************************
9103 * Compile a Regular Expression *
9104 *************************************************/
9106 /* This function takes a string and returns a pointer to a block of store
9107 holding a compiled version of the expression. The original API for this
9108 function had no error code return variable; it is retained for backwards
9109 compatibility. The new function is given a new name.
9112 pattern the regular expression
9113 options various option bits
9114 errorcodeptr pointer to error code variable (pcre_compile2() only)
9115 can be NULL if you don't want a code value
9116 errorptr pointer to pointer to error text
9117 erroroffset ptr offset in pattern where error was detected
9118 tables pointer to character tables or NULL
9120 Returns: pointer to compiled data block, or NULL on error,
9121 with errorptr and erroroffset set
9124 #if defined COMPILE_PCRE8
9125 PCRE_EXP_DEFN pcre
* PCRE_CALL_CONVENTION
9126 pcre_compile(const char *pattern
, int options
, const char **errorptr
,
9127 int *erroroffset
, const unsigned char *tables
)
9128 #elif defined COMPILE_PCRE16
9129 PCRE_EXP_DEFN pcre16
* PCRE_CALL_CONVENTION
9130 pcre16_compile(PCRE_SPTR16 pattern
, int options
, const char **errorptr
,
9131 int *erroroffset
, const unsigned char *tables
)
9132 #elif defined COMPILE_PCRE32
9133 PCRE_EXP_DEFN pcre32
* PCRE_CALL_CONVENTION
9134 pcre32_compile(PCRE_SPTR32 pattern
, int options
, const char **errorptr
,
9135 int *erroroffset
, const unsigned char *tables
)
9138 #if defined COMPILE_PCRE8
9139 return pcre_compile2(pattern
, options
, NULL
, errorptr
, erroroffset
, tables
);
9140 #elif defined COMPILE_PCRE16
9141 return pcre16_compile2(pattern
, options
, NULL
, errorptr
, erroroffset
, tables
);
9142 #elif defined COMPILE_PCRE32
9143 return pcre32_compile2(pattern
, options
, NULL
, errorptr
, erroroffset
, tables
);
9148 #if defined COMPILE_PCRE8
9149 PCRE_EXP_DEFN pcre
* PCRE_CALL_CONVENTION
9150 pcre_compile2(const char *pattern
, int options
, int *errorcodeptr
,
9151 const char **errorptr
, int *erroroffset
, const unsigned char *tables
)
9152 #elif defined COMPILE_PCRE16
9153 PCRE_EXP_DEFN pcre16
* PCRE_CALL_CONVENTION
9154 pcre16_compile2(PCRE_SPTR16 pattern
, int options
, int *errorcodeptr
,
9155 const char **errorptr
, int *erroroffset
, const unsigned char *tables
)
9156 #elif defined COMPILE_PCRE32
9157 PCRE_EXP_DEFN pcre32
* PCRE_CALL_CONVENTION
9158 pcre32_compile2(PCRE_SPTR32 pattern
, int options
, int *errorcodeptr
,
9159 const char **errorptr
, int *erroroffset
, const unsigned char *tables
)
9163 int length
= 1; /* For final END opcode */
9164 pcre_int32 firstcharflags
, reqcharflags
;
9165 pcre_uint32 firstchar
, reqchar
;
9166 pcre_uint32 limit_match
= PCRE_UINT32_MAX
;
9167 pcre_uint32 limit_recursion
= PCRE_UINT32_MAX
;
9171 int skipatstart
= 0;
9173 BOOL never_utf
= FALSE
;
9176 const pcre_uchar
*codestart
;
9177 const pcre_uchar
*ptr
;
9178 const pcre_uchar
*endptr
;
9179 compile_data compile_block
;
9180 compile_data
*cd
= &compile_block
;
9182 /* This space is used for "compiling" into during the first phase, when we are
9183 computing the amount of memory that is needed. Compiled items are thrown away
9184 as soon as possible, so that a fairly large buffer should be sufficient for
9185 this purpose. The same space is used in the second phase for remembering where
9186 to fill in forward references to subpatterns. That may overflow, in which case
9187 new memory is obtained from malloc(). */
9189 pcre_uchar cworkspace
[COMPILE_WORK_SIZE
];
9191 /* This vector is used for remembering name groups during the pre-compile. In a
9192 similar way to cworkspace, it can be expanded using malloc() if necessary. */
9194 named_group named_groups
[NAMED_GROUP_LIST_SIZE
];
9196 /* Set this early so that early errors get offset 0. */
9198 ptr
= (const pcre_uchar
*)pattern
;
9200 /* We can't pass back an error message if errorptr is NULL; I guess the best we
9201 can do is just return NULL, but we can set a code value if there is a code
9204 if (errorptr
== NULL
)
9206 if (errorcodeptr
!= NULL
) *errorcodeptr
= 99;
9211 if (errorcodeptr
!= NULL
) *errorcodeptr
= ERR0
;
9213 /* However, we can give a message for this error */
9215 if (erroroffset
== NULL
)
9218 goto PCRE_EARLY_ERROR_RETURN2
;
9223 /* Set up pointers to the individual character tables */
9225 if (tables
== NULL
) tables
= PRIV(default_tables
);
9226 cd
->lcc
= tables
+ lcc_offset
;
9227 cd
->fcc
= tables
+ fcc_offset
;
9228 cd
->cbits
= tables
+ cbits_offset
;
9229 cd
->ctypes
= tables
+ ctypes_offset
;
9231 /* Check that all undefined public option bits are zero */
9233 if ((options
& ~(PUBLIC_COMPILE_OPTIONS
| PUBLIC_EXTENDED_COMPILE_OPTIONS
)) != 0 ||
9234 (((options
& PCRE_XC1OPTIONS
) != 0) &&
9235 (options
& (PUBLIC_EXTENDED_COMPILE_OPTIONS
& ~PCRE_XC1OPTIONS
)) == 0) ||
9236 (((options
& PCRE_XC1OPTIONS
) == 0) &&
9237 (options
& (PUBLIC_EXTENDED_COMPILE_OPTIONS
& ~PCRE_XC1OPTIONS
)) != 0))
9241 goto PCRE_EARLY_ERROR_RETURN
;
9244 /* Extract the extended public options (stripping the PCRE_XC1OPTIONS bit) */
9246 options2
= options
& (PUBLIC_EXTENDED_COMPILE_OPTIONS
& ~PCRE_XC1OPTIONS
);
9247 options
&= ~PUBLIC_EXTENDED_COMPILE_OPTIONS
;
9251 *** LOOK AWAY NOW!!! YOUR EYES WILL BUG OUT OF YOUR HEAD IF YOU DON'T!!! ;)
9254 /* Handle PCRE_ALLOW_EMBEDDED_NUL_BIT here. */
9256 if (options2
& PCRE_ALLOW_EMBEDDED_NUL_BIT
)
9258 # define ISXDIG(x) (digitab[x] & ctype_xdigit)
9259 # define XVAL(x) (IS_DIGIT(x)?((x)&0xf):(((x)+0x9)&0xf)) /* ASCII or EBCDIC */
9260 # define XPTR(x) ((const pcre_uchar *)(void *)(uintptr_t)(x))
9261 const unsigned char *uptr
= (const unsigned char *)pattern
;
9262 const pcre_uchar
**vptrs
;
9263 uintptr_t vinfo
= 0;
9264 int xdigcnt
= sizeof(void *) * 2;
9266 while (xdigcnt
&& ISXDIG(*uptr
))
9269 vinfo
|= XVAL(*uptr
);
9273 if (xdigcnt
|| *uptr
) goto BADOPTIONS
;
9274 vptrs
= (const pcre_uchar
**)XPTR(vinfo
);
9275 if (vptrs
[0] != XPTR(0x4841434b /* odd magic number ;) */) ||
9276 vptrs
[1] != XPTR(0x01010101 /* version number one */))
9280 if (vptrs
[2] == endptr
)
9282 /* only case where trailing NUL may be omitted */
9283 ptr
= sub_embedded_nul
+ 4; /* sub_embedded_nul is always "\\000\0" */
9284 options2
&= ~PCRE_ALLOW_EMBEDDED_NUL_BIT
;
9288 if (*endptr
) goto BADOPTIONS
;
9291 pattern
= (const char *)ptr
;
9297 /* If PCRE_NEVER_UTF is set, remember it. */
9299 if ((options
& PCRE_NEVER_UTF
) != 0) never_utf
= TRUE
;
9301 /* Check for global one-time settings at the start of the pattern, and remember
9302 the offset for later. */
9304 cd
->external_flags
= 0; /* Initialize here for LIMIT_MATCH/RECURSION */
9306 while (ptr
[skipatstart
] == CHAR_LEFT_PARENTHESIS
&&
9307 ptr
[skipatstart
+1] == CHAR_ASTERISK
&&
9308 !(options2
& (PCRE_POSIX_BASIC_ESC_BIT
|PCRE_VERBATIM_BIT
)))
9313 /* For completeness and backward compatibility, (*UTFn) is supported in the
9314 relevant libraries, but (*UTF) is generic and always supported. Note that
9315 PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. */
9317 #ifdef COMPILE_PCRE8
9318 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF8_RIGHTPAR
, 5) == 0)
9319 { skipatstart
+= 7; options
|= PCRE_UTF8
; continue; }
9321 #ifdef COMPILE_PCRE16
9322 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF16_RIGHTPAR
, 6) == 0)
9323 { skipatstart
+= 8; options
|= PCRE_UTF16
; continue; }
9325 #ifdef COMPILE_PCRE32
9326 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF32_RIGHTPAR
, 6) == 0)
9327 { skipatstart
+= 8; options
|= PCRE_UTF32
; continue; }
9330 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF_RIGHTPAR
, 4) == 0)
9331 { skipatstart
+= 6; options
|= PCRE_UTF8
; continue; }
9332 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UCP_RIGHTPAR
, 4) == 0)
9333 { skipatstart
+= 6; options
|= PCRE_UCP
; continue; }
9334 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_NO_AUTO_POSSESS_RIGHTPAR
, 16) == 0)
9335 { skipatstart
+= 18; options
|= PCRE_NO_AUTO_POSSESS
; continue; }
9336 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_NO_START_OPT_RIGHTPAR
, 13) == 0)
9337 { skipatstart
+= 15; options
|= PCRE_NO_START_OPTIMIZE
; continue; }
9339 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_LIMIT_MATCH_EQ
, 12) == 0)
9342 int p
= skipatstart
+ 14;
9343 while (isdigit(ptr
[p
]))
9345 if (c
> PCRE_UINT32_MAX
/ 10 - 1) break; /* Integer overflow */
9346 c
= c
*10 + ptr
[p
++] - CHAR_0
;
9348 if (ptr
[p
++] != CHAR_RIGHT_PARENTHESIS
) break;
9349 if (c
< limit_match
)
9352 cd
->external_flags
|= PCRE_MLSET
;
9358 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_LIMIT_RECURSION_EQ
, 16) == 0)
9361 int p
= skipatstart
+ 18;
9362 while (isdigit(ptr
[p
]))
9364 if (c
> PCRE_UINT32_MAX
/ 10 - 1) break; /* Integer overflow check */
9365 c
= c
*10 + ptr
[p
++] - CHAR_0
;
9367 if (ptr
[p
++] != CHAR_RIGHT_PARENTHESIS
) break;
9368 if (c
< limit_recursion
)
9370 limit_recursion
= c
;
9371 cd
->external_flags
|= PCRE_RLSET
;
9377 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_CR_RIGHTPAR
, 3) == 0)
9378 { skipatstart
+= 5; newnl
= PCRE_NEWLINE_CR
; }
9379 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_LF_RIGHTPAR
, 3) == 0)
9380 { skipatstart
+= 5; newnl
= PCRE_NEWLINE_LF
; }
9381 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_CRLF_RIGHTPAR
, 5) == 0)
9382 { skipatstart
+= 7; newnl
= PCRE_NEWLINE_CR
+ PCRE_NEWLINE_LF
; }
9383 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_ANY_RIGHTPAR
, 4) == 0)
9384 { skipatstart
+= 6; newnl
= PCRE_NEWLINE_ANY
; }
9385 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_ANYCRLF_RIGHTPAR
, 8) == 0)
9386 { skipatstart
+= 10; newnl
= PCRE_NEWLINE_ANYCRLF
; }
9388 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_BSR_ANYCRLF_RIGHTPAR
, 12) == 0)
9389 { skipatstart
+= 14; newbsr
= PCRE_BSR_ANYCRLF
; }
9390 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_BSR_UNICODE_RIGHTPAR
, 12) == 0)
9391 { skipatstart
+= 14; newbsr
= PCRE_BSR_UNICODE
; }
9394 options
= (options
& ~PCRE_NEWLINE_BITS
) | newnl
;
9395 else if (newbsr
!= 0)
9396 options
= (options
& ~(PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
)) | newbsr
;
9400 /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
9401 utf
= (options
& PCRE_UTF8
) != 0;
9402 if (utf
&& never_utf
)
9405 goto PCRE_EARLY_ERROR_RETURN2
;
9408 /* Can't support UTF unless PCRE has been compiled to include the code. The
9409 return of an error code from PRIV(valid_utf)() is a new feature, introduced in
9410 release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
9414 if (utf
&& (options
& PCRE_NO_UTF8_CHECK
) == 0 &&
9415 (errorcode
= PRIV(valid_utf
)((PCRE_PUCHAR
)pattern
, -1, erroroffset
)) != 0)
9417 #if defined COMPILE_PCRE8
9419 #elif defined COMPILE_PCRE16
9421 #elif defined COMPILE_PCRE32
9424 goto PCRE_EARLY_ERROR_RETURN2
;
9430 goto PCRE_EARLY_ERROR_RETURN
;
9434 /* Can't support UCP unless PCRE has been compiled to include the code. */
9437 if ((options
& PCRE_UCP
) != 0)
9440 goto PCRE_EARLY_ERROR_RETURN
;
9444 /* Check validity of \R options. */
9446 if ((options
& (PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
)) ==
9447 (PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
))
9450 goto PCRE_EARLY_ERROR_RETURN
;
9453 /* Handle different types of newline. The three bits give seven cases. The
9454 current code allows for fixed one- or two-byte sequences, plus "any" and
9457 switch (options
& PCRE_NEWLINE_BITS
)
9459 case 0: newline
= NEWLINE
; break; /* Build-time default */
9460 case PCRE_NEWLINE_CR
: newline
= CHAR_CR
; break;
9461 case PCRE_NEWLINE_LF
: newline
= CHAR_NL
; break;
9462 case PCRE_NEWLINE_CR
+
9463 PCRE_NEWLINE_LF
: newline
= (CHAR_CR
<< 8) | CHAR_NL
; break;
9464 case PCRE_NEWLINE_ANY
: newline
= -1; break;
9465 case PCRE_NEWLINE_ANYCRLF
: newline
= -2; break;
9466 default: errorcode
= ERR56
; goto PCRE_EARLY_ERROR_RETURN
;
9471 cd
->nltype
= NLTYPE_ANYCRLF
;
9473 else if (newline
< 0)
9475 cd
->nltype
= NLTYPE_ANY
;
9479 cd
->nltype
= NLTYPE_FIXED
;
9483 cd
->nl
[0] = (newline
>> 8) & 255;
9484 cd
->nl
[1] = newline
& 255;
9489 cd
->nl
[0] = newline
;
9493 /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
9494 references to help in deciding whether (.*) can be treated as anchored or not.
9497 cd
->top_backref
= 0;
9498 cd
->backref_map
= 0;
9500 /* Reflect pattern for debugging output */
9502 DPRINTF(("------------------------------------------------------------------\n"));
9504 print_puchar(stdout
, (PCRE_PUCHAR
)pattern
);
9508 /* Pretend to compile the pattern while actually just accumulating the length
9509 of memory required. This behaviour is triggered by passing a non-NULL final
9510 argument to compile_regex(). We pass a block of workspace (cworkspace) for it
9511 to compile parts of the pattern into; the compiled code is discarded when it is
9512 no longer needed, so hopefully this workspace will never overflow, though there
9513 is a test for its doing so. */
9515 cd
->bracount
= cd
->final_bracount
= 0;
9516 cd
->names_found
= 0;
9517 cd
->name_entry_size
= 0;
9518 cd
->name_table
= NULL
;
9519 cd
->dupnames
= FALSE
;
9520 cd
->dupgroups
= FALSE
;
9521 cd
->namedrefcount
= 0;
9522 cd
->start_code
= cworkspace
;
9523 cd
->hwm
= cworkspace
;
9524 cd
->iscondassert
= FALSE
;
9525 cd
->start_workspace
= cworkspace
;
9526 cd
->workspace_size
= COMPILE_WORK_SIZE
;
9527 cd
->named_groups
= named_groups
;
9528 cd
->named_group_list_size
= NAMED_GROUP_LIST_SIZE
;
9529 cd
->start_pattern
= (const pcre_uchar
*)pattern
;
9530 if (options2
& PCRE_ALLOW_EMBEDDED_NUL_BIT
)
9531 cd
->end_pattern
= endptr
;
9533 cd
->end_pattern
= (const pcre_uchar
*)(pattern
+ STRLEN_UC((const pcre_uchar
*)pattern
));
9534 cd
->req_varyopt
= 0;
9535 cd
->parens_depth
= 0;
9536 cd
->assert_depth
= 0;
9537 cd
->max_lookbehind
= 0;
9538 cd
->external_options
= options
;
9539 cd
->extended_options
= options2
;
9540 cd
->open_caps
= NULL
;
9542 /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
9543 don't need to look at the result of the function here. The initial options have
9544 been put into the cd block so that they can be changed if an option setting is
9545 found within the regex right at the beginning. Bringing initial option settings
9546 outside can help speed up starting point checks. */
9552 (void)compile_regex(cd
->external_options
, &code
, &ptr
, &errorcode
, FALSE
,
9553 FALSE
, 0, 0, &firstchar
, &firstcharflags
, &reqchar
, &reqcharflags
, NULL
,
9555 if (errorcode
!= 0) goto PCRE_EARLY_ERROR_RETURN
;
9557 DPRINTF(("end pre-compile: length=%d workspace=%d\n", length
,
9558 (int)(cd
->hwm
- cworkspace
)));
9560 if (length
> MAX_PATTERN_SIZE
)
9563 goto PCRE_EARLY_ERROR_RETURN
;
9566 /* Compute the size of the data block for storing the compiled pattern. Integer
9567 overflow should no longer be possible because nowadays we limit the maximum
9568 value of cd->names_found and cd->name_entry_size. */
9570 size
= sizeof(REAL_PCRE
) +
9571 (length
+ cd
->names_found
* cd
->name_entry_size
) * sizeof(pcre_uchar
);
9573 /* Get the memory. */
9575 re
= (REAL_PCRE
*)(PUBL(malloc
))(size
);
9579 goto PCRE_EARLY_ERROR_RETURN
;
9582 /* Put in the magic number, and save the sizes, initial options, internal
9583 flags, and character table pointer. NULL is used for the default character
9584 tables. The nullpad field is at the end; it's there to help in the case when a
9585 regex compiled on a system with 4-byte pointers is run on another with 8-byte
9588 re
->magic_number
= MAGIC_NUMBER
;
9589 re
->size
= (int)size
;
9590 re
->options
= cd
->external_options
;
9591 re
->flags
= cd
->external_flags
;
9592 re
->limit_match
= limit_match
;
9593 re
->limit_recursion
= limit_recursion
;
9596 re
->name_table_offset
= sizeof(REAL_PCRE
) / sizeof(pcre_uchar
);
9597 re
->name_entry_size
= cd
->name_entry_size
;
9598 re
->name_count
= cd
->names_found
;
9600 re
->tables
= (tables
== PRIV(default_tables
))? NULL
: tables
;
9602 #ifdef COMPILE_PCRE32
9605 re
->dummy1
= re
->dummy2
= re
->dummy3
= 0;
9608 /* The starting points of the name/number translation table and of the code are
9609 passed around in the compile data block. The start/end pattern and initial
9610 options are already set from the pre-compile phase, as is the name_entry_size
9611 field. Reset the bracket count and the names_found field. Also reset the hwm
9612 field; this time it's used for remembering forward references to subpatterns.
9615 cd
->final_bracount
= cd
->bracount
; /* Save for checking forward references */
9616 cd
->parens_depth
= 0;
9617 cd
->assert_depth
= 0;
9619 cd
->max_lookbehind
= 0;
9620 cd
->name_table
= (pcre_uchar
*)re
+ re
->name_table_offset
;
9621 codestart
= cd
->name_table
+ re
->name_entry_size
* re
->name_count
;
9622 cd
->start_code
= codestart
;
9623 cd
->hwm
= (pcre_uchar
*)(cd
->start_workspace
);
9624 cd
->iscondassert
= FALSE
;
9625 cd
->req_varyopt
= 0;
9626 cd
->had_accept
= FALSE
;
9627 cd
->had_pruneorskip
= FALSE
;
9628 cd
->check_lookbehind
= FALSE
;
9629 cd
->open_caps
= NULL
;
9631 /* If any named groups were found, create the name/number table from the list
9632 created in the first pass. */
9634 if (cd
->names_found
> 0)
9636 int i
= cd
->names_found
;
9637 named_group
*ng
= cd
->named_groups
;
9638 cd
->names_found
= 0;
9639 for (; i
> 0; i
--, ng
++)
9640 add_name(cd
, ng
->name
, ng
->length
, ng
->number
);
9641 if (cd
->named_group_list_size
> NAMED_GROUP_LIST_SIZE
)
9642 (PUBL(free
))((void *)cd
->named_groups
);
9645 /* Set up a starting, non-extracting bracket, then compile the expression. On
9646 error, errorcode will be set non-zero, so we don't need to look at the result
9647 of the function here. */
9649 ptr
= (const pcre_uchar
*)pattern
+ skipatstart
;
9650 code
= (pcre_uchar
*)codestart
;
9652 (void)compile_regex(re
->options
, &code
, &ptr
, &errorcode
, FALSE
, FALSE
, 0, 0,
9653 &firstchar
, &firstcharflags
, &reqchar
, &reqcharflags
, NULL
, cd
, NULL
);
9654 re
->top_bracket
= cd
->bracount
;
9655 re
->top_backref
= cd
->top_backref
;
9656 re
->max_lookbehind
= cd
->max_lookbehind
;
9657 re
->flags
= cd
->external_flags
| PCRE_MODE
;
9661 reqchar
= 0; /* Must disable after (*ACCEPT) */
9662 reqcharflags
= REQ_NONE
;
9665 /* If not reached end of pattern on success, there's an excess bracket. */
9667 if (errorcode
== 0 && *ptr
!= CHAR_NULL
) errorcode
= ERR22
;
9669 /* Fill in the terminating state and check for disastrous overflow, but
9670 if debugging, leave the test till after things are printed out. */
9675 if (code
- codestart
> length
) errorcode
= ERR23
;
9678 #ifdef SUPPORT_VALGRIND
9679 /* If the estimated length exceeds the really used length, mark the extra
9680 allocated memory as unaddressable, so that any out-of-bound reads can be
9682 VALGRIND_MAKE_MEM_NOACCESS(code
, (length
- (code
- codestart
)) * sizeof(pcre_uchar
));
9685 /* Fill in any forward references that are required. There may be repeated
9686 references; optimize for them, as searching a large regex takes time. */
9688 if (cd
->hwm
> cd
->start_workspace
)
9690 int prev_recno
= -1;
9691 const pcre_uchar
*groupptr
= NULL
;
9692 while (errorcode
== 0 && cd
->hwm
> cd
->start_workspace
)
9695 cd
->hwm
-= LINK_SIZE
;
9696 offset
= GET(cd
->hwm
, 0);
9698 /* Check that the hwm handling hasn't gone wrong. This whole area is
9699 rewritten in PCRE2 because there are some obscure cases. */
9701 if (offset
== 0 || codestart
[offset
-1] != OP_RECURSE
)
9707 recno
= GET(codestart
, offset
);
9708 if (recno
!= prev_recno
)
9710 groupptr
= PRIV(find_bracket
)(codestart
, utf
, recno
);
9713 if (groupptr
== NULL
) errorcode
= ERR53
;
9714 else PUT(((pcre_uchar
*)codestart
), offset
, (int)(groupptr
- codestart
));
9718 /* If the workspace had to be expanded, free the new memory. Set the pointer to
9719 NULL to indicate that forward references have been filled in. */
9721 if (cd
->workspace_size
> COMPILE_WORK_SIZE
)
9722 (PUBL(free
))((void *)cd
->start_workspace
);
9723 cd
->start_workspace
= NULL
;
9725 /* Give an error if there's back reference to a non-existent capturing
9728 if (errorcode
== 0 && re
->top_backref
> re
->top_bracket
) errorcode
= ERR15
;
9730 /* Unless disabled, check whether any single character iterators can be
9731 auto-possessified. The function overwrites the appropriate opcode values, so
9732 the type of the pointer must be cast. NOTE: the intermediate variable "temp" is
9733 used in this code because at least one compiler gives a warning about loss of
9734 "const" attribute if the cast (pcre_uchar *)codestart is used directly in the
9737 if (errorcode
== 0 && (options
& PCRE_NO_AUTO_POSSESS
) == 0)
9739 pcre_uchar
*temp
= (pcre_uchar
*)codestart
;
9740 auto_possessify(temp
, utf
, cd
);
9743 /* If there were any lookbehind assertions that contained OP_RECURSE
9744 (recursions or subroutine calls), a flag is set for them to be checked here,
9745 because they may contain forward references. Actual recursions cannot be fixed
9746 length, but subroutine calls can. It is done like this so that those without
9747 OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
9748 exceptional ones forgo this. We scan the pattern to check that they are fixed
9749 length, and set their lengths. */
9751 if (errorcode
== 0 && cd
->check_lookbehind
)
9753 pcre_uchar
*cc
= (pcre_uchar
*)codestart
;
9755 /* Loop, searching for OP_REVERSE items, and process those that do not have
9756 their length set. (Actually, it will also re-process any that have a length
9757 of zero, but that is a pathological case, and it does no harm.) When we find
9758 one, we temporarily terminate the branch it is in while we scan it. */
9760 for (cc
= (pcre_uchar
*)PRIV(find_bracket
)(codestart
, utf
, -1);
9762 cc
= (pcre_uchar
*)PRIV(find_bracket
)(cc
, utf
, -1))
9764 if (GET(cc
, 1) == 0)
9767 pcre_uchar
*be
= cc
- 1 - LINK_SIZE
+ GET(cc
, -LINK_SIZE
);
9770 fixed_length
= find_fixedlength(cc
, (re
->options
& PCRE_UTF8
) != 0, TRUE
,
9773 DPRINTF(("fixed length = %d\n", fixed_length
));
9774 if (fixed_length
< 0)
9776 errorcode
= (fixed_length
== -2)? ERR36
:
9777 (fixed_length
== -4)? ERR70
: ERR25
;
9780 if (fixed_length
> cd
->max_lookbehind
) cd
->max_lookbehind
= fixed_length
;
9781 PUT(cc
, 1, fixed_length
);
9783 cc
+= 1 + LINK_SIZE
;
9787 /* Failed to compile, or error while post-processing */
9792 PCRE_EARLY_ERROR_RETURN
:
9793 *erroroffset
= (int)(ptr
- (const pcre_uchar
*)pattern
);
9794 PCRE_EARLY_ERROR_RETURN2
:
9795 *errorptr
= find_error_text(errorcode
);
9796 if (errorcodeptr
!= NULL
) *errorcodeptr
= errorcode
;
9800 /* If the anchored option was not passed, set the flag if we can determine that
9801 the pattern is anchored by virtue of ^ characters or \A or anything else, such
9802 as starting with non-atomic .* when DOTALL is set and there are no occurrences
9805 Otherwise, if we know what the first byte has to be, save it, because that
9806 speeds up unanchored matches no end. If not, see if we can set the
9807 PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
9808 start with ^. and also when all branches start with non-atomic .* for
9809 non-DOTALL matches when *PRUNE and SKIP are not present. */
9811 if ((re
->options
& PCRE_ANCHORED
) == 0)
9813 if (is_anchored(codestart
, 0, cd
, 0)) re
->options
|= PCRE_ANCHORED
;
9816 if (firstcharflags
< 0)
9817 firstchar
= find_firstassertedchar(codestart
, &firstcharflags
, FALSE
);
9818 if (firstcharflags
>= 0) /* Remove caseless flag for non-caseable chars */
9820 #if defined COMPILE_PCRE8
9821 re
->first_char
= firstchar
& 0xff;
9822 #elif defined COMPILE_PCRE16
9823 re
->first_char
= firstchar
& 0xffff;
9824 #elif defined COMPILE_PCRE32
9825 re
->first_char
= firstchar
;
9827 if ((firstcharflags
& REQ_CASELESS
) != 0)
9829 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
9830 /* We ignore non-ASCII first chars in 8 bit mode. */
9833 if (re
->first_char
< 128)
9835 if (cd
->fcc
[re
->first_char
] != re
->first_char
)
9836 re
->flags
|= PCRE_FCH_CASELESS
;
9838 else if (UCD_OTHERCASE(re
->first_char
) != re
->first_char
)
9839 re
->flags
|= PCRE_FCH_CASELESS
;
9843 if (MAX_255(re
->first_char
)
9844 && cd
->fcc
[re
->first_char
] != re
->first_char
)
9845 re
->flags
|= PCRE_FCH_CASELESS
;
9848 re
->flags
|= PCRE_FIRSTSET
;
9851 else if (is_startline(codestart
, 0, cd
, 0, FALSE
)) re
->flags
|= PCRE_STARTLINE
;
9855 /* For an anchored pattern, we use the "required byte" only if it follows a
9856 variable length item in the regex. Remove the caseless flag for non-caseable
9859 if (reqcharflags
>= 0 &&
9860 ((re
->options
& PCRE_ANCHORED
) == 0 || (reqcharflags
& REQ_VARY
) != 0))
9862 #if defined COMPILE_PCRE8
9863 re
->req_char
= reqchar
& 0xff;
9864 #elif defined COMPILE_PCRE16
9865 re
->req_char
= reqchar
& 0xffff;
9866 #elif defined COMPILE_PCRE32
9867 re
->req_char
= reqchar
;
9869 if ((reqcharflags
& REQ_CASELESS
) != 0)
9871 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
9872 /* We ignore non-ASCII first chars in 8 bit mode. */
9875 if (re
->req_char
< 128)
9877 if (cd
->fcc
[re
->req_char
] != re
->req_char
)
9878 re
->flags
|= PCRE_RCH_CASELESS
;
9880 else if (UCD_OTHERCASE(re
->req_char
) != re
->req_char
)
9881 re
->flags
|= PCRE_RCH_CASELESS
;
9885 if (MAX_255(re
->req_char
) && cd
->fcc
[re
->req_char
] != re
->req_char
)
9886 re
->flags
|= PCRE_RCH_CASELESS
;
9889 re
->flags
|= PCRE_REQCHSET
;
9892 /* Print out the compiled data if debugging is enabled. This is never the
9893 case when building a production library. */
9896 printf("Length = %d top_bracket = %d top_backref = %d\n",
9897 length
, re
->top_bracket
, re
->top_backref
);
9899 printf("Options=%08x\n", re
->options
);
9901 if ((re
->flags
& PCRE_FIRSTSET
) != 0)
9903 pcre_uchar ch
= re
->first_char
;
9904 const char *caseless
=
9905 ((re
->flags
& PCRE_FCH_CASELESS
) == 0)? "" : " (caseless)";
9906 if (PRINTABLE(ch
)) printf("First char = %c%s\n", ch
, caseless
);
9907 else printf("First char = \\x%02x%s\n", ch
, caseless
);
9910 if ((re
->flags
& PCRE_REQCHSET
) != 0)
9912 pcre_uchar ch
= re
->req_char
;
9913 const char *caseless
=
9914 ((re
->flags
& PCRE_RCH_CASELESS
) == 0)? "" : " (caseless)";
9915 if (PRINTABLE(ch
)) printf("Req char = %c%s\n", ch
, caseless
);
9916 else printf("Req char = \\x%02x%s\n", ch
, caseless
);
9919 #if defined COMPILE_PCRE8
9920 pcre_printint((pcre
*)re
, stdout
, TRUE
);
9921 #elif defined COMPILE_PCRE16
9922 pcre16_printint((pcre
*)re
, stdout
, TRUE
);
9923 #elif defined COMPILE_PCRE32
9924 pcre32_printint((pcre
*)re
, stdout
, TRUE
);
9927 /* This check is done here in the debugging case so that the code that
9928 was compiled can be seen. */
9930 if (code
- codestart
> length
)
9933 *errorptr
= find_error_text(ERR23
);
9934 *erroroffset
= ptr
- (pcre_uchar
*)pattern
;
9935 if (errorcodeptr
!= NULL
) *errorcodeptr
= ERR23
;
9938 #endif /* PCRE_DEBUG */
9940 /* Check for a pattern than can match an empty string, so that this information
9941 can be provided to applications. */
9945 if (could_be_empty_branch(codestart
, code
, utf
, cd
, NULL
))
9947 re
->flags
|= PCRE_MATCH_EMPTY
;
9950 codestart
+= GET(codestart
, 1);
9952 while (*codestart
== OP_ALT
);
9954 #if defined COMPILE_PCRE8
9956 #elif defined COMPILE_PCRE16
9957 return (pcre16
*)re
;
9958 #elif defined COMPILE_PCRE32
9959 return (pcre32
*)re
;
9963 /* End of pcre_compile.c */