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-2012 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. */
47 #define NLBLOCK cd /* Block containing newline information */
48 #define PSSTART start_pattern /* Field containing processed string start */
49 #define PSEND end_pattern /* Field containing processed string end */
51 #include "pcre_internal.h"
53 #ifdef GLIB_COMPILATION
54 #include "gstrfuncs.h"
59 /* When PCRE_DEBUG is defined, we need the pcre(16)_printint() function, which
60 is also used by pcretest. PCRE_DEBUG is not defined when building a production
61 library. We do not need to select pcre16_printint.c specially, because the
62 COMPILE_PCREx macro will already be appropriately set. */
65 /* pcre_printint.c should not include any headers */
67 #include "pcre_printint.c"
72 /* Macro for setting individual bits in class bitmaps. */
74 #define SETBIT(a,b) a[b/8] |= (1 << (b%8))
76 /* Maximum length value to check against when making sure that the integer that
77 holds the compiled pattern length does not overflow. We make it a bit less than
78 INT_MAX to allow for adding in group terminating bytes, so that we don't have
79 to check them every time. */
81 #define OFLOW_MAX (INT_MAX - 20)
84 /*************************************************
85 * Code parameters and static tables *
86 *************************************************/
88 /* This value specifies the size of stack workspace that is used during the
89 first pre-compile phase that determines how much memory is required. The regex
90 is partly compiled into this space, but the compiled parts are discarded as
91 soon as they can be, so that hopefully there will never be an overrun. The code
92 does, however, check for an overrun. The largest amount I've seen used is 218,
93 so this number is very generous.
95 The same workspace is used during the second, actual compile phase for
96 remembering forward references to groups so that they can be filled in at the
97 end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
98 is 4 there is plenty of room for most patterns. However, the memory can get
99 filled up by repetitions of forward references, for example patterns like
100 /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
101 that the workspace is expanded using malloc() in this situation. The value
102 below is therefore a minimum, and we put a maximum on it for safety. The
103 minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
104 kicks in at the same number of forward references in all cases. */
106 #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
107 #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
109 /* The overrun tests check for a slightly smaller size so that they detect the
110 overrun before it actually does run off the end of the data block. */
112 #define WORK_SIZE_SAFETY_MARGIN (100)
114 /* Private flags added to firstchar and reqchar. */
116 #define REQ_CASELESS 0x10000000l /* Indicates caselessness */
117 #define REQ_VARY 0x20000000l /* Reqchar followed non-literal item */
119 /* Repeated character flags. */
121 #define UTF_LENGTH 0x10000000l /* The char contains its length. */
123 /* Table for handling escaped characters in the range '0'-'z'. Positive returns
124 are simple data values; negative values are for special things like \d and so
125 on. Zero means further processing is needed (for things like \x), or the escape
130 /* This is the "normal" table for ASCII systems or for EBCDIC systems running
133 static const short int escapes
[] = {
139 CHAR_COLON
, CHAR_SEMICOLON
,
140 CHAR_LESS_THAN_SIGN
, CHAR_EQUALS_SIGN
,
141 CHAR_GREATER_THAN_SIGN
, CHAR_QUESTION_MARK
,
142 CHAR_COMMERCIAL_AT
, -ESC_A
,
155 -ESC_Z
, CHAR_LEFT_SQUARE_BRACKET
,
156 CHAR_BACKSLASH
, CHAR_RIGHT_SQUARE_BRACKET
,
157 CHAR_CIRCUMFLEX_ACCENT
, CHAR_UNDERSCORE
,
158 CHAR_GRAVE_ACCENT
, 7,
176 /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
178 static const short int escapes
[] = {
179 /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
180 /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
181 /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
182 /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
183 /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
184 /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
185 /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
186 /* 80 */ 0, 7, -ESC_b
, 0, -ESC_d
, ESC_e
, ESC_f
, 0,
187 /* 88 */-ESC_h
, 0, 0, '{', 0, 0, 0, 0,
188 /* 90 */ 0, 0, -ESC_k
, 'l', 0, ESC_n
, 0, -ESC_p
,
189 /* 98 */ 0, ESC_r
, 0, '}', 0, 0, 0, 0,
190 /* A0 */ 0, '~', -ESC_s
, ESC_tee
, 0,-ESC_v
, -ESC_w
, 0,
191 /* A8 */ 0,-ESC_z
, 0, 0, 0, '[', 0, 0,
192 /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
193 /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
194 /* C0 */ '{',-ESC_A
, -ESC_B
, -ESC_C
, -ESC_D
,-ESC_E
, 0, -ESC_G
,
195 /* C8 */-ESC_H
, 0, 0, 0, 0, 0, 0, 0,
196 /* D0 */ '}', 0, -ESC_K
, 0, 0,-ESC_N
, 0, -ESC_P
,
197 /* D8 */-ESC_Q
,-ESC_R
, 0, 0, 0, 0, 0, 0,
198 /* E0 */ '\\', 0, -ESC_S
, 0, 0,-ESC_V
, -ESC_W
, -ESC_X
,
199 /* E8 */ 0,-ESC_Z
, 0, 0, 0, 0, 0, 0,
200 /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
201 /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
206 /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
207 searched linearly. Put all the names into a single string, in order to reduce
208 the number of relocations when a shared library is dynamically linked. The
209 string is built from string macros so that it works in UTF-8 mode on EBCDIC
212 typedef struct verbitem
{
213 int len
; /* Length of verb name */
214 int op
; /* Op when no arg, or -1 if arg mandatory */
215 int op_arg
; /* Op when arg present, or -1 if not allowed */
218 static const char verbnames
[] =
219 "\0" /* Empty name is a shorthand for MARK */
229 static const verbitem verbs
[] = {
232 { 6, OP_ACCEPT
, -1 },
233 { 6, OP_COMMIT
, -1 },
236 { 5, OP_PRUNE
, OP_PRUNE_ARG
},
237 { 4, OP_SKIP
, OP_SKIP_ARG
},
238 { 4, OP_THEN
, OP_THEN_ARG
}
241 static const int verbcount
= sizeof(verbs
)/sizeof(verbitem
);
244 /* Tables of names of POSIX character classes and their lengths. The names are
245 now all in a single string, to reduce the number of relocations when a shared
246 library is dynamically loaded. The list of lengths is terminated by a zero
247 length entry. The first three must be alpha, lower, upper, as this is assumed
248 for handling case independence. */
250 static const char posix_names
[] =
251 STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
252 STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
253 STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
254 STRING_word0 STRING_xdigit
;
256 static const pcre_uint8 posix_name_lengths
[] = {
257 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
259 /* Table of class bit maps for each POSIX class. Each class is formed from a
260 base map, with an optional addition or removal of another map. Then, for some
261 classes, there is some additional tweaking: for [:blank:] the vertical space
262 characters are removed, and for [:alpha:] and [:alnum:] the underscore
263 character is removed. The triples in the table consist of the base map offset,
264 second map offset or -1 if no second map, and a non-negative value for map
265 addition or a negative value for map subtraction (if there are two maps). The
266 absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
267 remove vertical space characters, 2 => remove underscore. */
269 static const int posix_class_maps
[] = {
270 cbit_word
, cbit_digit
, -2, /* alpha */
271 cbit_lower
, -1, 0, /* lower */
272 cbit_upper
, -1, 0, /* upper */
273 cbit_word
, -1, 2, /* alnum - word without underscore */
274 cbit_print
, cbit_cntrl
, 0, /* ascii */
275 cbit_space
, -1, 1, /* blank - a GNU extension */
276 cbit_cntrl
, -1, 0, /* cntrl */
277 cbit_digit
, -1, 0, /* digit */
278 cbit_graph
, -1, 0, /* graph */
279 cbit_print
, -1, 0, /* print */
280 cbit_punct
, -1, 0, /* punct */
281 cbit_space
, -1, 0, /* space */
282 cbit_word
, -1, 0, /* word - a Perl extension */
283 cbit_xdigit
,-1, 0 /* xdigit */
286 /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class
287 substitutes must be in the order of the names, defined above, and there are
288 both positive and negative cases. NULL means no substitute. */
291 static const pcre_uchar string_PNd
[] = {
292 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
293 CHAR_N
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
294 static const pcre_uchar string_pNd
[] = {
295 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
296 CHAR_N
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
297 static const pcre_uchar string_PXsp
[] = {
298 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
299 CHAR_X
, CHAR_s
, CHAR_p
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
300 static const pcre_uchar string_pXsp
[] = {
301 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
302 CHAR_X
, CHAR_s
, CHAR_p
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
303 static const pcre_uchar string_PXwd
[] = {
304 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
305 CHAR_X
, CHAR_w
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
306 static const pcre_uchar string_pXwd
[] = {
307 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
308 CHAR_X
, CHAR_w
, CHAR_d
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
310 static const pcre_uchar
*substitutes
[] = {
313 string_PXsp
, /* \S */ /* NOTE: Xsp is Perl space */
314 string_pXsp
, /* \s */
315 string_PXwd
, /* \W */
319 static const pcre_uchar string_pL
[] = {
320 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
321 CHAR_L
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
322 static const pcre_uchar string_pLl
[] = {
323 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
324 CHAR_L
, CHAR_l
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
325 static const pcre_uchar string_pLu
[] = {
326 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
327 CHAR_L
, CHAR_u
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
328 static const pcre_uchar string_pXan
[] = {
329 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
330 CHAR_X
, CHAR_a
, CHAR_n
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
331 static const pcre_uchar string_h
[] = {
332 CHAR_BACKSLASH
, CHAR_h
, '\0' };
333 static const pcre_uchar string_pXps
[] = {
334 CHAR_BACKSLASH
, CHAR_p
, CHAR_LEFT_CURLY_BRACKET
,
335 CHAR_X
, CHAR_p
, CHAR_s
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
336 static const pcre_uchar string_PL
[] = {
337 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
338 CHAR_L
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
339 static const pcre_uchar string_PLl
[] = {
340 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
341 CHAR_L
, CHAR_l
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
342 static const pcre_uchar string_PLu
[] = {
343 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
344 CHAR_L
, CHAR_u
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
345 static const pcre_uchar string_PXan
[] = {
346 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
347 CHAR_X
, CHAR_a
, CHAR_n
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
348 static const pcre_uchar string_H
[] = {
349 CHAR_BACKSLASH
, CHAR_H
, '\0' };
350 static const pcre_uchar string_PXps
[] = {
351 CHAR_BACKSLASH
, CHAR_P
, CHAR_LEFT_CURLY_BRACKET
,
352 CHAR_X
, CHAR_p
, CHAR_s
, CHAR_RIGHT_CURLY_BRACKET
, '\0' };
354 static const pcre_uchar
*posix_substitutes
[] = {
355 string_pL
, /* alpha */
356 string_pLl
, /* lower */
357 string_pLu
, /* upper */
358 string_pXan
, /* alnum */
360 string_h
, /* blank */
362 string_pNd
, /* digit */
366 string_pXps
, /* space */ /* NOTE: Xps is POSIX space */
367 string_pXwd
, /* word */
370 string_PL
, /* ^alpha */
371 string_PLl
, /* ^lower */
372 string_PLu
, /* ^upper */
373 string_PXan
, /* ^alnum */
375 string_H
, /* ^blank */
377 string_PNd
, /* ^digit */
381 string_PXps
, /* ^space */ /* NOTE: Xps is POSIX space */
382 string_PXwd
, /* ^word */
385 #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
388 #define STRING(a) # a
389 #define XSTRING(s) STRING(s)
391 /* The texts of compile-time error messages. These are "char *" because they
392 are passed to the outside world. Do not ever re-use any error number, because
393 they are documented. Always add a new error instead. Messages marked DEAD below
394 are no longer used. This used to be a table of strings, but in order to reduce
395 the number of relocations needed when a shared library is loaded dynamically,
396 it is now one long string. We cannot use a table of offsets, because the
397 lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
398 simply count through to the one we want - this isn't a performance issue
399 because these strings are used only when there is a compilation error.
401 Each substring ends with \0 to insert a null character. This includes the final
402 substring, so that the whole string ends with \0\0, which can be detected when
405 static const char error_texts
[] =
407 "\\ at end of pattern\0"
408 "\\c at end of pattern\0"
409 "unrecognized character follows \\\0"
410 "numbers out of order in {} quantifier\0"
412 "number too big in {} quantifier\0"
413 "missing terminating ] for character class\0"
414 "invalid escape sequence in character class\0"
415 "range out of order in character class\0"
416 "nothing to repeat\0"
418 "operand of unlimited repeat could match the empty string\0" /** DEAD **/
419 "internal error: unexpected repeat\0"
420 "unrecognized character after (? or (?-\0"
421 "POSIX named classes are supported only within a class\0"
424 "reference to non-existent subpattern\0"
425 "erroffset passed as NULL\0"
426 "unknown option bit(s) set\0"
427 "missing ) after comment\0"
428 "parentheses nested too deeply\0" /** DEAD **/
430 "regular expression is too large\0"
431 "failed to get memory\0"
432 "unmatched parentheses\0"
433 "internal error: code overflow\0"
434 "unrecognized character after (?<\0"
436 "lookbehind assertion is not fixed length\0"
437 "malformed number or name after (?(\0"
438 "conditional group contains more than two branches\0"
439 "assertion expected after (?(\0"
440 "(?R or (?[+-]digits must be followed by )\0"
442 "unknown POSIX class name\0"
443 "POSIX collating elements are not supported\0"
444 "this version of PCRE is compiled without UTF support\0"
445 "spare error\0" /** DEAD **/
446 "character value in \\x{...} sequence is too large\0"
448 "invalid condition (?(0)\0"
449 "\\C not allowed in lookbehind assertion\0"
450 "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
451 "number after (?C is > 255\0"
452 "closing ) for (?C expected\0"
454 "recursive call could loop indefinitely\0"
455 "unrecognized character after (?P\0"
456 "syntax error in subpattern name (missing terminator)\0"
457 "two named subpatterns have the same name\0"
458 "invalid UTF-8 string\0"
460 "support for \\P, \\p, and \\X has not been compiled\0"
461 "malformed \\P or \\p sequence\0"
462 "unknown property name after \\P or \\p\0"
463 "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE
) " characters)\0"
464 "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT
) ")\0"
466 "repeated subpattern is too long\0" /** DEAD **/
467 "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
468 "internal error: overran compiling workspace\0"
469 "internal error: previously-checked referenced subpattern not found\0"
470 "DEFINE group contains more than one branch\0"
472 "repeating a DEFINE group is not allowed\0" /** DEAD **/
473 "inconsistent NEWLINE options\0"
474 "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
475 "a numbered reference must not be zero\0"
476 "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
478 "(*VERB) not recognized\0"
479 "number is too big\0"
480 "subpattern name expected\0"
481 "digit expected after (?+\0"
482 "] is an invalid data character in JavaScript compatibility mode\0"
484 "different names for subpatterns of the same number are not allowed\0"
485 "(*MARK) must have an argument\0"
486 "this version of PCRE is not compiled with Unicode property support\0"
487 "\\c must be followed by an ASCII character\0"
488 "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
490 "internal error: unknown opcode in find_fixedlength()\0"
491 "\\N is not supported in a class\0"
492 "too many forward references\0"
493 "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
494 "invalid UTF-16 string\0"
496 "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
497 "character value in \\u.... sequence is too large\0"
500 /* Table to identify digits and hex digits. This is used when compiling
501 patterns. Note that the tables in chartables are dependent on the locale, and
502 may mark arbitrary characters as digits - but the PCRE compiling code expects
503 to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
504 a private table here. It costs 256 bytes, but it is a lot faster than doing
505 character value tests (at least in some simple cases I timed), and in some
506 applications one wants PCRE to compile efficiently as well as match
509 For convenience, we use the same bit definitions as in chartables:
512 0x08 hexadecimal digit
514 Then we can use ctype_digit and ctype_xdigit in the code. */
516 /* Using a simple comparison for decimal numbers rather than a memory read
517 is much faster, and the resulting code is simpler (the compiler turns it
518 into a subtraction and unsigned comparison). */
520 #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
525 /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
528 static const pcre_uint8 digitab
[] =
530 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
531 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
532 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
533 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
534 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
535 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
536 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
537 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
538 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
539 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
540 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
541 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
542 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
543 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
544 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
545 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
546 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
547 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
548 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
549 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
550 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
551 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
552 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
553 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
554 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
555 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
556 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
557 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
558 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
559 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
560 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
561 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
565 /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
567 static const pcre_uint8 digitab
[] =
569 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
570 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
571 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
572 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
573 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
574 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
575 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
576 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
577 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
578 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
579 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
580 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
581 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
582 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
583 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
584 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
585 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
586 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
587 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
588 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
589 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
590 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
591 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
592 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
593 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
594 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
595 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
596 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
597 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
598 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
599 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
600 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
602 static const pcre_uint8 ebcdic_chartab
[] = { /* chartable partial dup */
603 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
604 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
605 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
606 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
607 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
608 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
609 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
610 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
611 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
612 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
613 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
614 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
615 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
616 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
617 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
618 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
619 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
620 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
621 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
622 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
623 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
624 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
625 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
626 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
627 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
628 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
629 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
630 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
631 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
632 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
633 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
634 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
638 /* Definition to allow mutual recursion */
641 compile_regex(int, pcre_uchar
**, const pcre_uchar
**, int *, BOOL
, BOOL
, int, int,
642 int *, int *, branch_chain
*, compile_data
*, int *);
646 /*************************************************
647 * Find an error text *
648 *************************************************/
650 /* The error texts are now all in one long string, to save on relocations. As
651 some of the text is of unknown length, we can't use a table of offsets.
652 Instead, just count through the strings. This is not a performance issue
653 because it happens only when there has been a compilation error.
655 Argument: the error number
656 Returns: pointer to the error string
660 find_error_text(int n
)
662 const char *s
= error_texts
;
665 while (*s
++ != 0) {};
666 if (*s
== 0) return "Error text not found (please report)";
672 /*************************************************
673 * Expand the workspace *
674 *************************************************/
676 /* This function is called during the second compiling phase, if the number of
677 forward references fills the existing workspace, which is originally a block on
678 the stack. A larger block is obtained from malloc() unless the ultimate limit
679 has been reached or the increase will be rather small.
681 Argument: pointer to the compile data block
682 Returns: 0 if all went well, else an error number
686 expand_workspace(compile_data
*cd
)
688 pcre_uchar
*newspace
;
689 int newsize
= cd
->workspace_size
* 2;
691 if (newsize
> COMPILE_WORK_SIZE_MAX
) newsize
= COMPILE_WORK_SIZE_MAX
;
692 if (cd
->workspace_size
>= COMPILE_WORK_SIZE_MAX
||
693 newsize
- cd
->workspace_size
< WORK_SIZE_SAFETY_MARGIN
)
696 newspace
= (PUBL(malloc
))(IN_UCHARS(newsize
));
697 if (newspace
== NULL
) return ERR21
;
698 memcpy(newspace
, cd
->start_workspace
, cd
->workspace_size
* sizeof(pcre_uchar
));
699 cd
->hwm
= (pcre_uchar
*)newspace
+ (cd
->hwm
- cd
->start_workspace
);
700 if (cd
->workspace_size
> COMPILE_WORK_SIZE
)
701 (PUBL(free
))((void *)cd
->start_workspace
);
702 cd
->start_workspace
= newspace
;
703 cd
->workspace_size
= newsize
;
709 /*************************************************
710 * Check for counted repeat *
711 *************************************************/
713 /* This function is called when a '{' is encountered in a place where it might
714 start a quantifier. It looks ahead to see if it really is a quantifier or not.
715 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
716 where the ddds are digits.
719 p pointer to the first char after '{'
721 Returns: TRUE or FALSE
725 is_counted_repeat(const pcre_uchar
*p
)
727 if (!IS_DIGIT(*p
)) return FALSE
;
729 while (IS_DIGIT(*p
)) p
++;
730 if (*p
== CHAR_RIGHT_CURLY_BRACKET
) return TRUE
;
732 if (*p
++ != CHAR_COMMA
) return FALSE
;
733 if (*p
== CHAR_RIGHT_CURLY_BRACKET
) return TRUE
;
735 if (!IS_DIGIT(*p
)) return FALSE
;
737 while (IS_DIGIT(*p
)) p
++;
739 return (*p
== CHAR_RIGHT_CURLY_BRACKET
);
744 /*************************************************
746 *************************************************/
748 /* This function is called when a \ has been encountered. It either returns a
749 positive value for a simple escape such as \n, or a negative value which
750 encodes one of the more complicated things such as \d. A backreference to group
751 n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
752 UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
753 ptr is pointing at the \. On exit, it is on the final character of the escape
757 ptrptr points to the pattern position pointer
758 errorcodeptr points to the errorcode variable
759 bracount number of previous extracting brackets
760 options the options bits
761 isclass TRUE if inside a character class
763 Returns: zero or positive => a data character
764 negative => a special escape sequence
765 on error, errorcodeptr is set
769 check_escape(const pcre_uchar
**ptrptr
, int *errorcodeptr
, int bracount
,
770 int options
, BOOL isclass
)
772 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
773 BOOL utf
= (options
& PCRE_UTF8
) != 0;
774 const pcre_uchar
*ptr
= *ptrptr
+ 1;
778 GETCHARINCTEST(c
, ptr
); /* Get character value, increment pointer */
779 ptr
--; /* Set pointer back to the last byte */
781 /* If backslash is at the end of the pattern, it's an error. */
783 if (c
== 0) *errorcodeptr
= ERR1
;
785 /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
786 in a table. A non-zero result is something that can be returned immediately.
787 Otherwise further processing may be required. */
789 #ifndef EBCDIC /* ASCII/UTF-8 coding */
790 /* Not alphanumeric */
791 else if (c
< CHAR_0
|| c
> CHAR_z
) {}
792 else if ((i
= escapes
[c
- CHAR_0
]) != 0) c
= i
;
794 #else /* EBCDIC coding */
795 /* Not alphanumeric */
796 else if (c
< 'a' || (!MAX_255(c
) || (ebcdic_chartab
[c
] & 0x0E) == 0)) {}
797 else if ((i
= escapes
[c
- 0x48]) != 0) c
= i
;
800 /* Escapes that need further processing, or are illegal. */
804 const pcre_uchar
*oldptr
;
805 BOOL braced
, negated
;
809 /* A number of Perl escapes are not handled by PCRE. We give an explicit
814 *errorcodeptr
= ERR37
;
818 if ((options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
820 /* In JavaScript, \u must be followed by four hexadecimal numbers.
821 Otherwise it is a lowercase u letter. */
822 if (MAX_255(ptr
[1]) && g_ascii_isxdigit(ptr
[1]) != 0
823 && MAX_255(ptr
[2]) && g_ascii_isxdigit(ptr
[2]) != 0
824 && MAX_255(ptr
[3]) && g_ascii_isxdigit(ptr
[3]) != 0
825 && MAX_255(ptr
[4]) && g_ascii_isxdigit(ptr
[4]) != 0)
828 for (i
= 0; i
< 4; ++i
)
831 #ifndef EBCDIC /* ASCII/UTF-8 coding */
832 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
833 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
834 #else /* EBCDIC coding */
835 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
836 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
841 if (c
> (utf
? 0x10ffff : 0xff))
843 #ifdef COMPILE_PCRE16
844 if (c
> (utf
? 0x10ffff : 0xffff))
848 *errorcodeptr
= ERR76
;
850 else if (utf
&& c
>= 0xd800 && c
<= 0xdfff) *errorcodeptr
= ERR73
;
854 *errorcodeptr
= ERR37
;
858 /* In JavaScript, \U is an uppercase U letter. */
859 if ((options
& PCRE_JAVASCRIPT_COMPAT
) == 0) *errorcodeptr
= ERR37
;
862 /* In a character class, \g is just a literal "g". Outside a character
863 class, \g must be followed by one of a number of specific things:
865 (1) A number, either plain or braced. If positive, it is an absolute
866 backreference. If negative, it is a relative backreference. This is a Perl
869 (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
870 is part of Perl's movement towards a unified syntax for back references. As
871 this is synonymous with \k{name}, we fudge it up by pretending it really
874 (3) For Oniguruma compatibility we also support \g followed by a name or a
875 number either in angle brackets or in single quotes. However, these are
876 (possibly recursive) subroutine calls, _not_ backreferences. Just return
877 the -ESC_g code (cf \k). */
881 if (ptr
[1] == CHAR_LESS_THAN_SIGN
|| ptr
[1] == CHAR_APOSTROPHE
)
887 /* Handle the Perl-compatible cases */
889 if (ptr
[1] == CHAR_LEFT_CURLY_BRACKET
)
892 for (p
= ptr
+2; *p
!= 0 && *p
!= CHAR_RIGHT_CURLY_BRACKET
; p
++)
893 if (*p
!= CHAR_MINUS
&& !IS_DIGIT(*p
)) break;
894 if (*p
!= 0 && *p
!= CHAR_RIGHT_CURLY_BRACKET
)
904 if (ptr
[1] == CHAR_MINUS
)
909 else negated
= FALSE
;
911 /* The integer range is limited by the machine's int representation. */
913 while (IS_DIGIT(ptr
[1]))
915 if (((unsigned int)c
) > INT_MAX
/ 10) /* Integer overflow */
920 c
= c
* 10 + *(++ptr
) - CHAR_0
;
922 if (((unsigned int)c
) > INT_MAX
) /* Integer overflow */
924 while (IS_DIGIT(ptr
[1]))
926 *errorcodeptr
= ERR61
;
930 if (braced
&& *(++ptr
) != CHAR_RIGHT_CURLY_BRACKET
)
932 *errorcodeptr
= ERR57
;
938 *errorcodeptr
= ERR58
;
946 *errorcodeptr
= ERR15
;
949 c
= bracount
- (c
- 1);
955 /* The handling of escape sequences consisting of a string of digits
956 starting with one that is not zero is not straightforward. By experiment,
957 the way Perl works seems to be as follows:
959 Outside a character class, the digits are read as a decimal number. If the
960 number is less than 10, or if there are that many previous extracting
961 left brackets, then it is a back reference. Otherwise, up to three octal
962 digits are read to form an escaped byte. Thus \123 is likely to be octal
963 123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
964 value is greater than 377, the least significant 8 bits are taken. Inside a
965 character class, \ followed by a digit is always an octal number. */
967 case CHAR_1
: case CHAR_2
: case CHAR_3
: case CHAR_4
: case CHAR_5
:
968 case CHAR_6
: case CHAR_7
: case CHAR_8
: case CHAR_9
:
973 /* The integer range is limited by the machine's int representation. */
975 while (IS_DIGIT(ptr
[1]))
977 if (((unsigned int)c
) > INT_MAX
/ 10) /* Integer overflow */
982 c
= c
* 10 + *(++ptr
) - CHAR_0
;
984 if (((unsigned int)c
) > INT_MAX
) /* Integer overflow */
986 while (IS_DIGIT(ptr
[1]))
988 *errorcodeptr
= ERR61
;
991 if (c
< 10 || c
<= bracount
)
996 ptr
= oldptr
; /* Put the pointer back and fall through */
999 /* Handle an octal number following \. If the first digit is 8 or 9, Perl
1000 generates a binary zero byte and treats the digit as a following literal.
1001 Thus we have to pull back the pointer by one. */
1003 if ((c
= *ptr
) >= CHAR_8
)
1010 /* \0 always starts an octal number, but we may drop through to here with a
1011 larger first octal digit. The original code used just to take the least
1012 significant 8 bits of octal numbers (I think this is what early Perls used
1013 to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1014 but no more than 3 octal digits. */
1018 while(i
++ < 2 && ptr
[1] >= CHAR_0
&& ptr
[1] <= CHAR_7
)
1019 c
= c
* 8 + *(++ptr
) - CHAR_0
;
1020 #ifdef COMPILE_PCRE8
1021 if (!utf
&& c
> 0xff) *errorcodeptr
= ERR51
;
1025 /* \x is complicated. \x{ddd} is a character number which can be greater
1026 than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
1027 If not, { is treated as a data character. */
1030 if ((options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
1032 /* In JavaScript, \x must be followed by two hexadecimal numbers.
1033 Otherwise it is a lowercase x letter. */
1034 if (MAX_255(ptr
[1]) && g_ascii_isxdigit(ptr
[1]) != 0
1035 && MAX_255(ptr
[2]) && g_ascii_isxdigit(ptr
[2]) != 0)
1038 for (i
= 0; i
< 2; ++i
)
1041 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1042 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1043 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1044 #else /* EBCDIC coding */
1045 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1046 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1053 if (ptr
[1] == CHAR_LEFT_CURLY_BRACKET
)
1055 const pcre_uchar
*pt
= ptr
+ 2;
1058 while (MAX_255(*pt
) && g_ascii_isxdigit(*pt
) != 0)
1061 if (c
== 0 && cc
== CHAR_0
) continue; /* Leading zeroes */
1063 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1064 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1065 c
= (c
<< 4) + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1066 #else /* EBCDIC coding */
1067 if (cc
>= CHAR_a
&& cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1068 c
= (c
<< 4) + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1071 #ifdef COMPILE_PCRE8
1072 if (c
> (utf
? 0x10ffff : 0xff)) { c
= -1; break; }
1074 #ifdef COMPILE_PCRE16
1075 if (c
> (utf
? 0x10ffff : 0xffff)) { c
= -1; break; }
1082 while (MAX_255(*pt
) && g_ascii_isxdigit(*pt
) != 0) pt
++;
1083 *errorcodeptr
= ERR34
;
1086 if (*pt
== CHAR_RIGHT_CURLY_BRACKET
)
1088 if (utf
&& c
>= 0xd800 && c
<= 0xdfff) *errorcodeptr
= ERR73
;
1093 /* If the sequence of hex digits does not end with '}', then we don't
1094 recognize this construct; fall through to the normal \x handling. */
1097 /* Read just a single-byte hex-defined char */
1100 while (i
++ < 2 && MAX_255(ptr
[1]) && g_ascii_isxdigit(ptr
[1]) != 0)
1102 int cc
; /* Some compilers don't like */
1103 cc
= *(++ptr
); /* ++ in initializers */
1104 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1105 if (cc
>= CHAR_a
) cc
-= 32; /* Convert to upper case */
1106 c
= c
* 16 + cc
- ((cc
< CHAR_A
)? CHAR_0
: (CHAR_A
- 10));
1107 #else /* EBCDIC coding */
1108 if (cc
<= CHAR_z
) cc
+= 64; /* Convert to upper case */
1109 c
= c
* 16 + cc
- ((cc
>= CHAR_0
)? CHAR_0
: (CHAR_A
- 10));
1114 /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1115 An error is given if the byte following \c is not an ASCII character. This
1116 coding is ASCII-specific, but then the whole concept of \cx is
1117 ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1123 *errorcodeptr
= ERR2
;
1126 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1127 if (c
> 127) /* Excludes all non-ASCII in either mode */
1129 *errorcodeptr
= ERR68
;
1132 if (c
>= CHAR_a
&& c
<= CHAR_z
) c
-= 32;
1134 #else /* EBCDIC coding */
1135 if (c
>= CHAR_a
&& c
<= CHAR_z
) c
+= 64;
1140 /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1141 other alphanumeric following \ is an error if PCRE_EXTRA was set;
1142 otherwise, for Perl compatibility, it is a literal. This code looks a bit
1143 odd, but there used to be some cases other than the default, and there may
1144 be again in future, so I haven't "optimized" it. */
1147 if ((options
& PCRE_EXTRA
) != 0) switch(c
)
1150 *errorcodeptr
= ERR3
;
1157 /* Perl supports \N{name} for character names, as well as plain \N for "not
1158 newline". PCRE does not support \N{name}. However, it does support
1159 quantification such as \N{2,3}. */
1161 if (c
== -ESC_N
&& ptr
[1] == CHAR_LEFT_CURLY_BRACKET
&&
1162 !is_counted_repeat(ptr
+2))
1163 *errorcodeptr
= ERR37
;
1165 /* If PCRE_UCP is set, we change the values for \d etc. */
1167 if ((options
& PCRE_UCP
) != 0 && c
<= -ESC_D
&& c
>= -ESC_w
)
1168 c
-= (ESC_DU
- ESC_D
);
1170 /* Set the pointer to the final character before returning. */
1179 /*************************************************
1180 * Handle \P and \p *
1181 *************************************************/
1183 /* This function is called after \P or \p has been encountered, provided that
1184 PCRE is compiled with support for Unicode properties. On entry, ptrptr is
1185 pointing at the P or p. On exit, it is pointing at the final character of the
1189 ptrptr points to the pattern position pointer
1190 negptr points to a boolean that is set TRUE for negation else FALSE
1191 dptr points to an int that is set to the detailed property value
1192 errorcodeptr points to the error code variable
1194 Returns: type value from ucp_type_table, or -1 for an invalid type
1198 get_ucp(const pcre_uchar
**ptrptr
, BOOL
*negptr
, int *dptr
, int *errorcodeptr
)
1201 const pcre_uchar
*ptr
= *ptrptr
;
1202 pcre_uchar name
[32];
1205 if (c
== 0) goto ERROR_RETURN
;
1209 /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1212 if (c
== CHAR_LEFT_CURLY_BRACKET
)
1214 if (ptr
[1] == CHAR_CIRCUMFLEX_ACCENT
)
1219 for (i
= 0; i
< (int)(sizeof(name
) / sizeof(pcre_uchar
)) - 1; i
++)
1222 if (c
== 0) goto ERROR_RETURN
;
1223 if (c
== CHAR_RIGHT_CURLY_BRACKET
) break;
1226 if (c
!= CHAR_RIGHT_CURLY_BRACKET
) goto ERROR_RETURN
;
1230 /* Otherwise there is just one following character */
1240 /* Search for a recognized property name using binary chop */
1243 top
= PRIV(utt_size
);
1247 i
= (bot
+ top
) >> 1;
1248 c
= STRCMP_UC_C8(name
, PRIV(utt_names
) + PRIV(utt
)[i
].name_offset
);
1251 *dptr
= PRIV(utt
)[i
].value
;
1252 return PRIV(utt
)[i
].type
;
1254 if (c
> 0) bot
= i
+ 1; else top
= i
;
1257 *errorcodeptr
= ERR47
;
1262 *errorcodeptr
= ERR46
;
1271 /*************************************************
1272 * Read repeat counts *
1273 *************************************************/
1275 /* Read an item of the form {n,m} and return the values. This is called only
1276 after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
1277 so the syntax is guaranteed to be correct, but we need to check the values.
1280 p pointer to first char after '{'
1281 minp pointer to int for min
1282 maxp pointer to int for max
1283 returned as -1 if no max
1284 errorcodeptr points to error code variable
1286 Returns: pointer to '}' on success;
1287 current ptr on error, with errorcodeptr set non-zero
1290 static const pcre_uchar
*
1291 read_repeat_counts(const pcre_uchar
*p
, int *minp
, int *maxp
, int *errorcodeptr
)
1296 /* Read the minimum value and do a paranoid check: a negative value indicates
1297 an integer overflow. */
1299 while (IS_DIGIT(*p
)) min
= min
* 10 + *p
++ - CHAR_0
;
1300 if (min
< 0 || min
> 65535)
1302 *errorcodeptr
= ERR5
;
1306 /* Read the maximum value if there is one, and again do a paranoid on its size.
1307 Also, max must not be less than min. */
1309 if (*p
== CHAR_RIGHT_CURLY_BRACKET
) max
= min
; else
1311 if (*(++p
) != CHAR_RIGHT_CURLY_BRACKET
)
1314 while(IS_DIGIT(*p
)) max
= max
* 10 + *p
++ - CHAR_0
;
1315 if (max
< 0 || max
> 65535)
1317 *errorcodeptr
= ERR5
;
1322 *errorcodeptr
= ERR4
;
1328 /* Fill in the required variables, and pass back the pointer to the terminating
1338 /*************************************************
1339 * Subroutine for finding forward reference *
1340 *************************************************/
1342 /* This recursive function is called only from find_parens() below. The
1343 top-level call starts at the beginning of the pattern. All other calls must
1344 start at a parenthesis. It scans along a pattern's text looking for capturing
1345 subpatterns, and counting them. If it finds a named pattern that matches the
1346 name it is given, it returns its number. Alternatively, if the name is NULL, it
1347 returns when it reaches a given numbered subpattern. Recursion is used to keep
1348 track of subpatterns that reset the capturing group numbers - the (?| feature.
1350 This function was originally called only from the second pass, in which we know
1351 that if (?< or (?' or (?P< is encountered, the name will be correctly
1352 terminated because that is checked in the first pass. There is now one call to
1353 this function in the first pass, to check for a recursive back reference by
1354 name (so that we can make the whole group atomic). In this case, we need check
1355 only up to the current position in the pattern, and that is still OK because
1356 and previous occurrences will have been checked. To make this work, the test
1357 for "end of pattern" is a check against cd->end_pattern in the main loop,
1358 instead of looking for a binary zero. This means that the special first-pass
1359 call can adjust cd->end_pattern temporarily. (Checks for binary zero while
1360 processing items within the loop are OK, because afterwards the main loop will
1364 ptrptr address of the current character pointer (updated)
1365 cd compile background data
1366 name name to seek, or NULL if seeking a numbered subpattern
1367 lorn name length, or subpattern number if name is NULL
1368 xmode TRUE if we are in /x mode
1369 utf TRUE if we are in UTF-8 / UTF-16 mode
1370 count pointer to the current capturing subpattern number (updated)
1372 Returns: the number of the named subpattern, or -1 if not found
1376 find_parens_sub(pcre_uchar
**ptrptr
, compile_data
*cd
, const pcre_uchar
*name
, int lorn
,
1377 BOOL xmode
, BOOL utf
, int *count
)
1379 pcre_uchar
*ptr
= *ptrptr
;
1380 int start_count
= *count
;
1381 int hwm_count
= start_count
;
1382 BOOL dup_parens
= FALSE
;
1384 /* If the first character is a parenthesis, check on the type of group we are
1385 dealing with. The very first call may not start with a parenthesis. */
1387 if (ptr
[0] == CHAR_LEFT_PARENTHESIS
)
1389 /* Handle specials such as (*SKIP) or (*UTF8) etc. */
1391 if (ptr
[1] == CHAR_ASTERISK
) ptr
+= 2;
1393 /* Handle a normal, unnamed capturing parenthesis. */
1395 else if (ptr
[1] != CHAR_QUESTION_MARK
)
1398 if (name
== NULL
&& *count
== lorn
) return *count
;
1402 /* All cases now have (? at the start. Remember when we are in a group
1403 where the parenthesis numbers are duplicated. */
1405 else if (ptr
[2] == CHAR_VERTICAL_LINE
)
1411 /* Handle comments; all characters are allowed until a ket is reached. */
1413 else if (ptr
[2] == CHAR_NUMBER_SIGN
)
1415 for (ptr
+= 3; *ptr
!= 0; ptr
++) if (*ptr
== CHAR_RIGHT_PARENTHESIS
) break;
1419 /* Handle a condition. If it is an assertion, just carry on so that it
1420 is processed as normal. If not, skip to the closing parenthesis of the
1421 condition (there can't be any nested parens). */
1423 else if (ptr
[2] == CHAR_LEFT_PARENTHESIS
)
1426 if (ptr
[1] != CHAR_QUESTION_MARK
)
1428 while (*ptr
!= 0 && *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
1429 if (*ptr
!= 0) ptr
++;
1433 /* Start with (? but not a condition. */
1438 if (*ptr
== CHAR_P
) ptr
++; /* Allow optional P */
1440 /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */
1442 if ((*ptr
== CHAR_LESS_THAN_SIGN
&& ptr
[1] != CHAR_EXCLAMATION_MARK
&&
1443 ptr
[1] != CHAR_EQUALS_SIGN
) || *ptr
== CHAR_APOSTROPHE
)
1446 const pcre_uchar
*thisname
;
1448 if (name
== NULL
&& *count
== lorn
) return *count
;
1450 if (term
== CHAR_LESS_THAN_SIGN
) term
= CHAR_GREATER_THAN_SIGN
;
1452 while (*ptr
!= term
) ptr
++;
1453 if (name
!= NULL
&& lorn
== ptr
- thisname
&&
1454 STRNCMP_UC_UC(name
, thisname
, lorn
) == 0)
1461 /* Past any initial parenthesis handling, scan for parentheses or vertical
1462 bars. Stop if we get to cd->end_pattern. Note that this is important for the
1463 first-pass call when this value is temporarily adjusted to stop at the current
1464 position. So DO NOT change this to a test for binary zero. */
1466 for (; ptr
< cd
->end_pattern
; ptr
++)
1468 /* Skip over backslashed characters and also entire \Q...\E */
1470 if (*ptr
== CHAR_BACKSLASH
)
1472 if (*(++ptr
) == 0) goto FAIL_EXIT
;
1473 if (*ptr
== CHAR_Q
) for (;;)
1475 while (*(++ptr
) != 0 && *ptr
!= CHAR_BACKSLASH
) {};
1476 if (*ptr
== 0) goto FAIL_EXIT
;
1477 if (*(++ptr
) == CHAR_E
) break;
1482 /* Skip over character classes; this logic must be similar to the way they
1483 are handled for real. If the first character is '^', skip it. Also, if the
1484 first few characters (either before or after ^) are \Q\E or \E we skip them
1485 too. This makes for compatibility with Perl. Note the use of STR macros to
1486 encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */
1488 if (*ptr
== CHAR_LEFT_SQUARE_BRACKET
)
1490 BOOL negate_class
= FALSE
;
1493 if (ptr
[1] == CHAR_BACKSLASH
)
1495 if (ptr
[2] == CHAR_E
)
1497 else if (STRNCMP_UC_C8(ptr
+ 2,
1498 STR_Q STR_BACKSLASH STR_E
, 3) == 0)
1503 else if (!negate_class
&& ptr
[1] == CHAR_CIRCUMFLEX_ACCENT
)
1505 negate_class
= TRUE
;
1511 /* If the next character is ']', it is a data character that must be
1512 skipped, except in JavaScript compatibility mode. */
1514 if (ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
&&
1515 (cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) == 0)
1518 while (*(++ptr
) != CHAR_RIGHT_SQUARE_BRACKET
)
1520 if (*ptr
== 0) return -1;
1521 if (*ptr
== CHAR_BACKSLASH
)
1523 if (*(++ptr
) == 0) goto FAIL_EXIT
;
1524 if (*ptr
== CHAR_Q
) for (;;)
1526 while (*(++ptr
) != 0 && *ptr
!= CHAR_BACKSLASH
) {};
1527 if (*ptr
== 0) goto FAIL_EXIT
;
1528 if (*(++ptr
) == CHAR_E
) break;
1536 /* Skip comments in /x mode */
1538 if (xmode
&& *ptr
== CHAR_NUMBER_SIGN
)
1543 if (IS_NEWLINE(ptr
)) { ptr
+= cd
->nllen
- 1; break; }
1546 if (utf
) FORWARDCHAR(ptr
);
1549 if (*ptr
== 0) goto FAIL_EXIT
;
1553 /* Check for the special metacharacters */
1555 if (*ptr
== CHAR_LEFT_PARENTHESIS
)
1557 int rc
= find_parens_sub(&ptr
, cd
, name
, lorn
, xmode
, utf
, count
);
1558 if (rc
> 0) return rc
;
1559 if (*ptr
== 0) goto FAIL_EXIT
;
1562 else if (*ptr
== CHAR_RIGHT_PARENTHESIS
)
1564 if (dup_parens
&& *count
< hwm_count
) *count
= hwm_count
;
1568 else if (*ptr
== CHAR_VERTICAL_LINE
&& dup_parens
)
1570 if (*count
> hwm_count
) hwm_count
= *count
;
1571 *count
= start_count
;
1583 /*************************************************
1584 * Find forward referenced subpattern *
1585 *************************************************/
1587 /* This function scans along a pattern's text looking for capturing
1588 subpatterns, and counting them. If it finds a named pattern that matches the
1589 name it is given, it returns its number. Alternatively, if the name is NULL, it
1590 returns when it reaches a given numbered subpattern. This is used for forward
1591 references to subpatterns. We used to be able to start this scan from the
1592 current compiling point, using the current count value from cd->bracount, and
1593 do it all in a single loop, but the addition of the possibility of duplicate
1594 subpattern numbers means that we have to scan from the very start, in order to
1595 take account of such duplicates, and to use a recursive function to keep track
1596 of the different types of group.
1599 cd compile background data
1600 name name to seek, or NULL if seeking a numbered subpattern
1601 lorn name length, or subpattern number if name is NULL
1602 xmode TRUE if we are in /x mode
1603 utf TRUE if we are in UTF-8 / UTF-16 mode
1605 Returns: the number of the found subpattern, or -1 if not found
1609 find_parens(compile_data
*cd
, const pcre_uchar
*name
, int lorn
, BOOL xmode
,
1612 pcre_uchar
*ptr
= (pcre_uchar
*)cd
->start_pattern
;
1616 /* If the pattern does not start with an opening parenthesis, the first call
1617 to find_parens_sub() will scan right to the end (if necessary). However, if it
1618 does start with a parenthesis, find_parens_sub() will return when it hits the
1619 matching closing parens. That is why we have to have a loop. */
1623 rc
= find_parens_sub(&ptr
, cd
, name
, lorn
, xmode
, utf
, &count
);
1624 if (rc
> 0 || *ptr
++ == 0) break;
1633 /*************************************************
1634 * Find first significant op code *
1635 *************************************************/
1637 /* This is called by several functions that scan a compiled expression looking
1638 for a fixed first character, or an anchoring op code etc. It skips over things
1639 that do not influence this. For some calls, it makes sense to skip negative
1640 forward and all backward assertions, and also the \b assertion; for others it
1644 code pointer to the start of the group
1645 skipassert TRUE if certain assertions are to be skipped
1647 Returns: pointer to the first significant opcode
1650 static const pcre_uchar
*
1651 first_significant_code(const pcre_uchar
*code
, BOOL skipassert
)
1659 case OP_ASSERTBACK_NOT
:
1660 if (!skipassert
) return code
;
1661 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
1662 code
+= PRIV(OP_lengths
)[*code
];
1665 case OP_WORD_BOUNDARY
:
1666 case OP_NOT_WORD_BOUNDARY
:
1667 if (!skipassert
) return code
;
1676 code
+= PRIV(OP_lengths
)[*code
];
1683 /* Control never reaches here */
1689 /*************************************************
1690 * Find the fixed length of a branch *
1691 *************************************************/
1693 /* Scan a branch and compute the fixed length of subject that will match it,
1694 if the length is fixed. This is needed for dealing with backward assertions.
1695 In UTF8 mode, the result is in characters rather than bytes. The branch is
1696 temporarily terminated with OP_END when this function is called.
1698 This function is called when a backward assertion is encountered, so that if it
1699 fails, the error message can point to the correct place in the pattern.
1700 However, we cannot do this when the assertion contains subroutine calls,
1701 because they can be forward references. We solve this by remembering this case
1702 and doing the check at the end; a flag specifies which mode we are running in.
1705 code points to the start of the pattern (the bracket)
1706 utf TRUE in UTF-8 / UTF-16 mode
1707 atend TRUE if called when the pattern is complete
1708 cd the "compile data" structure
1710 Returns: the fixed length,
1711 or -1 if there is no fixed length,
1712 or -2 if \C was encountered (in UTF-8 mode only)
1713 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1714 or -4 if an unknown opcode was encountered (internal error)
1718 find_fixedlength(pcre_uchar
*code
, BOOL utf
, BOOL atend
, compile_data
*cd
)
1722 int branchlength
= 0;
1723 pcre_uchar
*cc
= code
+ 1 + LINK_SIZE
;
1725 /* Scan along the opcodes for this branch. If we get to the end of the
1726 branch, check the length against that of the other branches. */
1731 pcre_uchar
*ce
, *cs
;
1736 /* We only need to continue for OP_CBRA (normal capturing bracket) and
1737 OP_BRA (normal non-capturing bracket) because the other variants of these
1738 opcodes are all concerned with unlimited repeated groups, which of course
1739 are not of fixed length. */
1746 d
= find_fixedlength(cc
+ ((op
== OP_CBRA
)? IMM2_SIZE
: 0), utf
, atend
, cd
);
1747 if (d
< 0) return d
;
1749 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
1750 cc
+= 1 + LINK_SIZE
;
1753 /* Reached end of a branch; if it's a ket it is the end of a nested call.
1754 If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1755 an ALT. If it is END it's the end of the outer call. All can be handled by
1756 the same code. Note that we must not include the OP_KETRxxx opcodes here,
1757 because they all imply an unlimited repeat. */
1763 case OP_ASSERT_ACCEPT
:
1764 if (length
< 0) length
= branchlength
;
1765 else if (length
!= branchlength
) return -1;
1766 if (*cc
!= OP_ALT
) return length
;
1767 cc
+= 1 + LINK_SIZE
;
1771 /* A true recursion implies not fixed length, but a subroutine call may
1772 be OK. If the subroutine is a forward reference, we can't deal with
1773 it until the end of the pattern, so return -3. */
1776 if (!atend
) return -3;
1777 cs
= ce
= (pcre_uchar
*)cd
->start_code
+ GET(cc
, 1); /* Start subpattern */
1778 do ce
+= GET(ce
, 1); while (*ce
== OP_ALT
); /* End subpattern */
1779 if (cc
> cs
&& cc
< ce
) return -1; /* Recursion */
1780 d
= find_fixedlength(cs
+ IMM2_SIZE
, utf
, atend
, cd
);
1781 if (d
< 0) return d
;
1783 cc
+= 1 + LINK_SIZE
;
1786 /* Skip over assertive subpatterns */
1791 case OP_ASSERTBACK_NOT
:
1792 do cc
+= GET(cc
, 1); while (*cc
== OP_ALT
);
1793 cc
+= PRIV(OP_lengths
)[*cc
];
1796 /* Skip over things that don't match chars */
1802 cc
+= cc
[1] + PRIV(OP_lengths
)[*cc
];
1819 case OP_NOT_WORD_BOUNDARY
:
1828 case OP_WORD_BOUNDARY
:
1829 cc
+= PRIV(OP_lengths
)[*cc
];
1832 /* Handle literal characters */
1841 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
1845 /* Handle exact repetitions. The count is already in characters, but we
1846 need to skip over a multibyte character in UTF8 mode. */
1852 branchlength
+= GET2(cc
,1);
1853 cc
+= 2 + IMM2_SIZE
;
1855 if (utf
&& HAS_EXTRALEN(cc
[-1])) cc
+= GET_EXTRALEN(cc
[-1]);
1860 branchlength
+= GET2(cc
,1);
1861 if (cc
[1 + IMM2_SIZE
] == OP_PROP
|| cc
[1 + IMM2_SIZE
] == OP_NOTPROP
) cc
+= 2;
1862 cc
+= 1 + IMM2_SIZE
+ 1;
1865 /* Handle single-char matchers */
1878 case OP_NOT_WHITESPACE
:
1880 case OP_NOT_WORDCHAR
:
1888 /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1889 otherwise \C is coded as OP_ALLANY. */
1894 /* Check a class for variable quantification */
1896 #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1898 cc
+= GET(cc
, 1) - PRIV(OP_lengths
)[OP_CLASS
];
1904 cc
+= PRIV(OP_lengths
)[OP_CLASS
];
1918 if (GET2(cc
,1) != GET2(cc
,1+IMM2_SIZE
)) return -1;
1919 branchlength
+= GET2(cc
,1);
1920 cc
+= 1 + 2 * IMM2_SIZE
;
1928 /* Anything else is variable length */
1949 case OP_NOTMINPLUSI
:
1950 case OP_NOTMINQUERY
:
1951 case OP_NOTMINQUERYI
:
1953 case OP_NOTMINSTARI
:
1955 case OP_NOTMINUPTOI
:
1959 case OP_NOTPOSPLUSI
:
1960 case OP_NOTPOSQUERY
:
1961 case OP_NOTPOSQUERYI
:
1963 case OP_NOTPOSSTARI
:
1965 case OP_NOTPOSUPTOI
:
1994 case OP_TYPEMINPLUS
:
1995 case OP_TYPEMINQUERY
:
1996 case OP_TYPEMINSTAR
:
1997 case OP_TYPEMINUPTO
:
1999 case OP_TYPEPOSPLUS
:
2000 case OP_TYPEPOSQUERY
:
2001 case OP_TYPEPOSSTAR
:
2002 case OP_TYPEPOSUPTO
:
2010 /* Catch unrecognized opcodes so that when new ones are added they
2011 are not forgotten, as has happened in the past. */
2017 /* Control never gets here */
2023 /*************************************************
2024 * Scan compiled regex for specific bracket *
2025 *************************************************/
2027 /* This little function scans through a compiled pattern until it finds a
2028 capturing bracket with the given number, or, if the number is negative, an
2029 instance of OP_REVERSE for a lookbehind. The function is global in the C sense
2030 so that it can be called from pcre_study() when finding the minimum matching
2034 code points to start of expression
2035 utf TRUE in UTF-8 / UTF-16 mode
2036 number the required bracket number or negative to find a lookbehind
2038 Returns: pointer to the opcode for the bracket, or NULL if not found
2042 PRIV(find_bracket
)(const pcre_uchar
*code
, BOOL utf
, int number
)
2048 if (c
== OP_END
) return NULL
;
2050 /* XCLASS is used for classes that cannot be represented just by a bit
2051 map. This includes negated single high-valued characters. The length in
2052 the table is zero; the actual length is stored in the compiled code. */
2054 if (c
== OP_XCLASS
) code
+= GET(code
, 1);
2056 /* Handle recursion */
2058 else if (c
== OP_REVERSE
)
2060 if (number
< 0) return (pcre_uchar
*)code
;
2061 code
+= PRIV(OP_lengths
)[c
];
2064 /* Handle capturing bracket */
2066 else if (c
== OP_CBRA
|| c
== OP_SCBRA
||
2067 c
== OP_CBRAPOS
|| c
== OP_SCBRAPOS
)
2069 int n
= GET2(code
, 1+LINK_SIZE
);
2070 if (n
== number
) return (pcre_uchar
*)code
;
2071 code
+= PRIV(OP_lengths
)[c
];
2074 /* Otherwise, we can get the item's length from the table, except that for
2075 repeated character types, we have to test for \p and \P, which have an extra
2076 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2077 must add in its length. */
2084 case OP_TYPEMINSTAR
:
2086 case OP_TYPEMINPLUS
:
2088 case OP_TYPEMINQUERY
:
2089 case OP_TYPEPOSSTAR
:
2090 case OP_TYPEPOSPLUS
:
2091 case OP_TYPEPOSQUERY
:
2092 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2096 case OP_TYPEMINUPTO
:
2098 case OP_TYPEPOSUPTO
:
2099 if (code
[1 + IMM2_SIZE
] == OP_PROP
2100 || code
[1 + IMM2_SIZE
] == OP_NOTPROP
) code
+= 2;
2114 /* Add in the fixed length from the table */
2116 code
+= PRIV(OP_lengths
)[c
];
2118 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2119 a multi-byte character. The length in the table is a minimum, so we have to
2120 arrange to skip the extra bytes. */
2153 if (HAS_EXTRALEN(code
[-1])) code
+= GET_EXTRALEN(code
[-1]);
2157 (void)(utf
); /* Keep compiler happy by referencing function argument */
2165 /*************************************************
2166 * Scan compiled regex for recursion reference *
2167 *************************************************/
2169 /* This little function scans through a compiled pattern until it finds an
2170 instance of OP_RECURSE.
2173 code points to start of expression
2174 utf TRUE in UTF-8 / UTF-16 mode
2176 Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
2179 static const pcre_uchar
*
2180 find_recurse(const pcre_uchar
*code
, BOOL utf
)
2185 if (c
== OP_END
) return NULL
;
2186 if (c
== OP_RECURSE
) return code
;
2188 /* XCLASS is used for classes that cannot be represented just by a bit
2189 map. This includes negated single high-valued characters. The length in
2190 the table is zero; the actual length is stored in the compiled code. */
2192 if (c
== OP_XCLASS
) code
+= GET(code
, 1);
2194 /* Otherwise, we can get the item's length from the table, except that for
2195 repeated character types, we have to test for \p and \P, which have an extra
2196 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2197 must add in its length. */
2204 case OP_TYPEMINSTAR
:
2206 case OP_TYPEMINPLUS
:
2208 case OP_TYPEMINQUERY
:
2209 case OP_TYPEPOSSTAR
:
2210 case OP_TYPEPOSPLUS
:
2211 case OP_TYPEPOSQUERY
:
2212 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2215 case OP_TYPEPOSUPTO
:
2217 case OP_TYPEMINUPTO
:
2219 if (code
[1 + IMM2_SIZE
] == OP_PROP
2220 || code
[1 + IMM2_SIZE
] == OP_NOTPROP
) code
+= 2;
2234 /* Add in the fixed length from the table */
2236 code
+= PRIV(OP_lengths
)[c
];
2238 /* In UTF-8 mode, opcodes that are followed by a character may be followed
2239 by a multi-byte character. The length in the table is a minimum, so we have
2240 to arrange to skip the extra bytes. */
2260 case OP_NOTMINUPTOI
:
2264 case OP_NOTPOSUPTOI
:
2272 case OP_NOTMINSTARI
:
2276 case OP_NOTPOSSTARI
:
2284 case OP_NOTMINPLUSI
:
2288 case OP_NOTPOSPLUSI
:
2295 case OP_NOTMINQUERY
:
2296 case OP_NOTMINQUERYI
:
2299 case OP_NOTPOSQUERY
:
2300 case OP_NOTPOSQUERYI
:
2301 if (HAS_EXTRALEN(code
[-1])) code
+= GET_EXTRALEN(code
[-1]);
2305 (void)(utf
); /* Keep compiler happy by referencing function argument */
2313 /*************************************************
2314 * Scan compiled branch for non-emptiness *
2315 *************************************************/
2317 /* This function scans through a branch of a compiled pattern to see whether it
2318 can match the empty string or not. It is called from could_be_empty()
2319 below and from compile_branch() when checking for an unlimited repeat of a
2320 group that can match nothing. Note that first_significant_code() skips over
2321 backward and negative forward assertions when its final argument is TRUE. If we
2322 hit an unclosed bracket, we return "empty" - this means we've struck an inner
2323 bracket whose current branch will already have been scanned.
2326 code points to start of search
2327 endcode points to where to stop
2328 utf TRUE if in UTF-8 / UTF-16 mode
2329 cd contains pointers to tables etc.
2331 Returns: TRUE if what is matched could be empty
2335 could_be_empty_branch(const pcre_uchar
*code
, const pcre_uchar
*endcode
,
2336 BOOL utf
, compile_data
*cd
)
2339 for (code
= first_significant_code(code
+ PRIV(OP_lengths
)[*code
], TRUE
);
2341 code
= first_significant_code(code
+ PRIV(OP_lengths
)[c
], TRUE
))
2343 const pcre_uchar
*ccode
;
2347 /* Skip over forward assertions; the other assertions are skipped by
2348 first_significant_code() with a TRUE final argument. */
2352 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2357 /* For a recursion/subroutine call, if its end has been reached, which
2358 implies a backward reference subroutine call, we can scan it. If it's a
2359 forward reference subroutine call, we can't. To detect forward reference
2360 we have to scan up the list that is kept in the workspace. This function is
2361 called only when doing the real compile, not during the pre-compile that
2362 measures the size of the compiled pattern. */
2364 if (c
== OP_RECURSE
)
2366 const pcre_uchar
*scode
;
2369 /* Test for forward reference */
2371 for (scode
= cd
->start_workspace
; scode
< cd
->hwm
; scode
+= LINK_SIZE
)
2372 if (GET(scode
, 0) == code
+ 1 - cd
->start_code
) return TRUE
;
2374 /* Not a forward reference, test for completed backward reference */
2376 empty_branch
= FALSE
;
2377 scode
= cd
->start_code
+ GET(code
, 1);
2378 if (GET(scode
, 1) == 0) return TRUE
; /* Unclosed */
2380 /* Completed backwards reference */
2384 if (could_be_empty_branch(scode
, endcode
, utf
, cd
))
2386 empty_branch
= TRUE
;
2389 scode
+= GET(scode
, 1);
2391 while (*scode
== OP_ALT
);
2393 if (!empty_branch
) return FALSE
; /* All branches are non-empty */
2397 /* Groups with zero repeats can of course be empty; skip them. */
2399 if (c
== OP_BRAZERO
|| c
== OP_BRAMINZERO
|| c
== OP_SKIPZERO
||
2402 code
+= PRIV(OP_lengths
)[c
];
2403 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2408 /* A nested group that is already marked as "could be empty" can just be
2411 if (c
== OP_SBRA
|| c
== OP_SBRAPOS
||
2412 c
== OP_SCBRA
|| c
== OP_SCBRAPOS
)
2414 do code
+= GET(code
, 1); while (*code
== OP_ALT
);
2419 /* For other groups, scan the branches. */
2421 if (c
== OP_BRA
|| c
== OP_BRAPOS
||
2422 c
== OP_CBRA
|| c
== OP_CBRAPOS
||
2423 c
== OP_ONCE
|| c
== OP_ONCE_NC
||
2427 if (GET(code
, 1) == 0) return TRUE
; /* Hit unclosed bracket */
2429 /* If a conditional group has only one branch, there is a second, implied,
2430 empty branch, so just skip over the conditional, because it could be empty.
2431 Otherwise, scan the individual branches of the group. */
2433 if (c
== OP_COND
&& code
[GET(code
, 1)] != OP_ALT
)
2434 code
+= GET(code
, 1);
2437 empty_branch
= FALSE
;
2440 if (!empty_branch
&& could_be_empty_branch(code
, endcode
, utf
, cd
))
2441 empty_branch
= TRUE
;
2442 code
+= GET(code
, 1);
2444 while (*code
== OP_ALT
);
2445 if (!empty_branch
) return FALSE
; /* All branches are non-empty */
2452 /* Handle the other opcodes */
2456 /* Check for quantifiers after a class. XCLASS is used for classes that
2457 cannot be represented just by a bit map. This includes negated single
2458 high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2459 actual length is stored in the compiled code, so we must update "code"
2462 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2464 ccode
= code
+= GET(code
, 1);
2465 goto CHECK_CLASS_REPEAT
;
2470 ccode
= code
+ PRIV(OP_lengths
)[OP_CLASS
];
2472 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2478 case OP_CRSTAR
: /* These could be empty; continue */
2484 default: /* Non-repeat => class must match */
2485 case OP_CRPLUS
: /* These repeats aren't empty */
2491 if (GET2(ccode
, 1) > 0) return FALSE
; /* Minimum > 0 */
2496 /* Opcodes that must match a character */
2503 case OP_NOT_WHITESPACE
:
2505 case OP_NOT_WORDCHAR
:
2523 case OP_TYPEMINPLUS
:
2524 case OP_TYPEPOSPLUS
:
2528 /* These are going to continue, as they may be empty, but we have to
2529 fudge the length for the \p and \P cases. */
2532 case OP_TYPEMINSTAR
:
2533 case OP_TYPEPOSSTAR
:
2535 case OP_TYPEMINQUERY
:
2536 case OP_TYPEPOSQUERY
:
2537 if (code
[1] == OP_PROP
|| code
[1] == OP_NOTPROP
) code
+= 2;
2540 /* Same for these */
2543 case OP_TYPEMINUPTO
:
2544 case OP_TYPEPOSUPTO
:
2545 if (code
[1 + IMM2_SIZE
] == OP_PROP
2546 || code
[1 + IMM2_SIZE
] == OP_NOTPROP
) code
+= 2;
2558 /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2559 MINUPTO, and POSUPTO may be followed by a multibyte character */
2574 if (utf
&& HAS_EXTRALEN(code
[1])) code
+= GET_EXTRALEN(code
[1]);
2583 if (utf
&& HAS_EXTRALEN(code
[1 + IMM2_SIZE
])) code
+= GET_EXTRALEN(code
[1 + IMM2_SIZE
]);
2587 /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2600 /* None of the remaining opcodes are required to match a character. */
2612 /*************************************************
2613 * Scan compiled regex for non-emptiness *
2614 *************************************************/
2616 /* This function is called to check for left recursive calls. We want to check
2617 the current branch of the current pattern to see if it could match the empty
2618 string. If it could, we must look outwards for branches at other levels,
2619 stopping when we pass beyond the bracket which is the subject of the recursion.
2620 This function is called only during the real compile, not during the
2624 code points to start of the recursion
2625 endcode points to where to stop (current RECURSE item)
2626 bcptr points to the chain of current (unclosed) branch starts
2627 utf TRUE if in UTF-8 / UTF-16 mode
2628 cd pointers to tables etc
2630 Returns: TRUE if what is matched could be empty
2634 could_be_empty(const pcre_uchar
*code
, const pcre_uchar
*endcode
,
2635 branch_chain
*bcptr
, BOOL utf
, compile_data
*cd
)
2637 while (bcptr
!= NULL
&& bcptr
->current_branch
>= code
)
2639 if (!could_be_empty_branch(bcptr
->current_branch
, endcode
, utf
, cd
))
2641 bcptr
= bcptr
->outer
;
2648 /*************************************************
2649 * Check for POSIX class syntax *
2650 *************************************************/
2652 /* This function is called when the sequence "[:" or "[." or "[=" is
2653 encountered in a character class. It checks whether this is followed by a
2654 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
2655 reach an unescaped ']' without the special preceding character, return FALSE.
2657 Originally, this function only recognized a sequence of letters between the
2658 terminators, but it seems that Perl recognizes any sequence of characters,
2659 though of course unknown POSIX names are subsequently rejected. Perl gives an
2660 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
2661 didn't consider this to be a POSIX class. Likewise for [:1234:].
2663 The problem in trying to be exactly like Perl is in the handling of escapes. We
2664 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
2665 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
2666 below handles the special case of \], but does not try to do any other escape
2667 processing. This makes it different from Perl for cases such as [:l\ower:]
2668 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
2669 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2672 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2673 It seems that the appearance of a nested POSIX class supersedes an apparent
2674 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2677 In Perl, unescaped square brackets may also appear as part of class names. For
2678 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2679 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2680 seem right at all. PCRE does not allow closing square brackets in POSIX class
2684 ptr pointer to the initial [
2685 endptr where to return the end pointer
2687 Returns: TRUE or FALSE
2691 check_posix_syntax(const pcre_uchar
*ptr
, const pcre_uchar
**endptr
)
2693 int terminator
; /* Don't combine these lines; the Solaris cc */
2694 terminator
= *(++ptr
); /* compiler warns about "non-constant" initializer. */
2695 for (++ptr
; *ptr
!= 0; ptr
++)
2697 if (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
)
2699 else if (*ptr
== CHAR_RIGHT_SQUARE_BRACKET
) return FALSE
;
2702 if (*ptr
== terminator
&& ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
)
2707 if (*ptr
== CHAR_LEFT_SQUARE_BRACKET
&&
2708 (ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
2709 ptr
[1] == CHAR_EQUALS_SIGN
) &&
2710 check_posix_syntax(ptr
, endptr
))
2720 /*************************************************
2721 * Check POSIX class name *
2722 *************************************************/
2724 /* This function is called to check the name given in a POSIX-style class entry
2728 ptr points to the first letter
2729 len the length of the name
2731 Returns: a value representing the name, or -1 if unknown
2735 check_posix_name(const pcre_uchar
*ptr
, int len
)
2737 const char *pn
= posix_names
;
2739 while (posix_name_lengths
[yield
] != 0)
2741 if (len
== posix_name_lengths
[yield
] &&
2742 STRNCMP_UC_C8(ptr
, pn
, len
) == 0) return yield
;
2743 pn
+= posix_name_lengths
[yield
] + 1;
2750 /*************************************************
2751 * Adjust OP_RECURSE items in repeated group *
2752 *************************************************/
2754 /* OP_RECURSE items contain an offset from the start of the regex to the group
2755 that is referenced. This means that groups can be replicated for fixed
2756 repetition simply by copying (because the recursion is allowed to refer to
2757 earlier groups that are outside the current group). However, when a group is
2758 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
2759 inserted before it, after it has been compiled. This means that any OP_RECURSE
2760 items within it that refer to the group itself or any contained groups have to
2761 have their offsets adjusted. That one of the jobs of this function. Before it
2762 is called, the partially compiled regex must be temporarily terminated with
2765 This function has been extended with the possibility of forward references for
2766 recursions and subroutine calls. It must also check the list of such references
2767 for the group we are dealing with. If it finds that one of the recursions in
2768 the current group is on this list, it adjusts the offset in the list, not the
2769 value in the reference (which is a group number).
2772 group points to the start of the group
2773 adjust the amount by which the group is to be moved
2774 utf TRUE in UTF-8 / UTF-16 mode
2775 cd contains pointers to tables etc.
2776 save_hwm the hwm forward reference pointer at the start of the group
2782 adjust_recurse(pcre_uchar
*group
, int adjust
, BOOL utf
, compile_data
*cd
,
2783 pcre_uchar
*save_hwm
)
2785 pcre_uchar
*ptr
= group
;
2787 while ((ptr
= (pcre_uchar
*)find_recurse(ptr
, utf
)) != NULL
)
2792 /* See if this recursion is on the forward reference list. If so, adjust the
2795 for (hc
= save_hwm
; hc
< cd
->hwm
; hc
+= LINK_SIZE
)
2797 offset
= GET(hc
, 0);
2798 if (cd
->start_code
+ offset
== ptr
+ 1)
2800 PUT(hc
, 0, offset
+ adjust
);
2805 /* Otherwise, adjust the recursion offset if it's after the start of this
2810 offset
= GET(ptr
, 1);
2811 if (cd
->start_code
+ offset
>= group
) PUT(ptr
, 1, offset
+ adjust
);
2814 ptr
+= 1 + LINK_SIZE
;
2820 /*************************************************
2821 * Insert an automatic callout point *
2822 *************************************************/
2824 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
2825 callout points before each pattern item.
2828 code current code pointer
2829 ptr current pattern pointer
2830 cd pointers to tables etc
2832 Returns: new code pointer
2836 auto_callout(pcre_uchar
*code
, const pcre_uchar
*ptr
, compile_data
*cd
)
2838 *code
++ = OP_CALLOUT
;
2840 PUT(code
, 0, (int)(ptr
- cd
->start_pattern
)); /* Pattern offset */
2841 PUT(code
, LINK_SIZE
, 0); /* Default length */
2842 return code
+ 2 * LINK_SIZE
;
2847 /*************************************************
2848 * Complete a callout item *
2849 *************************************************/
2851 /* A callout item contains the length of the next item in the pattern, which
2852 we can't fill in till after we have reached the relevant point. This is used
2853 for both automatic and manual callouts.
2856 previous_callout points to previous callout item
2857 ptr current pattern pointer
2858 cd pointers to tables etc
2864 complete_callout(pcre_uchar
*previous_callout
, const pcre_uchar
*ptr
, compile_data
*cd
)
2866 int length
= (int)(ptr
- cd
->start_pattern
- GET(previous_callout
, 2));
2867 PUT(previous_callout
, 2 + LINK_SIZE
, length
);
2873 /*************************************************
2874 * Get othercase range *
2875 *************************************************/
2877 /* This function is passed the start and end of a class range, in UTF-8 mode
2878 with UCP support. It searches up the characters, looking for internal ranges of
2879 characters in the "other" case. Each call returns the next one, updating the
2883 cptr points to starting character value; updated
2885 ocptr where to put start of othercase range
2886 odptr where to put end of othercase range
2888 Yield: TRUE when range returned; FALSE when no more
2892 get_othercase_range(unsigned int *cptr
, unsigned int d
, unsigned int *ocptr
,
2893 unsigned int *odptr
)
2895 unsigned int c
, othercase
, next
;
2897 for (c
= *cptr
; c
<= d
; c
++)
2898 { if ((othercase
= UCD_OTHERCASE(c
)) != c
) break; }
2900 if (c
> d
) return FALSE
;
2903 next
= othercase
+ 1;
2905 for (++c
; c
<= d
; c
++)
2907 if (UCD_OTHERCASE(c
) != next
) break;
2919 /*************************************************
2920 * Check a character and a property *
2921 *************************************************/
2923 /* This function is called by check_auto_possessive() when a property item
2924 is adjacent to a fixed character.
2928 ptype the property type
2929 pdata the data for the type
2930 negated TRUE if it's a negated property (\P or \p{^)
2932 Returns: TRUE if auto-possessifying is OK
2936 check_char_prop(int c
, int ptype
, int pdata
, BOOL negated
)
2938 const pcre_uint8 chartype
= UCD_CHARTYPE(c
);
2942 return (chartype
== ucp_Lu
||
2943 chartype
== ucp_Ll
||
2944 chartype
== ucp_Lt
) == negated
;
2947 return (pdata
== PRIV(ucp_gentype
)[chartype
]) == negated
;
2950 return (pdata
== chartype
) == negated
;
2953 return (pdata
== UCD_SCRIPT(c
)) == negated
;
2955 /* These are specials */
2958 return (PRIV(ucp_gentype
)[chartype
] == ucp_L
||
2959 PRIV(ucp_gentype
)[chartype
] == ucp_N
) == negated
;
2961 case PT_SPACE
: /* Perl space */
2962 return (PRIV(ucp_gentype
)[chartype
] == ucp_Z
||
2963 c
== CHAR_HT
|| c
== CHAR_NL
|| c
== CHAR_FF
|| c
== CHAR_CR
)
2966 case PT_PXSPACE
: /* POSIX space */
2967 return (PRIV(ucp_gentype
)[chartype
] == ucp_Z
||
2968 c
== CHAR_HT
|| c
== CHAR_NL
|| c
== CHAR_VT
||
2969 c
== CHAR_FF
|| c
== CHAR_CR
)
2973 return (PRIV(ucp_gentype
)[chartype
] == ucp_L
||
2974 PRIV(ucp_gentype
)[chartype
] == ucp_N
||
2975 c
== CHAR_UNDERSCORE
) == negated
;
2979 #endif /* SUPPORT_UCP */
2983 /*************************************************
2984 * Check if auto-possessifying is possible *
2985 *************************************************/
2987 /* This function is called for unlimited repeats of certain items, to see
2988 whether the next thing could possibly match the repeated item. If not, it makes
2989 sense to automatically possessify the repeated item.
2992 previous pointer to the repeated opcode
2993 utf TRUE in UTF-8 / UTF-16 mode
2994 ptr next character in pattern
2995 options options bits
2996 cd contains pointers to tables etc.
2998 Returns: TRUE if possessifying is wanted
3002 check_auto_possessive(const pcre_uchar
*previous
, BOOL utf
,
3003 const pcre_uchar
*ptr
, int options
, compile_data
*cd
)
3006 int op_code
= *previous
++;
3008 /* Skip whitespace and comments in extended mode */
3010 if ((options
& PCRE_EXTENDED
) != 0)
3014 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_space
) != 0) ptr
++;
3015 if (*ptr
== CHAR_NUMBER_SIGN
)
3020 if (IS_NEWLINE(ptr
)) { ptr
+= cd
->nllen
; break; }
3023 if (utf
) FORWARDCHAR(ptr
);
3031 /* If the next item is one that we can handle, get its value. A non-negative
3032 value is a character, a negative value is an escape value. */
3034 if (*ptr
== CHAR_BACKSLASH
)
3036 int temperrorcode
= 0;
3037 next
= check_escape(&ptr
, &temperrorcode
, cd
->bracount
, options
, FALSE
);
3038 if (temperrorcode
!= 0) return FALSE
;
3039 ptr
++; /* Point after the escape sequence */
3041 else if (!MAX_255(*ptr
) || (cd
->ctypes
[*ptr
] & ctype_meta
) == 0)
3044 if (utf
) { GETCHARINC(next
, ptr
); } else
3050 /* Skip whitespace and comments in extended mode */
3052 if ((options
& PCRE_EXTENDED
) != 0)
3056 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_space
) != 0) ptr
++;
3057 if (*ptr
== CHAR_NUMBER_SIGN
)
3062 if (IS_NEWLINE(ptr
)) { ptr
+= cd
->nllen
; break; }
3065 if (utf
) FORWARDCHAR(ptr
);
3073 /* If the next thing is itself optional, we have to give up. */
3075 if (*ptr
== CHAR_ASTERISK
|| *ptr
== CHAR_QUESTION_MARK
||
3076 STRNCMP_UC_C8(ptr
, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA
, 3) == 0)
3079 /* Now compare the next item with the previous opcode. First, handle cases when
3080 the next item is a character. */
3082 if (next
>= 0) switch(op_code
)
3086 GETCHARTEST(c
, previous
);
3092 /* For CHARI (caseless character) we must check the other case. If we have
3093 Unicode property support, we can use it to test the other case of
3094 high-valued characters. */
3098 GETCHARTEST(c
, previous
);
3102 if (c
== next
) return FALSE
;
3106 unsigned int othercase
;
3107 if (next
< 128) othercase
= cd
->fcc
[next
]; else
3109 othercase
= UCD_OTHERCASE((unsigned int)next
);
3111 othercase
= NOTACHAR
;
3113 return (unsigned int)c
!= othercase
;
3116 #endif /* SUPPORT_UTF */
3117 return (c
!= TABLE_GET((unsigned int)next
, cd
->fcc
, next
)); /* Non-UTF-8 mode */
3121 GETCHARTEST(c
, previous
);
3129 GETCHARTEST(c
, previous
);
3133 if (c
== next
) return TRUE
;
3137 unsigned int othercase
;
3138 if (next
< 128) othercase
= cd
->fcc
[next
]; else
3140 othercase
= UCD_OTHERCASE((unsigned int)next
);
3142 othercase
= NOTACHAR
;
3144 return (unsigned int)c
== othercase
;
3147 #endif /* SUPPORT_UTF */
3148 return (c
== TABLE_GET((unsigned int)next
, cd
->fcc
, next
)); /* Non-UTF-8 mode */
3150 /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3151 When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3154 return next
> 255 || (cd
->ctypes
[next
] & ctype_digit
) == 0;
3157 return next
<= 255 && (cd
->ctypes
[next
] & ctype_digit
) != 0;
3160 return next
> 255 || (cd
->ctypes
[next
] & ctype_space
) == 0;
3162 case OP_NOT_WHITESPACE
:
3163 return next
<= 255 && (cd
->ctypes
[next
] & ctype_space
) != 0;
3166 return next
> 255 || (cd
->ctypes
[next
] & ctype_word
) == 0;
3168 case OP_NOT_WORDCHAR
:
3169 return next
<= 255 && (cd
->ctypes
[next
] & ctype_word
) != 0;
3194 return op_code
== OP_NOT_HSPACE
;
3196 return op_code
!= OP_NOT_HSPACE
;
3211 return op_code
== OP_NOT_VSPACE
;
3213 return op_code
!= OP_NOT_VSPACE
;
3218 return check_char_prop(next
, previous
[0], previous
[1], FALSE
);
3221 return check_char_prop(next
, previous
[0], previous
[1], TRUE
);
3229 /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3230 is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3231 generated only when PCRE_UCP is *not* set, that is, when only ASCII
3232 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3233 replaced by OP_PROP codes when PCRE_UCP is set. */
3240 GETCHARTEST(c
, previous
);
3247 return c
> 255 || (cd
->ctypes
[c
] & ctype_digit
) == 0;
3250 return c
<= 255 && (cd
->ctypes
[c
] & ctype_digit
) != 0;
3253 return c
> 255 || (cd
->ctypes
[c
] & ctype_space
) == 0;
3256 return c
<= 255 && (cd
->ctypes
[c
] & ctype_space
) != 0;
3259 return c
> 255 || (cd
->ctypes
[c
] & ctype_word
) == 0;
3262 return c
<= 255 && (cd
->ctypes
[c
] & ctype_word
) != 0;
3287 return -next
!= ESC_h
;
3289 return -next
== ESC_h
;
3303 return -next
!= ESC_v
;
3305 return -next
== ESC_v
;
3308 /* When PCRE_UCP is set, these values get generated for \d etc. Find
3309 their substitutions and process them. The result will always be either
3310 -ESC_p or -ESC_P. Then fall through to process those values. */
3320 int temperrorcode
= 0;
3321 ptr
= substitutes
[-next
- ESC_DU
];
3322 next
= check_escape(&ptr
, &temperrorcode
, 0, options
, FALSE
);
3323 if (temperrorcode
!= 0) return FALSE
;
3324 ptr
++; /* For compatibility */
3331 int ptype
, pdata
, errorcodeptr
;
3334 ptr
--; /* Make ptr point at the p or P */
3335 ptype
= get_ucp(&ptr
, &negated
, &pdata
, &errorcodeptr
);
3336 if (ptype
< 0) return FALSE
;
3337 ptr
++; /* Point past the final curly ket */
3339 /* If the property item is optional, we have to give up. (When generated
3340 from \d etc by PCRE_UCP, this test will have been applied much earlier,
3341 to the original \d etc. At this point, ptr will point to a zero byte. */
3343 if (*ptr
== CHAR_ASTERISK
|| *ptr
== CHAR_QUESTION_MARK
||
3344 STRNCMP_UC_C8(ptr
, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA
, 3) == 0)
3347 /* Do the property check. */
3349 return check_char_prop(c
, ptype
, pdata
, (next
== -ESC_P
) != negated
);
3357 /* In principle, support for Unicode properties should be integrated here as
3358 well. It means re-organizing the above code so as to get hold of the property
3359 values before switching on the op-code. However, I wonder how many patterns
3360 combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3361 these op-codes are never generated.) */
3364 return next
== -ESC_D
|| next
== -ESC_s
|| next
== -ESC_W
||
3365 next
== -ESC_h
|| next
== -ESC_v
|| next
== -ESC_R
;
3368 return next
== -ESC_d
;
3371 return next
== -ESC_S
|| next
== -ESC_d
|| next
== -ESC_w
;
3373 case OP_NOT_WHITESPACE
:
3374 return next
== -ESC_s
|| next
== -ESC_h
|| next
== -ESC_v
|| next
== -ESC_R
;
3377 return next
== -ESC_S
|| next
== -ESC_H
|| next
== -ESC_d
||
3378 next
== -ESC_w
|| next
== -ESC_v
|| next
== -ESC_R
;
3381 return next
== -ESC_h
;
3383 /* Can't have \S in here because VT matches \S (Perl anomaly) */
3386 return next
== -ESC_V
|| next
== -ESC_d
|| next
== -ESC_w
;
3389 return next
== -ESC_v
|| next
== -ESC_R
;
3392 return next
== -ESC_W
|| next
== -ESC_s
|| next
== -ESC_h
||
3393 next
== -ESC_v
|| next
== -ESC_R
;
3395 case OP_NOT_WORDCHAR
:
3396 return next
== -ESC_w
|| next
== -ESC_d
;
3402 /* Control does not reach here */
3407 /*************************************************
3408 * Compile one branch *
3409 *************************************************/
3411 /* Scan the pattern, compiling it into the a vector. If the options are
3412 changed during the branch, the pointer is used to change the external options
3413 bits. This function is used during the pre-compile phase when we are trying
3414 to find out the amount of memory needed, as well as during the real compile
3415 phase. The value of lengthptr distinguishes the two phases.
3418 optionsptr pointer to the option bits
3419 codeptr points to the pointer to the current code point
3420 ptrptr points to the current pattern pointer
3421 errorcodeptr points to error code variable
3422 firstcharptr set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3423 reqcharptr set to the last literal character required, else < 0
3424 bcptr points to current branch chain
3425 cond_depth conditional nesting depth
3426 cd contains pointers to tables etc.
3427 lengthptr NULL during the real compile phase
3428 points to length accumulator during pre-compile phase
3430 Returns: TRUE on success
3431 FALSE, with *errorcodeptr set non-zero on error
3435 compile_branch(int *optionsptr
, pcre_uchar
**codeptr
,
3436 const pcre_uchar
**ptrptr
, int *errorcodeptr
, pcre_int32
*firstcharptr
,
3437 pcre_int32
*reqcharptr
, branch_chain
*bcptr
, int cond_depth
,
3438 compile_data
*cd
, int *lengthptr
)
3440 int repeat_type
, op_type
;
3441 int repeat_min
= 0, repeat_max
= 0; /* To please picky compilers */
3443 int greedy_default
, greedy_non_default
;
3444 pcre_int32 firstchar
, reqchar
;
3445 pcre_int32 zeroreqchar
, zerofirstchar
;
3446 pcre_int32 req_caseopt
, reqvary
, tempreqvary
;
3447 int options
= *optionsptr
; /* May change dynamically */
3448 int after_manual_callout
= 0;
3449 int length_prevgroup
= 0;
3451 pcre_uchar
*code
= *codeptr
;
3452 pcre_uchar
*last_code
= code
;
3453 pcre_uchar
*orig_code
= code
;
3454 pcre_uchar
*tempcode
;
3455 BOOL inescq
= FALSE
;
3456 BOOL groupsetfirstchar
= FALSE
;
3457 const pcre_uchar
*ptr
= *ptrptr
;
3458 const pcre_uchar
*tempptr
;
3459 const pcre_uchar
*nestptr
= NULL
;
3460 pcre_uchar
*previous
= NULL
;
3461 pcre_uchar
*previous_callout
= NULL
;
3462 pcre_uchar
*save_hwm
= NULL
;
3463 pcre_uint8 classbits
[32];
3465 /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3466 must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3467 dynamically as we process the pattern. */
3470 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3471 BOOL utf
= (options
& PCRE_UTF8
) != 0;
3472 pcre_uchar utf_chars
[6];
3477 /* Helper variables for OP_XCLASS opcode (for characters > 255). */
3479 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3481 pcre_uchar
*class_uchardata
;
3482 pcre_uchar
*class_uchardata_base
;
3486 if (lengthptr
!= NULL
) DPRINTF((">> start branch\n"));
3489 /* Set up the default and non-default settings for greediness */
3491 greedy_default
= ((options
& PCRE_UNGREEDY
) != 0);
3492 greedy_non_default
= greedy_default
^ 1;
3494 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3495 matching encountered yet". It gets changed to REQ_NONE if we hit something that
3496 matches a non-fixed char first char; reqchar just remains unset if we never
3499 When we hit a repeat whose minimum is zero, we may have to adjust these values
3500 to take the zero repeat into account. This is implemented by setting them to
3501 zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3502 item types that can be repeated set these backoff variables appropriately. */
3504 firstchar
= reqchar
= zerofirstchar
= zeroreqchar
= REQ_UNSET
;
3506 /* The variable req_caseopt contains either the REQ_CASELESS value
3507 or zero, according to the current setting of the caseless flag. The
3508 REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3509 firstchar or reqchar variables to record the case status of the
3510 value. This is used only for ASCII characters. */
3512 req_caseopt
= ((options
& PCRE_CASELESS
) != 0)? REQ_CASELESS
:0;
3514 /* Switch on next character until the end of the branch */
3519 BOOL should_flip_negation
;
3520 BOOL possessive_quantifier
;
3523 BOOL reset_bracount
;
3524 int class_has_8bitchar
;
3525 int class_single_char
;
3535 pcre_uchar mcbuffer
[8];
3537 /* Get next character in the pattern */
3541 /* If we are at the end of a nested substitution, revert to the outer level
3542 string. Nesting only happens one level deep. */
3544 if (c
== 0 && nestptr
!= NULL
)
3551 /* If we are in the pre-compile phase, accumulate the length used for the
3552 previous cycle of this loop. */
3554 if (lengthptr
!= NULL
)
3557 if (code
> cd
->hwm
) cd
->hwm
= code
; /* High water info */
3559 if (code
> cd
->start_workspace
+ cd
->workspace_size
-
3560 WORK_SIZE_SAFETY_MARGIN
) /* Check for overrun */
3562 *errorcodeptr
= ERR52
;
3566 /* There is at least one situation where code goes backwards: this is the
3567 case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
3568 the class is simply eliminated. However, it is created first, so we have to
3569 allow memory for it. Therefore, don't ever reduce the length at this point.
3572 if (code
< last_code
) code
= last_code
;
3574 /* Paranoid check for integer overflow */
3576 if (OFLOW_MAX
- *lengthptr
< code
- last_code
)
3578 *errorcodeptr
= ERR20
;
3582 *lengthptr
+= (int)(code
- last_code
);
3583 DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr
,
3584 (int)(code
- last_code
), c
, c
));
3586 /* If "previous" is set and it is not at the start of the work space, move
3587 it back to there, in order to avoid filling up the work space. Otherwise,
3588 if "previous" is NULL, reset the current code pointer to the start. */
3590 if (previous
!= NULL
)
3592 if (previous
> orig_code
)
3594 memmove(orig_code
, previous
, IN_UCHARS(code
- previous
));
3595 code
-= previous
- orig_code
;
3596 previous
= orig_code
;
3599 else code
= orig_code
;
3601 /* Remember where this code item starts so we can pick up the length
3607 /* In the real compile phase, just check the workspace used by the forward
3610 else if (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
-
3611 WORK_SIZE_SAFETY_MARGIN
)
3613 *errorcodeptr
= ERR52
;
3617 /* If in \Q...\E, check for the end; if not, we have a literal */
3619 if (inescq
&& c
!= 0)
3621 if (c
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
)
3629 if (previous_callout
!= NULL
)
3631 if (lengthptr
== NULL
) /* Don't attempt in pre-compile phase */
3632 complete_callout(previous_callout
, ptr
, cd
);
3633 previous_callout
= NULL
;
3635 if ((options
& PCRE_AUTO_CALLOUT
) != 0)
3637 previous_callout
= code
;
3638 code
= auto_callout(code
, ptr
, cd
);
3644 /* Fill in length of a previous callout, except when the next thing is
3648 c
== CHAR_ASTERISK
|| c
== CHAR_PLUS
|| c
== CHAR_QUESTION_MARK
||
3649 (c
== CHAR_LEFT_CURLY_BRACKET
&& is_counted_repeat(ptr
+1));
3651 if (!is_quantifier
&& previous_callout
!= NULL
&&
3652 after_manual_callout
-- <= 0)
3654 if (lengthptr
== NULL
) /* Don't attempt in pre-compile phase */
3655 complete_callout(previous_callout
, ptr
, cd
);
3656 previous_callout
= NULL
;
3659 /* In extended mode, skip white space and comments. */
3661 if ((options
& PCRE_EXTENDED
) != 0)
3663 if (MAX_255(*ptr
) && (cd
->ctypes
[c
] & ctype_space
) != 0) continue;
3664 if (c
== CHAR_NUMBER_SIGN
)
3669 if (IS_NEWLINE(ptr
)) { ptr
+= cd
->nllen
- 1; break; }
3672 if (utf
) FORWARDCHAR(ptr
);
3675 if (*ptr
!= 0) continue;
3677 /* Else fall through to handle end of string */
3682 /* No auto callout for quantifiers. */
3684 if ((options
& PCRE_AUTO_CALLOUT
) != 0 && !is_quantifier
)
3686 previous_callout
= code
;
3687 code
= auto_callout(code
, ptr
, cd
);
3692 /* ===================================================================*/
3693 case 0: /* The branch terminates at string end */
3694 case CHAR_VERTICAL_LINE
: /* or | or ) */
3695 case CHAR_RIGHT_PARENTHESIS
:
3696 *firstcharptr
= firstchar
;
3697 *reqcharptr
= reqchar
;
3700 if (lengthptr
!= NULL
)
3702 if (OFLOW_MAX
- *lengthptr
< code
- last_code
)
3704 *errorcodeptr
= ERR20
;
3707 *lengthptr
+= (int)(code
- last_code
); /* To include callout length */
3708 DPRINTF((">> end branch\n"));
3713 /* ===================================================================*/
3714 /* Handle single-character metacharacters. In multiline mode, ^ disables
3715 the setting of any following char as a first character. */
3717 case CHAR_CIRCUMFLEX_ACCENT
:
3719 if ((options
& PCRE_MULTILINE
) != 0)
3721 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
3724 else *code
++ = OP_CIRC
;
3727 case CHAR_DOLLAR_SIGN
:
3729 *code
++ = ((options
& PCRE_MULTILINE
) != 0)? OP_DOLLM
: OP_DOLL
;
3732 /* There can never be a first char if '.' is first, whatever happens about
3733 repeats. The value of reqchar doesn't change either. */
3736 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
3737 zerofirstchar
= firstchar
;
3738 zeroreqchar
= reqchar
;
3740 *code
++ = ((options
& PCRE_DOTALL
) != 0)? OP_ALLANY
: OP_ANY
;
3744 /* ===================================================================*/
3745 /* Character classes. If the included characters are all < 256, we build a
3746 32-byte bitmap of the permitted characters, except in the special case
3747 where there is only one such character. For negated classes, we build the
3748 map as usual, then invert it at the end. However, we use a different opcode
3749 so that data characters > 255 can be handled correctly.
3751 If the class contains characters outside the 0-255 range, a different
3752 opcode is compiled. It may optionally have a bit map for characters < 256,
3753 but those above are are explicitly listed afterwards. A flag byte tells
3754 whether the bitmap is present, and whether this is a negated class or not.
3756 In JavaScript compatibility mode, an isolated ']' causes an error. In
3757 default (Perl) mode, it is treated as a data character. */
3759 case CHAR_RIGHT_SQUARE_BRACKET
:
3760 if ((cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
3762 *errorcodeptr
= ERR64
;
3767 case CHAR_LEFT_SQUARE_BRACKET
:
3770 /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
3771 they are encountered at the top level, so we'll do that too. */
3773 if ((ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
3774 ptr
[1] == CHAR_EQUALS_SIGN
) &&
3775 check_posix_syntax(ptr
, &tempptr
))
3777 *errorcodeptr
= (ptr
[1] == CHAR_COLON
)? ERR13
: ERR31
;
3781 /* If the first character is '^', set the negation flag and skip it. Also,
3782 if the first few characters (either before or after ^) are \Q\E or \E we
3783 skip them too. This makes for compatibility with Perl. */
3785 negate_class
= FALSE
;
3789 if (c
== CHAR_BACKSLASH
)
3791 if (ptr
[1] == CHAR_E
)
3793 else if (STRNCMP_UC_C8(ptr
+ 1, STR_Q STR_BACKSLASH STR_E
, 3) == 0)
3798 else if (!negate_class
&& c
== CHAR_CIRCUMFLEX_ACCENT
)
3799 negate_class
= TRUE
;
3803 /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
3804 an initial ']' is taken as a data character -- the code below handles
3805 that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
3806 [^] must match any character, so generate OP_ALLANY. */
3808 if (c
== CHAR_RIGHT_SQUARE_BRACKET
&&
3809 (cd
->external_options
& PCRE_JAVASCRIPT_COMPAT
) != 0)
3811 *code
++ = negate_class
? OP_ALLANY
: OP_FAIL
;
3812 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
3813 zerofirstchar
= firstchar
;
3817 /* If a class contains a negative special such as \S, we need to flip the
3818 negation flag at the end, so that support for characters > 255 works
3819 correctly (they are all included in the class). */
3821 should_flip_negation
= FALSE
;
3823 /* For optimization purposes, we track some properties of the class.
3824 class_has_8bitchar will be non-zero, if the class contains at least one
3825 < 256 character. class_single_char will be 1 if the class contains only
3826 a single character. */
3828 class_has_8bitchar
= 0;
3829 class_single_char
= 0;
3831 /* Initialize the 32-char bit map to all zeros. We build the map in a
3832 temporary bit of memory, in case the class contains only 1 character (less
3833 than 256), because in that case the compiled code doesn't use the bit map.
3836 memset(classbits
, 0, 32 * sizeof(pcre_uint8
));
3838 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3839 xclass
= FALSE
; /* No chars >= 256 */
3840 class_uchardata
= code
+ LINK_SIZE
+ 2; /* For UTF-8 items */
3841 class_uchardata_base
= class_uchardata
; /* For resetting in pass 1 */
3844 /* Process characters until ] is reached. By writing this as a "do" it
3845 means that an initial ] is taken as a data character. At the start of the
3846 loop, c contains the first byte of the character. */
3850 const pcre_uchar
*oldptr
;
3853 if (utf
&& HAS_EXTRALEN(c
))
3854 { /* Braces are required because the */
3855 GETCHARLEN(c
, ptr
, ptr
); /* macro generates multiple statements */
3859 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3860 /* In the pre-compile phase, accumulate the length of any extra
3861 data and reset the pointer. This is so that very large classes that
3862 contain a zillion > 255 characters no longer overwrite the work space
3863 (which is on the stack). */
3865 if (lengthptr
!= NULL
)
3867 *lengthptr
+= class_uchardata
- class_uchardata_base
;
3868 class_uchardata
= class_uchardata_base
;
3872 /* Inside \Q...\E everything is literal except \E */
3876 if (c
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
) /* If we are at \E */
3878 inescq
= FALSE
; /* Reset literal state */
3879 ptr
++; /* Skip the 'E' */
3880 continue; /* Carry on with next */
3882 goto CHECK_RANGE
; /* Could be range if \E follows */
3885 /* Handle POSIX class names. Perl allows a negation extension of the
3886 form [:^name:]. A square bracket that doesn't match the syntax is
3887 treated as a literal. We also recognize the POSIX constructions
3888 [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
3891 if (c
== CHAR_LEFT_SQUARE_BRACKET
&&
3892 (ptr
[1] == CHAR_COLON
|| ptr
[1] == CHAR_DOT
||
3893 ptr
[1] == CHAR_EQUALS_SIGN
) && check_posix_syntax(ptr
, &tempptr
))
3895 BOOL local_negate
= FALSE
;
3896 int posix_class
, taboffset
, tabopt
;
3897 const pcre_uint8
*cbits
= cd
->cbits
;
3898 pcre_uint8 pbits
[32];
3900 if (ptr
[1] != CHAR_COLON
)
3902 *errorcodeptr
= ERR31
;
3907 if (*ptr
== CHAR_CIRCUMFLEX_ACCENT
)
3909 local_negate
= TRUE
;
3910 should_flip_negation
= TRUE
; /* Note negative special */
3914 posix_class
= check_posix_name(ptr
, (int)(tempptr
- ptr
));
3915 if (posix_class
< 0)
3917 *errorcodeptr
= ERR30
;
3921 /* If matching is caseless, upper and lower are converted to
3922 alpha. This relies on the fact that the class table starts with
3923 alpha, lower, upper as the first 3 entries. */
3925 if ((options
& PCRE_CASELESS
) != 0 && posix_class
<= 2)
3928 /* When PCRE_UCP is set, some of the POSIX classes are converted to
3929 different escape sequences that use Unicode properties. */
3932 if ((options
& PCRE_UCP
) != 0)
3934 int pc
= posix_class
+ ((local_negate
)? POSIX_SUBSIZE
/2 : 0);
3935 if (posix_substitutes
[pc
] != NULL
)
3937 nestptr
= tempptr
+ 1;
3938 ptr
= posix_substitutes
[pc
] - 1;
3943 /* In the non-UCP case, we build the bit map for the POSIX class in a
3944 chunk of local store because we may be adding and subtracting from it,
3945 and we don't want to subtract bits that may be in the main map already.
3946 At the end we or the result into the bit map that is being built. */
3950 /* Copy in the first table (always present) */
3952 memcpy(pbits
, cbits
+ posix_class_maps
[posix_class
],
3953 32 * sizeof(pcre_uint8
));
3955 /* If there is a second table, add or remove it as required. */
3957 taboffset
= posix_class_maps
[posix_class
+ 1];
3958 tabopt
= posix_class_maps
[posix_class
+ 2];
3963 for (c
= 0; c
< 32; c
++) pbits
[c
] |= cbits
[c
+ taboffset
];
3965 for (c
= 0; c
< 32; c
++) pbits
[c
] &= ~cbits
[c
+ taboffset
];
3968 /* Not see if we need to remove any special characters. An option
3969 value of 1 removes vertical space and 2 removes underscore. */
3971 if (tabopt
< 0) tabopt
= -tabopt
;
3972 if (tabopt
== 1) pbits
[1] &= ~0x3c;
3973 else if (tabopt
== 2) pbits
[11] &= 0x7f;
3975 /* Add the POSIX table or its complement into the main table that is
3976 being built and we are done. */
3979 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~pbits
[c
];
3981 for (c
= 0; c
< 32; c
++) classbits
[c
] |= pbits
[c
];
3984 /* Every class contains at least one < 256 characters. */
3985 class_has_8bitchar
= 1;
3986 /* Every class contains at least two characters. */
3987 class_single_char
= 2;
3988 continue; /* End of POSIX syntax handling */
3991 /* Backslash may introduce a single character, or it may introduce one
3992 of the specials, which just set a flag. The sequence \b is a special
3993 case. Inside a class (and only there) it is treated as backspace. We
3994 assume that other escapes have more than one character in them, so
3995 speculatively set both class_has_8bitchar and class_single_char bigger
3996 than one. Unrecognized escapes fall through and are either treated
3997 as literal characters (by default), or are faulted if
3998 PCRE_EXTRA is set. */
4000 if (c
== CHAR_BACKSLASH
)
4002 c
= check_escape(&ptr
, errorcodeptr
, cd
->bracount
, options
, TRUE
);
4003 if (*errorcodeptr
!= 0) goto FAILED
;
4005 if (-c
== ESC_b
) c
= CHAR_BS
; /* \b is backspace in a class */
4006 else if (-c
== ESC_N
) /* \N is not supported in a class */
4008 *errorcodeptr
= ERR71
;
4011 else if (-c
== ESC_Q
) /* Handle start of quoted string */
4013 if (ptr
[1] == CHAR_BACKSLASH
&& ptr
[2] == CHAR_E
)
4015 ptr
+= 2; /* avoid empty string */
4020 else if (-c
== ESC_E
) continue; /* Ignore orphan \E */
4024 const pcre_uint8
*cbits
= cd
->cbits
;
4025 /* Every class contains at least two < 256 characters. */
4026 class_has_8bitchar
++;
4027 /* Every class contains at least two characters. */
4028 class_single_char
+= 2;
4033 case ESC_du
: /* These are the values given for \d etc */
4034 case ESC_DU
: /* when PCRE_UCP is set. We replace the */
4035 case ESC_wu
: /* escape sequence with an appropriate \p */
4036 case ESC_WU
: /* or \P to test Unicode properties instead */
4037 case ESC_su
: /* of the default ASCII testing. */
4040 ptr
= substitutes
[-c
- ESC_DU
] - 1; /* Just before substitute */
4041 class_has_8bitchar
--; /* Undo! */
4045 for (c
= 0; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_digit
];
4049 should_flip_negation
= TRUE
;
4050 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_digit
];
4054 for (c
= 0; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_word
];
4058 should_flip_negation
= TRUE
;
4059 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_word
];
4062 /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4063 if it was previously set by something earlier in the character
4067 classbits
[0] |= cbits
[cbit_space
];
4068 classbits
[1] |= cbits
[cbit_space
+1] & ~0x08;
4069 for (c
= 2; c
< 32; c
++) classbits
[c
] |= cbits
[c
+cbit_space
];
4073 should_flip_negation
= TRUE
;
4074 for (c
= 0; c
< 32; c
++) classbits
[c
] |= ~cbits
[c
+cbit_space
];
4075 classbits
[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */
4079 SETBIT(classbits
, 0x09); /* VT */
4080 SETBIT(classbits
, 0x20); /* SPACE */
4081 SETBIT(classbits
, 0xa0); /* NSBP */
4082 #ifndef COMPILE_PCRE8
4084 *class_uchardata
++ = XCL_SINGLE
;
4085 *class_uchardata
++ = 0x1680;
4086 *class_uchardata
++ = XCL_SINGLE
;
4087 *class_uchardata
++ = 0x180e;
4088 *class_uchardata
++ = XCL_RANGE
;
4089 *class_uchardata
++ = 0x2000;
4090 *class_uchardata
++ = 0x200a;
4091 *class_uchardata
++ = XCL_SINGLE
;
4092 *class_uchardata
++ = 0x202f;
4093 *class_uchardata
++ = XCL_SINGLE
;
4094 *class_uchardata
++ = 0x205f;
4095 *class_uchardata
++ = XCL_SINGLE
;
4096 *class_uchardata
++ = 0x3000;
4097 #elif defined SUPPORT_UTF
4101 *class_uchardata
++ = XCL_SINGLE
;
4102 class_uchardata
+= PRIV(ord2utf
)(0x1680, class_uchardata
);
4103 *class_uchardata
++ = XCL_SINGLE
;
4104 class_uchardata
+= PRIV(ord2utf
)(0x180e, class_uchardata
);
4105 *class_uchardata
++ = XCL_RANGE
;
4106 class_uchardata
+= PRIV(ord2utf
)(0x2000, class_uchardata
);
4107 class_uchardata
+= PRIV(ord2utf
)(0x200a, class_uchardata
);
4108 *class_uchardata
++ = XCL_SINGLE
;
4109 class_uchardata
+= PRIV(ord2utf
)(0x202f, class_uchardata
);
4110 *class_uchardata
++ = XCL_SINGLE
;
4111 class_uchardata
+= PRIV(ord2utf
)(0x205f, class_uchardata
);
4112 *class_uchardata
++ = XCL_SINGLE
;
4113 class_uchardata
+= PRIV(ord2utf
)(0x3000, class_uchardata
);
4119 for (c
= 0; c
< 32; c
++)
4124 case 0x09/8: x
^= 1 << (0x09%8); break;
4125 case 0x20/8: x
^= 1 << (0x20%8); break;
4126 case 0xa0/8: x
^= 1 << (0xa0%8); break;
4131 #ifndef COMPILE_PCRE8
4133 *class_uchardata
++ = XCL_RANGE
;
4134 *class_uchardata
++ = 0x0100;
4135 *class_uchardata
++ = 0x167f;
4136 *class_uchardata
++ = XCL_RANGE
;
4137 *class_uchardata
++ = 0x1681;
4138 *class_uchardata
++ = 0x180d;
4139 *class_uchardata
++ = XCL_RANGE
;
4140 *class_uchardata
++ = 0x180f;
4141 *class_uchardata
++ = 0x1fff;
4142 *class_uchardata
++ = XCL_RANGE
;
4143 *class_uchardata
++ = 0x200b;
4144 *class_uchardata
++ = 0x202e;
4145 *class_uchardata
++ = XCL_RANGE
;
4146 *class_uchardata
++ = 0x2030;
4147 *class_uchardata
++ = 0x205e;
4148 *class_uchardata
++ = XCL_RANGE
;
4149 *class_uchardata
++ = 0x2060;
4150 *class_uchardata
++ = 0x2fff;
4151 *class_uchardata
++ = XCL_RANGE
;
4152 *class_uchardata
++ = 0x3001;
4155 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
4158 *class_uchardata
++ = 0xffff;
4159 #elif defined SUPPORT_UTF
4163 *class_uchardata
++ = XCL_RANGE
;
4164 class_uchardata
+= PRIV(ord2utf
)(0x0100, class_uchardata
);
4165 class_uchardata
+= PRIV(ord2utf
)(0x167f, class_uchardata
);
4166 *class_uchardata
++ = XCL_RANGE
;
4167 class_uchardata
+= PRIV(ord2utf
)(0x1681, class_uchardata
);
4168 class_uchardata
+= PRIV(ord2utf
)(0x180d, class_uchardata
);
4169 *class_uchardata
++ = XCL_RANGE
;
4170 class_uchardata
+= PRIV(ord2utf
)(0x180f, class_uchardata
);
4171 class_uchardata
+= PRIV(ord2utf
)(0x1fff, class_uchardata
);
4172 *class_uchardata
++ = XCL_RANGE
;
4173 class_uchardata
+= PRIV(ord2utf
)(0x200b, class_uchardata
);
4174 class_uchardata
+= PRIV(ord2utf
)(0x202e, class_uchardata
);
4175 *class_uchardata
++ = XCL_RANGE
;
4176 class_uchardata
+= PRIV(ord2utf
)(0x2030, class_uchardata
);
4177 class_uchardata
+= PRIV(ord2utf
)(0x205e, class_uchardata
);
4178 *class_uchardata
++ = XCL_RANGE
;
4179 class_uchardata
+= PRIV(ord2utf
)(0x2060, class_uchardata
);
4180 class_uchardata
+= PRIV(ord2utf
)(0x2fff, class_uchardata
);
4181 *class_uchardata
++ = XCL_RANGE
;
4182 class_uchardata
+= PRIV(ord2utf
)(0x3001, class_uchardata
);
4183 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
4189 SETBIT(classbits
, 0x0a); /* LF */
4190 SETBIT(classbits
, 0x0b); /* VT */
4191 SETBIT(classbits
, 0x0c); /* FF */
4192 SETBIT(classbits
, 0x0d); /* CR */
4193 SETBIT(classbits
, 0x85); /* NEL */
4194 #ifndef COMPILE_PCRE8
4196 *class_uchardata
++ = XCL_RANGE
;
4197 *class_uchardata
++ = 0x2028;
4198 *class_uchardata
++ = 0x2029;
4199 #elif defined SUPPORT_UTF
4203 *class_uchardata
++ = XCL_RANGE
;
4204 class_uchardata
+= PRIV(ord2utf
)(0x2028, class_uchardata
);
4205 class_uchardata
+= PRIV(ord2utf
)(0x2029, class_uchardata
);
4211 for (c
= 0; c
< 32; c
++)
4216 case 0x0a/8: x
^= 1 << (0x0a%8);
4221 case 0x85/8: x
^= 1 << (0x85%8); break;
4227 #ifndef COMPILE_PCRE8
4229 *class_uchardata
++ = XCL_RANGE
;
4230 *class_uchardata
++ = 0x0100;
4231 *class_uchardata
++ = 0x2027;
4232 *class_uchardata
++ = XCL_RANGE
;
4233 *class_uchardata
++ = 0x202a;
4236 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
4239 *class_uchardata
++ = 0xffff;
4240 #elif defined SUPPORT_UTF
4244 *class_uchardata
++ = XCL_RANGE
;
4245 class_uchardata
+= PRIV(ord2utf
)(0x0100, class_uchardata
);
4246 class_uchardata
+= PRIV(ord2utf
)(0x2027, class_uchardata
);
4247 *class_uchardata
++ = XCL_RANGE
;
4248 class_uchardata
+= PRIV(ord2utf
)(0x202a, class_uchardata
);
4249 class_uchardata
+= PRIV(ord2utf
)(0x10ffff, class_uchardata
);
4260 int ptype
= get_ucp(&ptr
, &negated
, &pdata
, errorcodeptr
);
4261 if (ptype
< 0) goto FAILED
;
4263 *class_uchardata
++ = ((-c
== ESC_p
) != negated
)?
4264 XCL_PROP
: XCL_NOTPROP
;
4265 *class_uchardata
++ = ptype
;
4266 *class_uchardata
++ = pdata
;
4267 class_has_8bitchar
--; /* Undo! */
4271 /* Unrecognized escapes are faulted if PCRE is running in its
4272 strict mode. By default, for compatibility with Perl, they are
4273 treated as literals. */
4276 if ((options
& PCRE_EXTRA
) != 0)
4278 *errorcodeptr
= ERR7
;
4281 class_has_8bitchar
--; /* Undo the speculative increase. */
4282 class_single_char
-= 2; /* Undo the speculative increase. */
4283 c
= *ptr
; /* Get the final character and fall through */
4288 /* Fall through if we have a single character (c >= 0). This may be
4289 greater than 256. */
4291 } /* End of backslash handling */
4293 /* A single character may be followed by '-' to form a range. However,
4294 Perl does not permit ']' to be the end of the range. A '-' character
4295 at the end is treated as a literal. Perl ignores orphaned \E sequences
4296 entirely. The code for handling \Q and \E is messy. */
4299 while (ptr
[1] == CHAR_BACKSLASH
&& ptr
[2] == CHAR_E
)
4307 /* Remember \r or \n */
4309 if (c
== CHAR_CR
|| c
== CHAR_NL
) cd
->external_flags
|= PCRE_HASCRORLF
;
4311 /* Check for range */
4313 if (!inescq
&& ptr
[1] == CHAR_MINUS
)
4317 while (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
) ptr
+= 2;
4319 /* If we hit \Q (not followed by \E) at this point, go into escaped
4322 while (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_Q
)
4325 if (*ptr
== CHAR_BACKSLASH
&& ptr
[1] == CHAR_E
)
4326 { ptr
+= 2; continue; }
4331 if (*ptr
== 0 || (!inescq
&& *ptr
== CHAR_RIGHT_SQUARE_BRACKET
))
4334 goto LONE_SINGLE_CHARACTER
;
4339 { /* Braces are required because the */
4340 GETCHARLEN(d
, ptr
, ptr
); /* macro generates multiple statements */
4344 d
= *ptr
; /* Not UTF-8 mode */
4346 /* The second part of a range can be a single-character escape, but
4347 not any of the other escapes. Perl 5.6 treats a hyphen as a literal
4348 in such circumstances. */
4350 if (!inescq
&& d
== CHAR_BACKSLASH
)
4352 d
= check_escape(&ptr
, errorcodeptr
, cd
->bracount
, options
, TRUE
);
4353 if (*errorcodeptr
!= 0) goto FAILED
;
4355 /* \b is backspace; any other special means the '-' was literal */
4359 if (d
== -ESC_b
) d
= CHAR_BS
; else
4362 goto LONE_SINGLE_CHARACTER
; /* A few lines below */
4367 /* Check that the two values are in the correct order. Optimize
4368 one-character ranges */
4372 *errorcodeptr
= ERR8
;
4376 if (d
== c
) goto LONE_SINGLE_CHARACTER
; /* A few lines below */
4378 /* Remember \r or \n */
4380 if (d
== CHAR_CR
|| d
== CHAR_NL
) cd
->external_flags
|= PCRE_HASCRORLF
;
4382 /* Since we found a character range, single character optimizations
4383 cannot be done anymore. */
4384 class_single_char
= 2;
4386 /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless
4387 matching, we have to use an XCLASS with extra data items. Caseless
4388 matching for characters > 127 is available only if UCP support is
4391 #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)
4392 if ((d
> 255) || (utf
&& ((options
& PCRE_CASELESS
) != 0 && d
> 127)))
4393 #elif defined SUPPORT_UTF
4394 if (utf
&& (d
> 255 || ((options
& PCRE_CASELESS
) != 0 && d
> 127)))
4395 #elif !(defined COMPILE_PCRE8)
4398 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
4402 /* With UCP support, we can find the other case equivalents of
4403 the relevant characters. There may be several ranges. Optimize how
4404 they fit with the basic range. */
4407 #ifndef COMPILE_PCRE8
4408 if (utf
&& (options
& PCRE_CASELESS
) != 0)
4410 if ((options
& PCRE_CASELESS
) != 0)
4413 unsigned int occ
, ocd
;
4414 unsigned int cc
= c
;
4415 unsigned int origd
= d
;
4416 while (get_othercase_range(&cc
, origd
, &occ
, &ocd
))
4418 if (occ
>= (unsigned int)c
&&
4419 ocd
<= (unsigned int)d
)
4420 continue; /* Skip embedded ranges */
4422 if (occ
< (unsigned int)c
&&
4423 ocd
>= (unsigned int)c
- 1) /* Extend the basic range */
4424 { /* if there is overlap, */
4425 c
= occ
; /* noting that if occ < c */
4426 continue; /* we can't have ocd > d */
4427 } /* because a subrange is */
4428 if (ocd
> (unsigned int)d
&&
4429 occ
<= (unsigned int)d
+ 1) /* always shorter than */
4430 { /* the basic range. */
4437 *class_uchardata
++ = XCL_SINGLE
;
4441 *class_uchardata
++ = XCL_RANGE
;
4442 class_uchardata
+= PRIV(ord2utf
)(occ
, class_uchardata
);
4444 class_uchardata
+= PRIV(ord2utf
)(ocd
, class_uchardata
);
4447 #endif /* SUPPORT_UCP */
4449 /* Now record the original range, possibly modified for UCP caseless
4450 overlapping ranges. */
4452 *class_uchardata
++ = XCL_RANGE
;
4454 #ifndef COMPILE_PCRE8
4457 class_uchardata
+= PRIV(ord2utf
)(c
, class_uchardata
);
4458 class_uchardata
+= PRIV(ord2utf
)(d
, class_uchardata
);
4462 *class_uchardata
++ = c
;
4463 *class_uchardata
++ = d
;
4466 class_uchardata
+= PRIV(ord2utf
)(c
, class_uchardata
);
4467 class_uchardata
+= PRIV(ord2utf
)(d
, class_uchardata
);
4469 #else /* SUPPORT_UTF */
4470 *class_uchardata
++ = c
;
4471 *class_uchardata
++ = d
;
4472 #endif /* SUPPORT_UTF */
4474 /* With UCP support, we are done. Without UCP support, there is no
4475 caseless matching for UTF characters > 127; we can use the bit map
4476 for the smaller ones. As for 16 bit characters without UTF, we
4480 #ifndef COMPILE_PCRE8
4483 continue; /* With next character in the class */
4484 #endif /* SUPPORT_UCP */
4486 #if defined SUPPORT_UTF && !defined(SUPPORT_UCP) && !(defined COMPILE_PCRE8)
4489 if ((options
& PCRE_CASELESS
) == 0 || c
> 127) continue;
4490 /* Adjust upper limit and fall through to set up the map */
4495 if (c
> 255) continue;
4496 /* Adjust upper limit and fall through to set up the map */
4499 #elif defined SUPPORT_UTF && !defined(SUPPORT_UCP)
4500 if ((options
& PCRE_CASELESS
) == 0 || c
> 127) continue;
4501 /* Adjust upper limit and fall through to set up the map */
4504 if (c
> 255) continue;
4505 /* Adjust upper limit and fall through to set up the map */
4507 #endif /* SUPPORT_UTF && !SUPPORT_UCP && !COMPILE_PCRE8 */
4509 #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
4511 /* We use the bit map for 8 bit mode, or when the characters fall
4512 partially or entirely to [0-255] ([0-127] for UCP) ranges. */
4514 class_has_8bitchar
= 1;
4516 /* We can save a bit of time by skipping this in the pre-compile. */
4518 if (lengthptr
== NULL
) for (; c
<= d
; c
++)
4520 classbits
[c
/8] |= (1 << (c
&7));
4521 if ((options
& PCRE_CASELESS
) != 0)
4523 int uc
= cd
->fcc
[c
]; /* flip case */
4524 classbits
[uc
/8] |= (1 << (uc
&7));
4528 continue; /* Go get the next char in the class */
4531 /* Handle a lone single character - we can get here for a normal
4532 non-escape char, or after \ that introduces a single character or for an
4533 apparent range that isn't. */
4535 LONE_SINGLE_CHARACTER
:
4537 /* Only the value of 1 matters for class_single_char. */
4539 if (class_single_char
< 2) class_single_char
++;
4541 /* If class_charcount is 1, we saw precisely one character. As long as
4542 there was no use of \p or \P, in other words, no use of any XCLASS
4543 features, we can optimize.
4545 The optimization throws away the bit map. We turn the item into a
4546 1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.
4547 In the positive case, it can cause firstchar to be set. Otherwise, there
4548 can be no first char if this item is first, whatever repeat count may
4549 follow. In the case of reqchar, save the previous value for reinstating. */
4551 if (class_single_char
== 1 && ptr
[1] == CHAR_RIGHT_SQUARE_BRACKET
)
4554 zeroreqchar
= reqchar
;
4558 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
4559 zerofirstchar
= firstchar
;
4560 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_NOTI
: OP_NOT
;
4562 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
4563 code
+= PRIV(ord2utf
)(c
, code
);
4570 /* For a single, positive character, get the value into mcbuffer, and
4571 then we can handle this with the normal one-character code. */
4574 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
4575 mclength
= PRIV(ord2utf
)(c
, mcbuffer
);
4583 } /* End of 1-char optimization */
4585 /* Handle a character that cannot go in the bit map. */
4587 #if defined SUPPORT_UTF && !(defined COMPILE_PCRE8)
4588 if ((c
> 255) || (utf
&& ((options
& PCRE_CASELESS
) != 0 && c
> 127)))
4589 #elif defined SUPPORT_UTF
4590 if (utf
&& (c
> 255 || ((options
& PCRE_CASELESS
) != 0 && c
> 127)))
4591 #elif !(defined COMPILE_PCRE8)
4595 #if defined SUPPORT_UTF || !(defined COMPILE_PCRE8)
4598 *class_uchardata
++ = XCL_SINGLE
;
4600 #ifndef COMPILE_PCRE8
4601 /* In non 8 bit mode, we can get here even if we are not in UTF mode. */
4603 *class_uchardata
++ = c
;
4606 class_uchardata
+= PRIV(ord2utf
)(c
, class_uchardata
);
4607 #else /* SUPPORT_UTF */
4608 *class_uchardata
++ = c
;
4609 #endif /* SUPPORT_UTF */
4612 #ifdef COMPILE_PCRE8
4613 if ((options
& PCRE_CASELESS
) != 0)
4615 /* In non 8 bit mode, we can get here even if we are not in UTF mode. */
4616 if (utf
&& (options
& PCRE_CASELESS
) != 0)
4619 unsigned int othercase
;
4620 if ((int)(othercase
= UCD_OTHERCASE(c
)) != c
)
4622 *class_uchardata
++ = XCL_SINGLE
;
4623 class_uchardata
+= PRIV(ord2utf
)(othercase
, class_uchardata
);
4626 #endif /* SUPPORT_UCP */
4630 #endif /* SUPPORT_UTF || COMPILE_PCRE16 */
4632 /* Handle a single-byte character */
4634 class_has_8bitchar
= 1;
4635 classbits
[c
/8] |= (1 << (c
&7));
4636 if ((options
& PCRE_CASELESS
) != 0)
4638 c
= cd
->fcc
[c
]; /* flip case */
4639 classbits
[c
/8] |= (1 << (c
&7));
4644 /* Loop until ']' reached. This "while" is the end of the "do" far above.
4645 If we are at the end of an internal nested string, revert to the outer
4648 while (((c
= *(++ptr
)) != 0 ||
4650 (ptr
= nestptr
, nestptr
= NULL
, c
= *(++ptr
)) != 0)) &&
4651 (c
!= CHAR_RIGHT_SQUARE_BRACKET
|| inescq
));
4653 /* Check for missing terminating ']' */
4657 *errorcodeptr
= ERR6
;
4661 /* If this is the first thing in the branch, there can be no first char
4662 setting, whatever the repeat count. Any reqchar setting must remain
4663 unchanged after any kind of repeat. */
4665 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
4666 zerofirstchar
= firstchar
;
4667 zeroreqchar
= reqchar
;
4669 /* If there are characters with values > 255, we have to compile an
4670 extended class, with its own opcode, unless there was a negated special
4671 such as \S in the class, and PCRE_UCP is not set, because in that case all
4672 characters > 255 are in the class, so any that were explicitly given as
4673 well can be ignored. If (when there are explicit characters > 255 that must
4674 be listed) there are no characters < 256, we can omit the bitmap in the
4675 actual compiled code. */
4678 if (xclass
&& (!should_flip_negation
|| (options
& PCRE_UCP
) != 0))
4679 #elif !defined COMPILE_PCRE8
4680 if (xclass
&& !should_flip_negation
)
4682 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4684 *class_uchardata
++ = XCL_END
; /* Marks the end of extra data */
4685 *code
++ = OP_XCLASS
;
4687 *code
= negate_class
? XCL_NOT
:0;
4689 /* If the map is required, move up the extra data to make room for it;
4690 otherwise just move the code pointer to the end of the extra data. */
4692 if (class_has_8bitchar
> 0)
4695 memmove(code
+ (32 / sizeof(pcre_uchar
)), code
,
4696 IN_UCHARS(class_uchardata
- code
));
4697 memcpy(code
, classbits
, 32);
4698 code
= class_uchardata
+ (32 / sizeof(pcre_uchar
));
4700 else code
= class_uchardata
;
4702 /* Now fill in the complete length of the item */
4704 PUT(previous
, 1, (int)(code
- previous
));
4705 break; /* End of class handling */
4709 /* If there are no characters > 255, or they are all to be included or
4710 excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4711 whole class was negated and whether there were negative specials such as \S
4712 (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4713 negating it if necessary. */
4715 *code
++ = (negate_class
== should_flip_negation
) ? OP_CLASS
: OP_NCLASS
;
4716 if (lengthptr
== NULL
) /* Save time in the pre-compile phase */
4719 for (c
= 0; c
< 32; c
++) classbits
[c
] = ~classbits
[c
];
4720 memcpy(code
, classbits
, 32);
4722 code
+= 32 / sizeof(pcre_uchar
);
4727 /* ===================================================================*/
4728 /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
4729 has been tested above. */
4731 case CHAR_LEFT_CURLY_BRACKET
:
4732 if (!is_quantifier
) goto NORMAL_CHAR
;
4733 ptr
= read_repeat_counts(ptr
+1, &repeat_min
, &repeat_max
, errorcodeptr
);
4734 if (*errorcodeptr
!= 0) goto FAILED
;
4747 case CHAR_QUESTION_MARK
:
4752 if (previous
== NULL
)
4754 *errorcodeptr
= ERR9
;
4758 if (repeat_min
== 0)
4760 firstchar
= zerofirstchar
; /* Adjust for zero repeat */
4761 reqchar
= zeroreqchar
; /* Ditto */
4764 /* Remember whether this is a variable length repeat */
4766 reqvary
= (repeat_min
== repeat_max
)? 0 : REQ_VARY
;
4768 op_type
= 0; /* Default single-char op codes */
4769 possessive_quantifier
= FALSE
; /* Default not possessive quantifier */
4771 /* Save start of previous item, in case we have to move it up in order to
4772 insert something before it. */
4774 tempcode
= previous
;
4776 /* If the next character is '+', we have a possessive quantifier. This
4777 implies greediness, whatever the setting of the PCRE_UNGREEDY option.
4778 If the next character is '?' this is a minimizing repeat, by default,
4779 but if PCRE_UNGREEDY is set, it works the other way round. We change the
4780 repeat type to the non-default. */
4782 if (ptr
[1] == CHAR_PLUS
)
4784 repeat_type
= 0; /* Force greedy */
4785 possessive_quantifier
= TRUE
;
4788 else if (ptr
[1] == CHAR_QUESTION_MARK
)
4790 repeat_type
= greedy_non_default
;
4793 else repeat_type
= greedy_default
;
4795 /* If previous was a recursion call, wrap it in atomic brackets so that
4796 previous becomes the atomic group. All recursions were so wrapped in the
4797 past, but it no longer happens for non-repeated recursions. In fact, the
4798 repeated ones could be re-implemented independently so as not to need this,
4799 but for the moment we rely on the code for repeating groups. */
4801 if (*previous
== OP_RECURSE
)
4803 memmove(previous
+ 1 + LINK_SIZE
, previous
, IN_UCHARS(1 + LINK_SIZE
));
4804 *previous
= OP_ONCE
;
4805 PUT(previous
, 1, 2 + 2*LINK_SIZE
);
4806 previous
[2 + 2*LINK_SIZE
] = OP_KET
;
4807 PUT(previous
, 3 + 2*LINK_SIZE
, 2 + 2*LINK_SIZE
);
4808 code
+= 2 + 2 * LINK_SIZE
;
4809 length_prevgroup
= 3 + 3*LINK_SIZE
;
4811 /* When actually compiling, we need to check whether this was a forward
4812 reference, and if so, adjust the offset. */
4814 if (lengthptr
== NULL
&& cd
->hwm
>= cd
->start_workspace
+ LINK_SIZE
)
4816 int offset
= GET(cd
->hwm
, -LINK_SIZE
);
4817 if (offset
== previous
+ 1 - cd
->start_code
)
4818 PUT(cd
->hwm
, -LINK_SIZE
, offset
+ 1 + LINK_SIZE
);
4822 /* Now handle repetition for the different types of item. */
4824 /* If previous was a character or negated character match, abolish the item
4825 and generate a repeat item instead. If a char item has a minimum of more
4826 than one, ensure that it is set in reqchar - it might not be if a sequence
4827 such as x{3} is the first thing in a branch because the x will have gone
4828 into firstchar instead. */
4830 if (*previous
== OP_CHAR
|| *previous
== OP_CHARI
4831 || *previous
== OP_NOT
|| *previous
== OP_NOTI
)
4835 default: /* Make compiler happy. */
4836 case OP_CHAR
: op_type
= OP_STAR
- OP_STAR
; break;
4837 case OP_CHARI
: op_type
= OP_STARI
- OP_STAR
; break;
4838 case OP_NOT
: op_type
= OP_NOTSTAR
- OP_STAR
; break;
4839 case OP_NOTI
: op_type
= OP_NOTSTARI
- OP_STAR
; break;
4842 /* Deal with UTF characters that take up more than one character. It's
4843 easier to write this out separately than try to macrify it. Use c to
4844 hold the length of the character in bytes, plus UTF_LENGTH to flag that
4845 it's a length rather than a small character. */
4848 if (utf
&& NOT_FIRSTCHAR(code
[-1]))
4850 pcre_uchar
*lastchar
= code
- 1;
4852 c
= (int)(code
- lastchar
); /* Length of UTF-8 character */
4853 memcpy(utf_chars
, lastchar
, IN_UCHARS(c
)); /* Save the char */
4854 c
|= UTF_LENGTH
; /* Flag c as a length */
4857 #endif /* SUPPORT_UTF */
4859 /* Handle the case of a single character - either with no UTF support, or
4860 with UTF disabled, or for a single character UTF character. */
4863 if (*previous
<= OP_CHARI
&& repeat_min
> 1)
4864 reqchar
= c
| req_caseopt
| cd
->req_varyopt
;
4867 /* If the repetition is unlimited, it pays to see if the next thing on
4868 the line is something that cannot possibly match this character. If so,
4869 automatically possessifying this item gains some performance in the case
4870 where the match fails. */
4872 if (!possessive_quantifier
&&
4874 check_auto_possessive(previous
, utf
, ptr
+ 1, options
, cd
))
4876 repeat_type
= 0; /* Force greedy */
4877 possessive_quantifier
= TRUE
;
4880 goto OUTPUT_SINGLE_REPEAT
; /* Code shared with single character types */
4883 /* If previous was a character type match (\d or similar), abolish it and
4884 create a suitable repeat item. The code is shared with single-character
4885 repeats by setting op_type to add a suitable offset into repeat_type. Note
4886 the the Unicode property types will be present only when SUPPORT_UCP is
4887 defined, but we don't wrap the little bits of code here because it just
4888 makes it horribly messy. */
4890 else if (*previous
< OP_EODN
)
4892 pcre_uchar
*oldcode
;
4893 int prop_type
, prop_value
;
4894 op_type
= OP_TYPESTAR
- OP_STAR
; /* Use type opcodes */
4897 if (!possessive_quantifier
&&
4899 check_auto_possessive(previous
, utf
, ptr
+ 1, options
, cd
))
4901 repeat_type
= 0; /* Force greedy */
4902 possessive_quantifier
= TRUE
;
4905 OUTPUT_SINGLE_REPEAT
:
4906 if (*previous
== OP_PROP
|| *previous
== OP_NOTPROP
)
4908 prop_type
= previous
[1];
4909 prop_value
= previous
[2];
4911 else prop_type
= prop_value
= -1;
4914 code
= previous
; /* Usually overwrite previous item */
4916 /* If the maximum is zero then the minimum must also be zero; Perl allows
4917 this case, so we do too - by simply omitting the item altogether. */
4919 if (repeat_max
== 0) goto END_REPEAT
;
4921 /*--------------------------------------------------------------------*/
4922 /* This code is obsolete from release 8.00; the restriction was finally
4925 /* All real repeats make it impossible to handle partial matching (maybe
4926 one day we will be able to remove this restriction). */
4928 /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
4929 /*--------------------------------------------------------------------*/
4931 /* Combine the op_type with the repeat_type */
4933 repeat_type
+= op_type
;
4935 /* A minimum of zero is handled either as the special case * or ?, or as
4936 an UPTO, with the maximum given. */
4938 if (repeat_min
== 0)
4940 if (repeat_max
== -1) *code
++ = OP_STAR
+ repeat_type
;
4941 else if (repeat_max
== 1) *code
++ = OP_QUERY
+ repeat_type
;
4944 *code
++ = OP_UPTO
+ repeat_type
;
4945 PUT2INC(code
, 0, repeat_max
);
4949 /* A repeat minimum of 1 is optimized into some special cases. If the
4950 maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
4951 left in place and, if the maximum is greater than 1, we use OP_UPTO with
4952 one less than the maximum. */
4954 else if (repeat_min
== 1)
4956 if (repeat_max
== -1)
4957 *code
++ = OP_PLUS
+ repeat_type
;
4960 code
= oldcode
; /* leave previous item in place */
4961 if (repeat_max
== 1) goto END_REPEAT
;
4962 *code
++ = OP_UPTO
+ repeat_type
;
4963 PUT2INC(code
, 0, repeat_max
- 1);
4967 /* The case {n,n} is just an EXACT, while the general case {n,m} is
4968 handled as an EXACT followed by an UPTO. */
4972 *code
++ = OP_EXACT
+ op_type
; /* NB EXACT doesn't have repeat_type */
4973 PUT2INC(code
, 0, repeat_min
);
4975 /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
4976 we have to insert the character for the previous code. For a repeated
4977 Unicode property match, there are two extra bytes that define the
4978 required property. In UTF-8 mode, long characters have their length in
4979 c, with the UTF_LENGTH bit as a flag. */
4984 if (utf
&& (c
& UTF_LENGTH
) != 0)
4986 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
4995 *code
++ = prop_type
;
4996 *code
++ = prop_value
;
4999 *code
++ = OP_STAR
+ repeat_type
;
5002 /* Else insert an UPTO if the max is greater than the min, again
5003 preceded by the character, for the previously inserted code. If the
5004 UPTO is just for 1 instance, we can use QUERY instead. */
5006 else if (repeat_max
!= repeat_min
)
5009 if (utf
&& (c
& UTF_LENGTH
) != 0)
5011 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
5019 *code
++ = prop_type
;
5020 *code
++ = prop_value
;
5022 repeat_max
-= repeat_min
;
5024 if (repeat_max
== 1)
5026 *code
++ = OP_QUERY
+ repeat_type
;
5030 *code
++ = OP_UPTO
+ repeat_type
;
5031 PUT2INC(code
, 0, repeat_max
);
5036 /* The character or character type itself comes last in all cases. */
5039 if (utf
&& (c
& UTF_LENGTH
) != 0)
5041 memcpy(code
, utf_chars
, IN_UCHARS(c
& 7));
5048 /* For a repeated Unicode property match, there are two extra bytes that
5049 define the required property. */
5054 *code
++ = prop_type
;
5055 *code
++ = prop_value
;
5060 /* If previous was a character class or a back reference, we put the repeat
5061 stuff after it, but just skip the item if the repeat was {0,0}. */
5063 else if (*previous
== OP_CLASS
||
5064 *previous
== OP_NCLASS
||
5065 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5066 *previous
== OP_XCLASS
||
5068 *previous
== OP_REF
||
5069 *previous
== OP_REFI
)
5071 if (repeat_max
== 0)
5077 /*--------------------------------------------------------------------*/
5078 /* This code is obsolete from release 8.00; the restriction was finally
5081 /* All real repeats make it impossible to handle partial matching (maybe
5082 one day we will be able to remove this restriction). */
5084 /* if (repeat_max != 1) cd->external_flags |= PCRE_NOPARTIAL; */
5085 /*--------------------------------------------------------------------*/
5087 if (repeat_min
== 0 && repeat_max
== -1)
5088 *code
++ = OP_CRSTAR
+ repeat_type
;
5089 else if (repeat_min
== 1 && repeat_max
== -1)
5090 *code
++ = OP_CRPLUS
+ repeat_type
;
5091 else if (repeat_min
== 0 && repeat_max
== 1)
5092 *code
++ = OP_CRQUERY
+ repeat_type
;
5095 *code
++ = OP_CRRANGE
+ repeat_type
;
5096 PUT2INC(code
, 0, repeat_min
);
5097 if (repeat_max
== -1) repeat_max
= 0; /* 2-byte encoding for max */
5098 PUT2INC(code
, 0, repeat_max
);
5102 /* If previous was a bracket group, we may have to replicate it in certain
5103 cases. Note that at this point we can encounter only the "basic" bracket
5104 opcodes such as BRA and CBRA, as this is the place where they get converted
5105 into the more special varieties such as BRAPOS and SBRA. A test for >=
5106 OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
5107 ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
5108 repetition of assertions, but now it does, for Perl compatibility. */
5110 else if (*previous
>= OP_ASSERT
&& *previous
<= OP_COND
)
5113 int len
= (int)(code
- previous
);
5114 pcre_uchar
*bralink
= NULL
;
5115 pcre_uchar
*brazeroptr
= NULL
;
5117 /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
5118 we just ignore the repeat. */
5120 if (*previous
== OP_COND
&& previous
[LINK_SIZE
+1] == OP_DEF
)
5123 /* There is no sense in actually repeating assertions. The only potential
5124 use of repetition is in cases when the assertion is optional. Therefore,
5125 if the minimum is greater than zero, just ignore the repeat. If the
5126 maximum is not not zero or one, set it to 1. */
5128 if (*previous
< OP_ONCE
) /* Assertion */
5130 if (repeat_min
> 0) goto END_REPEAT
;
5131 if (repeat_max
< 0 || repeat_max
> 1) repeat_max
= 1;
5134 /* The case of a zero minimum is special because of the need to stick
5135 OP_BRAZERO in front of it, and because the group appears once in the
5136 data, whereas in other cases it appears the minimum number of times. For
5137 this reason, it is simplest to treat this case separately, as otherwise
5138 the code gets far too messy. There are several special subcases when the
5141 if (repeat_min
== 0)
5143 /* If the maximum is also zero, we used to just omit the group from the
5144 output altogether, like this:
5146 ** if (repeat_max == 0)
5152 However, that fails when a group or a subgroup within it is referenced
5153 as a subroutine from elsewhere in the pattern, so now we stick in
5154 OP_SKIPZERO in front of it so that it is skipped on execution. As we
5155 don't have a list of which groups are referenced, we cannot do this
5158 If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
5159 and do no more at this point. However, we do need to adjust any
5160 OP_RECURSE calls inside the group that refer to the group itself or any
5161 internal or forward referenced group, because the offset is from the
5162 start of the whole regex. Temporarily terminate the pattern while doing
5165 if (repeat_max
<= 1) /* Covers 0, 1, and unlimited */
5168 adjust_recurse(previous
, 1, utf
, cd
, save_hwm
);
5169 memmove(previous
+ 1, previous
, IN_UCHARS(len
));
5171 if (repeat_max
== 0)
5173 *previous
++ = OP_SKIPZERO
;
5176 brazeroptr
= previous
; /* Save for possessive optimizing */
5177 *previous
++ = OP_BRAZERO
+ repeat_type
;
5180 /* If the maximum is greater than 1 and limited, we have to replicate
5181 in a nested fashion, sticking OP_BRAZERO before each set of brackets.
5182 The first one has to be handled carefully because it's the original
5183 copy, which has to be moved up. The remainder can be handled by code
5184 that is common with the non-zero minimum case below. We have to
5185 adjust the value or repeat_max, since one less copy is required. Once
5186 again, we may have to adjust any OP_RECURSE calls inside the group. */
5192 adjust_recurse(previous
, 2 + LINK_SIZE
, utf
, cd
, save_hwm
);
5193 memmove(previous
+ 2 + LINK_SIZE
, previous
, IN_UCHARS(len
));
5194 code
+= 2 + LINK_SIZE
;
5195 *previous
++ = OP_BRAZERO
+ repeat_type
;
5196 *previous
++ = OP_BRA
;
5198 /* We chain together the bracket offset fields that have to be
5199 filled in later when the ends of the brackets are reached. */
5201 offset
= (bralink
== NULL
)? 0 : (int)(previous
- bralink
);
5203 PUTINC(previous
, 0, offset
);
5209 /* If the minimum is greater than zero, replicate the group as many
5210 times as necessary, and adjust the maximum to the number of subsequent
5211 copies that we need. If we set a first char from the group, and didn't
5212 set a required char, copy the latter from the former. If there are any
5213 forward reference subroutine calls in the group, there will be entries on
5214 the workspace list; replicate these with an appropriate increment. */
5220 /* In the pre-compile phase, we don't actually do the replication. We
5221 just adjust the length as if we had. Do some paranoid checks for
5222 potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
5223 integer type when available, otherwise double. */
5225 if (lengthptr
!= NULL
)
5227 int delta
= (repeat_min
- 1)*length_prevgroup
;
5228 if ((INT64_OR_DOUBLE
)(repeat_min
- 1)*
5229 (INT64_OR_DOUBLE
)length_prevgroup
>
5230 (INT64_OR_DOUBLE
)INT_MAX
||
5231 OFLOW_MAX
- *lengthptr
< delta
)
5233 *errorcodeptr
= ERR20
;
5236 *lengthptr
+= delta
;
5239 /* This is compiling for real. If there is a set first byte for
5240 the group, and we have not yet set a "required byte", set it. Make
5241 sure there is enough workspace for copying forward references before
5246 if (groupsetfirstchar
&& reqchar
< 0) reqchar
= firstchar
;
5248 for (i
= 1; i
< repeat_min
; i
++)
5251 pcre_uchar
*this_hwm
= cd
->hwm
;
5252 memcpy(code
, previous
, IN_UCHARS(len
));
5254 while (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
-
5255 WORK_SIZE_SAFETY_MARGIN
- (this_hwm
- save_hwm
))
5257 int save_offset
= save_hwm
- cd
->start_workspace
;
5258 int this_offset
= this_hwm
- cd
->start_workspace
;
5259 *errorcodeptr
= expand_workspace(cd
);
5260 if (*errorcodeptr
!= 0) goto FAILED
;
5261 save_hwm
= (pcre_uchar
*)cd
->start_workspace
+ save_offset
;
5262 this_hwm
= (pcre_uchar
*)cd
->start_workspace
+ this_offset
;
5265 for (hc
= save_hwm
; hc
< this_hwm
; hc
+= LINK_SIZE
)
5267 PUT(cd
->hwm
, 0, GET(hc
, 0) + len
);
5268 cd
->hwm
+= LINK_SIZE
;
5270 save_hwm
= this_hwm
;
5276 if (repeat_max
> 0) repeat_max
-= repeat_min
;
5279 /* This code is common to both the zero and non-zero minimum cases. If
5280 the maximum is limited, it replicates the group in a nested fashion,
5281 remembering the bracket starts on a stack. In the case of a zero minimum,
5282 the first one was set up above. In all cases the repeat_max now specifies
5283 the number of additional copies needed. Again, we must remember to
5284 replicate entries on the forward reference list. */
5286 if (repeat_max
>= 0)
5288 /* In the pre-compile phase, we don't actually do the replication. We
5289 just adjust the length as if we had. For each repetition we must add 1
5290 to the length for BRAZERO and for all but the last repetition we must
5291 add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
5292 paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
5293 a 64-bit integer type when available, otherwise double. */
5295 if (lengthptr
!= NULL
&& repeat_max
> 0)
5297 int delta
= repeat_max
* (length_prevgroup
+ 1 + 2 + 2*LINK_SIZE
) -
5298 2 - 2*LINK_SIZE
; /* Last one doesn't nest */
5299 if ((INT64_OR_DOUBLE
)repeat_max
*
5300 (INT64_OR_DOUBLE
)(length_prevgroup
+ 1 + 2 + 2*LINK_SIZE
)
5301 > (INT64_OR_DOUBLE
)INT_MAX
||
5302 OFLOW_MAX
- *lengthptr
< delta
)
5304 *errorcodeptr
= ERR20
;
5307 *lengthptr
+= delta
;
5310 /* This is compiling for real */
5312 else for (i
= repeat_max
- 1; i
>= 0; i
--)
5315 pcre_uchar
*this_hwm
= cd
->hwm
;
5317 *code
++ = OP_BRAZERO
+ repeat_type
;
5319 /* All but the final copy start a new nesting, maintaining the
5320 chain of brackets outstanding. */
5326 offset
= (bralink
== NULL
)? 0 : (int)(code
- bralink
);
5328 PUTINC(code
, 0, offset
);
5331 memcpy(code
, previous
, IN_UCHARS(len
));
5333 /* Ensure there is enough workspace for forward references before
5336 while (cd
->hwm
> cd
->start_workspace
+ cd
->workspace_size
-
5337 WORK_SIZE_SAFETY_MARGIN
- (this_hwm
- save_hwm
))
5339 int save_offset
= save_hwm
- cd
->start_workspace
;
5340 int this_offset
= this_hwm
- cd
->start_workspace
;
5341 *errorcodeptr
= expand_workspace(cd
);
5342 if (*errorcodeptr
!= 0) goto FAILED
;
5343 save_hwm
= (pcre_uchar
*)cd
->start_workspace
+ save_offset
;
5344 this_hwm
= (pcre_uchar
*)cd
->start_workspace
+ this_offset
;
5347 for (hc
= save_hwm
; hc
< this_hwm
; hc
+= LINK_SIZE
)
5349 PUT(cd
->hwm
, 0, GET(hc
, 0) + len
+ ((i
!= 0)? 2+LINK_SIZE
: 1));
5350 cd
->hwm
+= LINK_SIZE
;
5352 save_hwm
= this_hwm
;
5356 /* Now chain through the pending brackets, and fill in their length
5357 fields (which are holding the chain links pro tem). */
5359 while (bralink
!= NULL
)
5362 int offset
= (int)(code
- bralink
+ 1);
5363 pcre_uchar
*bra
= code
- offset
;
5364 oldlinkoffset
= GET(bra
, 1);
5365 bralink
= (oldlinkoffset
== 0)? NULL
: bralink
- oldlinkoffset
;
5367 PUTINC(code
, 0, offset
);
5368 PUT(bra
, 1, offset
);
5372 /* If the maximum is unlimited, set a repeater in the final copy. For
5373 ONCE brackets, that's all we need to do. However, possessively repeated
5374 ONCE brackets can be converted into non-capturing brackets, as the
5375 behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5376 deal with possessive ONCEs specially.
5378 Otherwise, when we are doing the actual compile phase, check to see
5379 whether this group is one that could match an empty string. If so,
5380 convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5381 that runtime checking can be done. [This check is also applied to ONCE
5382 groups at runtime, but in a different way.]
5384 Then, if the quantifier was possessive and the bracket is not a
5385 conditional, we convert the BRA code to the POS form, and the KET code to
5386 KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5387 subpattern at both the start and at the end.) The use of special opcodes
5388 makes it possible to reduce greatly the stack usage in pcre_exec(). If
5389 the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5391 Then, if the minimum number of matches is 1 or 0, cancel the possessive
5392 flag so that the default action below, of wrapping everything inside
5393 atomic brackets, does not happen. When the minimum is greater than 1,
5394 there will be earlier copies of the group, and so we still have to wrap
5399 pcre_uchar
*ketcode
= code
- 1 - LINK_SIZE
;
5400 pcre_uchar
*bracode
= ketcode
- GET(ketcode
, 1);
5402 /* Convert possessive ONCE brackets to non-capturing */
5404 if ((*bracode
== OP_ONCE
|| *bracode
== OP_ONCE_NC
) &&
5405 possessive_quantifier
) *bracode
= OP_BRA
;
5407 /* For non-possessive ONCE brackets, all we need to do is to
5410 if (*bracode
== OP_ONCE
|| *bracode
== OP_ONCE_NC
)
5411 *ketcode
= OP_KETRMAX
+ repeat_type
;
5413 /* Handle non-ONCE brackets and possessive ONCEs (which have been
5414 converted to non-capturing above). */
5418 /* In the compile phase, check for empty string matching. */
5420 if (lengthptr
== NULL
)
5422 pcre_uchar
*scode
= bracode
;
5425 if (could_be_empty_branch(scode
, ketcode
, utf
, cd
))
5427 *bracode
+= OP_SBRA
- OP_BRA
;
5430 scode
+= GET(scode
, 1);
5432 while (*scode
== OP_ALT
);
5435 /* Handle possessive quantifiers. */
5437 if (possessive_quantifier
)
5439 /* For COND brackets, we wrap the whole thing in a possessively
5440 repeated non-capturing bracket, because we have not invented POS
5441 versions of the COND opcodes. Because we are moving code along, we
5442 must ensure that any pending recursive references are updated. */
5444 if (*bracode
== OP_COND
|| *bracode
== OP_SCOND
)
5446 int nlen
= (int)(code
- bracode
);
5448 adjust_recurse(bracode
, 1 + LINK_SIZE
, utf
, cd
, save_hwm
);
5449 memmove(bracode
+ 1 + LINK_SIZE
, bracode
, IN_UCHARS(nlen
));
5450 code
+= 1 + LINK_SIZE
;
5451 nlen
+= 1 + LINK_SIZE
;
5452 *bracode
= OP_BRAPOS
;
5453 *code
++ = OP_KETRPOS
;
5454 PUTINC(code
, 0, nlen
);
5455 PUT(bracode
, 1, nlen
);
5458 /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5462 *bracode
+= 1; /* Switch to xxxPOS opcodes */
5463 *ketcode
= OP_KETRPOS
;
5466 /* If the minimum is zero, mark it as possessive, then unset the
5467 possessive flag when the minimum is 0 or 1. */
5469 if (brazeroptr
!= NULL
) *brazeroptr
= OP_BRAPOSZERO
;
5470 if (repeat_min
< 2) possessive_quantifier
= FALSE
;
5473 /* Non-possessive quantifier */
5475 else *ketcode
= OP_KETRMAX
+ repeat_type
;
5480 /* If previous is OP_FAIL, it was generated by an empty class [] in
5481 JavaScript mode. The other ways in which OP_FAIL can be generated, that is
5482 by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
5483 error above. We can just ignore the repeat in JS case. */
5485 else if (*previous
== OP_FAIL
) goto END_REPEAT
;
5487 /* Else there's some kind of shambles */
5491 *errorcodeptr
= ERR11
;
5495 /* If the character following a repeat is '+', or if certain optimization
5496 tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5497 there are special alternative opcodes for this case. For anything else, we
5498 wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5499 notation is just syntactic sugar, taken from Sun's Java package, but the
5500 special opcodes can optimize it.
5502 Some (but not all) possessively repeated subpatterns have already been
5503 completely handled in the code just above. For them, possessive_quantifier
5504 is always FALSE at this stage.
5506 Note that the repeated item starts at tempcode, not at previous, which
5507 might be the first part of a string whose (former) last char we repeated.
5509 Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5510 an 'upto' may follow. We skip over an 'exact' item, and then test the
5511 length of what remains before proceeding. */
5513 if (possessive_quantifier
)
5517 if (*tempcode
== OP_TYPEEXACT
)
5518 tempcode
+= PRIV(OP_lengths
)[*tempcode
] +
5519 ((tempcode
[1 + IMM2_SIZE
] == OP_PROP
5520 || tempcode
[1 + IMM2_SIZE
] == OP_NOTPROP
)? 2 : 0);
5522 else if (*tempcode
== OP_EXACT
|| *tempcode
== OP_NOTEXACT
)
5524 tempcode
+= PRIV(OP_lengths
)[*tempcode
];
5526 if (utf
&& HAS_EXTRALEN(tempcode
[-1]))
5527 tempcode
+= GET_EXTRALEN(tempcode
[-1]);
5531 len
= (int)(code
- tempcode
);
5532 if (len
> 0) switch (*tempcode
)
5534 case OP_STAR
: *tempcode
= OP_POSSTAR
; break;
5535 case OP_PLUS
: *tempcode
= OP_POSPLUS
; break;
5536 case OP_QUERY
: *tempcode
= OP_POSQUERY
; break;
5537 case OP_UPTO
: *tempcode
= OP_POSUPTO
; break;
5539 case OP_STARI
: *tempcode
= OP_POSSTARI
; break;
5540 case OP_PLUSI
: *tempcode
= OP_POSPLUSI
; break;
5541 case OP_QUERYI
: *tempcode
= OP_POSQUERYI
; break;
5542 case OP_UPTOI
: *tempcode
= OP_POSUPTOI
; break;
5544 case OP_NOTSTAR
: *tempcode
= OP_NOTPOSSTAR
; break;
5545 case OP_NOTPLUS
: *tempcode
= OP_NOTPOSPLUS
; break;
5546 case OP_NOTQUERY
: *tempcode
= OP_NOTPOSQUERY
; break;
5547 case OP_NOTUPTO
: *tempcode
= OP_NOTPOSUPTO
; break;
5549 case OP_NOTSTARI
: *tempcode
= OP_NOTPOSSTARI
; break;
5550 case OP_NOTPLUSI
: *tempcode
= OP_NOTPOSPLUSI
; break;
5551 case OP_NOTQUERYI
: *tempcode
= OP_NOTPOSQUERYI
; break;
5552 case OP_NOTUPTOI
: *tempcode
= OP_NOTPOSUPTOI
; break;
5554 case OP_TYPESTAR
: *tempcode
= OP_TYPEPOSSTAR
; break;
5555 case OP_TYPEPLUS
: *tempcode
= OP_TYPEPOSPLUS
; break;
5556 case OP_TYPEQUERY
: *tempcode
= OP_TYPEPOSQUERY
; break;
5557 case OP_TYPEUPTO
: *tempcode
= OP_TYPEPOSUPTO
; break;
5559 /* Because we are moving code along, we must ensure that any
5560 pending recursive references are updated. */
5564 adjust_recurse(tempcode
, 1 + LINK_SIZE
, utf
, cd
, save_hwm
);
5565 memmove(tempcode
+ 1 + LINK_SIZE
, tempcode
, IN_UCHARS(len
));
5566 code
+= 1 + LINK_SIZE
;
5567 len
+= 1 + LINK_SIZE
;
5568 tempcode
[0] = OP_ONCE
;
5570 PUTINC(code
, 0, len
);
5571 PUT(tempcode
, 1, len
);
5576 /* In all case we no longer have a previous item. We also set the
5577 "follows varying string" flag for subsequently encountered reqchars if
5578 it isn't already set and we have just passed a varying length item. */
5582 cd
->req_varyopt
|= reqvary
;
5586 /* ===================================================================*/
5587 /* Start of nested parenthesized sub-expression, or comment or lookahead or
5588 lookbehind or option setting or condition or all the other extended
5589 parenthesis forms. */
5591 case CHAR_LEFT_PARENTHESIS
:
5592 newoptions
= options
;
5596 reset_bracount
= FALSE
;
5598 /* First deal with various "verbs" that can be introduced by '*'. */
5601 if (ptr
[0] == CHAR_ASTERISK
&& (ptr
[1] == ':'
5602 || (MAX_255(ptr
[1]) && ((cd
->ctypes
[ptr
[1]] & ctype_letter
) != 0))))
5606 const char *vn
= verbnames
;
5607 const pcre_uchar
*name
= ptr
+ 1;
5608 const pcre_uchar
*arg
= NULL
;
5611 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_letter
) != 0) ptr
++;
5612 namelen
= (int)(ptr
- name
);
5614 /* It appears that Perl allows any characters whatsoever, other than
5615 a closing parenthesis, to appear in arguments, so we no longer insist on
5616 letters, digits, and underscores. */
5618 if (*ptr
== CHAR_COLON
)
5621 while (*ptr
!= 0 && *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
5622 arglen
= (int)(ptr
- arg
);
5623 if (arglen
> (int)MAX_MARK
)
5625 *errorcodeptr
= ERR75
;
5630 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
5632 *errorcodeptr
= ERR60
;
5636 /* Scan the table of verb names */
5638 for (i
= 0; i
< verbcount
; i
++)
5640 if (namelen
== verbs
[i
].len
&&
5641 STRNCMP_UC_C8(name
, vn
, namelen
) == 0)
5643 /* Check for open captures before ACCEPT and convert it to
5644 ASSERT_ACCEPT if in an assertion. */
5646 if (verbs
[i
].op
== OP_ACCEPT
)
5651 *errorcodeptr
= ERR59
;
5654 cd
->had_accept
= TRUE
;
5655 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
5658 PUT2INC(code
, 0, oc
->number
);
5660 *code
++ = (cd
->assert_depth
> 0)? OP_ASSERT_ACCEPT
: OP_ACCEPT
;
5662 /* Do not set firstchar after *ACCEPT */
5663 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
5666 /* Handle other cases with/without an argument */
5668 else if (arglen
== 0)
5670 if (verbs
[i
].op
< 0) /* Argument is mandatory */
5672 *errorcodeptr
= ERR66
;
5675 *code
= verbs
[i
].op
;
5676 if (*code
++ == OP_THEN
) cd
->external_flags
|= PCRE_HASTHEN
;
5681 if (verbs
[i
].op_arg
< 0) /* Argument is forbidden */
5683 *errorcodeptr
= ERR59
;
5686 *code
= verbs
[i
].op_arg
;
5687 if (*code
++ == OP_THEN_ARG
) cd
->external_flags
|= PCRE_HASTHEN
;
5689 memcpy(code
, arg
, IN_UCHARS(arglen
));
5694 break; /* Found verb, exit loop */
5697 vn
+= verbs
[i
].len
+ 1;
5700 if (i
< verbcount
) continue; /* Successfully handled a verb */
5701 *errorcodeptr
= ERR60
; /* Verb not recognized */
5705 /* Deal with the extended parentheses; all are introduced by '?', and the
5706 appearance of any of them means that this is not a capturing group. */
5708 else if (*ptr
== CHAR_QUESTION_MARK
)
5710 int i
, set
, unset
, namelen
;
5712 const pcre_uchar
*name
;
5717 case CHAR_NUMBER_SIGN
: /* Comment; skip to ket */
5719 while (*ptr
!= 0 && *ptr
!= CHAR_RIGHT_PARENTHESIS
) ptr
++;
5722 *errorcodeptr
= ERR18
;
5728 /* ------------------------------------------------------------ */
5729 case CHAR_VERTICAL_LINE
: /* Reset capture count for each branch */
5730 reset_bracount
= TRUE
;
5733 /* ------------------------------------------------------------ */
5734 case CHAR_COLON
: /* Non-capturing bracket */
5740 /* ------------------------------------------------------------ */
5741 case CHAR_LEFT_PARENTHESIS
:
5742 bravalue
= OP_COND
; /* Conditional group */
5744 /* A condition can be an assertion, a number (referring to a numbered
5745 group), a name (referring to a named group), or 'R', referring to
5746 recursion. R<digits> and R&name are also permitted for recursion tests.
5748 There are several syntaxes for testing a named group: (?(name)) is used
5749 by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
5751 There are two unfortunate ambiguities, caused by history. (a) 'R' can
5752 be the recursive thing or the name 'R' (and similarly for 'R' followed
5753 by digits), and (b) a number could be a name that consists of digits.
5754 In both cases, we look for a name first; if not found, we try the other
5757 /* For conditions that are assertions, check the syntax, and then exit
5758 the switch. This will take control down to where bracketed groups,
5759 including assertions, are processed. */
5761 if (ptr
[1] == CHAR_QUESTION_MARK
&& (ptr
[2] == CHAR_EQUALS_SIGN
||
5762 ptr
[2] == CHAR_EXCLAMATION_MARK
|| ptr
[2] == CHAR_LESS_THAN_SIGN
))
5765 /* Most other conditions use OP_CREF (a couple change to OP_RREF
5766 below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5768 code
[1+LINK_SIZE
] = OP_CREF
;
5769 skipbytes
= 1+IMM2_SIZE
;
5772 /* Check for a test for recursion in a named group. */
5774 if (ptr
[1] == CHAR_R
&& ptr
[2] == CHAR_AMPERSAND
)
5778 code
[1+LINK_SIZE
] = OP_RREF
; /* Change the type of test */
5781 /* Check for a test for a named group's having been set, using the Perl
5782 syntax (?(<name>) or (?('name') */
5784 else if (ptr
[1] == CHAR_LESS_THAN_SIGN
)
5786 terminator
= CHAR_GREATER_THAN_SIGN
;
5789 else if (ptr
[1] == CHAR_APOSTROPHE
)
5791 terminator
= CHAR_APOSTROPHE
;
5797 if (ptr
[1] == CHAR_MINUS
|| ptr
[1] == CHAR_PLUS
) refsign
= *(++ptr
);
5800 /* We now expect to read a name; any thing else is an error */
5802 if (!MAX_255(ptr
[1]) || (cd
->ctypes
[ptr
[1]] & ctype_word
) == 0)
5804 ptr
+= 1; /* To get the right offset */
5805 *errorcodeptr
= ERR28
;
5809 /* Read the name, but also get it as a number if it's all digits */
5813 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0)
5816 recno
= (IS_DIGIT(*ptr
))? recno
* 10 + *ptr
- CHAR_0
: -1;
5819 namelen
= (int)(ptr
- name
);
5821 if ((terminator
> 0 && *ptr
++ != terminator
) ||
5822 *ptr
++ != CHAR_RIGHT_PARENTHESIS
)
5824 ptr
--; /* Error offset */
5825 *errorcodeptr
= ERR26
;
5829 /* Do no further checking in the pre-compile phase. */
5831 if (lengthptr
!= NULL
) break;
5833 /* In the real compile we do the work of looking for the actual
5834 reference. If the string started with "+" or "-" we require the rest to
5835 be digits, in which case recno will be set. */
5841 *errorcodeptr
= ERR58
;
5844 recno
= (refsign
== CHAR_MINUS
)?
5845 cd
->bracount
- recno
+ 1 : recno
+cd
->bracount
;
5846 if (recno
<= 0 || recno
> cd
->final_bracount
)
5848 *errorcodeptr
= ERR15
;
5851 PUT2(code
, 2+LINK_SIZE
, recno
);
5855 /* Otherwise (did not start with "+" or "-"), start by looking for the
5856 name. If we find a name, add one to the opcode to change OP_CREF or
5857 OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
5858 except they record that the reference was originally to a name. The
5859 information is used to check duplicate names. */
5861 slot
= cd
->name_table
;
5862 for (i
= 0; i
< cd
->names_found
; i
++)
5864 if (STRNCMP_UC_UC(name
, slot
+IMM2_SIZE
, namelen
) == 0) break;
5865 slot
+= cd
->name_entry_size
;
5868 /* Found a previous named subpattern */
5870 if (i
< cd
->names_found
)
5872 recno
= GET2(slot
, 0);
5873 PUT2(code
, 2+LINK_SIZE
, recno
);
5874 code
[1+LINK_SIZE
]++;
5877 /* Search the pattern for a forward reference */
5879 else if ((i
= find_parens(cd
, name
, namelen
,
5880 (options
& PCRE_EXTENDED
) != 0, utf
)) > 0)
5882 PUT2(code
, 2+LINK_SIZE
, i
);
5883 code
[1+LINK_SIZE
]++;
5886 /* If terminator == 0 it means that the name followed directly after
5887 the opening parenthesis [e.g. (?(abc)...] and in this case there are
5888 some further alternatives to try. For the cases where terminator != 0
5889 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
5890 now checked all the possibilities, so give an error. */
5892 else if (terminator
!= 0)
5894 *errorcodeptr
= ERR15
;
5898 /* Check for (?(R) for recursion. Allow digits after R to specify a
5899 specific group number. */
5901 else if (*name
== CHAR_R
)
5904 for (i
= 1; i
< namelen
; i
++)
5906 if (!IS_DIGIT(name
[i
]))
5908 *errorcodeptr
= ERR15
;
5911 recno
= recno
* 10 + name
[i
] - CHAR_0
;
5913 if (recno
== 0) recno
= RREF_ANY
;
5914 code
[1+LINK_SIZE
] = OP_RREF
; /* Change test type */
5915 PUT2(code
, 2+LINK_SIZE
, recno
);
5918 /* Similarly, check for the (?(DEFINE) "condition", which is always
5921 else if (namelen
== 6 && STRNCMP_UC_C8(name
, STRING_DEFINE
, 6) == 0)
5923 code
[1+LINK_SIZE
] = OP_DEF
;
5927 /* Check for the "name" actually being a subpattern number. We are
5928 in the second pass here, so final_bracount is set. */
5930 else if (recno
> 0 && recno
<= cd
->final_bracount
)
5932 PUT2(code
, 2+LINK_SIZE
, recno
);
5935 /* Either an unidentified subpattern, or a reference to (?(0) */
5939 *errorcodeptr
= (recno
== 0)? ERR35
: ERR15
;
5945 /* ------------------------------------------------------------ */
5946 case CHAR_EQUALS_SIGN
: /* Positive lookahead */
5947 bravalue
= OP_ASSERT
;
5948 cd
->assert_depth
+= 1;
5953 /* ------------------------------------------------------------ */
5954 case CHAR_EXCLAMATION_MARK
: /* Negative lookahead */
5956 if (*ptr
== CHAR_RIGHT_PARENTHESIS
) /* Optimize (?!) */
5962 bravalue
= OP_ASSERT_NOT
;
5963 cd
->assert_depth
+= 1;
5967 /* ------------------------------------------------------------ */
5968 case CHAR_LESS_THAN_SIGN
: /* Lookbehind or named define */
5971 case CHAR_EQUALS_SIGN
: /* Positive lookbehind */
5972 bravalue
= OP_ASSERTBACK
;
5973 cd
->assert_depth
+= 1;
5977 case CHAR_EXCLAMATION_MARK
: /* Negative lookbehind */
5978 bravalue
= OP_ASSERTBACK_NOT
;
5979 cd
->assert_depth
+= 1;
5983 default: /* Could be name define, else bad */
5984 if (MAX_255(ptr
[1]) && (cd
->ctypes
[ptr
[1]] & ctype_word
) != 0)
5986 ptr
++; /* Correct offset for error */
5987 *errorcodeptr
= ERR24
;
5993 /* ------------------------------------------------------------ */
5994 case CHAR_GREATER_THAN_SIGN
: /* One-time brackets */
6000 /* ------------------------------------------------------------ */
6001 case CHAR_C
: /* Callout - may be followed by digits; */
6002 previous_callout
= code
; /* Save for later completion */
6003 after_manual_callout
= 1; /* Skip one item before completing */
6004 *code
++ = OP_CALLOUT
;
6008 while(IS_DIGIT(*ptr
))
6009 n
= n
* 10 + *ptr
++ - CHAR_0
;
6010 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
6012 *errorcodeptr
= ERR39
;
6017 *errorcodeptr
= ERR38
;
6021 PUT(code
, 0, (int)(ptr
- cd
->start_pattern
+ 1)); /* Pattern offset */
6022 PUT(code
, LINK_SIZE
, 0); /* Default length */
6023 code
+= 2 * LINK_SIZE
;
6029 /* ------------------------------------------------------------ */
6030 case CHAR_P
: /* Python-style named subpattern handling */
6031 if (*(++ptr
) == CHAR_EQUALS_SIGN
||
6032 *ptr
== CHAR_GREATER_THAN_SIGN
) /* Reference or recursion */
6034 is_recurse
= *ptr
== CHAR_GREATER_THAN_SIGN
;
6035 terminator
= CHAR_RIGHT_PARENTHESIS
;
6036 goto NAMED_REF_OR_RECURSE
;
6038 else if (*ptr
!= CHAR_LESS_THAN_SIGN
) /* Test for Python-style defn */
6040 *errorcodeptr
= ERR41
;
6043 /* Fall through to handle (?P< as (?< is handled */
6046 /* ------------------------------------------------------------ */
6047 DEFINE_NAME
: /* Come here from (?< handling */
6048 case CHAR_APOSTROPHE
:
6050 terminator
= (*ptr
== CHAR_LESS_THAN_SIGN
)?
6051 CHAR_GREATER_THAN_SIGN
: CHAR_APOSTROPHE
;
6054 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0) ptr
++;
6055 namelen
= (int)(ptr
- name
);
6057 /* In the pre-compile phase, just do a syntax check. */
6059 if (lengthptr
!= NULL
)
6061 if (*ptr
!= terminator
)
6063 *errorcodeptr
= ERR42
;
6066 if (cd
->names_found
>= MAX_NAME_COUNT
)
6068 *errorcodeptr
= ERR49
;
6071 if (namelen
+ IMM2_SIZE
+ 1 > cd
->name_entry_size
)
6073 cd
->name_entry_size
= namelen
+ IMM2_SIZE
+ 1;
6074 if (namelen
> MAX_NAME_SIZE
)
6076 *errorcodeptr
= ERR48
;
6082 /* In the real compile, create the entry in the table, maintaining
6083 alphabetical order. Duplicate names for different numbers are
6084 permitted only if PCRE_DUPNAMES is set. Duplicate names for the same
6085 number are always OK. (An existing number can be re-used if (?|
6086 appears in the pattern.) In either event, a duplicate name results in
6087 a duplicate entry in the table, even if the number is the same. This
6088 is because the number of names, and hence the table size, is computed
6089 in the pre-compile, and it affects various numbers and pointers which
6090 would all have to be modified, and the compiled code moved down, if
6091 duplicates with the same number were omitted from the table. This
6092 doesn't seem worth the hassle. However, *different* names for the
6093 same number are not permitted. */
6097 BOOL dupname
= FALSE
;
6098 slot
= cd
->name_table
;
6100 for (i
= 0; i
< cd
->names_found
; i
++)
6102 int crc
= memcmp(name
, slot
+IMM2_SIZE
, IN_UCHARS(namelen
));
6105 if (slot
[IMM2_SIZE
+namelen
] == 0)
6107 if (GET2(slot
, 0) != cd
->bracount
+ 1 &&
6108 (options
& PCRE_DUPNAMES
) == 0)
6110 *errorcodeptr
= ERR43
;
6113 else dupname
= TRUE
;
6115 else crc
= -1; /* Current name is a substring */
6118 /* Make space in the table and break the loop for an earlier
6119 name. For a duplicate or later name, carry on. We do this for
6120 duplicates so that in the simple case (when ?(| is not used) they
6121 are in order of their numbers. */
6125 memmove(slot
+ cd
->name_entry_size
, slot
,
6126 IN_UCHARS((cd
->names_found
- i
) * cd
->name_entry_size
));
6130 /* Continue the loop for a later or duplicate name */
6132 slot
+= cd
->name_entry_size
;
6135 /* For non-duplicate names, check for a duplicate number before
6136 adding the new name. */
6140 pcre_uchar
*cslot
= cd
->name_table
;
6141 for (i
= 0; i
< cd
->names_found
; i
++)
6145 if (GET2(cslot
, 0) == cd
->bracount
+ 1)
6147 *errorcodeptr
= ERR65
;
6152 cslot
+= cd
->name_entry_size
;
6156 PUT2(slot
, 0, cd
->bracount
+ 1);
6157 memcpy(slot
+ IMM2_SIZE
, name
, IN_UCHARS(namelen
));
6158 slot
[IMM2_SIZE
+ namelen
] = 0;
6162 /* In both pre-compile and compile, count the number of names we've
6166 ptr
++; /* Move past > or ' */
6167 goto NUMBERED_GROUP
;
6170 /* ------------------------------------------------------------ */
6171 case CHAR_AMPERSAND
: /* Perl recursion/subroutine syntax */
6172 terminator
= CHAR_RIGHT_PARENTHESIS
;
6176 /* We come here from the Python syntax above that handles both
6177 references (?P=name) and recursion (?P>name), as well as falling
6178 through from the Perl recursion syntax (?&name). We also come here from
6179 the Perl \k<name> or \k'name' back reference syntax and the \k{name}
6180 .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
6182 NAMED_REF_OR_RECURSE
:
6184 while (MAX_255(*ptr
) && (cd
->ctypes
[*ptr
] & ctype_word
) != 0) ptr
++;
6185 namelen
= (int)(ptr
- name
);
6187 /* In the pre-compile phase, do a syntax check. We used to just set
6188 a dummy reference number, because it was not used in the first pass.
6189 However, with the change of recursive back references to be atomic,
6190 we have to look for the number so that this state can be identified, as
6191 otherwise the incorrect length is computed. If it's not a backwards
6192 reference, the dummy number will do. */
6194 if (lengthptr
!= NULL
)
6196 const pcre_uchar
*temp
;
6200 *errorcodeptr
= ERR62
;
6203 if (*ptr
!= terminator
)
6205 *errorcodeptr
= ERR42
;
6208 if (namelen
> MAX_NAME_SIZE
)
6210 *errorcodeptr
= ERR48
;
6214 /* The name table does not exist in the first pass, so we cannot
6215 do a simple search as in the code below. Instead, we have to scan the
6216 pattern to find the number. It is important that we scan it only as
6217 far as we have got because the syntax of named subpatterns has not
6218 been checked for the rest of the pattern, and find_parens() assumes
6219 correct syntax. In any case, it's a waste of resources to scan
6220 further. We stop the scan at the current point by temporarily
6221 adjusting the value of cd->endpattern. */
6223 temp
= cd
->end_pattern
;
6224 cd
->end_pattern
= ptr
;
6225 recno
= find_parens(cd
, name
, namelen
,
6226 (options
& PCRE_EXTENDED
) != 0, utf
);
6227 cd
->end_pattern
= temp
;
6228 if (recno
< 0) recno
= 0; /* Forward ref; set dummy number */
6231 /* In the real compile, seek the name in the table. We check the name
6232 first, and then check that we have reached the end of the name in the
6233 table. That way, if the name that is longer than any in the table,
6234 the comparison will fail without reading beyond the table entry. */
6238 slot
= cd
->name_table
;
6239 for (i
= 0; i
< cd
->names_found
; i
++)
6241 if (STRNCMP_UC_UC(name
, slot
+IMM2_SIZE
, namelen
) == 0 &&
6242 slot
[IMM2_SIZE
+namelen
] == 0)
6244 slot
+= cd
->name_entry_size
;
6247 if (i
< cd
->names_found
) /* Back reference */
6249 recno
= GET2(slot
, 0);
6251 else if ((recno
= /* Forward back reference */
6252 find_parens(cd
, name
, namelen
,
6253 (options
& PCRE_EXTENDED
) != 0, utf
)) <= 0)
6255 *errorcodeptr
= ERR15
;
6260 /* In both phases, we can now go to the code than handles numerical
6261 recursion or backreferences. */
6263 if (is_recurse
) goto HANDLE_RECURSION
;
6264 else goto HANDLE_REFERENCE
;
6267 /* ------------------------------------------------------------ */
6268 case CHAR_R
: /* Recursion */
6269 ptr
++; /* Same as (?0) */
6273 /* ------------------------------------------------------------ */
6274 case CHAR_MINUS
: case CHAR_PLUS
: /* Recursion or subroutine */
6275 case CHAR_0
: case CHAR_1
: case CHAR_2
: case CHAR_3
: case CHAR_4
:
6276 case CHAR_5
: case CHAR_6
: case CHAR_7
: case CHAR_8
: case CHAR_9
:
6278 const pcre_uchar
*called
;
6279 terminator
= CHAR_RIGHT_PARENTHESIS
;
6281 /* Come here from the \g<...> and \g'...' code (Oniguruma
6282 compatibility). However, the syntax has been checked to ensure that
6283 the ... are a (signed) number, so that neither ERR63 nor ERR29 will
6284 be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
6287 HANDLE_NUMERICAL_RECURSION
:
6289 if ((refsign
= *ptr
) == CHAR_PLUS
)
6292 if (!IS_DIGIT(*ptr
))
6294 *errorcodeptr
= ERR63
;
6298 else if (refsign
== CHAR_MINUS
)
6300 if (!IS_DIGIT(ptr
[1]))
6301 goto OTHER_CHAR_AFTER_QUERY
;
6306 while(IS_DIGIT(*ptr
))
6307 recno
= recno
* 10 + *ptr
++ - CHAR_0
;
6309 if (*ptr
!= terminator
)
6311 *errorcodeptr
= ERR29
;
6315 if (refsign
== CHAR_MINUS
)
6319 *errorcodeptr
= ERR58
;
6322 recno
= cd
->bracount
- recno
+ 1;
6325 *errorcodeptr
= ERR15
;
6329 else if (refsign
== CHAR_PLUS
)
6333 *errorcodeptr
= ERR58
;
6336 recno
+= cd
->bracount
;
6339 /* Come here from code above that handles a named recursion */
6344 called
= cd
->start_code
;
6346 /* When we are actually compiling, find the bracket that is being
6347 referenced. Temporarily end the regex in case it doesn't exist before
6348 this point. If we end up with a forward reference, first check that
6349 the bracket does occur later so we can give the error (and position)
6350 now. Then remember this forward reference in the workspace so it can
6351 be filled in at the end. */
6353 if (lengthptr
== NULL
)
6357 called
= PRIV(find_bracket
)(cd
->start_code
, utf
, recno
);
6359 /* Forward reference */
6363 if (find_parens(cd
, NULL
, recno
,
6364 (options
& PCRE_EXTENDED
) != 0, utf
) < 0)
6366 *errorcodeptr
= ERR15
;
6370 /* Fudge the value of "called" so that when it is inserted as an
6371 offset below, what it actually inserted is the reference number
6372 of the group. Then remember the forward reference. */
6374 called
= cd
->start_code
+ recno
;
6375 if (cd
->hwm
>= cd
->start_workspace
+ cd
->workspace_size
-
6376 WORK_SIZE_SAFETY_MARGIN
)
6378 *errorcodeptr
= expand_workspace(cd
);
6379 if (*errorcodeptr
!= 0) goto FAILED
;
6381 PUTINC(cd
->hwm
, 0, (int)(code
+ 1 - cd
->start_code
));
6384 /* If not a forward reference, and the subpattern is still open,
6385 this is a recursive call. We check to see if this is a left
6386 recursion that could loop for ever, and diagnose that case. We
6387 must not, however, do this check if we are in a conditional
6388 subpattern because the condition might be testing for recursion in
6389 a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6390 Forever loops are also detected at runtime, so those that occur in
6391 conditional subpatterns will be picked up then. */
6393 else if (GET(called
, 1) == 0 && cond_depth
<= 0 &&
6394 could_be_empty(called
, code
, bcptr
, utf
, cd
))
6396 *errorcodeptr
= ERR40
;
6401 /* Insert the recursion/subroutine item. It does not have a set first
6402 character (relevant if it is repeated, because it will then be
6403 wrapped with ONCE brackets). */
6406 PUT(code
, 1, (int)(called
- cd
->start_code
));
6407 code
+= 1 + LINK_SIZE
;
6408 groupsetfirstchar
= FALSE
;
6411 /* Can't determine a first byte now */
6413 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
6417 /* ------------------------------------------------------------ */
6418 default: /* Other characters: check option setting */
6419 OTHER_CHAR_AFTER_QUERY
:
6423 while (*ptr
!= CHAR_RIGHT_PARENTHESIS
&& *ptr
!= CHAR_COLON
)
6427 case CHAR_MINUS
: optset
= &unset
; break;
6429 case CHAR_J
: /* Record that it changed in the external options */
6430 *optset
|= PCRE_DUPNAMES
;
6431 cd
->external_flags
|= PCRE_JCHANGED
;
6434 case CHAR_i
: *optset
|= PCRE_CASELESS
; break;
6435 case CHAR_m
: *optset
|= PCRE_MULTILINE
; break;
6436 case CHAR_s
: *optset
|= PCRE_DOTALL
; break;
6437 case CHAR_x
: *optset
|= PCRE_EXTENDED
; break;
6438 case CHAR_U
: *optset
|= PCRE_UNGREEDY
; break;
6439 case CHAR_X
: *optset
|= PCRE_EXTRA
; break;
6441 default: *errorcodeptr
= ERR12
;
6442 ptr
--; /* Correct the offset */
6447 /* Set up the changed option bits, but don't change anything yet. */
6449 newoptions
= (options
| set
) & (~unset
);
6451 /* If the options ended with ')' this is not the start of a nested
6452 group with option changes, so the options change at this level. If this
6453 item is right at the start of the pattern, the options can be
6454 abstracted and made external in the pre-compile phase, and ignored in
6455 the compile phase. This can be helpful when matching -- for instance in
6456 caseless checking of required bytes.
6458 If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are
6459 definitely *not* at the start of the pattern because something has been
6460 compiled. In the pre-compile phase, however, the code pointer can have
6461 that value after the start, because it gets reset as code is discarded
6462 during the pre-compile. However, this can happen only at top level - if
6463 we are within parentheses, the starting BRA will still be present. At
6464 any parenthesis level, the length value can be used to test if anything
6465 has been compiled at that level. Thus, a test for both these conditions
6466 is necessary to ensure we correctly detect the start of the pattern in
6469 If we are not at the pattern start, reset the greedy defaults and the
6470 case value for firstchar and reqchar. */
6472 if (*ptr
== CHAR_RIGHT_PARENTHESIS
)
6474 if (code
== cd
->start_code
+ 1 + LINK_SIZE
&&
6475 (lengthptr
== NULL
|| *lengthptr
== 2 + 2*LINK_SIZE
))
6477 cd
->external_options
= newoptions
;
6481 greedy_default
= ((newoptions
& PCRE_UNGREEDY
) != 0);
6482 greedy_non_default
= greedy_default
^ 1;
6483 req_caseopt
= ((newoptions
& PCRE_CASELESS
) != 0)? REQ_CASELESS
:0;
6486 /* Change options at this level, and pass them back for use
6487 in subsequent branches. */
6489 *optionsptr
= options
= newoptions
;
6490 previous
= NULL
; /* This item can't be repeated */
6491 continue; /* It is complete */
6494 /* If the options ended with ':' we are heading into a nested group
6495 with possible change of options. Such groups are non-capturing and are
6496 not assertions of any kind. All we need to do is skip over the ':';
6497 the newoptions value is handled below. */
6501 } /* End of switch for character following (? */
6502 } /* End of (? handling */
6504 /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
6505 is set, all unadorned brackets become non-capturing and behave like (?:...)
6508 else if ((options
& PCRE_NO_AUTO_CAPTURE
) != 0)
6513 /* Else we have a capturing group. */
6519 PUT2(code
, 1+LINK_SIZE
, cd
->bracount
);
6520 skipbytes
= IMM2_SIZE
;
6523 /* Process nested bracketed regex. Assertions used not to be repeatable,
6524 but this was changed for Perl compatibility, so all kinds can now be
6525 repeated. We copy code into a non-register variable (tempcode) in order to
6526 be able to pass its address because some compilers complain otherwise. */
6528 previous
= code
; /* For handling repetition */
6531 tempreqvary
= cd
->req_varyopt
; /* Save value before bracket */
6532 tempbracount
= cd
->bracount
; /* Save value before bracket */
6533 length_prevgroup
= 0; /* Initialize for pre-compile phase */
6536 newoptions
, /* The complete new option state */
6537 &tempcode
, /* Where to put code (updated) */
6538 &ptr
, /* Input pointer (updated) */
6539 errorcodeptr
, /* Where to put an error message */
6540 (bravalue
== OP_ASSERTBACK
||
6541 bravalue
== OP_ASSERTBACK_NOT
), /* TRUE if back assert */
6542 reset_bracount
, /* True if (?| group */
6543 skipbytes
, /* Skip over bracket number */
6545 ((bravalue
== OP_COND
)?1:0), /* Depth of condition subpatterns */
6546 &subfirstchar
, /* For possible first char */
6547 &subreqchar
, /* For possible last char */
6548 bcptr
, /* Current branch chain */
6549 cd
, /* Tables block */
6550 (lengthptr
== NULL
)? NULL
: /* Actual compile phase */
6551 &length_prevgroup
/* Pre-compile phase */
6555 /* If this was an atomic group and there are no capturing groups within it,
6556 generate OP_ONCE_NC instead of OP_ONCE. */
6558 if (bravalue
== OP_ONCE
&& cd
->bracount
<= tempbracount
)
6561 if (bravalue
>= OP_ASSERT
&& bravalue
<= OP_ASSERTBACK_NOT
)
6562 cd
->assert_depth
-= 1;
6564 /* At the end of compiling, code is still pointing to the start of the
6565 group, while tempcode has been updated to point past the end of the group.
6566 The pattern pointer (ptr) is on the bracket.
6568 If this is a conditional bracket, check that there are no more than
6569 two branches in the group, or just one if it's a DEFINE group. We do this
6570 in the real compile phase, not in the pre-pass, where the whole group may
6571 not be available. */
6573 if (bravalue
== OP_COND
&& lengthptr
== NULL
)
6575 pcre_uchar
*tc
= code
;
6582 while (*tc
!= OP_KET
);
6584 /* A DEFINE group is never obeyed inline (the "condition" is always
6585 false). It must have only one branch. */
6587 if (code
[LINK_SIZE
+1] == OP_DEF
)
6591 *errorcodeptr
= ERR54
;
6594 bravalue
= OP_DEF
; /* Just a flag to suppress char handling below */
6597 /* A "normal" conditional group. If there is just one branch, we must not
6598 make use of its firstchar or reqchar, because this is equivalent to an
6599 empty second branch. */
6605 *errorcodeptr
= ERR27
;
6608 if (condcount
== 1) subfirstchar
= subreqchar
= REQ_NONE
;
6612 /* Error if hit end of pattern */
6614 if (*ptr
!= CHAR_RIGHT_PARENTHESIS
)
6616 *errorcodeptr
= ERR14
;
6620 /* In the pre-compile phase, update the length by the length of the group,
6621 less the brackets at either end. Then reduce the compiled code to just a
6622 set of non-capturing brackets so that it doesn't use much memory if it is
6623 duplicated by a quantifier.*/
6625 if (lengthptr
!= NULL
)
6627 if (OFLOW_MAX
- *lengthptr
< length_prevgroup
- 2 - 2*LINK_SIZE
)
6629 *errorcodeptr
= ERR20
;
6632 *lengthptr
+= length_prevgroup
- 2 - 2*LINK_SIZE
;
6633 code
++; /* This already contains bravalue */
6634 PUTINC(code
, 0, 1 + LINK_SIZE
);
6636 PUTINC(code
, 0, 1 + LINK_SIZE
);
6637 break; /* No need to waste time with special character handling */
6640 /* Otherwise update the main code pointer to the end of the group. */
6644 /* For a DEFINE group, required and first character settings are not
6647 if (bravalue
== OP_DEF
) break;
6649 /* Handle updating of the required and first characters for other types of
6650 group. Update for normal brackets of all kinds, and conditions with two
6651 branches (see code above). If the bracket is followed by a quantifier with
6652 zero repeat, we have to back off. Hence the definition of zeroreqchar and
6653 zerofirstchar outside the main loop so that they can be accessed for the
6656 zeroreqchar
= reqchar
;
6657 zerofirstchar
= firstchar
;
6658 groupsetfirstchar
= FALSE
;
6660 if (bravalue
>= OP_ONCE
)
6662 /* If we have not yet set a firstchar in this branch, take it from the
6663 subpattern, remembering that it was set here so that a repeat of more
6664 than one can replicate it as reqchar if necessary. If the subpattern has
6665 no firstchar, set "none" for the whole branch. In both cases, a zero
6666 repeat forces firstchar to "none". */
6668 if (firstchar
== REQ_UNSET
)
6670 if (subfirstchar
>= 0)
6672 firstchar
= subfirstchar
;
6673 groupsetfirstchar
= TRUE
;
6675 else firstchar
= REQ_NONE
;
6676 zerofirstchar
= REQ_NONE
;
6679 /* If firstchar was previously set, convert the subpattern's firstchar
6680 into reqchar if there wasn't one, using the vary flag that was in
6681 existence beforehand. */
6683 else if (subfirstchar
>= 0 && subreqchar
< 0)
6684 subreqchar
= subfirstchar
| tempreqvary
;
6686 /* If the subpattern set a required byte (or set a first byte that isn't
6687 really the first byte - see above), set it. */
6689 if (subreqchar
>= 0) reqchar
= subreqchar
;
6692 /* For a forward assertion, we take the reqchar, if set. This can be
6693 helpful if the pattern that follows the assertion doesn't set a different
6694 char. For example, it's useful for /(?=abcde).+/. We can't set firstchar
6695 for an assertion, however because it leads to incorrect effect for patterns
6696 such as /(?=a)a.+/ when the "real" "a" would then become a reqchar instead
6697 of a firstchar. This is overcome by a scan at the end if there's no
6698 firstchar, looking for an asserted first char. */
6700 else if (bravalue
== OP_ASSERT
&& subreqchar
>= 0) reqchar
= subreqchar
;
6701 break; /* End of processing '(' */
6704 /* ===================================================================*/
6705 /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
6706 are arranged to be the negation of the corresponding OP_values in the
6707 default case when PCRE_UCP is not set. For the back references, the values
6708 are ESC_REF plus the reference number. Only back references and those types
6709 that consume a character may be repeated. We can test for values between
6710 ESC_b and ESC_Z for the latter; this may have to change if any new ones are
6713 case CHAR_BACKSLASH
:
6715 c
= check_escape(&ptr
, errorcodeptr
, cd
->bracount
, options
, FALSE
);
6716 if (*errorcodeptr
!= 0) goto FAILED
;
6720 if (-c
== ESC_Q
) /* Handle start of quoted string */
6722 if (ptr
[1] == CHAR_BACKSLASH
&& ptr
[2] == CHAR_E
)
6723 ptr
+= 2; /* avoid empty string */
6728 if (-c
== ESC_E
) continue; /* Perl ignores an orphan \E */
6730 /* For metasequences that actually match a character, we disable the
6731 setting of a first character if it hasn't already been set. */
6733 if (firstchar
== REQ_UNSET
&& -c
> ESC_b
&& -c
< ESC_Z
)
6734 firstchar
= REQ_NONE
;
6736 /* Set values to reset to if this is followed by a zero repeat. */
6738 zerofirstchar
= firstchar
;
6739 zeroreqchar
= reqchar
;
6741 /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
6742 is a subroutine call by number (Oniguruma syntax). In fact, the value
6743 -ESC_g is returned only for these cases. So we don't need to check for <
6744 or ' if the value is -ESC_g. For the Perl syntax \g{n} the value is
6745 -ESC_REF+n, and for the Perl syntax \g{name} the result is -ESC_k (as
6746 that is a synonym for a named back reference). */
6750 const pcre_uchar
*p
;
6751 save_hwm
= cd
->hwm
; /* Normally this is set when '(' is read */
6752 terminator
= (*(++ptr
) == CHAR_LESS_THAN_SIGN
)?
6753 CHAR_GREATER_THAN_SIGN
: CHAR_APOSTROPHE
;
6755 /* These two statements stop the compiler for warning about possibly
6756 unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
6757 fact, because we actually check for a number below, the paths that
6758 would actually be in error are never taken. */
6761 reset_bracount
= FALSE
;
6763 /* Test for a name */
6765 if (ptr
[1] != CHAR_PLUS
&& ptr
[1] != CHAR_MINUS
)
6767 BOOL is_a_number
= TRUE
;
6768 for (p
= ptr
+ 1; *p
!= 0 && *p
!= terminator
; p
++)
6770 if (!MAX_255(*p
)) { is_a_number
= FALSE
; break; }
6771 if ((cd
->ctypes
[*p
] & ctype_digit
) == 0) is_a_number
= FALSE
;
6772 if ((cd
->ctypes
[*p
] & ctype_word
) == 0) break;
6774 if (*p
!= terminator
)
6776 *errorcodeptr
= ERR57
;
6782 goto HANDLE_NUMERICAL_RECURSION
;
6785 goto NAMED_REF_OR_RECURSE
;
6788 /* Test a signed number in angle brackets or quotes. */
6791 while (IS_DIGIT(*p
)) p
++;
6792 if (*p
!= terminator
)
6794 *errorcodeptr
= ERR57
;
6798 goto HANDLE_NUMERICAL_RECURSION
;
6801 /* \k<name> or \k'name' is a back reference by name (Perl syntax).
6802 We also support \k{name} (.NET syntax). */
6806 if ((ptr
[1] != CHAR_LESS_THAN_SIGN
&&
6807 ptr
[1] != CHAR_APOSTROPHE
&& ptr
[1] != CHAR_LEFT_CURLY_BRACKET
))
6809 *errorcodeptr
= ERR69
;
6813 terminator
= (*(++ptr
) == CHAR_LESS_THAN_SIGN
)?
6814 CHAR_GREATER_THAN_SIGN
: (*ptr
== CHAR_APOSTROPHE
)?
6815 CHAR_APOSTROPHE
: CHAR_RIGHT_CURLY_BRACKET
;
6816 goto NAMED_REF_OR_RECURSE
;
6819 /* Back references are handled specially; must disable firstchar if
6820 not set to cope with cases like (?=(\w+))\1: which would otherwise set
6826 recno
= -c
- ESC_REF
;
6828 HANDLE_REFERENCE
: /* Come here from named backref handling */
6829 if (firstchar
== REQ_UNSET
) firstchar
= REQ_NONE
;
6831 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_REFI
: OP_REF
;
6832 PUT2INC(code
, 0, recno
);
6833 cd
->backref_map
|= (recno
< 32)? (1 << recno
) : 1;
6834 if (recno
> cd
->top_backref
) cd
->top_backref
= recno
;
6836 /* Check to see if this back reference is recursive, that it, it
6837 is inside the group that it references. A flag is set so that the
6838 group can be made atomic. */
6840 for (oc
= cd
->open_caps
; oc
!= NULL
; oc
= oc
->next
)
6842 if (oc
->number
== recno
)
6850 /* So are Unicode property matches, if supported. */
6853 else if (-c
== ESC_P
|| -c
== ESC_p
)
6857 int ptype
= get_ucp(&ptr
, &negated
, &pdata
, errorcodeptr
);
6858 if (ptype
< 0) goto FAILED
;
6860 *code
++ = ((-c
== ESC_p
) != negated
)? OP_PROP
: OP_NOTPROP
;
6866 /* If Unicode properties are not supported, \X, \P, and \p are not
6869 else if (-c
== ESC_X
|| -c
== ESC_P
|| -c
== ESC_p
)
6871 *errorcodeptr
= ERR45
;
6876 /* For the rest (including \X when Unicode properties are supported), we
6877 can obtain the OP value by negating the escape value in the default
6878 situation when PCRE_UCP is not set. When it *is* set, we substitute
6879 Unicode property tests. Note that \b and \B do a one-character
6884 if ((-c
== ESC_b
|| -c
== ESC_B
) && cd
->max_lookbehind
== 0)
6885 cd
->max_lookbehind
= 1;
6887 if (-c
>= ESC_DU
&& -c
<= ESC_wu
)
6889 nestptr
= ptr
+ 1; /* Where to resume */
6890 ptr
= substitutes
[-c
- ESC_DU
] - 1; /* Just before substitute */
6894 /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
6895 so that it works in DFA mode and in lookbehinds. */
6898 previous
= (-c
> ESC_b
&& -c
< ESC_Z
)? code
: NULL
;
6899 *code
++ = (!utf
&& c
== -ESC_C
)? OP_ALLANY
: -c
;
6905 /* We have a data character whose value is in c. In UTF-8 mode it may have
6906 a value > 127. We set its representation in the length/buffer, and then
6907 handle it as a data character. */
6910 if (utf
&& c
> MAX_VALUE_FOR_SINGLE_CHAR
)
6911 mclength
= PRIV(ord2utf
)(c
, mcbuffer
);
6922 /* ===================================================================*/
6923 /* Handle a literal character. It is guaranteed not to be whitespace or #
6924 when the extended flag is set. If we are in UTF-8 mode, it may be a
6925 multi-byte literal character. */
6933 if (utf
&& HAS_EXTRALEN(c
))
6934 ACROSSCHAR(TRUE
, ptr
[1], mcbuffer
[mclength
++] = *(++ptr
));
6937 /* At this point we have the character's bytes in mcbuffer, and the length
6938 in mclength. When not in UTF-8 mode, the length is always 1. */
6942 *code
++ = ((options
& PCRE_CASELESS
) != 0)? OP_CHARI
: OP_CHAR
;
6943 for (c
= 0; c
< mclength
; c
++) *code
++ = mcbuffer
[c
];
6945 /* Remember if \r or \n were seen */
6947 if (mcbuffer
[0] == CHAR_CR
|| mcbuffer
[0] == CHAR_NL
)
6948 cd
->external_flags
|= PCRE_HASCRORLF
;
6950 /* Set the first and required bytes appropriately. If no previous first
6951 byte, set it from this character, but revert to none on a zero repeat.
6952 Otherwise, leave the firstchar value alone, and don't change it on a zero
6955 if (firstchar
== REQ_UNSET
)
6957 zerofirstchar
= REQ_NONE
;
6958 zeroreqchar
= reqchar
;
6960 /* If the character is more than one byte long, we can set firstchar
6961 only if it is not to be matched caselessly. */
6963 if (mclength
== 1 || req_caseopt
== 0)
6965 firstchar
= mcbuffer
[0] | req_caseopt
;
6966 if (mclength
!= 1) reqchar
= code
[-1] | cd
->req_varyopt
;
6968 else firstchar
= reqchar
= REQ_NONE
;
6971 /* firstchar was previously set; we can set reqchar only if the length is
6972 1 or the matching is caseful. */
6976 zerofirstchar
= firstchar
;
6977 zeroreqchar
= reqchar
;
6978 if (mclength
== 1 || req_caseopt
== 0)
6979 reqchar
= code
[-1] | req_caseopt
| cd
->req_varyopt
;
6982 break; /* End of literal character handling */
6984 } /* end of big loop */
6987 /* Control never reaches here by falling through, only by a goto for all the
6988 error states. Pass back the position in the pattern so that it can be displayed
6989 to the user for diagnosing the error. */
6999 /*************************************************
7000 * Compile sequence of alternatives *
7001 *************************************************/
7003 /* On entry, ptr is pointing past the bracket character, but on return it
7004 points to the closing bracket, or vertical bar, or end of string. The code
7005 variable is pointing at the byte into which the BRA operator has been stored.
7006 This function is used during the pre-compile phase when we are trying to find
7007 out the amount of memory needed, as well as during the real compile phase. The
7008 value of lengthptr distinguishes the two phases.
7011 options option bits, including any changes for this subpattern
7012 codeptr -> the address of the current code pointer
7013 ptrptr -> the address of the current pattern pointer
7014 errorcodeptr -> pointer to error code variable
7015 lookbehind TRUE if this is a lookbehind assertion
7016 reset_bracount TRUE to reset the count for each branch
7017 skipbytes skip this many bytes at start (for brackets and OP_COND)
7018 cond_depth depth of nesting for conditional subpatterns
7019 firstcharptr place to put the first required character, or a negative number
7020 reqcharptr place to put the last required character, or a negative number
7021 bcptr pointer to the chain of currently open branches
7022 cd points to the data block with tables pointers etc.
7023 lengthptr NULL during the real compile phase
7024 points to length accumulator during pre-compile phase
7026 Returns: TRUE on success
7030 compile_regex(int options
, pcre_uchar
**codeptr
, const pcre_uchar
**ptrptr
,
7031 int *errorcodeptr
, BOOL lookbehind
, BOOL reset_bracount
, int skipbytes
,
7032 int cond_depth
, pcre_int32
*firstcharptr
, pcre_int32
*reqcharptr
,
7033 branch_chain
*bcptr
, compile_data
*cd
, int *lengthptr
)
7035 const pcre_uchar
*ptr
= *ptrptr
;
7036 pcre_uchar
*code
= *codeptr
;
7037 pcre_uchar
*last_branch
= code
;
7038 pcre_uchar
*start_bracket
= code
;
7039 pcre_uchar
*reverse_count
= NULL
;
7040 open_capitem capitem
;
7042 pcre_int32 firstchar
, reqchar
;
7043 pcre_int32 branchfirstchar
, branchreqchar
;
7050 bc
.current_branch
= code
;
7052 firstchar
= reqchar
= REQ_UNSET
;
7054 /* Accumulate the length for use in the pre-compile phase. Start with the
7055 length of the BRA and KET and any extra bytes that are required at the
7056 beginning. We accumulate in a local variable to save frequent testing of
7057 lenthptr for NULL. We cannot do this by looking at the value of code at the
7058 start and end of each alternative, because compiled items are discarded during
7059 the pre-compile phase so that the work space is not exceeded. */
7061 length
= 2 + 2*LINK_SIZE
+ skipbytes
;
7063 /* WARNING: If the above line is changed for any reason, you must also change
7064 the code that abstracts option settings at the start of the pattern and makes
7065 them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
7066 pre-compile phase to find out whether anything has yet been compiled or not. */
7068 /* If this is a capturing subpattern, add to the chain of open capturing items
7069 so that we can detect them if (*ACCEPT) is encountered. This is also used to
7070 detect groups that contain recursive back references to themselves. Note that
7071 only OP_CBRA need be tested here; changing this opcode to one of its variants,
7072 e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
7074 if (*code
== OP_CBRA
)
7076 capnumber
= GET2(code
, 1 + LINK_SIZE
);
7077 capitem
.number
= capnumber
;
7078 capitem
.next
= cd
->open_caps
;
7079 capitem
.flag
= FALSE
;
7080 cd
->open_caps
= &capitem
;
7083 /* Offset is set zero to mark that this bracket is still open */
7086 code
+= 1 + LINK_SIZE
+ skipbytes
;
7088 /* Loop for each alternative branch */
7090 orig_bracount
= max_bracount
= cd
->bracount
;
7093 /* For a (?| group, reset the capturing bracket count so that each branch
7094 uses the same numbers. */
7096 if (reset_bracount
) cd
->bracount
= orig_bracount
;
7098 /* Set up dummy OP_REVERSE if lookbehind assertion */
7102 *code
++ = OP_REVERSE
;
7103 reverse_count
= code
;
7105 length
+= 1 + LINK_SIZE
;
7108 /* Now compile the branch; in the pre-compile phase its length gets added
7111 if (!compile_branch(&options
, &code
, &ptr
, errorcodeptr
, &branchfirstchar
,
7112 &branchreqchar
, &bc
, cond_depth
, cd
,
7113 (lengthptr
== NULL
)? NULL
: &length
))
7119 /* Keep the highest bracket count in case (?| was used and some branch
7120 has fewer than the rest. */
7122 if (cd
->bracount
> max_bracount
) max_bracount
= cd
->bracount
;
7124 /* In the real compile phase, there is some post-processing to be done. */
7126 if (lengthptr
== NULL
)
7128 /* If this is the first branch, the firstchar and reqchar values for the
7129 branch become the values for the regex. */
7131 if (*last_branch
!= OP_ALT
)
7133 firstchar
= branchfirstchar
;
7134 reqchar
= branchreqchar
;
7137 /* If this is not the first branch, the first char and reqchar have to
7138 match the values from all the previous branches, except that if the
7139 previous value for reqchar didn't have REQ_VARY set, it can still match,
7140 and we set REQ_VARY for the regex. */
7144 /* If we previously had a firstchar, but it doesn't match the new branch,
7145 we have to abandon the firstchar for the regex, but if there was
7146 previously no reqchar, it takes on the value of the old firstchar. */
7148 if (firstchar
>= 0 && firstchar
!= branchfirstchar
)
7150 if (reqchar
< 0) reqchar
= firstchar
;
7151 firstchar
= REQ_NONE
;
7154 /* If we (now or from before) have no firstchar, a firstchar from the
7155 branch becomes a reqchar if there isn't a branch reqchar. */
7157 if (firstchar
< 0 && branchfirstchar
>= 0 && branchreqchar
< 0)
7158 branchreqchar
= branchfirstchar
;
7160 /* Now ensure that the reqchars match */
7162 if ((reqchar
& ~REQ_VARY
) != (branchreqchar
& ~REQ_VARY
))
7164 else reqchar
|= branchreqchar
; /* To "or" REQ_VARY */
7167 /* If lookbehind, check that this branch matches a fixed-length string, and
7168 put the length into the OP_REVERSE item. Temporarily mark the end of the
7169 branch with OP_END. If the branch contains OP_RECURSE, the result is -3
7170 because there may be forward references that we can't check here. Set a
7171 flag to cause another lookbehind check at the end. Why not do it all at the
7172 end? Because common, erroneous checks are picked up here and the offset of
7173 the problem can be shown. */
7179 fixed_length
= find_fixedlength(last_branch
, (options
& PCRE_UTF8
) != 0,
7181 DPRINTF(("fixed length = %d\n", fixed_length
));
7182 if (fixed_length
== -3)
7184 cd
->check_lookbehind
= TRUE
;
7186 else if (fixed_length
< 0)
7188 *errorcodeptr
= (fixed_length
== -2)? ERR36
:
7189 (fixed_length
== -4)? ERR70
: ERR25
;
7195 if (fixed_length
> cd
->max_lookbehind
)
7196 cd
->max_lookbehind
= fixed_length
;
7197 PUT(reverse_count
, 0, fixed_length
);
7202 /* Reached end of expression, either ')' or end of pattern. In the real
7203 compile phase, go back through the alternative branches and reverse the chain
7204 of offsets, with the field in the BRA item now becoming an offset to the
7205 first alternative. If there are no alternatives, it points to the end of the
7206 group. The length in the terminating ket is always the length of the whole
7207 bracketed item. Return leaving the pointer at the terminating char. */
7209 if (*ptr
!= CHAR_VERTICAL_LINE
)
7211 if (lengthptr
== NULL
)
7213 int branch_length
= (int)(code
- last_branch
);
7216 int prev_length
= GET(last_branch
, 1);
7217 PUT(last_branch
, 1, branch_length
);
7218 branch_length
= prev_length
;
7219 last_branch
-= branch_length
;
7221 while (branch_length
> 0);
7224 /* Fill in the ket */
7227 PUT(code
, 1, (int)(code
- start_bracket
));
7228 code
+= 1 + LINK_SIZE
;
7230 /* If it was a capturing subpattern, check to see if it contained any
7231 recursive back references. If so, we must wrap it in atomic brackets.
7232 In any event, remove the block from the chain. */
7236 if (cd
->open_caps
->flag
)
7238 memmove(start_bracket
+ 1 + LINK_SIZE
, start_bracket
,
7239 IN_UCHARS(code
- start_bracket
));
7240 *start_bracket
= OP_ONCE
;
7241 code
+= 1 + LINK_SIZE
;
7242 PUT(start_bracket
, 1, (int)(code
- start_bracket
));
7244 PUT(code
, 1, (int)(code
- start_bracket
));
7245 code
+= 1 + LINK_SIZE
;
7246 length
+= 2 + 2*LINK_SIZE
;
7248 cd
->open_caps
= cd
->open_caps
->next
;
7251 /* Retain the highest bracket number, in case resetting was used. */
7253 cd
->bracount
= max_bracount
;
7255 /* Set values to pass back */
7259 *firstcharptr
= firstchar
;
7260 *reqcharptr
= reqchar
;
7261 if (lengthptr
!= NULL
)
7263 if (OFLOW_MAX
- *lengthptr
< length
)
7265 *errorcodeptr
= ERR20
;
7268 *lengthptr
+= length
;
7273 /* Another branch follows. In the pre-compile phase, we can move the code
7274 pointer back to where it was for the start of the first branch. (That is,
7275 pretend that each branch is the only one.)
7277 In the real compile phase, insert an ALT node. Its length field points back
7278 to the previous branch while the bracket remains open. At the end the chain
7279 is reversed. It's done like this so that the start of the bracket has a
7280 zero offset until it is closed, making it possible to detect recursion. */
7282 if (lengthptr
!= NULL
)
7284 code
= *codeptr
+ 1 + LINK_SIZE
+ skipbytes
;
7285 length
+= 1 + LINK_SIZE
;
7290 PUT(code
, 1, (int)(code
- last_branch
));
7291 bc
.current_branch
= last_branch
= code
;
7292 code
+= 1 + LINK_SIZE
;
7297 /* Control never reaches here */
7303 /*************************************************
7304 * Check for anchored expression *
7305 *************************************************/
7307 /* Try to find out if this is an anchored regular expression. Consider each
7308 alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
7309 all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
7310 it's anchored. However, if this is a multiline pattern, then only OP_SOD will
7311 be found, because ^ generates OP_CIRCM in that mode.
7313 We can also consider a regex to be anchored if OP_SOM starts all its branches.
7314 This is the code for \G, which means "match at start of match position, taking
7315 into account the match offset".
7317 A branch is also implicitly anchored if it starts with .* and DOTALL is set,
7318 because that will try the rest of the pattern at all possible matching points,
7319 so there is no point trying again.... er ....
7321 .... except when the .* appears inside capturing parentheses, and there is a
7322 subsequent back reference to those parentheses. We haven't enough information
7323 to catch that case precisely.
7325 At first, the best we could do was to detect when .* was in capturing brackets
7326 and the highest back reference was greater than or equal to that level.
7327 However, by keeping a bitmap of the first 31 back references, we can catch some
7328 of the more common cases more precisely.
7331 code points to start of expression (the bracket)
7332 bracket_map a bitmap of which brackets we are inside while testing; this
7333 handles up to substring 31; after that we just have to take
7334 the less precise approach
7335 backref_map the back reference bitmap
7337 Returns: TRUE or FALSE
7341 is_anchored(const pcre_uchar
*code
, unsigned int bracket_map
,
7342 unsigned int backref_map
)
7345 const pcre_uchar
*scode
= first_significant_code(
7346 code
+ PRIV(OP_lengths
)[*code
], FALSE
);
7349 /* Non-capturing brackets */
7351 if (op
== OP_BRA
|| op
== OP_BRAPOS
||
7352 op
== OP_SBRA
|| op
== OP_SBRAPOS
)
7354 if (!is_anchored(scode
, bracket_map
, backref_map
)) return FALSE
;
7357 /* Capturing brackets */
7359 else if (op
== OP_CBRA
|| op
== OP_CBRAPOS
||
7360 op
== OP_SCBRA
|| op
== OP_SCBRAPOS
)
7362 int n
= GET2(scode
, 1+LINK_SIZE
);
7363 int new_map
= bracket_map
| ((n
< 32)? (1 << n
) : 1);
7364 if (!is_anchored(scode
, new_map
, backref_map
)) return FALSE
;
7367 /* Other brackets */
7369 else if (op
== OP_ASSERT
|| op
== OP_ONCE
|| op
== OP_ONCE_NC
||
7372 if (!is_anchored(scode
, bracket_map
, backref_map
)) return FALSE
;
7375 /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
7376 it isn't in brackets that are or may be referenced. */
7378 else if ((op
== OP_TYPESTAR
|| op
== OP_TYPEMINSTAR
||
7379 op
== OP_TYPEPOSSTAR
))
7381 if (scode
[1] != OP_ALLANY
|| (bracket_map
& backref_map
) != 0)
7385 /* Check for explicit anchoring */
7387 else if (op
!= OP_SOD
&& op
!= OP_SOM
&& op
!= OP_CIRC
) return FALSE
;
7388 code
+= GET(code
, 1);
7390 while (*code
== OP_ALT
); /* Loop for each alternative */
7396 /*************************************************
7397 * Check for starting with ^ or .* *
7398 *************************************************/
7400 /* This is called to find out if every branch starts with ^ or .* so that
7401 "first char" processing can be done to speed things up in multiline
7402 matching and for non-DOTALL patterns that start with .* (which must start at
7403 the beginning or after \n). As in the case of is_anchored() (see above), we
7404 have to take account of back references to capturing brackets that contain .*
7405 because in that case we can't make the assumption.
7408 code points to start of expression (the bracket)
7409 bracket_map a bitmap of which brackets we are inside while testing; this
7410 handles up to substring 31; after that we just have to take
7411 the less precise approach
7412 backref_map the back reference bitmap
7414 Returns: TRUE or FALSE
7418 is_startline(const pcre_uchar
*code
, unsigned int bracket_map
,
7419 unsigned int backref_map
)
7422 const pcre_uchar
*scode
= first_significant_code(
7423 code
+ PRIV(OP_lengths
)[*code
], FALSE
);
7426 /* If we are at the start of a conditional assertion group, *both* the
7427 conditional assertion *and* what follows the condition must satisfy the test
7428 for start of line. Other kinds of condition fail. Note that there may be an
7429 auto-callout at the start of a condition. */
7433 scode
+= 1 + LINK_SIZE
;
7434 if (*scode
== OP_CALLOUT
) scode
+= PRIV(OP_lengths
)[OP_CALLOUT
];
7444 default: /* Assertion */
7445 if (!is_startline(scode
, bracket_map
, backref_map
)) return FALSE
;
7446 do scode
+= GET(scode
, 1); while (*scode
== OP_ALT
);
7447 scode
+= 1 + LINK_SIZE
;
7450 scode
= first_significant_code(scode
, FALSE
);
7454 /* Non-capturing brackets */
7456 if (op
== OP_BRA
|| op
== OP_BRAPOS
||
7457 op
== OP_SBRA
|| op
== OP_SBRAPOS
)
7459 if (!is_startline(scode
, bracket_map
, backref_map
)) return FALSE
;
7462 /* Capturing brackets */
7464 else if (op
== OP_CBRA
|| op
== OP_CBRAPOS
||
7465 op
== OP_SCBRA
|| op
== OP_SCBRAPOS
)
7467 int n
= GET2(scode
, 1+LINK_SIZE
);
7468 int new_map
= bracket_map
| ((n
< 32)? (1 << n
) : 1);
7469 if (!is_startline(scode
, new_map
, backref_map
)) return FALSE
;
7472 /* Other brackets */
7474 else if (op
== OP_ASSERT
|| op
== OP_ONCE
|| op
== OP_ONCE_NC
)
7476 if (!is_startline(scode
, bracket_map
, backref_map
)) return FALSE
;
7479 /* .* means "start at start or after \n" if it isn't in brackets that
7480 may be referenced. */
7482 else if (op
== OP_TYPESTAR
|| op
== OP_TYPEMINSTAR
|| op
== OP_TYPEPOSSTAR
)
7484 if (scode
[1] != OP_ANY
|| (bracket_map
& backref_map
) != 0) return FALSE
;
7487 /* Check for explicit circumflex */
7489 else if (op
!= OP_CIRC
&& op
!= OP_CIRCM
) return FALSE
;
7491 /* Move on to the next alternative */
7493 code
+= GET(code
, 1);
7495 while (*code
== OP_ALT
); /* Loop for each alternative */
7501 /*************************************************
7502 * Check for asserted fixed first char *
7503 *************************************************/
7505 /* During compilation, the "first char" settings from forward assertions are
7506 discarded, because they can cause conflicts with actual literals that follow.
7507 However, if we end up without a first char setting for an unanchored pattern,
7508 it is worth scanning the regex to see if there is an initial asserted first
7509 char. If all branches start with the same asserted char, or with a bracket all
7510 of whose alternatives start with the same asserted char (recurse ad lib), then
7511 we return that char, otherwise -1.
7514 code points to start of expression (the bracket)
7515 inassert TRUE if in an assertion
7517 Returns: -1 or the fixed first char
7521 find_firstassertedchar(const pcre_uchar
*code
, BOOL inassert
)
7526 int xl
= (*code
== OP_CBRA
|| *code
== OP_SCBRA
||
7527 *code
== OP_CBRAPOS
|| *code
== OP_SCBRAPOS
)? IMM2_SIZE
:0;
7528 const pcre_uchar
*scode
= first_significant_code(code
+ 1+LINK_SIZE
+ xl
,
7547 if ((d
= find_firstassertedchar(scode
, op
== OP_ASSERT
)) < 0)
7549 if (c
< 0) c
= d
; else if (c
!= d
) return -1;
7560 if (!inassert
) return -1;
7561 if (c
< 0) c
= scode
[1];
7562 else if (c
!= scode
[1]) return -1;
7573 if (!inassert
) return -1;
7574 if (c
< 0) c
= scode
[1] | REQ_CASELESS
;
7575 else if (c
!= scode
[1]) return -1;
7579 code
+= GET(code
, 1);
7581 while (*code
== OP_ALT
);
7587 /*************************************************
7588 * Compile a Regular Expression *
7589 *************************************************/
7591 /* This function takes a string and returns a pointer to a block of store
7592 holding a compiled version of the expression. The original API for this
7593 function had no error code return variable; it is retained for backwards
7594 compatibility. The new function is given a new name.
7597 pattern the regular expression
7598 options various option bits
7599 errorcodeptr pointer to error code variable (pcre_compile2() only)
7600 can be NULL if you don't want a code value
7601 errorptr pointer to pointer to error text
7602 erroroffset ptr offset in pattern where error was detected
7603 tables pointer to character tables or NULL
7605 Returns: pointer to compiled data block, or NULL on error,
7606 with errorptr and erroroffset set
7609 #ifdef COMPILE_PCRE8
7610 PCRE_EXP_DEFN pcre
* PCRE_CALL_CONVENTION
7611 pcre_compile(const char *pattern
, int options
, const char **errorptr
,
7612 int *erroroffset
, const unsigned char *tables
)
7614 PCRE_EXP_DEFN pcre16
* PCRE_CALL_CONVENTION
7615 pcre16_compile(PCRE_SPTR16 pattern
, int options
, const char **errorptr
,
7616 int *erroroffset
, const unsigned char *tables
)
7619 #ifdef COMPILE_PCRE8
7620 return pcre_compile2(pattern
, options
, NULL
, errorptr
, erroroffset
, tables
);
7622 return pcre16_compile2(pattern
, options
, NULL
, errorptr
, erroroffset
, tables
);
7627 #ifdef COMPILE_PCRE8
7628 PCRE_EXP_DEFN pcre
* PCRE_CALL_CONVENTION
7629 pcre_compile2(const char *pattern
, int options
, int *errorcodeptr
,
7630 const char **errorptr
, int *erroroffset
, const unsigned char *tables
)
7632 PCRE_EXP_DEFN pcre16
* PCRE_CALL_CONVENTION
7633 pcre16_compile2(PCRE_SPTR16 pattern
, int options
, int *errorcodeptr
,
7634 const char **errorptr
, int *erroroffset
, const unsigned char *tables
)
7638 int length
= 1; /* For final END opcode */
7639 pcre_int32 firstchar
, reqchar
;
7642 int skipatstart
= 0;
7646 const pcre_uchar
*codestart
;
7647 const pcre_uchar
*ptr
;
7648 compile_data compile_block
;
7649 compile_data
*cd
= &compile_block
;
7651 /* This space is used for "compiling" into during the first phase, when we are
7652 computing the amount of memory that is needed. Compiled items are thrown away
7653 as soon as possible, so that a fairly large buffer should be sufficient for
7654 this purpose. The same space is used in the second phase for remembering where
7655 to fill in forward references to subpatterns. That may overflow, in which case
7656 new memory is obtained from malloc(). */
7658 pcre_uchar cworkspace
[COMPILE_WORK_SIZE
];
7660 /* Set this early so that early errors get offset 0. */
7662 ptr
= (const pcre_uchar
*)pattern
;
7664 /* We can't pass back an error message if errorptr is NULL; I guess the best we
7665 can do is just return NULL, but we can set a code value if there is a code
7668 if (errorptr
== NULL
)
7670 if (errorcodeptr
!= NULL
) *errorcodeptr
= 99;
7675 if (errorcodeptr
!= NULL
) *errorcodeptr
= ERR0
;
7677 /* However, we can give a message for this error */
7679 if (erroroffset
== NULL
)
7682 goto PCRE_EARLY_ERROR_RETURN2
;
7687 /* Set up pointers to the individual character tables */
7689 if (tables
== NULL
) tables
= PRIV(default_tables
);
7690 cd
->lcc
= tables
+ lcc_offset
;
7691 cd
->fcc
= tables
+ fcc_offset
;
7692 cd
->cbits
= tables
+ cbits_offset
;
7693 cd
->ctypes
= tables
+ ctypes_offset
;
7695 /* Check that all undefined public option bits are zero */
7697 if ((options
& ~PUBLIC_COMPILE_OPTIONS
) != 0)
7700 goto PCRE_EARLY_ERROR_RETURN
;
7703 /* Check for global one-time settings at the start of the pattern, and remember
7704 the offset for later. */
7706 while (ptr
[skipatstart
] == CHAR_LEFT_PARENTHESIS
&&
7707 ptr
[skipatstart
+1] == CHAR_ASTERISK
)
7712 #ifdef COMPILE_PCRE8
7713 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF_RIGHTPAR
, 5) == 0)
7714 { skipatstart
+= 7; options
|= PCRE_UTF8
; continue; }
7716 #ifdef COMPILE_PCRE16
7717 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UTF_RIGHTPAR
, 6) == 0)
7718 { skipatstart
+= 8; options
|= PCRE_UTF16
; continue; }
7720 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_UCP_RIGHTPAR
, 4) == 0)
7721 { skipatstart
+= 6; options
|= PCRE_UCP
; continue; }
7722 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_NO_START_OPT_RIGHTPAR
, 13) == 0)
7723 { skipatstart
+= 15; options
|= PCRE_NO_START_OPTIMIZE
; continue; }
7725 if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_CR_RIGHTPAR
, 3) == 0)
7726 { skipatstart
+= 5; newnl
= PCRE_NEWLINE_CR
; }
7727 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_LF_RIGHTPAR
, 3) == 0)
7728 { skipatstart
+= 5; newnl
= PCRE_NEWLINE_LF
; }
7729 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_CRLF_RIGHTPAR
, 5) == 0)
7730 { skipatstart
+= 7; newnl
= PCRE_NEWLINE_CR
+ PCRE_NEWLINE_LF
; }
7731 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_ANY_RIGHTPAR
, 4) == 0)
7732 { skipatstart
+= 6; newnl
= PCRE_NEWLINE_ANY
; }
7733 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_ANYCRLF_RIGHTPAR
, 8) == 0)
7734 { skipatstart
+= 10; newnl
= PCRE_NEWLINE_ANYCRLF
; }
7736 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_BSR_ANYCRLF_RIGHTPAR
, 12) == 0)
7737 { skipatstart
+= 14; newbsr
= PCRE_BSR_ANYCRLF
; }
7738 else if (STRNCMP_UC_C8(ptr
+skipatstart
+2, STRING_BSR_UNICODE_RIGHTPAR
, 12) == 0)
7739 { skipatstart
+= 14; newbsr
= PCRE_BSR_UNICODE
; }
7742 options
= (options
& ~PCRE_NEWLINE_BITS
) | newnl
;
7743 else if (newbsr
!= 0)
7744 options
= (options
& ~(PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
)) | newbsr
;
7748 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
7749 utf
= (options
& PCRE_UTF8
) != 0;
7751 /* Can't support UTF unless PCRE has been compiled to include the code. The
7752 return of an error code from PRIV(valid_utf)() is a new feature, introduced in
7753 release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
7757 if (utf
&& (options
& PCRE_NO_UTF8_CHECK
) == 0 &&
7758 (errorcode
= PRIV(valid_utf
)((PCRE_PUCHAR
)pattern
, -1, erroroffset
)) != 0)
7760 #ifdef COMPILE_PCRE8
7765 goto PCRE_EARLY_ERROR_RETURN2
;
7771 goto PCRE_EARLY_ERROR_RETURN
;
7775 /* Can't support UCP unless PCRE has been compiled to include the code. */
7778 if ((options
& PCRE_UCP
) != 0)
7781 goto PCRE_EARLY_ERROR_RETURN
;
7785 /* Check validity of \R options. */
7787 if ((options
& (PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
)) ==
7788 (PCRE_BSR_ANYCRLF
|PCRE_BSR_UNICODE
))
7791 goto PCRE_EARLY_ERROR_RETURN
;
7794 /* Handle different types of newline. The three bits give seven cases. The
7795 current code allows for fixed one- or two-byte sequences, plus "any" and
7798 switch (options
& PCRE_NEWLINE_BITS
)
7800 case 0: newline
= NEWLINE
; break; /* Build-time default */
7801 case PCRE_NEWLINE_CR
: newline
= CHAR_CR
; break;
7802 case PCRE_NEWLINE_LF
: newline
= CHAR_NL
; break;
7803 case PCRE_NEWLINE_CR
+
7804 PCRE_NEWLINE_LF
: newline
= (CHAR_CR
<< 8) | CHAR_NL
; break;
7805 case PCRE_NEWLINE_ANY
: newline
= -1; break;
7806 case PCRE_NEWLINE_ANYCRLF
: newline
= -2; break;
7807 default: errorcode
= ERR56
; goto PCRE_EARLY_ERROR_RETURN
;
7812 cd
->nltype
= NLTYPE_ANYCRLF
;
7814 else if (newline
< 0)
7816 cd
->nltype
= NLTYPE_ANY
;
7820 cd
->nltype
= NLTYPE_FIXED
;
7824 cd
->nl
[0] = (newline
>> 8) & 255;
7825 cd
->nl
[1] = newline
& 255;
7830 cd
->nl
[0] = newline
;
7834 /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
7835 references to help in deciding whether (.*) can be treated as anchored or not.
7838 cd
->top_backref
= 0;
7839 cd
->backref_map
= 0;
7841 /* Reflect pattern for debugging output */
7843 DPRINTF(("------------------------------------------------------------------\n"));
7845 print_puchar(stdout
, (PCRE_PUCHAR
)pattern
);
7849 /* Pretend to compile the pattern while actually just accumulating the length
7850 of memory required. This behaviour is triggered by passing a non-NULL final
7851 argument to compile_regex(). We pass a block of workspace (cworkspace) for it
7852 to compile parts of the pattern into; the compiled code is discarded when it is
7853 no longer needed, so hopefully this workspace will never overflow, though there
7854 is a test for its doing so. */
7856 cd
->bracount
= cd
->final_bracount
= 0;
7857 cd
->names_found
= 0;
7858 cd
->name_entry_size
= 0;
7859 cd
->name_table
= NULL
;
7860 cd
->start_code
= cworkspace
;
7861 cd
->hwm
= cworkspace
;
7862 cd
->start_workspace
= cworkspace
;
7863 cd
->workspace_size
= COMPILE_WORK_SIZE
;
7864 cd
->start_pattern
= (const pcre_uchar
*)pattern
;
7865 cd
->end_pattern
= (const pcre_uchar
*)(pattern
+ STRLEN_UC((const pcre_uchar
*)pattern
));
7866 cd
->req_varyopt
= 0;
7867 cd
->assert_depth
= 0;
7868 cd
->max_lookbehind
= 0;
7869 cd
->external_options
= options
;
7870 cd
->external_flags
= 0;
7871 cd
->open_caps
= NULL
;
7873 /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
7874 don't need to look at the result of the function here. The initial options have
7875 been put into the cd block so that they can be changed if an option setting is
7876 found within the regex right at the beginning. Bringing initial option settings
7877 outside can help speed up starting point checks. */
7882 (void)compile_regex(cd
->external_options
, &code
, &ptr
, &errorcode
, FALSE
,
7883 FALSE
, 0, 0, &firstchar
, &reqchar
, NULL
, cd
, &length
);
7884 if (errorcode
!= 0) goto PCRE_EARLY_ERROR_RETURN
;
7886 DPRINTF(("end pre-compile: length=%d workspace=%d\n", length
,
7887 (int)(cd
->hwm
- cworkspace
)));
7889 if (length
> MAX_PATTERN_SIZE
)
7892 goto PCRE_EARLY_ERROR_RETURN
;
7895 /* Compute the size of data block needed and get it, either from malloc or
7896 externally provided function. Integer overflow should no longer be possible
7897 because nowadays we limit the maximum value of cd->names_found and
7898 cd->name_entry_size. */
7900 size
= sizeof(REAL_PCRE
) + (length
+ cd
->names_found
* cd
->name_entry_size
) * sizeof(pcre_uchar
);
7901 re
= (REAL_PCRE
*)(PUBL(malloc
))(size
);
7906 goto PCRE_EARLY_ERROR_RETURN
;
7909 /* Put in the magic number, and save the sizes, initial options, internal
7910 flags, and character table pointer. NULL is used for the default character
7911 tables. The nullpad field is at the end; it's there to help in the case when a
7912 regex compiled on a system with 4-byte pointers is run on another with 8-byte
7915 re
->magic_number
= MAGIC_NUMBER
;
7916 re
->size
= (int)size
;
7917 re
->options
= cd
->external_options
;
7918 re
->flags
= cd
->external_flags
;
7921 re
->name_table_offset
= sizeof(REAL_PCRE
) / sizeof(pcre_uchar
);
7922 re
->name_entry_size
= cd
->name_entry_size
;
7923 re
->name_count
= cd
->names_found
;
7925 re
->tables
= (tables
== PRIV(default_tables
))? NULL
: tables
;
7928 /* The starting points of the name/number translation table and of the code are
7929 passed around in the compile data block. The start/end pattern and initial
7930 options are already set from the pre-compile phase, as is the name_entry_size
7931 field. Reset the bracket count and the names_found field. Also reset the hwm
7932 field; this time it's used for remembering forward references to subpatterns.
7935 cd
->final_bracount
= cd
->bracount
; /* Save for checking forward references */
7936 cd
->assert_depth
= 0;
7938 cd
->max_lookbehind
= 0;
7939 cd
->names_found
= 0;
7940 cd
->name_table
= (pcre_uchar
*)re
+ re
->name_table_offset
;
7941 codestart
= cd
->name_table
+ re
->name_entry_size
* re
->name_count
;
7942 cd
->start_code
= codestart
;
7943 cd
->hwm
= (pcre_uchar
*)(cd
->start_workspace
);
7944 cd
->req_varyopt
= 0;
7945 cd
->had_accept
= FALSE
;
7946 cd
->check_lookbehind
= FALSE
;
7947 cd
->open_caps
= NULL
;
7949 /* Set up a starting, non-extracting bracket, then compile the expression. On
7950 error, errorcode will be set non-zero, so we don't need to look at the result
7951 of the function here. */
7953 ptr
= (const pcre_uchar
*)pattern
+ skipatstart
;
7954 code
= (pcre_uchar
*)codestart
;
7956 (void)compile_regex(re
->options
, &code
, &ptr
, &errorcode
, FALSE
, FALSE
, 0, 0,
7957 &firstchar
, &reqchar
, NULL
, cd
, NULL
);
7958 re
->top_bracket
= cd
->bracount
;
7959 re
->top_backref
= cd
->top_backref
;
7960 re
->max_lookbehind
= cd
->max_lookbehind
;
7961 re
->flags
= cd
->external_flags
| PCRE_MODE
;
7963 if (cd
->had_accept
) reqchar
= REQ_NONE
; /* Must disable after (*ACCEPT) */
7965 /* If not reached end of pattern on success, there's an excess bracket. */
7967 if (errorcode
== 0 && *ptr
!= 0) errorcode
= ERR22
;
7969 /* Fill in the terminating state and check for disastrous overflow, but
7970 if debugging, leave the test till after things are printed out. */
7975 if (code
- codestart
> length
) errorcode
= ERR23
;
7978 /* Fill in any forward references that are required. There may be repeated
7979 references; optimize for them, as searching a large regex takes time. */
7981 if (cd
->hwm
> cd
->start_workspace
)
7983 int prev_recno
= -1;
7984 const pcre_uchar
*groupptr
= NULL
;
7985 while (errorcode
== 0 && cd
->hwm
> cd
->start_workspace
)
7988 cd
->hwm
-= LINK_SIZE
;
7989 offset
= GET(cd
->hwm
, 0);
7990 recno
= GET(codestart
, offset
);
7991 if (recno
!= prev_recno
)
7993 groupptr
= PRIV(find_bracket
)(codestart
, utf
, recno
);
7996 if (groupptr
== NULL
) errorcode
= ERR53
;
7997 else PUT(((pcre_uchar
*)codestart
), offset
, (int)(groupptr
- codestart
));
8001 /* If the workspace had to be expanded, free the new memory. */
8003 if (cd
->workspace_size
> COMPILE_WORK_SIZE
)
8004 (PUBL(free
))((void *)cd
->start_workspace
);
8006 /* Give an error if there's back reference to a non-existent capturing
8009 if (errorcode
== 0 && re
->top_backref
> re
->top_bracket
) errorcode
= ERR15
;
8011 /* If there were any lookbehind assertions that contained OP_RECURSE
8012 (recursions or subroutine calls), a flag is set for them to be checked here,
8013 because they may contain forward references. Actual recursions can't be fixed
8014 length, but subroutine calls can. It is done like this so that those without
8015 OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
8016 exceptional ones forgo this. We scan the pattern to check that they are fixed
8017 length, and set their lengths. */
8019 if (cd
->check_lookbehind
)
8021 pcre_uchar
*cc
= (pcre_uchar
*)codestart
;
8023 /* Loop, searching for OP_REVERSE items, and process those that do not have
8024 their length set. (Actually, it will also re-process any that have a length
8025 of zero, but that is a pathological case, and it does no harm.) When we find
8026 one, we temporarily terminate the branch it is in while we scan it. */
8028 for (cc
= (pcre_uchar
*)PRIV(find_bracket
)(codestart
, utf
, -1);
8030 cc
= (pcre_uchar
*)PRIV(find_bracket
)(cc
, utf
, -1))
8032 if (GET(cc
, 1) == 0)
8035 pcre_uchar
*be
= cc
- 1 - LINK_SIZE
+ GET(cc
, -LINK_SIZE
);
8038 fixed_length
= find_fixedlength(cc
, (re
->options
& PCRE_UTF8
) != 0, TRUE
,
8041 DPRINTF(("fixed length = %d\n", fixed_length
));
8042 if (fixed_length
< 0)
8044 errorcode
= (fixed_length
== -2)? ERR36
:
8045 (fixed_length
== -4)? ERR70
: ERR25
;
8048 if (fixed_length
> cd
->max_lookbehind
) cd
->max_lookbehind
= fixed_length
;
8049 PUT(cc
, 1, fixed_length
);
8051 cc
+= 1 + LINK_SIZE
;
8055 /* Failed to compile, or error while post-processing */
8060 PCRE_EARLY_ERROR_RETURN
:
8061 *erroroffset
= (int)(ptr
- (const pcre_uchar
*)pattern
);
8062 PCRE_EARLY_ERROR_RETURN2
:
8063 *errorptr
= find_error_text(errorcode
);
8064 if (errorcodeptr
!= NULL
) *errorcodeptr
= errorcode
;
8068 /* If the anchored option was not passed, set the flag if we can determine that
8069 the pattern is anchored by virtue of ^ characters or \A or anything else (such
8070 as starting with .* when DOTALL is set).
8072 Otherwise, if we know what the first byte has to be, save it, because that
8073 speeds up unanchored matches no end. If not, see if we can set the
8074 PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
8075 start with ^. and also when all branches start with .* for non-DOTALL matches.
8078 if ((re
->options
& PCRE_ANCHORED
) == 0)
8080 if (is_anchored(codestart
, 0, cd
->backref_map
))
8081 re
->options
|= PCRE_ANCHORED
;
8085 firstchar
= find_firstassertedchar(codestart
, FALSE
);
8086 if (firstchar
>= 0) /* Remove caseless flag for non-caseable chars */
8088 #ifdef COMPILE_PCRE8
8089 re
->first_char
= firstchar
& 0xff;
8091 #ifdef COMPILE_PCRE16
8092 re
->first_char
= firstchar
& 0xffff;
8095 if ((firstchar
& REQ_CASELESS
) != 0)
8097 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
8098 /* We ignore non-ASCII first chars in 8 bit mode. */
8101 if (re
->first_char
< 128)
8103 if (cd
->fcc
[re
->first_char
] != re
->first_char
)
8104 re
->flags
|= PCRE_FCH_CASELESS
;
8106 else if (UCD_OTHERCASE(re
->first_char
) != re
->first_char
)
8107 re
->flags
|= PCRE_FCH_CASELESS
;
8111 if (MAX_255(re
->first_char
)
8112 && cd
->fcc
[re
->first_char
] != re
->first_char
)
8113 re
->flags
|= PCRE_FCH_CASELESS
;
8116 re
->flags
|= PCRE_FIRSTSET
;
8118 else if (is_startline(codestart
, 0, cd
->backref_map
))
8119 re
->flags
|= PCRE_STARTLINE
;
8123 /* For an anchored pattern, we use the "required byte" only if it follows a
8124 variable length item in the regex. Remove the caseless flag for non-caseable
8128 ((re
->options
& PCRE_ANCHORED
) == 0 || (reqchar
& REQ_VARY
) != 0))
8130 #ifdef COMPILE_PCRE8
8131 re
->req_char
= reqchar
& 0xff;
8133 #ifdef COMPILE_PCRE16
8134 re
->req_char
= reqchar
& 0xffff;
8137 if ((reqchar
& REQ_CASELESS
) != 0)
8139 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
8140 /* We ignore non-ASCII first chars in 8 bit mode. */
8143 if (re
->req_char
< 128)
8145 if (cd
->fcc
[re
->req_char
] != re
->req_char
)
8146 re
->flags
|= PCRE_RCH_CASELESS
;
8148 else if (UCD_OTHERCASE(re
->req_char
) != re
->req_char
)
8149 re
->flags
|= PCRE_RCH_CASELESS
;
8153 if (MAX_255(re
->req_char
) && cd
->fcc
[re
->req_char
] != re
->req_char
)
8154 re
->flags
|= PCRE_RCH_CASELESS
;
8157 re
->flags
|= PCRE_REQCHSET
;
8160 /* Print out the compiled data if debugging is enabled. This is never the
8161 case when building a production library. */
8164 printf("Length = %d top_bracket = %d top_backref = %d\n",
8165 length
, re
->top_bracket
, re
->top_backref
);
8167 printf("Options=%08x\n", re
->options
);
8169 if ((re
->flags
& PCRE_FIRSTSET
) != 0)
8171 pcre_uchar ch
= re
->first_char
;
8172 const char *caseless
=
8173 ((re
->flags
& PCRE_FCH_CASELESS
) == 0)? "" : " (caseless)";
8174 if (PRINTABLE(ch
)) printf("First char = %c%s\n", ch
, caseless
);
8175 else printf("First char = \\x%02x%s\n", ch
, caseless
);
8178 if ((re
->flags
& PCRE_REQCHSET
) != 0)
8180 pcre_uchar ch
= re
->req_char
;
8181 const char *caseless
=
8182 ((re
->flags
& PCRE_RCH_CASELESS
) == 0)? "" : " (caseless)";
8183 if (PRINTABLE(ch
)) printf("Req char = %c%s\n", ch
, caseless
);
8184 else printf("Req char = \\x%02x%s\n", ch
, caseless
);
8187 #ifdef COMPILE_PCRE8
8188 pcre_printint((pcre
*)re
, stdout
, TRUE
);
8190 pcre16_printint((pcre
*)re
, stdout
, TRUE
);
8193 /* This check is done here in the debugging case so that the code that
8194 was compiled can be seen. */
8196 if (code
- codestart
> length
)
8199 *errorptr
= find_error_text(ERR23
);
8200 *erroroffset
= ptr
- (pcre_uchar
*)pattern
;
8201 if (errorcodeptr
!= NULL
) *errorcodeptr
= ERR23
;
8204 #endif /* PCRE_DEBUG */
8206 #ifdef COMPILE_PCRE8
8209 return (pcre16
*)re
;
8213 /* End of pcre_compile.c */