tgupdate: merge pcreposix-compat base into pcreposix-compat
[pcreposix-compat.git] / pcre_compile.c
blobdbbc4ebddad74c05dbb419660cc7ebd5a834f6ba
1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Copyright (c) 1997-2020 University of Cambridge
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
41 /* This module contains the external function pcre_compile(), along with
42 supporting internal functions that are not used by other modules. */
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
49 #define NLBLOCK cd /* Block containing newline information */
50 #define PSSTART start_pattern /* Field containing pattern start */
51 #define PSEND end_pattern /* Field containing pattern end */
53 #include "pcre_internal.h"
56 /* When PCRE_DEBUG is defined, we need the pcre(16|32)_printint() function, which
57 is also used by pcretest. PCRE_DEBUG is not defined when building a production
58 library. We do not need to select pcre16_printint.c specially, because the
59 COMPILE_PCREx macro will already be appropriately set. */
61 #ifdef PCRE_DEBUG
62 /* pcre_printint.c should not include any headers */
63 #define PCRE_INCLUDED
64 #include "pcre_printint.c"
65 #undef PCRE_INCLUDED
66 #endif
69 /* Macro for setting individual bits in class bitmaps. */
71 #define SETBIT(a,b) a[(b)/8] |= (1U << ((b)&7))
73 /* Maximum length value to check against when making sure that the integer that
74 holds the compiled pattern length does not overflow. We make it a bit less than
75 INT_MAX to allow for adding in group terminating bytes, so that we don't have
76 to check them every time. */
78 #define OFLOW_MAX (INT_MAX - 20)
80 /* Definitions to allow mutual recursion */
82 static int
83 add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84 const pcre_uint32 *, unsigned int);
86 static BOOL
87 compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL, int, int,
88 pcre_uint32 *, pcre_int32 *, pcre_uint32 *, pcre_int32 *, branch_chain *,
89 compile_data *, int *);
93 /*************************************************
94 * Code parameters and static tables *
95 *************************************************/
97 /* This value specifies the size of stack workspace that is used during the
98 first pre-compile phase that determines how much memory is required. The regex
99 is partly compiled into this space, but the compiled parts are discarded as
100 soon as they can be, so that hopefully there will never be an overrun. The code
101 does, however, check for an overrun. The largest amount I've seen used is 218,
102 so this number is very generous.
104 The same workspace is used during the second, actual compile phase for
105 remembering forward references to groups so that they can be filled in at the
106 end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
107 is 4 there is plenty of room for most patterns. However, the memory can get
108 filled up by repetitions of forward references, for example patterns like
109 /(?1){0,1999}(b)/, and one user did hit the limit. The code has been changed so
110 that the workspace is expanded using malloc() in this situation. The value
111 below is therefore a minimum, and we put a maximum on it for safety. The
112 minimum is now also defined in terms of LINK_SIZE so that the use of malloc()
113 kicks in at the same number of forward references in all cases. */
115 #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116 #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
118 /* This value determines the size of the initial vector that is used for
119 remembering named groups during the pre-compile. It is allocated on the stack,
120 but if it is too small, it is expanded using malloc(), in a similar way to the
121 workspace. The value is the number of slots in the list. */
123 #define NAMED_GROUP_LIST_SIZE 20
125 /* The overrun tests check for a slightly smaller size so that they detect the
126 overrun before it actually does run off the end of the data block. */
128 #define WORK_SIZE_SAFETY_MARGIN (100)
130 /* Private flags added to firstchar and reqchar. */
132 #define REQ_CASELESS (1U << 0) /* Indicates caselessness */
133 #define REQ_VARY (1U << 1) /* Reqchar followed non-literal item */
134 /* Negative values for the firstchar and reqchar flags */
135 #define REQ_UNSET (-2)
136 #define REQ_NONE (-1)
138 /* Repeated character flags. */
140 #define UTF_LENGTH 0x10000000l /* The char contains its length. */
142 /* Table for handling escaped characters in the range '0'-'z'. Positive returns
143 are simple data values; negative values are for special things like \d and so
144 on. Zero means further processing is needed (for things like \x), or the escape
145 is invalid. */
147 #ifndef EBCDIC
149 /* This is the "normal" table for ASCII systems or for EBCDIC systems running
150 in UTF-8 mode. */
152 static const short int escapes[] = {
153 0, 0,
154 0, 0,
155 0, 0,
156 0, 0,
157 0, 0,
158 CHAR_COLON, CHAR_SEMICOLON,
159 CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN,
160 CHAR_GREATER_THAN_SIGN, CHAR_QUESTION_MARK,
161 CHAR_COMMERCIAL_AT, -ESC_A,
162 -ESC_B, -ESC_C,
163 -ESC_D, -ESC_E,
164 0, -ESC_G,
165 -ESC_H, 0,
166 0, -ESC_K,
167 0, 0,
168 -ESC_N, 0,
169 -ESC_P, -ESC_Q,
170 -ESC_R, -ESC_S,
171 0, 0,
172 -ESC_V, -ESC_W,
173 -ESC_X, 0,
174 -ESC_Z, CHAR_LEFT_SQUARE_BRACKET,
175 CHAR_BACKSLASH, CHAR_RIGHT_SQUARE_BRACKET,
176 CHAR_CIRCUMFLEX_ACCENT, CHAR_UNDERSCORE,
177 CHAR_GRAVE_ACCENT, ESC_a,
178 -ESC_b, 0,
179 -ESC_d, ESC_e,
180 ESC_f, 0,
181 -ESC_h, 0,
182 0, -ESC_k,
183 0, 0,
184 ESC_n, 0,
185 -ESC_p, 0,
186 ESC_r, -ESC_s,
187 ESC_tee, 0,
188 -ESC_v, -ESC_w,
189 0, 0,
190 -ESC_z
193 #else
195 /* This is the "abnormal" table for EBCDIC systems without UTF-8 support. */
197 static const short int escapes[] = {
198 /* 48 */ 0, 0, 0, '.', '<', '(', '+', '|',
199 /* 50 */ '&', 0, 0, 0, 0, 0, 0, 0,
200 /* 58 */ 0, 0, '!', '$', '*', ')', ';', '~',
201 /* 60 */ '-', '/', 0, 0, 0, 0, 0, 0,
202 /* 68 */ 0, 0, '|', ',', '%', '_', '>', '?',
203 /* 70 */ 0, 0, 0, 0, 0, 0, 0, 0,
204 /* 78 */ 0, '`', ':', '#', '@', '\'', '=', '"',
205 /* 80 */ 0, ESC_a, -ESC_b, 0, -ESC_d, ESC_e, ESC_f, 0,
206 /* 88 */-ESC_h, 0, 0, '{', 0, 0, 0, 0,
207 /* 90 */ 0, 0, -ESC_k, 0, 0, ESC_n, 0, -ESC_p,
208 /* 98 */ 0, ESC_r, 0, '}', 0, 0, 0, 0,
209 /* A0 */ 0, '~', -ESC_s, ESC_tee, 0,-ESC_v, -ESC_w, 0,
210 /* A8 */ 0,-ESC_z, 0, 0, 0, '[', 0, 0,
211 /* B0 */ 0, 0, 0, 0, 0, 0, 0, 0,
212 /* B8 */ 0, 0, 0, 0, 0, ']', '=', '-',
213 /* C0 */ '{',-ESC_A, -ESC_B, -ESC_C, -ESC_D,-ESC_E, 0, -ESC_G,
214 /* C8 */-ESC_H, 0, 0, 0, 0, 0, 0, 0,
215 /* D0 */ '}', 0, -ESC_K, 0, 0,-ESC_N, 0, -ESC_P,
216 /* D8 */-ESC_Q,-ESC_R, 0, 0, 0, 0, 0, 0,
217 /* E0 */ '\\', 0, -ESC_S, 0, 0,-ESC_V, -ESC_W, -ESC_X,
218 /* E8 */ 0,-ESC_Z, 0, 0, 0, 0, 0, 0,
219 /* F0 */ 0, 0, 0, 0, 0, 0, 0, 0,
220 /* F8 */ 0, 0, 0, 0, 0, 0, 0, 0
223 /* We also need a table of characters that may follow \c in an EBCDIC
224 environment for characters 0-31. */
226 static unsigned char ebcdic_escape_c[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
228 #endif
231 /* Table of special "verbs" like (*PRUNE). This is a short table, so it is
232 searched linearly. Put all the names into a single string, in order to reduce
233 the number of relocations when a shared library is dynamically linked. The
234 string is built from string macros so that it works in UTF-8 mode on EBCDIC
235 platforms. */
237 typedef struct verbitem {
238 int len; /* Length of verb name */
239 int op; /* Op when no arg, or -1 if arg mandatory */
240 int op_arg; /* Op when arg present, or -1 if not allowed */
241 } verbitem;
243 static const char verbnames[] =
244 "\0" /* Empty name is a shorthand for MARK */
245 STRING_MARK0
246 STRING_ACCEPT0
247 STRING_COMMIT0
248 STRING_F0
249 STRING_FAIL0
250 STRING_PRUNE0
251 STRING_SKIP0
252 STRING_THEN;
254 static const verbitem verbs[] = {
255 { 0, -1, OP_MARK },
256 { 4, -1, OP_MARK },
257 { 6, OP_ACCEPT, -1 },
258 { 6, OP_COMMIT, -1 },
259 { 1, OP_FAIL, -1 },
260 { 4, OP_FAIL, -1 },
261 { 5, OP_PRUNE, OP_PRUNE_ARG },
262 { 4, OP_SKIP, OP_SKIP_ARG },
263 { 4, OP_THEN, OP_THEN_ARG }
266 static const int verbcount = sizeof(verbs)/sizeof(verbitem);
269 /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
270 another regex library. */
272 static const pcre_uchar sub_start_of_word[] = {
273 CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
274 CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' };
276 static const pcre_uchar sub_end_of_word[] = {
277 CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
278 CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w,
279 CHAR_RIGHT_PARENTHESIS, '\0' };
282 /* Substitute for implicit newline. */
284 static const pcre_uchar sub_implicit_rsb_newline[] = {
285 CHAR_RIGHT_SQUARE_BRACKET, CHAR_BACKSLASH, CHAR_n, '\0' };
288 /* Substitute for embedded literal NUL. */
290 static const pcre_uchar sub_embedded_nul[] = {
291 CHAR_BACKSLASH, CHAR_0, CHAR_0, CHAR_0, '\0' };
294 /* Tables of names of POSIX character classes and their lengths. The names are
295 now all in a single string, to reduce the number of relocations when a shared
296 library is dynamically loaded. The list of lengths is terminated by a zero
297 length entry. The first three must be alpha, lower, upper, as this is assumed
298 for handling case independence. The indices for graph, print, and punct are
299 needed, so identify them. */
301 static const char posix_names[] =
302 STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
303 STRING_ascii0 STRING_blank0 STRING_cntrl0 STRING_digit0
304 STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
305 STRING_word0 STRING_xdigit;
307 static const pcre_uint8 posix_name_lengths[] = {
308 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
310 #define PC_GRAPH 8
311 #define PC_PRINT 9
312 #define PC_PUNCT 10
315 /* Table of class bit maps for each POSIX class. Each class is formed from a
316 base map, with an optional addition or removal of another map. Then, for some
317 classes, there is some additional tweaking: for [:blank:] the vertical space
318 characters are removed, and for [:alpha:] and [:alnum:] the underscore
319 character is removed. The triples in the table consist of the base map offset,
320 second map offset or -1 if no second map, and a non-negative value for map
321 addition or a negative value for map subtraction (if there are two maps). The
322 absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
323 remove vertical space characters, 2 => remove underscore. */
325 static const int posix_class_maps[] = {
326 cbit_word, cbit_digit, -2, /* alpha */
327 cbit_lower, -1, 0, /* lower */
328 cbit_upper, -1, 0, /* upper */
329 cbit_word, -1, 2, /* alnum - word without underscore */
330 cbit_print, cbit_cntrl, 0, /* ascii */
331 cbit_space, -1, 1, /* blank - a GNU extension */
332 cbit_cntrl, -1, 0, /* cntrl */
333 cbit_digit, -1, 0, /* digit */
334 cbit_graph, -1, 0, /* graph */
335 cbit_print, -1, 0, /* print */
336 cbit_punct, -1, 0, /* punct */
337 cbit_space, -1, 0, /* space */
338 cbit_word, -1, 0, /* word - a Perl extension */
339 cbit_xdigit,-1, 0 /* xdigit */
342 /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
343 Unicode property escapes. */
345 #ifdef SUPPORT_UCP
346 static const pcre_uchar string_PNd[] = {
347 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
348 CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
349 static const pcre_uchar string_pNd[] = {
350 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
351 CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
352 static const pcre_uchar string_PXsp[] = {
353 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
354 CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
355 static const pcre_uchar string_pXsp[] = {
356 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
357 CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
358 static const pcre_uchar string_PXwd[] = {
359 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360 CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361 static const pcre_uchar string_pXwd[] = {
362 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
363 CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
365 static const pcre_uchar *substitutes[] = {
366 string_PNd, /* \D */
367 string_pNd, /* \d */
368 string_PXsp, /* \S */ /* Xsp is Perl space, but from 8.34, Perl */
369 string_pXsp, /* \s */ /* space and POSIX space are the same. */
370 string_PXwd, /* \W */
371 string_pXwd /* \w */
374 /* The POSIX class substitutes must be in the order of the POSIX class names,
375 defined above, and there are both positive and negative cases. NULL means no
376 general substitute of a Unicode property escape (\p or \P). However, for some
377 POSIX classes (e.g. graph, print, punct) a special property code is compiled
378 directly. */
380 static const pcre_uchar string_pL[] = {
381 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
382 CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
383 static const pcre_uchar string_pLl[] = {
384 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
385 CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
386 static const pcre_uchar string_pLu[] = {
387 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
388 CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
389 static const pcre_uchar string_pXan[] = {
390 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
391 CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
392 static const pcre_uchar string_h[] = {
393 CHAR_BACKSLASH, CHAR_h, '\0' };
394 static const pcre_uchar string_pXps[] = {
395 CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
396 CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
397 static const pcre_uchar string_PL[] = {
398 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
399 CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
400 static const pcre_uchar string_PLl[] = {
401 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
402 CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
403 static const pcre_uchar string_PLu[] = {
404 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
405 CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
406 static const pcre_uchar string_PXan[] = {
407 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
408 CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
409 static const pcre_uchar string_H[] = {
410 CHAR_BACKSLASH, CHAR_H, '\0' };
411 static const pcre_uchar string_PXps[] = {
412 CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
413 CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
415 static const pcre_uchar *posix_substitutes[] = {
416 string_pL, /* alpha */
417 string_pLl, /* lower */
418 string_pLu, /* upper */
419 string_pXan, /* alnum */
420 NULL, /* ascii */
421 string_h, /* blank */
422 NULL, /* cntrl */
423 string_pNd, /* digit */
424 NULL, /* graph */
425 NULL, /* print */
426 NULL, /* punct */
427 string_pXps, /* space */ /* Xps is POSIX space, but from 8.34 */
428 string_pXwd, /* word */ /* Perl and POSIX space are the same */
429 NULL, /* xdigit */
430 /* Negated cases */
431 string_PL, /* ^alpha */
432 string_PLl, /* ^lower */
433 string_PLu, /* ^upper */
434 string_PXan, /* ^alnum */
435 NULL, /* ^ascii */
436 string_H, /* ^blank */
437 NULL, /* ^cntrl */
438 string_PNd, /* ^digit */
439 NULL, /* ^graph */
440 NULL, /* ^print */
441 NULL, /* ^punct */
442 string_PXps, /* ^space */ /* Xps is POSIX space, but from 8.34 */
443 string_PXwd, /* ^word */ /* Perl and POSIX space are the same */
444 NULL /* ^xdigit */
446 #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
447 #endif
449 #define STRING(a) # a
450 #define XSTRING(s) STRING(s)
452 /* The texts of compile-time error messages. These are "char *" because they
453 are passed to the outside world. Do not ever re-use any error number, because
454 they are documented. Always add a new error instead. Messages marked DEAD below
455 are no longer used. This used to be a table of strings, but in order to reduce
456 the number of relocations needed when a shared library is loaded dynamically,
457 it is now one long string. We cannot use a table of offsets, because the
458 lengths of inserts such as XSTRING(MAX_NAME_SIZE) are not known. Instead, we
459 simply count through to the one we want - this isn't a performance issue
460 because these strings are used only when there is a compilation error.
462 Each substring ends with \0 to insert a null character. This includes the final
463 substring, so that the whole string ends with \0\0, which can be detected when
464 counting through. */
466 static const char error_texts[] =
467 "no error\0"
468 "\\ at end of pattern\0"
469 "\\c at end of pattern\0"
470 "unrecognized character follows \\\0"
471 "numbers out of order in {} quantifier\0"
472 /* 5 */
473 "number too big in {} quantifier\0"
474 "missing terminating ] for character class\0"
475 "invalid escape sequence in character class\0"
476 "range out of order in character class\0"
477 "nothing to repeat\0"
478 /* 10 */
479 "internal error: invalid forward reference offset\0"
480 "internal error: unexpected repeat\0"
481 "unrecognized character after (? or (?-\0"
482 "POSIX named classes are supported only within a class\0"
483 "missing )\0"
484 /* 15 */
485 "reference to non-existent subpattern\0"
486 "erroffset passed as NULL\0"
487 "unknown option bit(s) set\0"
488 "missing ) after comment\0"
489 "parentheses nested too deeply\0" /** DEAD **/
490 /* 20 */
491 "regular expression is too large\0"
492 "failed to get memory\0"
493 "unmatched parentheses\0"
494 "internal error: code overflow\0"
495 "unrecognized character after (?<\0"
496 /* 25 */
497 "lookbehind assertion is not fixed length\0"
498 "malformed number or name after (?(\0"
499 "conditional group contains more than two branches\0"
500 "assertion expected after (?( or (?(?C)\0"
501 "(?R or (?[+-]digits must be followed by )\0"
502 /* 30 */
503 "unknown POSIX class name\0"
504 "POSIX collating elements are not supported\0"
505 "this version of PCRE is compiled without UTF support\0"
506 "spare error\0" /** DEAD **/
507 "character value in \\x{} or \\o{} is too large\0"
508 /* 35 */
509 "invalid condition (?(0)\0"
510 "\\C not allowed in lookbehind assertion\0"
511 "PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u\0"
512 "number after (?C is > 255\0"
513 "closing ) for (?C expected\0"
514 /* 40 */
515 "recursive call could loop indefinitely\0"
516 "unrecognized character after (?P\0"
517 "syntax error in subpattern name (missing terminator)\0"
518 "two named subpatterns have the same name\0"
519 "invalid UTF-8 string\0"
520 /* 45 */
521 "support for \\P, \\p, and \\X has not been compiled\0"
522 "malformed \\P or \\p sequence\0"
523 "unknown property name after \\P or \\p\0"
524 "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
525 "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
526 /* 50 */
527 "repeated subpattern is too long\0" /** DEAD **/
528 "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
529 "internal error: overran compiling workspace\0"
530 "internal error: previously-checked referenced subpattern not found\0"
531 "DEFINE group contains more than one branch\0"
532 /* 55 */
533 "repeating a DEFINE group is not allowed\0" /** DEAD **/
534 "inconsistent NEWLINE options\0"
535 "\\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number\0"
536 "a numbered reference must not be zero\0"
537 "an argument is not allowed for (*ACCEPT), (*FAIL), or (*COMMIT)\0"
538 /* 60 */
539 "(*VERB) not recognized or malformed\0"
540 "number is too big\0"
541 "subpattern name expected\0"
542 "digit expected after (?+\0"
543 "] is an invalid data character in JavaScript compatibility mode\0"
544 /* 65 */
545 "different names for subpatterns of the same number are not allowed\0"
546 "(*MARK) must have an argument\0"
547 "this version of PCRE is not compiled with Unicode property support\0"
548 #ifndef EBCDIC
549 "\\c must be followed by an ASCII character\0"
550 #else
551 "\\c must be followed by a letter or one of [\\]^_?\0"
552 #endif
553 "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
554 /* 70 */
555 "internal error: unknown opcode in find_fixedlength()\0"
556 "\\N is not supported in a class\0"
557 "too many forward references\0"
558 "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
559 "invalid UTF-16 string\0"
560 /* 75 */
561 "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
562 "character value in \\u.... sequence is too large\0"
563 "invalid UTF-32 string\0"
564 "setting UTF is disabled by the application\0"
565 "non-hex character in \\x{} (closing brace missing?)\0"
566 /* 80 */
567 "non-octal character in \\o{} (closing brace missing?)\0"
568 "missing opening brace after \\o\0"
569 "parentheses are too deeply nested\0"
570 "invalid range in character class\0"
571 "group name must start with a non-digit\0"
572 /* 85 */
573 "parentheses are too deeply nested (stack check)\0"
574 "digits missing in \\x{} or \\o{}\0"
575 "regular expression is too complicated\0"
578 /* Table to identify digits and hex digits. This is used when compiling
579 patterns. Note that the tables in chartables are dependent on the locale, and
580 may mark arbitrary characters as digits - but the PCRE compiling code expects
581 to handle only 0-9, a-z, and A-Z as digits when compiling. That is why we have
582 a private table here. It costs 256 bytes, but it is a lot faster than doing
583 character value tests (at least in some simple cases I timed), and in some
584 applications one wants PCRE to compile efficiently as well as match
585 efficiently.
587 For convenience, we use the same bit definitions as in chartables:
589 0x04 decimal digit
590 0x08 hexadecimal digit
592 Then we can use ctype_digit and ctype_xdigit in the code. */
594 /* Using a simple comparison for decimal numbers rather than a memory read
595 is much faster, and the resulting code is simpler (the compiler turns it
596 into a subtraction and unsigned comparison). */
598 #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
600 #ifndef EBCDIC
602 /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
603 UTF-8 mode. */
605 static const pcre_uint8 digitab[] =
607 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 */
608 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
609 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 */
610 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
611 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - ' */
612 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ( - / */
613 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 */
614 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, /* 8 - ? */
615 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* @ - G */
616 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H - O */
617 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* P - W */
618 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* X - _ */
619 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* ` - g */
620 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h - o */
621 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* p - w */
622 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* x -127 */
623 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128-135 */
624 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 136-143 */
625 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144-151 */
626 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 152-159 */
627 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160-167 */
628 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 168-175 */
629 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176-183 */
630 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
631 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192-199 */
632 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 200-207 */
633 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208-215 */
634 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 216-223 */
635 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224-231 */
636 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 232-239 */
637 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240-247 */
638 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};/* 248-255 */
640 #else
642 /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
644 static const pcre_uint8 digitab[] =
646 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0- 7 0 */
647 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 8- 15 */
648 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16- 23 10 */
649 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
650 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32- 39 20 */
651 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
652 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 30 */
653 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
654 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 40 */
655 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 72- | */
656 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 50 */
657 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88- 95 */
658 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 60 */
659 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104- ? */
660 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 70 */
661 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
662 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* 128- g 80 */
663 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
664 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144- p 90 */
665 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
666 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160- x A0 */
667 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
668 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 B0 */
669 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
670 0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, /* { - G C0 */
671 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
672 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* } - P D0 */
673 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
674 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* \ - X E0 */
675 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
676 0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /* 0 - 7 F0 */
677 0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
679 static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
680 0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 0- 7 */
681 0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /* 8- 15 */
682 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 16- 23 */
683 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24- 31 */
684 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /* 32- 39 */
685 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40- 47 */
686 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 48- 55 */
687 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 56- 63 */
688 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - 71 */
689 0x00,0x00,0x00,0x80,0x00,0x80,0x80,0x80, /* 72- | */
690 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* & - 87 */
691 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, /* 88- 95 */
692 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* - -103 */
693 0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x80, /* 104- ? */
694 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112-119 */
695 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 120- " */
696 0x00,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* 128- g */
697 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* h -143 */
698 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* 144- p */
699 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* q -159 */
700 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* 160- x */
701 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* y -175 */
702 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* ^ -183 */
703 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00, /* 184-191 */
704 0x80,0x1a,0x1a,0x1a,0x1a,0x1a,0x1a,0x12, /* { - G */
705 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* H -207 */
706 0x00,0x12,0x12,0x12,0x12,0x12,0x12,0x12, /* } - P */
707 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Q -223 */
708 0x00,0x00,0x12,0x12,0x12,0x12,0x12,0x12, /* \ - X */
709 0x12,0x12,0x00,0x00,0x00,0x00,0x00,0x00, /* Y -239 */
710 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c, /* 0 - 7 */
711 0x1c,0x1c,0x00,0x00,0x00,0x00,0x00,0x00};/* 8 -255 */
712 #endif
715 /* This table is used to check whether auto-possessification is possible
716 between adjacent character-type opcodes. The left-hand (repeated) opcode is
717 used to select the row, and the right-hand opcode is use to select the column.
718 A value of 1 means that auto-possessification is OK. For example, the second
719 value in the first row means that \D+\d can be turned into \D++\d.
721 The Unicode property types (\P and \p) have to be present to fill out the table
722 because of what their opcode values are, but the table values should always be
723 zero because property types are handled separately in the code. The last four
724 columns apply to items that cannot be repeated, so there is no need to have
725 rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
726 *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
728 #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
729 #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
731 static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
732 /* \D \d \S \s \W \w . .+ \C \P \p \R \H \h \V \v \X \Z \z $ $M */
733 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \D */
734 { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \d */
735 { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \S */
736 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \s */
737 { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \W */
738 { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 }, /* \w */
739 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* . */
740 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* .+ */
741 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, /* \C */
742 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \P */
743 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* \p */
744 { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \R */
745 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }, /* \H */
746 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \h */
747 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 }, /* \V */
748 { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 }, /* \v */
749 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 } /* \X */
753 /* This table is used to check whether auto-possessification is possible
754 between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
755 left-hand (repeated) opcode is used to select the row, and the right-hand
756 opcode is used to select the column. The values are as follows:
758 0 Always return FALSE (never auto-possessify)
759 1 Character groups are distinct (possessify if both are OP_PROP)
760 2 Check character categories in the same group (general or particular)
761 3 TRUE if the two opcodes are not the same (PROP vs NOTPROP)
763 4 Check left general category vs right particular category
764 5 Check right general category vs left particular category
766 6 Left alphanum vs right general category
767 7 Left space vs right general category
768 8 Left word vs right general category
770 9 Right alphanum vs left general category
771 10 Right space vs left general category
772 11 Right word vs left general category
774 12 Left alphanum vs right particular category
775 13 Left space vs right particular category
776 14 Left word vs right particular category
778 15 Right alphanum vs left particular category
779 16 Right space vs left particular category
780 17 Right word vs left particular category
783 static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
784 /* ANY LAMP GC PC SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
785 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_ANY */
786 { 0, 3, 0, 0, 0, 3, 1, 1, 0, 0, 0 }, /* PT_LAMP */
787 { 0, 0, 2, 4, 0, 9, 10, 10, 11, 0, 0 }, /* PT_GC */
788 { 0, 0, 5, 2, 0, 15, 16, 16, 17, 0, 0 }, /* PT_PC */
789 { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, /* PT_SC */
790 { 0, 3, 6, 12, 0, 3, 1, 1, 0, 0, 0 }, /* PT_ALNUM */
791 { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_SPACE */
792 { 0, 1, 7, 13, 0, 1, 3, 3, 1, 0, 0 }, /* PT_PXSPACE */
793 { 0, 0, 8, 14, 0, 0, 1, 1, 3, 0, 0 }, /* PT_WORD */
794 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* PT_CLIST */
795 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 } /* PT_UCNC */
798 /* This table is used to check whether auto-possessification is possible
799 between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
800 specifies a general category and the other specifies a particular category. The
801 row is selected by the general category and the column by the particular
802 category. The value is 1 if the particular category is not part of the general
803 category. */
805 static const pcre_uint8 catposstab[7][30] = {
806 /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
807 { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* C */
808 { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* L */
809 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* M */
810 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, /* N */
811 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 }, /* P */
812 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, /* S */
813 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 } /* Z */
816 /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
817 a general or particular category. The properties in each row are those
818 that apply to the character set in question. Duplication means that a little
819 unnecessary work is done when checking, but this keeps things much simpler
820 because they can all use the same code. For more details see the comment where
821 this table is used.
823 Note: SPACE and PXSPACE used to be different because Perl excluded VT from
824 "space", but from Perl 5.18 it's included, so both categories are treated the
825 same here. */
827 static const pcre_uint8 posspropstab[3][4] = {
828 { ucp_L, ucp_N, ucp_N, ucp_Nl }, /* ALNUM, 3rd and 4th values redundant */
829 { ucp_Z, ucp_Z, ucp_C, ucp_Cc }, /* SPACE and PXSPACE, 2nd value redundant */
830 { ucp_L, ucp_N, ucp_P, ucp_Po } /* WORD */
833 /* This table is used when converting repeating opcodes into possessified
834 versions as a result of an explicit possessive quantifier such as ++. A zero
835 value means there is no possessified version - in those cases the item in
836 question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
837 because all relevant opcodes are less than that. */
839 static const pcre_uint8 opcode_possessify[] = {
840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 - 15 */
841 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 - 31 */
843 0, /* NOTI */
844 OP_POSSTAR, 0, /* STAR, MINSTAR */
845 OP_POSPLUS, 0, /* PLUS, MINPLUS */
846 OP_POSQUERY, 0, /* QUERY, MINQUERY */
847 OP_POSUPTO, 0, /* UPTO, MINUPTO */
848 0, /* EXACT */
849 0, 0, 0, 0, /* POS{STAR,PLUS,QUERY,UPTO} */
851 OP_POSSTARI, 0, /* STARI, MINSTARI */
852 OP_POSPLUSI, 0, /* PLUSI, MINPLUSI */
853 OP_POSQUERYI, 0, /* QUERYI, MINQUERYI */
854 OP_POSUPTOI, 0, /* UPTOI, MINUPTOI */
855 0, /* EXACTI */
856 0, 0, 0, 0, /* POS{STARI,PLUSI,QUERYI,UPTOI} */
858 OP_NOTPOSSTAR, 0, /* NOTSTAR, NOTMINSTAR */
859 OP_NOTPOSPLUS, 0, /* NOTPLUS, NOTMINPLUS */
860 OP_NOTPOSQUERY, 0, /* NOTQUERY, NOTMINQUERY */
861 OP_NOTPOSUPTO, 0, /* NOTUPTO, NOTMINUPTO */
862 0, /* NOTEXACT */
863 0, 0, 0, 0, /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
865 OP_NOTPOSSTARI, 0, /* NOTSTARI, NOTMINSTARI */
866 OP_NOTPOSPLUSI, 0, /* NOTPLUSI, NOTMINPLUSI */
867 OP_NOTPOSQUERYI, 0, /* NOTQUERYI, NOTMINQUERYI */
868 OP_NOTPOSUPTOI, 0, /* NOTUPTOI, NOTMINUPTOI */
869 0, /* NOTEXACTI */
870 0, 0, 0, 0, /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
872 OP_TYPEPOSSTAR, 0, /* TYPESTAR, TYPEMINSTAR */
873 OP_TYPEPOSPLUS, 0, /* TYPEPLUS, TYPEMINPLUS */
874 OP_TYPEPOSQUERY, 0, /* TYPEQUERY, TYPEMINQUERY */
875 OP_TYPEPOSUPTO, 0, /* TYPEUPTO, TYPEMINUPTO */
876 0, /* TYPEEXACT */
877 0, 0, 0, 0, /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
879 OP_CRPOSSTAR, 0, /* CRSTAR, CRMINSTAR */
880 OP_CRPOSPLUS, 0, /* CRPLUS, CRMINPLUS */
881 OP_CRPOSQUERY, 0, /* CRQUERY, CRMINQUERY */
882 OP_CRPOSRANGE, 0, /* CRRANGE, CRMINRANGE */
883 0, 0, 0, 0, /* CRPOS{STAR,PLUS,QUERY,RANGE} */
885 0, 0, 0, /* CLASS, NCLASS, XCLASS */
886 0, 0, /* REF, REFI */
887 0, 0, /* DNREF, DNREFI */
888 0, 0 /* RECURSE, CALLOUT */
893 /*************************************************
894 * Find an error text *
895 *************************************************/
897 /* The error texts are now all in one long string, to save on relocations. As
898 some of the text is of unknown length, we can't use a table of offsets.
899 Instead, just count through the strings. This is not a performance issue
900 because it happens only when there has been a compilation error.
902 Argument: the error number
903 Returns: pointer to the error string
906 static const char *
907 find_error_text(int n)
909 const char *s = error_texts;
910 for (; n > 0; n--)
912 while (*s++ != CHAR_NULL) {};
913 if (*s == CHAR_NULL) return "Error text not found (please report)";
915 return s;
920 /*************************************************
921 * Expand the workspace *
922 *************************************************/
924 /* This function is called during the second compiling phase, if the number of
925 forward references fills the existing workspace, which is originally a block on
926 the stack. A larger block is obtained from malloc() unless the ultimate limit
927 has been reached or the increase will be rather small.
929 Argument: pointer to the compile data block
930 Returns: 0 if all went well, else an error number
933 static int
934 expand_workspace(compile_data *cd)
936 pcre_uchar *newspace;
937 int newsize = cd->workspace_size * 2;
939 if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
940 if (cd->workspace_size >= COMPILE_WORK_SIZE_MAX ||
941 newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
942 return ERR72;
944 newspace = (PUBL(malloc))(IN_UCHARS(newsize));
945 if (newspace == NULL) return ERR21;
946 memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
947 cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
948 if (cd->workspace_size > COMPILE_WORK_SIZE)
949 (PUBL(free))((void *)cd->start_workspace);
950 cd->start_workspace = newspace;
951 cd->workspace_size = newsize;
952 return 0;
957 /*************************************************
958 * Check for counted repeat *
959 *************************************************/
961 /* This function is called when a '{' is encountered in a place where it might
962 start a quantifier. It looks ahead to see if it really is a quantifier or not.
963 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
964 where the ddds are digits.
966 Arguments:
967 p pointer to the first char after '{'
969 Returns: TRUE or FALSE
972 static BOOL
973 is_counted_repeat(const pcre_uchar *p, BOOL basicre)
975 if (!IS_DIGIT(*p)) return FALSE;
976 p++;
977 while (IS_DIGIT(*p)) p++;
978 if ((*p == CHAR_RIGHT_CURLY_BRACKET && !basicre) ||
979 (basicre && *p == CHAR_BACKSLASH && p[1] == CHAR_RIGHT_CURLY_BRACKET))
980 return TRUE;
982 if (*p++ != CHAR_COMMA) return FALSE;
983 if ((*p == CHAR_RIGHT_CURLY_BRACKET && !basicre) ||
984 (basicre && *p == CHAR_BACKSLASH && p[1] == CHAR_RIGHT_CURLY_BRACKET))
985 return TRUE;
987 if (!IS_DIGIT(*p)) return FALSE;
988 p++;
989 while (IS_DIGIT(*p)) p++;
991 return ((*p == CHAR_RIGHT_CURLY_BRACKET && !basicre) ||
992 (basicre && *p == CHAR_BACKSLASH && p[1] == CHAR_RIGHT_CURLY_BRACKET));
997 /*************************************************
998 * Handle escapes *
999 *************************************************/
1001 /* This function is called when a \ has been encountered. It either returns a
1002 positive value for a simple escape such as \n, or 0 for a data character which
1003 will be placed in chptr. A backreference to group n is returned as negative n.
1004 When UTF-8 is enabled, a positive value greater than 255 may be returned in
1005 chptr. On entry, ptr is pointing at the \. On exit, it is on the final
1006 character of the escape sequence.
1008 Arguments:
1009 ptrptr points to the pattern position pointer
1010 chptr points to a returned data character
1011 errorcodeptr points to the errorcode variable
1012 bracount number of previous extracting brackets
1013 options the options bits
1014 isclass TRUE if inside a character class
1016 Returns: zero => a data character
1017 positive => a special escape sequence
1018 negative => a back reference
1019 on error, errorcodeptr is set
1022 static int
1023 check_escape(const pcre_uchar **ptrptr, pcre_uint32 *chptr, int *errorcodeptr,
1024 int bracount, int options, BOOL isclass)
1026 /* PCRE_UTF16 has the same value as PCRE_UTF8. */
1027 BOOL utf = (options & PCRE_UTF8) != 0;
1028 const pcre_uchar *ptr = *ptrptr + 1;
1029 pcre_uint32 c;
1030 int escape = 0;
1031 int i;
1033 GETCHARINCTEST(c, ptr); /* Get character value, increment pointer */
1034 ptr--; /* Set pointer back to the last byte */
1036 /* If backslash is at the end of the pattern, it's an error. */
1038 if (c == CHAR_NULL) *errorcodeptr = ERR1;
1040 /* Non-alphanumerics are literals. For digits or letters, do an initial lookup
1041 in a table. A non-zero result is something that can be returned immediately.
1042 Otherwise further processing may be required. */
1044 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1045 /* Not alphanumeric */
1046 else if (c < CHAR_0 || c > CHAR_z) {}
1047 else if ((i = escapes[c - CHAR_0]) != 0)
1048 { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1050 #else /* EBCDIC coding */
1051 /* Not alphanumeric */
1052 else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
1053 else if ((i = escapes[c - 0x48]) != 0) { if (i > 0) c = (pcre_uint32)i; else escape = -i; }
1054 #endif
1056 /* Escapes that need further processing, or are illegal. */
1058 else
1060 const pcre_uchar *oldptr;
1061 BOOL braced, negated, overflow;
1062 int s;
1064 switch (c)
1066 /* A number of Perl escapes are not handled by PCRE. We give an explicit
1067 error. */
1069 case CHAR_l:
1070 case CHAR_L:
1071 *errorcodeptr = ERR37;
1072 break;
1074 case CHAR_u:
1075 if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1077 /* In JavaScript, \u must be followed by four hexadecimal numbers.
1078 Otherwise it is a lowercase u letter. */
1079 if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1080 && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
1081 && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
1082 && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
1084 c = 0;
1085 for (i = 0; i < 4; ++i)
1087 register pcre_uint32 cc = *(++ptr);
1088 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1089 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1090 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1091 #else /* EBCDIC coding */
1092 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
1093 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1094 #endif
1097 #if defined COMPILE_PCRE8
1098 if (c > (utf ? 0x10ffffU : 0xffU))
1099 #elif defined COMPILE_PCRE16
1100 if (c > (utf ? 0x10ffffU : 0xffffU))
1101 #elif defined COMPILE_PCRE32
1102 if (utf && c > 0x10ffffU)
1103 #endif
1105 *errorcodeptr = ERR76;
1107 else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1110 else
1111 *errorcodeptr = ERR37;
1112 break;
1114 case CHAR_U:
1115 /* In JavaScript, \U is an uppercase U letter. */
1116 if ((options & PCRE_JAVASCRIPT_COMPAT) == 0) *errorcodeptr = ERR37;
1117 break;
1119 /* In a character class, \g is just a literal "g". Outside a character
1120 class, \g must be followed by one of a number of specific things:
1122 (1) A number, either plain or braced. If positive, it is an absolute
1123 backreference. If negative, it is a relative backreference. This is a Perl
1124 5.10 feature.
1126 (2) Perl 5.10 also supports \g{name} as a reference to a named group. This
1127 is part of Perl's movement towards a unified syntax for back references. As
1128 this is synonymous with \k{name}, we fudge it up by pretending it really
1129 was \k.
1131 (3) For Oniguruma compatibility we also support \g followed by a name or a
1132 number either in angle brackets or in single quotes. However, these are
1133 (possibly recursive) subroutine calls, _not_ backreferences. Just return
1134 the ESC_g code (cf \k). */
1136 case CHAR_g:
1137 if (isclass) break;
1138 if (ptr[1] == CHAR_LESS_THAN_SIGN || ptr[1] == CHAR_APOSTROPHE)
1140 escape = ESC_g;
1141 break;
1144 /* Handle the Perl-compatible cases */
1146 if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1148 const pcre_uchar *p;
1149 for (p = ptr+2; *p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
1150 if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
1151 if (*p != CHAR_NULL && *p != CHAR_RIGHT_CURLY_BRACKET)
1153 escape = ESC_k;
1154 break;
1156 braced = TRUE;
1157 ptr++;
1159 else braced = FALSE;
1161 if (ptr[1] == CHAR_MINUS)
1163 negated = TRUE;
1164 ptr++;
1166 else negated = FALSE;
1168 /* The integer range is limited by the machine's int representation. */
1169 s = 0;
1170 overflow = FALSE;
1171 while (IS_DIGIT(ptr[1]))
1173 if (s > INT_MAX / 10 - 1) /* Integer overflow */
1175 overflow = TRUE;
1176 break;
1178 s = s * 10 + (int)(*(++ptr) - CHAR_0);
1180 if (overflow) /* Integer overflow */
1182 while (IS_DIGIT(ptr[1]))
1183 ptr++;
1184 *errorcodeptr = ERR61;
1185 break;
1188 if (braced && *(++ptr) != CHAR_RIGHT_CURLY_BRACKET)
1190 *errorcodeptr = ERR57;
1191 break;
1194 if (s == 0)
1196 *errorcodeptr = ERR58;
1197 break;
1200 if (negated)
1202 if (s > bracount)
1204 *errorcodeptr = ERR15;
1205 break;
1207 s = bracount - (s - 1);
1210 escape = -s;
1211 break;
1213 /* The handling of escape sequences consisting of a string of digits
1214 starting with one that is not zero is not straightforward. Perl has changed
1215 over the years. Nowadays \g{} for backreferences and \o{} for octal are
1216 recommended to avoid the ambiguities in the old syntax.
1218 Outside a character class, the digits are read as a decimal number. If the
1219 number is less than 8 (used to be 10), or if there are that many previous
1220 extracting left brackets, then it is a back reference. Otherwise, up to
1221 three octal digits are read to form an escaped byte. Thus \123 is likely to
1222 be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1223 the octal value is greater than 377, the least significant 8 bits are
1224 taken. \8 and \9 are treated as the literal characters 8 and 9.
1226 Inside a character class, \ followed by a digit is always either a literal
1227 8 or 9 or an octal number. */
1229 case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1230 case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
1232 if (!isclass)
1234 oldptr = ptr;
1235 /* The integer range is limited by the machine's int representation. */
1236 s = (int)(c -CHAR_0);
1237 overflow = FALSE;
1238 while (IS_DIGIT(ptr[1]))
1240 if (s > INT_MAX / 10 - 1) /* Integer overflow */
1242 overflow = TRUE;
1243 break;
1245 s = s * 10 + (int)(*(++ptr) - CHAR_0);
1247 if (overflow) /* Integer overflow */
1249 while (IS_DIGIT(ptr[1]))
1250 ptr++;
1251 *errorcodeptr = ERR61;
1252 break;
1254 if (s < 8 || s <= bracount) /* Check for back reference */
1256 escape = -s;
1257 break;
1259 ptr = oldptr; /* Put the pointer back and fall through */
1262 /* Handle a digit following \ when the number is not a back reference. If
1263 the first digit is 8 or 9, Perl used to generate a binary zero byte and
1264 then treat the digit as a following literal. At least by Perl 5.18 this
1265 changed so as not to insert the binary zero. */
1267 if ((c = *ptr) >= CHAR_8) break;
1269 /* Fall through with a digit less than 8 */
1271 /* \0 always starts an octal number, but we may drop through to here with a
1272 larger first octal digit. The original code used just to take the least
1273 significant 8 bits of octal numbers (I think this is what early Perls used
1274 to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1275 but no more than 3 octal digits. */
1277 case CHAR_0:
1278 c -= CHAR_0;
1279 while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1280 c = c * 8 + *(++ptr) - CHAR_0;
1281 #ifdef COMPILE_PCRE8
1282 if (!utf && c > 0xff) *errorcodeptr = ERR51;
1283 #endif
1284 break;
1286 /* \o is a relatively new Perl feature, supporting a more general way of
1287 specifying character codes in octal. The only supported form is \o{ddd}. */
1289 case CHAR_o:
1290 if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1291 if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else
1293 ptr += 2;
1294 c = 0;
1295 overflow = FALSE;
1296 while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1298 register pcre_uint32 cc = *ptr++;
1299 if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
1300 #ifdef COMPILE_PCRE32
1301 if (c >= 0x20000000l) { overflow = TRUE; break; }
1302 #endif
1303 c = (c << 3) + cc - CHAR_0 ;
1304 #if defined COMPILE_PCRE8
1305 if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1306 #elif defined COMPILE_PCRE16
1307 if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1308 #elif defined COMPILE_PCRE32
1309 if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1310 #endif
1312 if (overflow)
1314 while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1315 *errorcodeptr = ERR34;
1317 else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1319 if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1321 else *errorcodeptr = ERR80;
1323 break;
1325 /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1326 numbers. Otherwise it is a lowercase x letter. */
1328 case CHAR_x:
1329 if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1331 if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1332 && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1334 c = 0;
1335 for (i = 0; i < 2; ++i)
1337 register pcre_uint32 cc = *(++ptr);
1338 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1339 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1340 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1341 #else /* EBCDIC coding */
1342 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
1343 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1344 #endif
1347 } /* End JavaScript handling */
1349 /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1350 greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1351 digits. If not, { used to be treated as a data character. However, Perl
1352 seems to read hex digits up to the first non-such, and ignore the rest, so
1353 that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1354 now gives an error. */
1356 else
1358 if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1360 ptr += 2;
1361 if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1363 *errorcodeptr = ERR86;
1364 break;
1366 c = 0;
1367 overflow = FALSE;
1368 while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1370 register pcre_uint32 cc = *ptr++;
1371 if (c == 0 && cc == CHAR_0) continue; /* Leading zeroes */
1373 #ifdef COMPILE_PCRE32
1374 if (c >= 0x10000000l) { overflow = TRUE; break; }
1375 #endif
1377 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1378 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1379 c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1380 #else /* EBCDIC coding */
1381 if (cc >= CHAR_a && cc <= CHAR_z) cc += 64; /* Convert to upper case */
1382 c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1383 #endif
1385 #if defined COMPILE_PCRE8
1386 if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1387 #elif defined COMPILE_PCRE16
1388 if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1389 #elif defined COMPILE_PCRE32
1390 if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1391 #endif
1394 if (overflow)
1396 while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1397 *errorcodeptr = ERR34;
1400 else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1402 if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1405 /* If the sequence of hex digits does not end with '}', give an error.
1406 We used just to recognize this construct and fall through to the normal
1407 \x handling, but nowadays Perl gives an error, which seems much more
1408 sensible, so we do too. */
1410 else *errorcodeptr = ERR79;
1411 } /* End of \x{} processing */
1413 /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1415 else
1417 c = 0;
1418 while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1420 pcre_uint32 cc; /* Some compilers don't like */
1421 cc = *(++ptr); /* ++ in initializers */
1422 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1423 if (cc >= CHAR_a) cc -= 32; /* Convert to upper case */
1424 c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1425 #else /* EBCDIC coding */
1426 if (cc <= CHAR_z) cc += 64; /* Convert to upper case */
1427 c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1428 #endif
1430 } /* End of \xdd handling */
1431 } /* End of Perl-style \x handling */
1432 break;
1434 /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
1435 An error is given if the byte following \c is not an ASCII character. This
1436 coding is ASCII-specific, but then the whole concept of \cx is
1437 ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
1439 case CHAR_c:
1440 c = *(++ptr);
1441 if (c == CHAR_NULL)
1443 *errorcodeptr = ERR2;
1444 break;
1446 #ifndef EBCDIC /* ASCII/UTF-8 coding */
1447 if (c > 127) /* Excludes all non-ASCII in either mode */
1449 *errorcodeptr = ERR68;
1450 break;
1452 if (c >= CHAR_a && c <= CHAR_z) c -= 32;
1453 c ^= 0x40;
1454 #else /* EBCDIC coding */
1455 if (c >= CHAR_a && c <= CHAR_z) c += 64;
1456 if (c == CHAR_QUESTION_MARK)
1457 c = ('\\' == 188 && '`' == 74)? 0x5f : 0xff;
1458 else
1460 for (i = 0; i < 32; i++)
1462 if (c == ebcdic_escape_c[i]) break;
1464 if (i < 32) c = i; else *errorcodeptr = ERR68;
1466 #endif
1467 break;
1469 /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
1470 other alphanumeric following \ is an error if PCRE_EXTRA was set;
1471 otherwise, for Perl compatibility, it is a literal. This code looks a bit
1472 odd, but there used to be some cases other than the default, and there may
1473 be again in future, so I haven't "optimized" it. */
1475 default:
1476 if ((options & PCRE_EXTRA) != 0) switch(c)
1478 default:
1479 *errorcodeptr = ERR3;
1480 break;
1482 break;
1486 /* Perl supports \N{name} for character names, as well as plain \N for "not
1487 newline". PCRE does not support \N{name}. However, it does support
1488 quantification such as \N{2,3}. */
1490 if (escape == ESC_N && ptr[1] == CHAR_LEFT_CURLY_BRACKET &&
1491 !is_counted_repeat(ptr+2, FALSE))
1492 *errorcodeptr = ERR37;
1494 /* If PCRE_UCP is set, we change the values for \d etc. */
1496 if ((options & PCRE_UCP) != 0 && escape >= ESC_D && escape <= ESC_w)
1497 escape += (ESC_DU - ESC_D);
1499 /* Set the pointer to the final character before returning. */
1501 *ptrptr = ptr;
1502 *chptr = c;
1503 return escape;
1508 #ifdef SUPPORT_UCP
1509 /*************************************************
1510 * Handle \P and \p *
1511 *************************************************/
1513 /* This function is called after \P or \p has been encountered, provided that
1514 PCRE is compiled with support for Unicode properties. On entry, ptrptr is
1515 pointing at the P or p. On exit, it is pointing at the final character of the
1516 escape sequence.
1518 Argument:
1519 ptrptr points to the pattern position pointer
1520 negptr points to a boolean that is set TRUE for negation else FALSE
1521 ptypeptr points to an unsigned int that is set to the type value
1522 pdataptr points to an unsigned int that is set to the detailed property value
1523 errorcodeptr points to the error code variable
1525 Returns: TRUE if the type value was found, or FALSE for an invalid type
1528 static BOOL
1529 get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, unsigned int *ptypeptr,
1530 unsigned int *pdataptr, int *errorcodeptr)
1532 pcre_uchar c;
1533 int i, bot, top;
1534 const pcre_uchar *ptr = *ptrptr;
1535 pcre_uchar name[32];
1537 c = *(++ptr);
1538 if (c == CHAR_NULL) goto ERROR_RETURN;
1540 *negptr = FALSE;
1542 /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
1543 negation. */
1545 if (c == CHAR_LEFT_CURLY_BRACKET)
1547 if (ptr[1] == CHAR_CIRCUMFLEX_ACCENT)
1549 *negptr = TRUE;
1550 ptr++;
1552 for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1554 c = *(++ptr);
1555 if (c == CHAR_NULL) goto ERROR_RETURN;
1556 if (c == CHAR_RIGHT_CURLY_BRACKET) break;
1557 name[i] = c;
1559 if (c != CHAR_RIGHT_CURLY_BRACKET) goto ERROR_RETURN;
1560 name[i] = 0;
1563 /* Otherwise there is just one following character */
1565 else
1567 name[0] = c;
1568 name[1] = 0;
1571 *ptrptr = ptr;
1573 /* Search for a recognized property name using binary chop */
1575 bot = 0;
1576 top = PRIV(utt_size);
1578 while (bot < top)
1580 int r;
1581 i = (bot + top) >> 1;
1582 r = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1583 if (r == 0)
1585 *ptypeptr = PRIV(utt)[i].type;
1586 *pdataptr = PRIV(utt)[i].value;
1587 return TRUE;
1589 if (r > 0) bot = i + 1; else top = i;
1592 *errorcodeptr = ERR47;
1593 *ptrptr = ptr;
1594 return FALSE;
1596 ERROR_RETURN:
1597 *errorcodeptr = ERR46;
1598 *ptrptr = ptr;
1599 return FALSE;
1601 #endif
1605 /*************************************************
1606 * Read repeat counts *
1607 *************************************************/
1609 /* Read an item of the form {n,m} and return the values. This is called only
1610 after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
1611 so the syntax is guaranteed to be correct, but we need to check the values.
1613 Arguments:
1614 p pointer to first char after '{'
1615 minp pointer to int for min
1616 maxp pointer to int for max
1617 returned as -1 if no max
1618 errorcodeptr points to error code variable
1620 Returns: pointer to '}' on success;
1621 current ptr on error, with errorcodeptr set non-zero
1624 static const pcre_uchar *
1625 read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1627 int min = 0;
1628 int max = -1;
1630 while (IS_DIGIT(*p))
1632 min = min * 10 + (int)(*p++ - CHAR_0);
1633 if (min > 65535)
1635 *errorcodeptr = ERR5;
1636 return p;
1640 if (*p == CHAR_BACKSLASH) ++p;
1641 if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1643 if (*(++p) != CHAR_RIGHT_CURLY_BRACKET && *p != CHAR_BACKSLASH)
1645 max = 0;
1646 while(IS_DIGIT(*p))
1648 max = max * 10 + (int)(*p++ - CHAR_0);
1649 if (max > 65535)
1651 *errorcodeptr = ERR5;
1652 return p;
1655 if (max < min)
1657 *errorcodeptr = ERR4;
1658 return p;
1661 if (*p == CHAR_BACKSLASH) ++p;
1664 *minp = min;
1665 *maxp = max;
1666 return p;
1671 /*************************************************
1672 * Find first significant op code *
1673 *************************************************/
1675 /* This is called by several functions that scan a compiled expression looking
1676 for a fixed first character, or an anchoring op code etc. It skips over things
1677 that do not influence this. For some calls, it makes sense to skip negative
1678 forward and all backward assertions, and also the \b assertion; for others it
1679 does not.
1681 Arguments:
1682 code pointer to the start of the group
1683 skipassert TRUE if certain assertions are to be skipped
1685 Returns: pointer to the first significant opcode
1688 static const pcre_uchar*
1689 first_significant_code(const pcre_uchar *code, BOOL skipassert)
1691 for (;;)
1693 switch ((int)*code)
1695 case OP_ASSERT_NOT:
1696 case OP_ASSERTBACK:
1697 case OP_ASSERTBACK_NOT:
1698 if (!skipassert) return code;
1699 do code += GET(code, 1); while (*code == OP_ALT);
1700 code += PRIV(OP_lengths)[*code];
1701 break;
1703 case OP_WORD_BOUNDARY:
1704 case OP_NOT_WORD_BOUNDARY:
1705 if (!skipassert) return code;
1706 /* Fall through */
1708 case OP_CALLOUT:
1709 case OP_CREF:
1710 case OP_DNCREF:
1711 case OP_RREF:
1712 case OP_DNRREF:
1713 case OP_DEF:
1714 code += PRIV(OP_lengths)[*code];
1715 break;
1717 default:
1718 return code;
1721 /* Control never reaches here */
1726 /*************************************************
1727 * Find the fixed length of a branch *
1728 *************************************************/
1730 /* Scan a branch and compute the fixed length of subject that will match it,
1731 if the length is fixed. This is needed for dealing with backward assertions.
1732 In UTF8 mode, the result is in characters rather than bytes. The branch is
1733 temporarily terminated with OP_END when this function is called.
1735 This function is called when a backward assertion is encountered, so that if it
1736 fails, the error message can point to the correct place in the pattern.
1737 However, we cannot do this when the assertion contains subroutine calls,
1738 because they can be forward references. We solve this by remembering this case
1739 and doing the check at the end; a flag specifies which mode we are running in.
1741 Arguments:
1742 code points to the start of the pattern (the bracket)
1743 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
1744 atend TRUE if called when the pattern is complete
1745 cd the "compile data" structure
1746 recurses chain of recurse_check to catch mutual recursion
1748 Returns: the fixed length,
1749 or -1 if there is no fixed length,
1750 or -2 if \C was encountered (in UTF-8 mode only)
1751 or -3 if an OP_RECURSE item was encountered and atend is FALSE
1752 or -4 if an unknown opcode was encountered (internal error)
1755 static int
1756 find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd,
1757 recurse_check *recurses)
1759 int length = -1;
1760 recurse_check this_recurse;
1761 register int branchlength = 0;
1762 register pcre_uchar *cc = code + 1 + LINK_SIZE;
1764 /* Scan along the opcodes for this branch. If we get to the end of the
1765 branch, check the length against that of the other branches. */
1767 for (;;)
1769 int d;
1770 pcre_uchar *ce, *cs;
1771 register pcre_uchar op = *cc;
1773 switch (op)
1775 /* We only need to continue for OP_CBRA (normal capturing bracket) and
1776 OP_BRA (normal non-capturing bracket) because the other variants of these
1777 opcodes are all concerned with unlimited repeated groups, which of course
1778 are not of fixed length. */
1780 case OP_CBRA:
1781 case OP_BRA:
1782 case OP_ONCE:
1783 case OP_ONCE_NC:
1784 case OP_COND:
1785 d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd,
1786 recurses);
1787 if (d < 0) return d;
1788 branchlength += d;
1789 do cc += GET(cc, 1); while (*cc == OP_ALT);
1790 cc += 1 + LINK_SIZE;
1791 break;
1793 /* Reached end of a branch; if it's a ket it is the end of a nested call.
1794 If it's ALT it is an alternation in a nested call. An ACCEPT is effectively
1795 an ALT. If it is END it's the end of the outer call. All can be handled by
1796 the same code. Note that we must not include the OP_KETRxxx opcodes here,
1797 because they all imply an unlimited repeat. */
1799 case OP_ALT:
1800 case OP_KET:
1801 case OP_END:
1802 case OP_ACCEPT:
1803 case OP_ASSERT_ACCEPT:
1804 if (length < 0) length = branchlength;
1805 else if (length != branchlength) return -1;
1806 if (*cc != OP_ALT) return length;
1807 cc += 1 + LINK_SIZE;
1808 branchlength = 0;
1809 break;
1811 /* A true recursion implies not fixed length, but a subroutine call may
1812 be OK. If the subroutine is a forward reference, we can't deal with
1813 it until the end of the pattern, so return -3. */
1815 case OP_RECURSE:
1816 if (!atend) return -3;
1817 cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1); /* Start subpattern */
1818 do ce += GET(ce, 1); while (*ce == OP_ALT); /* End subpattern */
1819 if (cc > cs && cc < ce) return -1; /* Recursion */
1820 else /* Check for mutual recursion */
1822 recurse_check *r = recurses;
1823 for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
1824 if (r != NULL) return -1; /* Mutual recursion */
1826 this_recurse.prev = recurses;
1827 this_recurse.group = cs;
1828 d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd, &this_recurse);
1829 if (d < 0) return d;
1830 branchlength += d;
1831 cc += 1 + LINK_SIZE;
1832 break;
1834 /* Skip over assertive subpatterns */
1836 case OP_ASSERT:
1837 case OP_ASSERT_NOT:
1838 case OP_ASSERTBACK:
1839 case OP_ASSERTBACK_NOT:
1840 do cc += GET(cc, 1); while (*cc == OP_ALT);
1841 cc += 1 + LINK_SIZE;
1842 break;
1844 /* Skip over things that don't match chars */
1846 case OP_MARK:
1847 case OP_PRUNE_ARG:
1848 case OP_SKIP_ARG:
1849 case OP_THEN_ARG:
1850 cc += cc[1] + PRIV(OP_lengths)[*cc];
1851 break;
1853 case OP_CALLOUT:
1854 case OP_CIRC:
1855 case OP_CIRCM:
1856 case OP_CLOSE:
1857 case OP_COMMIT:
1858 case OP_CREF:
1859 case OP_DEF:
1860 case OP_DNCREF:
1861 case OP_DNRREF:
1862 case OP_DOLL:
1863 case OP_DOLLM:
1864 case OP_EOD:
1865 case OP_EODN:
1866 case OP_FAIL:
1867 case OP_NOT_WORD_BOUNDARY:
1868 case OP_PRUNE:
1869 case OP_REVERSE:
1870 case OP_RREF:
1871 case OP_SET_SOM:
1872 case OP_SKIP:
1873 case OP_SOD:
1874 case OP_SOM:
1875 case OP_THEN:
1876 case OP_WORD_BOUNDARY:
1877 cc += PRIV(OP_lengths)[*cc];
1878 break;
1880 /* Handle literal characters */
1882 case OP_CHAR:
1883 case OP_CHARI:
1884 case OP_NOT:
1885 case OP_NOTI:
1886 branchlength++;
1887 cc += 2;
1888 #ifdef SUPPORT_UTF
1889 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1890 #endif
1891 break;
1893 /* Handle exact repetitions. The count is already in characters, but we
1894 need to skip over a multibyte character in UTF8 mode. */
1896 case OP_EXACT:
1897 case OP_EXACTI:
1898 case OP_NOTEXACT:
1899 case OP_NOTEXACTI:
1900 branchlength += (int)GET2(cc,1);
1901 cc += 2 + IMM2_SIZE;
1902 #ifdef SUPPORT_UTF
1903 if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1904 #endif
1905 break;
1907 case OP_TYPEEXACT:
1908 branchlength += GET2(cc,1);
1909 if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1910 cc += 2;
1911 cc += 1 + IMM2_SIZE + 1;
1912 break;
1914 /* Handle single-char matchers */
1916 case OP_PROP:
1917 case OP_NOTPROP:
1918 cc += 2;
1919 /* Fall through */
1921 case OP_HSPACE:
1922 case OP_VSPACE:
1923 case OP_NOT_HSPACE:
1924 case OP_NOT_VSPACE:
1925 case OP_NOT_DIGIT:
1926 case OP_DIGIT:
1927 case OP_NOT_WHITESPACE:
1928 case OP_WHITESPACE:
1929 case OP_NOT_WORDCHAR:
1930 case OP_WORDCHAR:
1931 case OP_ANY:
1932 case OP_ALLANY:
1933 branchlength++;
1934 cc++;
1935 break;
1937 /* The single-byte matcher isn't allowed. This only happens in UTF-8 mode;
1938 otherwise \C is coded as OP_ALLANY. */
1940 case OP_ANYBYTE:
1941 return -2;
1943 /* Check a class for variable quantification */
1945 case OP_CLASS:
1946 case OP_NCLASS:
1947 #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1948 case OP_XCLASS:
1949 /* The original code caused an unsigned overflow in 64 bit systems,
1950 so now we use a conditional statement. */
1951 if (op == OP_XCLASS)
1952 cc += GET(cc, 1);
1953 else
1954 cc += PRIV(OP_lengths)[OP_CLASS];
1955 #else
1956 cc += PRIV(OP_lengths)[OP_CLASS];
1957 #endif
1959 switch (*cc)
1961 case OP_CRSTAR:
1962 case OP_CRMINSTAR:
1963 case OP_CRPLUS:
1964 case OP_CRMINPLUS:
1965 case OP_CRQUERY:
1966 case OP_CRMINQUERY:
1967 case OP_CRPOSSTAR:
1968 case OP_CRPOSPLUS:
1969 case OP_CRPOSQUERY:
1970 return -1;
1972 case OP_CRRANGE:
1973 case OP_CRMINRANGE:
1974 case OP_CRPOSRANGE:
1975 if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1976 branchlength += (int)GET2(cc,1);
1977 cc += 1 + 2 * IMM2_SIZE;
1978 break;
1980 default:
1981 branchlength++;
1983 break;
1985 /* Anything else is variable length */
1987 case OP_ANYNL:
1988 case OP_BRAMINZERO:
1989 case OP_BRAPOS:
1990 case OP_BRAPOSZERO:
1991 case OP_BRAZERO:
1992 case OP_CBRAPOS:
1993 case OP_EXTUNI:
1994 case OP_KETRMAX:
1995 case OP_KETRMIN:
1996 case OP_KETRPOS:
1997 case OP_MINPLUS:
1998 case OP_MINPLUSI:
1999 case OP_MINQUERY:
2000 case OP_MINQUERYI:
2001 case OP_MINSTAR:
2002 case OP_MINSTARI:
2003 case OP_MINUPTO:
2004 case OP_MINUPTOI:
2005 case OP_NOTMINPLUS:
2006 case OP_NOTMINPLUSI:
2007 case OP_NOTMINQUERY:
2008 case OP_NOTMINQUERYI:
2009 case OP_NOTMINSTAR:
2010 case OP_NOTMINSTARI:
2011 case OP_NOTMINUPTO:
2012 case OP_NOTMINUPTOI:
2013 case OP_NOTPLUS:
2014 case OP_NOTPLUSI:
2015 case OP_NOTPOSPLUS:
2016 case OP_NOTPOSPLUSI:
2017 case OP_NOTPOSQUERY:
2018 case OP_NOTPOSQUERYI:
2019 case OP_NOTPOSSTAR:
2020 case OP_NOTPOSSTARI:
2021 case OP_NOTPOSUPTO:
2022 case OP_NOTPOSUPTOI:
2023 case OP_NOTQUERY:
2024 case OP_NOTQUERYI:
2025 case OP_NOTSTAR:
2026 case OP_NOTSTARI:
2027 case OP_NOTUPTO:
2028 case OP_NOTUPTOI:
2029 case OP_PLUS:
2030 case OP_PLUSI:
2031 case OP_POSPLUS:
2032 case OP_POSPLUSI:
2033 case OP_POSQUERY:
2034 case OP_POSQUERYI:
2035 case OP_POSSTAR:
2036 case OP_POSSTARI:
2037 case OP_POSUPTO:
2038 case OP_POSUPTOI:
2039 case OP_QUERY:
2040 case OP_QUERYI:
2041 case OP_REF:
2042 case OP_REFI:
2043 case OP_DNREF:
2044 case OP_DNREFI:
2045 case OP_SBRA:
2046 case OP_SBRAPOS:
2047 case OP_SCBRA:
2048 case OP_SCBRAPOS:
2049 case OP_SCOND:
2050 case OP_SKIPZERO:
2051 case OP_STAR:
2052 case OP_STARI:
2053 case OP_TYPEMINPLUS:
2054 case OP_TYPEMINQUERY:
2055 case OP_TYPEMINSTAR:
2056 case OP_TYPEMINUPTO:
2057 case OP_TYPEPLUS:
2058 case OP_TYPEPOSPLUS:
2059 case OP_TYPEPOSQUERY:
2060 case OP_TYPEPOSSTAR:
2061 case OP_TYPEPOSUPTO:
2062 case OP_TYPEQUERY:
2063 case OP_TYPESTAR:
2064 case OP_TYPEUPTO:
2065 case OP_UPTO:
2066 case OP_UPTOI:
2067 return -1;
2069 /* Catch unrecognized opcodes so that when new ones are added they
2070 are not forgotten, as has happened in the past. */
2072 default:
2073 return -4;
2076 /* Control never gets here */
2081 /*************************************************
2082 * Scan compiled regex for specific bracket *
2083 *************************************************/
2085 /* This little function scans through a compiled pattern until it finds a
2086 capturing bracket with the given number, or, if the number is negative, an
2087 instance of OP_REVERSE for a lookbehind. The function is global in the C sense
2088 so that it can be called from pcre_study() when finding the minimum matching
2089 length.
2091 Arguments:
2092 code points to start of expression
2093 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
2094 number the required bracket number or negative to find a lookbehind
2096 Returns: pointer to the opcode for the bracket, or NULL if not found
2099 const pcre_uchar *
2100 PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
2102 for (;;)
2104 register pcre_uchar c = *code;
2106 if (c == OP_END) return NULL;
2108 /* XCLASS is used for classes that cannot be represented just by a bit
2109 map. This includes negated single high-valued characters. The length in
2110 the table is zero; the actual length is stored in the compiled code. */
2112 if (c == OP_XCLASS) code += GET(code, 1);
2114 /* Handle recursion */
2116 else if (c == OP_REVERSE)
2118 if (number < 0) return (pcre_uchar *)code;
2119 code += PRIV(OP_lengths)[c];
2122 /* Handle capturing bracket */
2124 else if (c == OP_CBRA || c == OP_SCBRA ||
2125 c == OP_CBRAPOS || c == OP_SCBRAPOS)
2127 int n = (int)GET2(code, 1+LINK_SIZE);
2128 if (n == number) return (pcre_uchar *)code;
2129 code += PRIV(OP_lengths)[c];
2132 /* Otherwise, we can get the item's length from the table, except that for
2133 repeated character types, we have to test for \p and \P, which have an extra
2134 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2135 must add in its length. */
2137 else
2139 switch(c)
2141 case OP_TYPESTAR:
2142 case OP_TYPEMINSTAR:
2143 case OP_TYPEPLUS:
2144 case OP_TYPEMINPLUS:
2145 case OP_TYPEQUERY:
2146 case OP_TYPEMINQUERY:
2147 case OP_TYPEPOSSTAR:
2148 case OP_TYPEPOSPLUS:
2149 case OP_TYPEPOSQUERY:
2150 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2151 break;
2153 case OP_TYPEUPTO:
2154 case OP_TYPEMINUPTO:
2155 case OP_TYPEEXACT:
2156 case OP_TYPEPOSUPTO:
2157 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2158 code += 2;
2159 break;
2161 case OP_MARK:
2162 case OP_PRUNE_ARG:
2163 case OP_SKIP_ARG:
2164 case OP_THEN_ARG:
2165 code += code[1];
2166 break;
2169 /* Add in the fixed length from the table */
2171 code += PRIV(OP_lengths)[c];
2173 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2174 a multi-byte character. The length in the table is a minimum, so we have to
2175 arrange to skip the extra bytes. */
2177 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2178 if (utf) switch(c)
2180 case OP_CHAR:
2181 case OP_CHARI:
2182 case OP_NOT:
2183 case OP_NOTI:
2184 case OP_EXACT:
2185 case OP_EXACTI:
2186 case OP_NOTEXACT:
2187 case OP_NOTEXACTI:
2188 case OP_UPTO:
2189 case OP_UPTOI:
2190 case OP_NOTUPTO:
2191 case OP_NOTUPTOI:
2192 case OP_MINUPTO:
2193 case OP_MINUPTOI:
2194 case OP_NOTMINUPTO:
2195 case OP_NOTMINUPTOI:
2196 case OP_POSUPTO:
2197 case OP_POSUPTOI:
2198 case OP_NOTPOSUPTO:
2199 case OP_NOTPOSUPTOI:
2200 case OP_STAR:
2201 case OP_STARI:
2202 case OP_NOTSTAR:
2203 case OP_NOTSTARI:
2204 case OP_MINSTAR:
2205 case OP_MINSTARI:
2206 case OP_NOTMINSTAR:
2207 case OP_NOTMINSTARI:
2208 case OP_POSSTAR:
2209 case OP_POSSTARI:
2210 case OP_NOTPOSSTAR:
2211 case OP_NOTPOSSTARI:
2212 case OP_PLUS:
2213 case OP_PLUSI:
2214 case OP_NOTPLUS:
2215 case OP_NOTPLUSI:
2216 case OP_MINPLUS:
2217 case OP_MINPLUSI:
2218 case OP_NOTMINPLUS:
2219 case OP_NOTMINPLUSI:
2220 case OP_POSPLUS:
2221 case OP_POSPLUSI:
2222 case OP_NOTPOSPLUS:
2223 case OP_NOTPOSPLUSI:
2224 case OP_QUERY:
2225 case OP_QUERYI:
2226 case OP_NOTQUERY:
2227 case OP_NOTQUERYI:
2228 case OP_MINQUERY:
2229 case OP_MINQUERYI:
2230 case OP_NOTMINQUERY:
2231 case OP_NOTMINQUERYI:
2232 case OP_POSQUERY:
2233 case OP_POSQUERYI:
2234 case OP_NOTPOSQUERY:
2235 case OP_NOTPOSQUERYI:
2236 if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2237 break;
2239 #else
2240 (void)(utf); /* Keep compiler happy by referencing function argument */
2241 #endif
2248 /*************************************************
2249 * Scan compiled regex for recursion reference *
2250 *************************************************/
2252 /* This little function scans through a compiled pattern until it finds an
2253 instance of OP_RECURSE.
2255 Arguments:
2256 code points to start of expression
2257 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
2259 Returns: pointer to the opcode for OP_RECURSE, or NULL if not found
2262 static const pcre_uchar *
2263 find_recurse(const pcre_uchar *code, BOOL utf)
2265 for (;;)
2267 register pcre_uchar c = *code;
2268 if (c == OP_END) return NULL;
2269 if (c == OP_RECURSE) return code;
2271 /* XCLASS is used for classes that cannot be represented just by a bit
2272 map. This includes negated single high-valued characters. The length in
2273 the table is zero; the actual length is stored in the compiled code. */
2275 if (c == OP_XCLASS) code += GET(code, 1);
2277 /* Otherwise, we can get the item's length from the table, except that for
2278 repeated character types, we have to test for \p and \P, which have an extra
2279 two bytes of parameters, and for MARK/PRUNE/SKIP/THEN with an argument, we
2280 must add in its length. */
2282 else
2284 switch(c)
2286 case OP_TYPESTAR:
2287 case OP_TYPEMINSTAR:
2288 case OP_TYPEPLUS:
2289 case OP_TYPEMINPLUS:
2290 case OP_TYPEQUERY:
2291 case OP_TYPEMINQUERY:
2292 case OP_TYPEPOSSTAR:
2293 case OP_TYPEPOSPLUS:
2294 case OP_TYPEPOSQUERY:
2295 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2296 break;
2298 case OP_TYPEPOSUPTO:
2299 case OP_TYPEUPTO:
2300 case OP_TYPEMINUPTO:
2301 case OP_TYPEEXACT:
2302 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2303 code += 2;
2304 break;
2306 case OP_MARK:
2307 case OP_PRUNE_ARG:
2308 case OP_SKIP_ARG:
2309 case OP_THEN_ARG:
2310 code += code[1];
2311 break;
2314 /* Add in the fixed length from the table */
2316 code += PRIV(OP_lengths)[c];
2318 /* In UTF-8 mode, opcodes that are followed by a character may be followed
2319 by a multi-byte character. The length in the table is a minimum, so we have
2320 to arrange to skip the extra bytes. */
2322 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2323 if (utf) switch(c)
2325 case OP_CHAR:
2326 case OP_CHARI:
2327 case OP_NOT:
2328 case OP_NOTI:
2329 case OP_EXACT:
2330 case OP_EXACTI:
2331 case OP_NOTEXACT:
2332 case OP_NOTEXACTI:
2333 case OP_UPTO:
2334 case OP_UPTOI:
2335 case OP_NOTUPTO:
2336 case OP_NOTUPTOI:
2337 case OP_MINUPTO:
2338 case OP_MINUPTOI:
2339 case OP_NOTMINUPTO:
2340 case OP_NOTMINUPTOI:
2341 case OP_POSUPTO:
2342 case OP_POSUPTOI:
2343 case OP_NOTPOSUPTO:
2344 case OP_NOTPOSUPTOI:
2345 case OP_STAR:
2346 case OP_STARI:
2347 case OP_NOTSTAR:
2348 case OP_NOTSTARI:
2349 case OP_MINSTAR:
2350 case OP_MINSTARI:
2351 case OP_NOTMINSTAR:
2352 case OP_NOTMINSTARI:
2353 case OP_POSSTAR:
2354 case OP_POSSTARI:
2355 case OP_NOTPOSSTAR:
2356 case OP_NOTPOSSTARI:
2357 case OP_PLUS:
2358 case OP_PLUSI:
2359 case OP_NOTPLUS:
2360 case OP_NOTPLUSI:
2361 case OP_MINPLUS:
2362 case OP_MINPLUSI:
2363 case OP_NOTMINPLUS:
2364 case OP_NOTMINPLUSI:
2365 case OP_POSPLUS:
2366 case OP_POSPLUSI:
2367 case OP_NOTPOSPLUS:
2368 case OP_NOTPOSPLUSI:
2369 case OP_QUERY:
2370 case OP_QUERYI:
2371 case OP_NOTQUERY:
2372 case OP_NOTQUERYI:
2373 case OP_MINQUERY:
2374 case OP_MINQUERYI:
2375 case OP_NOTMINQUERY:
2376 case OP_NOTMINQUERYI:
2377 case OP_POSQUERY:
2378 case OP_POSQUERYI:
2379 case OP_NOTPOSQUERY:
2380 case OP_NOTPOSQUERYI:
2381 if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2382 break;
2384 #else
2385 (void)(utf); /* Keep compiler happy by referencing function argument */
2386 #endif
2393 /*************************************************
2394 * Scan compiled branch for non-emptiness *
2395 *************************************************/
2397 /* This function scans through a branch of a compiled pattern to see whether it
2398 can match the empty string or not. It is called from could_be_empty()
2399 below and from compile_branch() when checking for an unlimited repeat of a
2400 group that can match nothing. Note that first_significant_code() skips over
2401 backward and negative forward assertions when its final argument is TRUE. If we
2402 hit an unclosed bracket, we return "empty" - this means we've struck an inner
2403 bracket whose current branch will already have been scanned.
2405 Arguments:
2406 code points to start of search
2407 endcode points to where to stop
2408 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2409 cd contains pointers to tables etc.
2410 recurses chain of recurse_check to catch mutual recursion
2412 Returns: TRUE if what is matched could be empty
2415 static BOOL
2416 could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2417 BOOL utf, compile_data *cd, recurse_check *recurses)
2419 register pcre_uchar c;
2420 recurse_check this_recurse;
2422 for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2423 code < endcode;
2424 code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2426 const pcre_uchar *ccode;
2428 c = *code;
2430 /* Skip over forward assertions; the other assertions are skipped by
2431 first_significant_code() with a TRUE final argument. */
2433 if (c == OP_ASSERT)
2435 do code += GET(code, 1); while (*code == OP_ALT);
2436 c = *code;
2437 continue;
2440 /* For a recursion/subroutine call, if its end has been reached, which
2441 implies a backward reference subroutine call, we can scan it. If it's a
2442 forward reference subroutine call, we can't. To detect forward reference
2443 we have to scan up the list that is kept in the workspace. This function is
2444 called only when doing the real compile, not during the pre-compile that
2445 measures the size of the compiled pattern. */
2447 if (c == OP_RECURSE)
2449 const pcre_uchar *scode = cd->start_code + GET(code, 1);
2450 const pcre_uchar *endgroup = scode;
2451 BOOL empty_branch;
2453 /* Test for forward reference or uncompleted reference. This is disabled
2454 when called to scan a completed pattern by setting cd->start_workspace to
2455 NULL. */
2457 if (cd->start_workspace != NULL)
2459 const pcre_uchar *tcode;
2460 for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2461 if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2462 if (GET(scode, 1) == 0) return TRUE; /* Unclosed */
2465 /* If the reference is to a completed group, we need to detect whether this
2466 is a recursive call, as otherwise there will be an infinite loop. If it is
2467 a recursion, just skip over it. Simple recursions are easily detected. For
2468 mutual recursions we keep a chain on the stack. */
2470 do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2471 if (code >= scode && code <= endgroup) continue; /* Simple recursion */
2472 else
2474 recurse_check *r = recurses;
2475 for (r = recurses; r != NULL; r = r->prev)
2476 if (r->group == scode) break;
2477 if (r != NULL) continue; /* Mutual recursion */
2480 /* Completed reference; scan the referenced group, remembering it on the
2481 stack chain to detect mutual recursions. */
2483 empty_branch = FALSE;
2484 this_recurse.prev = recurses;
2485 this_recurse.group = scode;
2489 if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2491 empty_branch = TRUE;
2492 break;
2494 scode += GET(scode, 1);
2496 while (*scode == OP_ALT);
2498 if (!empty_branch) return FALSE; /* All branches are non-empty */
2499 continue;
2502 /* Groups with zero repeats can of course be empty; skip them. */
2504 if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2505 c == OP_BRAPOSZERO)
2507 code += PRIV(OP_lengths)[c];
2508 do code += GET(code, 1); while (*code == OP_ALT);
2509 c = *code;
2510 continue;
2513 /* A nested group that is already marked as "could be empty" can just be
2514 skipped. */
2516 if (c == OP_SBRA || c == OP_SBRAPOS ||
2517 c == OP_SCBRA || c == OP_SCBRAPOS)
2519 do code += GET(code, 1); while (*code == OP_ALT);
2520 c = *code;
2521 continue;
2524 /* For other groups, scan the branches. */
2526 if (c == OP_BRA || c == OP_BRAPOS ||
2527 c == OP_CBRA || c == OP_CBRAPOS ||
2528 c == OP_ONCE || c == OP_ONCE_NC ||
2529 c == OP_COND || c == OP_SCOND)
2531 BOOL empty_branch;
2532 if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
2534 /* If a conditional group has only one branch, there is a second, implied,
2535 empty branch, so just skip over the conditional, because it could be empty.
2536 Otherwise, scan the individual branches of the group. */
2538 if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2539 code += GET(code, 1);
2540 else
2542 empty_branch = FALSE;
2545 if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd,
2546 recurses)) empty_branch = TRUE;
2547 code += GET(code, 1);
2549 while (*code == OP_ALT);
2550 if (!empty_branch) return FALSE; /* All branches are non-empty */
2553 c = *code;
2554 continue;
2557 /* Handle the other opcodes */
2559 switch (c)
2561 /* Check for quantifiers after a class. XCLASS is used for classes that
2562 cannot be represented just by a bit map. This includes negated single
2563 high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2564 actual length is stored in the compiled code, so we must update "code"
2565 here. */
2567 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2568 case OP_XCLASS:
2569 ccode = code += GET(code, 1);
2570 goto CHECK_CLASS_REPEAT;
2571 #endif
2573 case OP_CLASS:
2574 case OP_NCLASS:
2575 ccode = code + PRIV(OP_lengths)[OP_CLASS];
2577 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2578 CHECK_CLASS_REPEAT:
2579 #endif
2581 switch (*ccode)
2583 case OP_CRSTAR: /* These could be empty; continue */
2584 case OP_CRMINSTAR:
2585 case OP_CRQUERY:
2586 case OP_CRMINQUERY:
2587 case OP_CRPOSSTAR:
2588 case OP_CRPOSQUERY:
2589 break;
2591 default: /* Non-repeat => class must match */
2592 case OP_CRPLUS: /* These repeats aren't empty */
2593 case OP_CRMINPLUS:
2594 case OP_CRPOSPLUS:
2595 return FALSE;
2597 case OP_CRRANGE:
2598 case OP_CRMINRANGE:
2599 case OP_CRPOSRANGE:
2600 if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
2601 break;
2603 break;
2605 /* Opcodes that must match a character */
2607 case OP_ANY:
2608 case OP_ALLANY:
2609 case OP_ANYBYTE:
2611 case OP_PROP:
2612 case OP_NOTPROP:
2613 case OP_ANYNL:
2615 case OP_NOT_HSPACE:
2616 case OP_HSPACE:
2617 case OP_NOT_VSPACE:
2618 case OP_VSPACE:
2619 case OP_EXTUNI:
2621 case OP_NOT_DIGIT:
2622 case OP_DIGIT:
2623 case OP_NOT_WHITESPACE:
2624 case OP_WHITESPACE:
2625 case OP_NOT_WORDCHAR:
2626 case OP_WORDCHAR:
2628 case OP_CHAR:
2629 case OP_CHARI:
2630 case OP_NOT:
2631 case OP_NOTI:
2633 case OP_PLUS:
2634 case OP_PLUSI:
2635 case OP_MINPLUS:
2636 case OP_MINPLUSI:
2638 case OP_NOTPLUS:
2639 case OP_NOTPLUSI:
2640 case OP_NOTMINPLUS:
2641 case OP_NOTMINPLUSI:
2643 case OP_POSPLUS:
2644 case OP_POSPLUSI:
2645 case OP_NOTPOSPLUS:
2646 case OP_NOTPOSPLUSI:
2648 case OP_EXACT:
2649 case OP_EXACTI:
2650 case OP_NOTEXACT:
2651 case OP_NOTEXACTI:
2653 case OP_TYPEPLUS:
2654 case OP_TYPEMINPLUS:
2655 case OP_TYPEPOSPLUS:
2656 case OP_TYPEEXACT:
2658 return FALSE;
2660 /* These are going to continue, as they may be empty, but we have to
2661 fudge the length for the \p and \P cases. */
2663 case OP_TYPESTAR:
2664 case OP_TYPEMINSTAR:
2665 case OP_TYPEPOSSTAR:
2666 case OP_TYPEQUERY:
2667 case OP_TYPEMINQUERY:
2668 case OP_TYPEPOSQUERY:
2669 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2670 break;
2672 /* Same for these */
2674 case OP_TYPEUPTO:
2675 case OP_TYPEMINUPTO:
2676 case OP_TYPEPOSUPTO:
2677 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2678 code += 2;
2679 break;
2681 /* End of branch */
2683 case OP_KET:
2684 case OP_KETRMAX:
2685 case OP_KETRMIN:
2686 case OP_KETRPOS:
2687 case OP_ALT:
2688 return TRUE;
2690 /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2691 MINUPTO, and POSUPTO and their caseless and negative versions may be
2692 followed by a multibyte character. */
2694 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2695 case OP_STAR:
2696 case OP_STARI:
2697 case OP_NOTSTAR:
2698 case OP_NOTSTARI:
2700 case OP_MINSTAR:
2701 case OP_MINSTARI:
2702 case OP_NOTMINSTAR:
2703 case OP_NOTMINSTARI:
2705 case OP_POSSTAR:
2706 case OP_POSSTARI:
2707 case OP_NOTPOSSTAR:
2708 case OP_NOTPOSSTARI:
2710 case OP_QUERY:
2711 case OP_QUERYI:
2712 case OP_NOTQUERY:
2713 case OP_NOTQUERYI:
2715 case OP_MINQUERY:
2716 case OP_MINQUERYI:
2717 case OP_NOTMINQUERY:
2718 case OP_NOTMINQUERYI:
2720 case OP_POSQUERY:
2721 case OP_POSQUERYI:
2722 case OP_NOTPOSQUERY:
2723 case OP_NOTPOSQUERYI:
2725 if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2726 break;
2728 case OP_UPTO:
2729 case OP_UPTOI:
2730 case OP_NOTUPTO:
2731 case OP_NOTUPTOI:
2733 case OP_MINUPTO:
2734 case OP_MINUPTOI:
2735 case OP_NOTMINUPTO:
2736 case OP_NOTMINUPTOI:
2738 case OP_POSUPTO:
2739 case OP_POSUPTOI:
2740 case OP_NOTPOSUPTO:
2741 case OP_NOTPOSUPTOI:
2743 if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2744 break;
2745 #endif
2747 /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2748 string. */
2750 case OP_MARK:
2751 case OP_PRUNE_ARG:
2752 case OP_SKIP_ARG:
2753 case OP_THEN_ARG:
2754 code += code[1];
2755 break;
2757 /* None of the remaining opcodes are required to match a character. */
2759 default:
2760 break;
2764 return TRUE;
2769 /*************************************************
2770 * Scan compiled regex for non-emptiness *
2771 *************************************************/
2773 /* This function is called to check for left recursive calls. We want to check
2774 the current branch of the current pattern to see if it could match the empty
2775 string. If it could, we must look outwards for branches at other levels,
2776 stopping when we pass beyond the bracket which is the subject of the recursion.
2777 This function is called only during the real compile, not during the
2778 pre-compile.
2780 Arguments:
2781 code points to start of the recursion
2782 endcode points to where to stop (current RECURSE item)
2783 bcptr points to the chain of current (unclosed) branch starts
2784 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2785 cd pointers to tables etc
2787 Returns: TRUE if what is matched could be empty
2790 static BOOL
2791 could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2792 branch_chain *bcptr, BOOL utf, compile_data *cd)
2794 while (bcptr != NULL && bcptr->current_branch >= code)
2796 if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2797 return FALSE;
2798 bcptr = bcptr->outer;
2800 return TRUE;
2805 /*************************************************
2806 * Base opcode of repeated opcodes *
2807 *************************************************/
2809 /* Returns the base opcode for repeated single character type opcodes. If the
2810 opcode is not a repeated character type, it returns with the original value.
2812 Arguments: c opcode
2813 Returns: base opcode for the type
2816 static pcre_uchar
2817 get_repeat_base(pcre_uchar c)
2819 return (c > OP_TYPEPOSUPTO)? c :
2820 (c >= OP_TYPESTAR)? OP_TYPESTAR :
2821 (c >= OP_NOTSTARI)? OP_NOTSTARI :
2822 (c >= OP_NOTSTAR)? OP_NOTSTAR :
2823 (c >= OP_STARI)? OP_STARI :
2824 OP_STAR;
2829 #ifdef SUPPORT_UCP
2830 /*************************************************
2831 * Check a character and a property *
2832 *************************************************/
2834 /* This function is called by check_auto_possessive() when a property item
2835 is adjacent to a fixed character.
2837 Arguments:
2838 c the character
2839 ptype the property type
2840 pdata the data for the type
2841 negated TRUE if it's a negated property (\P or \p{^)
2843 Returns: TRUE if auto-possessifying is OK
2846 static BOOL
2847 check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2848 BOOL negated)
2850 const pcre_uint32 *p;
2851 const ucd_record *prop = GET_UCD(c);
2853 switch(ptype)
2855 case PT_LAMP:
2856 return (prop->chartype == ucp_Lu ||
2857 prop->chartype == ucp_Ll ||
2858 prop->chartype == ucp_Lt) == negated;
2860 case PT_GC:
2861 return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2863 case PT_PC:
2864 return (pdata == prop->chartype) == negated;
2866 case PT_SC:
2867 return (pdata == prop->script) == negated;
2869 /* These are specials */
2871 case PT_ALNUM:
2872 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2873 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2875 /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2876 means that Perl space and POSIX space are now identical. PCRE was changed
2877 at release 8.34. */
2879 case PT_SPACE: /* Perl space */
2880 case PT_PXSPACE: /* POSIX space */
2881 switch(c)
2883 HSPACE_CASES:
2884 VSPACE_CASES:
2885 return negated;
2887 default:
2888 return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2890 break; /* Control never reaches here */
2892 case PT_WORD:
2893 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2894 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2895 c == CHAR_UNDERSCORE) == negated;
2897 case PT_CLIST:
2898 p = PRIV(ucd_caseless_sets) + prop->caseset;
2899 for (;;)
2901 if (c < *p) return !negated;
2902 if (c == *p++) return negated;
2904 break; /* Control never reaches here */
2907 return FALSE;
2909 #endif /* SUPPORT_UCP */
2913 /*************************************************
2914 * Fill the character property list *
2915 *************************************************/
2917 /* Checks whether the code points to an opcode that can take part in auto-
2918 possessification, and if so, fills a list with its properties.
2920 Arguments:
2921 code points to start of expression
2922 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2923 fcc points to case-flipping table
2924 list points to output list
2925 list[0] will be filled with the opcode
2926 list[1] will be non-zero if this opcode
2927 can match an empty character string
2928 list[2..7] depends on the opcode
2930 Returns: points to the start of the next opcode if *code is accepted
2931 NULL if *code is not accepted
2934 static const pcre_uchar *
2935 get_chr_property_list(const pcre_uchar *code, BOOL utf,
2936 const pcre_uint8 *fcc, pcre_uint32 *list)
2938 pcre_uchar c = *code;
2939 pcre_uchar base;
2940 const pcre_uchar *end;
2941 pcre_uint32 chr;
2943 #ifdef SUPPORT_UCP
2944 pcre_uint32 *clist_dest;
2945 const pcre_uint32 *clist_src;
2946 #else
2947 utf = utf; /* Suppress "unused parameter" compiler warning */
2948 #endif
2950 list[0] = c;
2951 list[1] = FALSE;
2952 code++;
2954 if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2956 base = get_repeat_base(c);
2957 c -= (base - OP_STAR);
2959 if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2960 code += IMM2_SIZE;
2962 list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
2964 switch(base)
2966 case OP_STAR:
2967 list[0] = OP_CHAR;
2968 break;
2970 case OP_STARI:
2971 list[0] = OP_CHARI;
2972 break;
2974 case OP_NOTSTAR:
2975 list[0] = OP_NOT;
2976 break;
2978 case OP_NOTSTARI:
2979 list[0] = OP_NOTI;
2980 break;
2982 case OP_TYPESTAR:
2983 list[0] = *code;
2984 code++;
2985 break;
2987 c = list[0];
2990 switch(c)
2992 case OP_NOT_DIGIT:
2993 case OP_DIGIT:
2994 case OP_NOT_WHITESPACE:
2995 case OP_WHITESPACE:
2996 case OP_NOT_WORDCHAR:
2997 case OP_WORDCHAR:
2998 case OP_ANY:
2999 case OP_ALLANY:
3000 case OP_ANYNL:
3001 case OP_NOT_HSPACE:
3002 case OP_HSPACE:
3003 case OP_NOT_VSPACE:
3004 case OP_VSPACE:
3005 case OP_EXTUNI:
3006 case OP_EODN:
3007 case OP_EOD:
3008 case OP_DOLL:
3009 case OP_DOLLM:
3010 return code;
3012 case OP_CHAR:
3013 case OP_NOT:
3014 GETCHARINCTEST(chr, code);
3015 list[2] = chr;
3016 list[3] = NOTACHAR;
3017 return code;
3019 case OP_CHARI:
3020 case OP_NOTI:
3021 list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
3022 GETCHARINCTEST(chr, code);
3023 list[2] = chr;
3025 #ifdef SUPPORT_UCP
3026 if (chr < 128 || (chr < 256 && !utf))
3027 list[3] = fcc[chr];
3028 else
3029 list[3] = UCD_OTHERCASE(chr);
3030 #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
3031 list[3] = (chr < 256) ? fcc[chr] : chr;
3032 #else
3033 list[3] = fcc[chr];
3034 #endif
3036 /* The othercase might be the same value. */
3038 if (chr == list[3])
3039 list[3] = NOTACHAR;
3040 else
3041 list[4] = NOTACHAR;
3042 return code;
3044 #ifdef SUPPORT_UCP
3045 case OP_PROP:
3046 case OP_NOTPROP:
3047 if (code[0] != PT_CLIST)
3049 list[2] = code[0];
3050 list[3] = code[1];
3051 return code + 2;
3054 /* Convert only if we have enough space. */
3056 clist_src = PRIV(ucd_caseless_sets) + code[1];
3057 clist_dest = list + 2;
3058 code += 2;
3060 do {
3061 if (clist_dest >= list + 8)
3063 /* Early return if there is not enough space. This should never
3064 happen, since all clists are shorter than 5 character now. */
3065 list[2] = code[0];
3066 list[3] = code[1];
3067 return code;
3069 *clist_dest++ = *clist_src;
3071 while(*clist_src++ != NOTACHAR);
3073 /* All characters are stored. The terminating NOTACHAR
3074 is copied form the clist itself. */
3076 list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
3077 return code;
3078 #endif
3080 case OP_NCLASS:
3081 case OP_CLASS:
3082 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3083 case OP_XCLASS:
3084 if (c == OP_XCLASS)
3085 end = code + GET(code, 0) - 1;
3086 else
3087 #endif
3088 end = code + 32 / sizeof(pcre_uchar);
3090 switch(*end)
3092 case OP_CRSTAR:
3093 case OP_CRMINSTAR:
3094 case OP_CRQUERY:
3095 case OP_CRMINQUERY:
3096 case OP_CRPOSSTAR:
3097 case OP_CRPOSQUERY:
3098 list[1] = TRUE;
3099 end++;
3100 break;
3102 case OP_CRPLUS:
3103 case OP_CRMINPLUS:
3104 case OP_CRPOSPLUS:
3105 end++;
3106 break;
3108 case OP_CRRANGE:
3109 case OP_CRMINRANGE:
3110 case OP_CRPOSRANGE:
3111 list[1] = (GET2(end, 1) == 0);
3112 end += 1 + 2 * IMM2_SIZE;
3113 break;
3115 list[2] = (pcre_uint32)(end - code);
3116 return end;
3118 return NULL; /* Opcode not accepted */
3123 /*************************************************
3124 * Scan further character sets for match *
3125 *************************************************/
3127 /* Checks whether the base and the current opcode have a common character, in
3128 which case the base cannot be possessified.
3130 Arguments:
3131 code points to the byte code
3132 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
3133 cd static compile data
3134 base_list the data list of the base opcode
3136 Returns: TRUE if the auto-possessification is possible
3139 static BOOL
3140 compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
3141 const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit)
3143 pcre_uchar c;
3144 pcre_uint32 list[8];
3145 const pcre_uint32 *chr_ptr;
3146 const pcre_uint32 *ochr_ptr;
3147 const pcre_uint32 *list_ptr;
3148 const pcre_uchar *next_code;
3149 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3150 const pcre_uchar *xclass_flags;
3151 #endif
3152 const pcre_uint8 *class_bitset;
3153 const pcre_uint8 *set1, *set2, *set_end;
3154 pcre_uint32 chr;
3155 BOOL accepted, invert_bits;
3156 BOOL entered_a_group = FALSE;
3158 if (*rec_limit == 0) return FALSE;
3159 --(*rec_limit);
3161 /* Note: the base_list[1] contains whether the current opcode has greedy
3162 (represented by a non-zero value) quantifier. This is a different from
3163 other character type lists, which stores here that the character iterator
3164 matches to an empty string (also represented by a non-zero value). */
3166 for(;;)
3168 /* All operations move the code pointer forward.
3169 Therefore infinite recursions are not possible. */
3171 c = *code;
3173 /* Skip over callouts */
3175 if (c == OP_CALLOUT)
3177 code += PRIV(OP_lengths)[c];
3178 continue;
3181 if (c == OP_ALT)
3183 do code += GET(code, 1); while (*code == OP_ALT);
3184 c = *code;
3187 switch(c)
3189 case OP_END:
3190 case OP_KETRPOS:
3191 /* TRUE only in greedy case. The non-greedy case could be replaced by
3192 an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3193 uses more memory, which we cannot get at this stage.) */
3195 return base_list[1] != 0;
3197 case OP_KET:
3198 /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3199 it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3200 cannot be converted to a possessive form. */
3202 if (base_list[1] == 0) return FALSE;
3204 switch(*(code - GET(code, 1)))
3206 case OP_ASSERT:
3207 case OP_ASSERT_NOT:
3208 case OP_ASSERTBACK:
3209 case OP_ASSERTBACK_NOT:
3210 case OP_ONCE:
3211 case OP_ONCE_NC:
3212 /* Atomic sub-patterns and assertions can always auto-possessify their
3213 last iterator. However, if the group was entered as a result of checking
3214 a previous iterator, this is not possible. */
3216 return !entered_a_group;
3219 code += PRIV(OP_lengths)[c];
3220 continue;
3222 case OP_ONCE:
3223 case OP_ONCE_NC:
3224 case OP_BRA:
3225 case OP_CBRA:
3226 next_code = code + GET(code, 1);
3227 code += PRIV(OP_lengths)[c];
3229 while (*next_code == OP_ALT)
3231 if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit))
3232 return FALSE;
3233 code = next_code + 1 + LINK_SIZE;
3234 next_code += GET(next_code, 1);
3237 entered_a_group = TRUE;
3238 continue;
3240 case OP_BRAZERO:
3241 case OP_BRAMINZERO:
3243 next_code = code + 1;
3244 if (*next_code != OP_BRA && *next_code != OP_CBRA
3245 && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE;
3247 do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3249 /* The bracket content will be checked by the
3250 OP_BRA/OP_CBRA case above. */
3251 next_code += 1 + LINK_SIZE;
3252 if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit))
3253 return FALSE;
3255 code += PRIV(OP_lengths)[c];
3256 continue;
3258 default:
3259 break;
3262 /* Check for a supported opcode, and load its properties. */
3264 code = get_chr_property_list(code, utf, cd->fcc, list);
3265 if (code == NULL) return FALSE; /* Unsupported */
3267 /* If either opcode is a small character list, set pointers for comparing
3268 characters from that list with another list, or with a property. */
3270 if (base_list[0] == OP_CHAR)
3272 chr_ptr = base_list + 2;
3273 list_ptr = list;
3275 else if (list[0] == OP_CHAR)
3277 chr_ptr = list + 2;
3278 list_ptr = base_list;
3281 /* Character bitsets can also be compared to certain opcodes. */
3283 else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
3284 #ifdef COMPILE_PCRE8
3285 /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
3286 || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
3287 #endif
3290 #ifdef COMPILE_PCRE8
3291 if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
3292 #else
3293 if (base_list[0] == OP_CLASS)
3294 #endif
3296 set1 = (pcre_uint8 *)(base_end - base_list[2]);
3297 list_ptr = list;
3299 else
3301 set1 = (pcre_uint8 *)(code - list[2]);
3302 list_ptr = base_list;
3305 invert_bits = FALSE;
3306 switch(list_ptr[0])
3308 case OP_CLASS:
3309 case OP_NCLASS:
3310 set2 = (pcre_uint8 *)
3311 ((list_ptr == list ? code : base_end) - list_ptr[2]);
3312 break;
3314 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3315 case OP_XCLASS:
3316 xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
3317 if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
3318 if ((*xclass_flags & XCL_MAP) == 0)
3320 /* No bits are set for characters < 256. */
3321 if (list[1] == 0) return (*xclass_flags & XCL_NOT) == 0;
3322 /* Might be an empty repeat. */
3323 continue;
3325 set2 = (pcre_uint8 *)(xclass_flags + 1);
3326 break;
3327 #endif
3329 case OP_NOT_DIGIT:
3330 invert_bits = TRUE;
3331 /* Fall through */
3332 case OP_DIGIT:
3333 set2 = (pcre_uint8 *)(cd->cbits + cbit_digit);
3334 break;
3336 case OP_NOT_WHITESPACE:
3337 invert_bits = TRUE;
3338 /* Fall through */
3339 case OP_WHITESPACE:
3340 set2 = (pcre_uint8 *)(cd->cbits + cbit_space);
3341 break;
3343 case OP_NOT_WORDCHAR:
3344 invert_bits = TRUE;
3345 /* Fall through */
3346 case OP_WORDCHAR:
3347 set2 = (pcre_uint8 *)(cd->cbits + cbit_word);
3348 break;
3350 default:
3351 return FALSE;
3354 /* Because the sets are unaligned, we need
3355 to perform byte comparison here. */
3356 set_end = set1 + 32;
3357 if (invert_bits)
3361 if ((*set1++ & ~(*set2++)) != 0) return FALSE;
3363 while (set1 < set_end);
3365 else
3369 if ((*set1++ & *set2++) != 0) return FALSE;
3371 while (set1 < set_end);
3374 if (list[1] == 0) return TRUE;
3375 /* Might be an empty repeat. */
3376 continue;
3379 /* Some property combinations also acceptable. Unicode property opcodes are
3380 processed specially; the rest can be handled with a lookup table. */
3382 else
3384 pcre_uint32 leftop, rightop;
3386 leftop = base_list[0];
3387 rightop = list[0];
3389 #ifdef SUPPORT_UCP
3390 accepted = FALSE; /* Always set in non-unicode case. */
3391 if (leftop == OP_PROP || leftop == OP_NOTPROP)
3393 if (rightop == OP_EOD)
3394 accepted = TRUE;
3395 else if (rightop == OP_PROP || rightop == OP_NOTPROP)
3397 int n;
3398 const pcre_uint8 *p;
3399 BOOL same = leftop == rightop;
3400 BOOL lisprop = leftop == OP_PROP;
3401 BOOL risprop = rightop == OP_PROP;
3402 BOOL bothprop = lisprop && risprop;
3404 /* There's a table that specifies how each combination is to be
3405 processed:
3406 0 Always return FALSE (never auto-possessify)
3407 1 Character groups are distinct (possessify if both are OP_PROP)
3408 2 Check character categories in the same group (general or particular)
3409 3 Return TRUE if the two opcodes are not the same
3410 ... see comments below
3413 n = propposstab[base_list[2]][list[2]];
3414 switch(n)
3416 case 0: break;
3417 case 1: accepted = bothprop; break;
3418 case 2: accepted = (base_list[3] == list[3]) != same; break;
3419 case 3: accepted = !same; break;
3421 case 4: /* Left general category, right particular category */
3422 accepted = risprop && catposstab[base_list[3]][list[3]] == same;
3423 break;
3425 case 5: /* Right general category, left particular category */
3426 accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
3427 break;
3429 /* This code is logically tricky. Think hard before fiddling with it.
3430 The posspropstab table has four entries per row. Each row relates to
3431 one of PCRE's special properties such as ALNUM or SPACE or WORD.
3432 Only WORD actually needs all four entries, but using repeats for the
3433 others means they can all use the same code below.
3435 The first two entries in each row are Unicode general categories, and
3436 apply always, because all the characters they include are part of the
3437 PCRE character set. The third and fourth entries are a general and a
3438 particular category, respectively, that include one or more relevant
3439 characters. One or the other is used, depending on whether the check
3440 is for a general or a particular category. However, in both cases the
3441 category contains more characters than the specials that are defined
3442 for the property being tested against. Therefore, it cannot be used
3443 in a NOTPROP case.
3445 Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3446 Underscore is covered by ucp_P or ucp_Po. */
3448 case 6: /* Left alphanum vs right general category */
3449 case 7: /* Left space vs right general category */
3450 case 8: /* Left word vs right general category */
3451 p = posspropstab[n-6];
3452 accepted = risprop && lisprop ==
3453 (list[3] != p[0] &&
3454 list[3] != p[1] &&
3455 (list[3] != p[2] || !lisprop));
3456 break;
3458 case 9: /* Right alphanum vs left general category */
3459 case 10: /* Right space vs left general category */
3460 case 11: /* Right word vs left general category */
3461 p = posspropstab[n-9];
3462 accepted = lisprop && risprop ==
3463 (base_list[3] != p[0] &&
3464 base_list[3] != p[1] &&
3465 (base_list[3] != p[2] || !risprop));
3466 break;
3468 case 12: /* Left alphanum vs right particular category */
3469 case 13: /* Left space vs right particular category */
3470 case 14: /* Left word vs right particular category */
3471 p = posspropstab[n-12];
3472 accepted = risprop && lisprop ==
3473 (catposstab[p[0]][list[3]] &&
3474 catposstab[p[1]][list[3]] &&
3475 (list[3] != p[3] || !lisprop));
3476 break;
3478 case 15: /* Right alphanum vs left particular category */
3479 case 16: /* Right space vs left particular category */
3480 case 17: /* Right word vs left particular category */
3481 p = posspropstab[n-15];
3482 accepted = lisprop && risprop ==
3483 (catposstab[p[0]][base_list[3]] &&
3484 catposstab[p[1]][base_list[3]] &&
3485 (base_list[3] != p[3] || !risprop));
3486 break;
3491 else
3492 #endif /* SUPPORT_UCP */
3494 accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3495 rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3496 autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3498 if (!accepted) return FALSE;
3500 if (list[1] == 0) return TRUE;
3501 /* Might be an empty repeat. */
3502 continue;
3505 /* Control reaches here only if one of the items is a small character list.
3506 All characters are checked against the other side. */
3510 chr = *chr_ptr;
3512 switch(list_ptr[0])
3514 case OP_CHAR:
3515 ochr_ptr = list_ptr + 2;
3518 if (chr == *ochr_ptr) return FALSE;
3519 ochr_ptr++;
3521 while(*ochr_ptr != NOTACHAR);
3522 break;
3524 case OP_NOT:
3525 ochr_ptr = list_ptr + 2;
3528 if (chr == *ochr_ptr)
3529 break;
3530 ochr_ptr++;
3532 while(*ochr_ptr != NOTACHAR);
3533 if (*ochr_ptr == NOTACHAR) return FALSE; /* Not found */
3534 break;
3536 /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3537 set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3539 case OP_DIGIT:
3540 if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3541 break;
3543 case OP_NOT_DIGIT:
3544 if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3545 break;
3547 case OP_WHITESPACE:
3548 if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3549 break;
3551 case OP_NOT_WHITESPACE:
3552 if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3553 break;
3555 case OP_WORDCHAR:
3556 if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3557 break;
3559 case OP_NOT_WORDCHAR:
3560 if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3561 break;
3563 case OP_HSPACE:
3564 switch(chr)
3566 HSPACE_CASES: return FALSE;
3567 default: break;
3569 break;
3571 case OP_NOT_HSPACE:
3572 switch(chr)
3574 HSPACE_CASES: break;
3575 default: return FALSE;
3577 break;
3579 case OP_ANYNL:
3580 case OP_VSPACE:
3581 switch(chr)
3583 VSPACE_CASES: return FALSE;
3584 default: break;
3586 break;
3588 case OP_NOT_VSPACE:
3589 switch(chr)
3591 VSPACE_CASES: break;
3592 default: return FALSE;
3594 break;
3596 case OP_DOLL:
3597 case OP_EODN:
3598 switch (chr)
3600 case CHAR_CR:
3601 case CHAR_LF:
3602 case CHAR_VT:
3603 case CHAR_FF:
3604 case CHAR_NEL:
3605 #ifndef EBCDIC
3606 case 0x2028:
3607 case 0x2029:
3608 #endif /* Not EBCDIC */
3609 return FALSE;
3611 break;
3613 case OP_EOD: /* Can always possessify before \z */
3614 break;
3616 #ifdef SUPPORT_UCP
3617 case OP_PROP:
3618 case OP_NOTPROP:
3619 if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3620 list_ptr[0] == OP_NOTPROP))
3621 return FALSE;
3622 break;
3623 #endif
3625 case OP_NCLASS:
3626 if (chr > 255) return FALSE;
3627 /* Fall through */
3629 case OP_CLASS:
3630 if (chr > 255) break;
3631 class_bitset = (pcre_uint8 *)
3632 ((list_ptr == list ? code : base_end) - list_ptr[2]);
3633 if ((class_bitset[chr >> 3] & (1U << (chr & 7))) != 0) return FALSE;
3634 break;
3636 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3637 case OP_XCLASS:
3638 if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
3639 list_ptr[2] + LINK_SIZE, utf)) return FALSE;
3640 break;
3641 #endif
3643 default:
3644 return FALSE;
3647 chr_ptr++;
3649 while(*chr_ptr != NOTACHAR);
3651 /* At least one character must be matched from this opcode. */
3653 if (list[1] == 0) return TRUE;
3656 /* Control never reaches here. There used to be a fail-save return FALSE; here,
3657 but some compilers complain about an unreachable statement. */
3663 /*************************************************
3664 * Scan compiled regex for auto-possession *
3665 *************************************************/
3667 /* Replaces single character iterations with their possessive alternatives
3668 if appropriate. This function modifies the compiled opcode!
3670 Arguments:
3671 code points to start of the byte code
3672 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
3673 cd static compile data
3675 Returns: nothing
3678 static void
3679 auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3681 register pcre_uchar c;
3682 const pcre_uchar *end;
3683 pcre_uchar *repeat_opcode;
3684 pcre_uint32 list[8];
3685 int rec_limit;
3687 for (;;)
3689 c = *code;
3691 /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK,
3692 it may compile without complaining, but may get into a loop here if the code
3693 pointer points to a bad value. This is, of course a documentated possibility,
3694 when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and
3695 just give up on this optimization. */
3697 if (c >= OP_TABLE_LENGTH) return;
3699 if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3701 c -= get_repeat_base(c) - OP_STAR;
3702 end = (c <= OP_MINUPTO) ?
3703 get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3704 list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3706 rec_limit = 1000;
3707 if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit))
3709 switch(c)
3711 case OP_STAR:
3712 *code += OP_POSSTAR - OP_STAR;
3713 break;
3715 case OP_MINSTAR:
3716 *code += OP_POSSTAR - OP_MINSTAR;
3717 break;
3719 case OP_PLUS:
3720 *code += OP_POSPLUS - OP_PLUS;
3721 break;
3723 case OP_MINPLUS:
3724 *code += OP_POSPLUS - OP_MINPLUS;
3725 break;
3727 case OP_QUERY:
3728 *code += OP_POSQUERY - OP_QUERY;
3729 break;
3731 case OP_MINQUERY:
3732 *code += OP_POSQUERY - OP_MINQUERY;
3733 break;
3735 case OP_UPTO:
3736 *code += OP_POSUPTO - OP_UPTO;
3737 break;
3739 case OP_MINUPTO:
3740 *code += OP_POSUPTO - OP_MINUPTO;
3741 break;
3744 c = *code;
3746 else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
3748 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3749 if (c == OP_XCLASS)
3750 repeat_opcode = code + GET(code, 1);
3751 else
3752 #endif
3753 repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar));
3755 c = *repeat_opcode;
3756 if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
3758 /* end must not be NULL. */
3759 end = get_chr_property_list(code, utf, cd->fcc, list);
3761 list[1] = (c & 1) == 0;
3763 rec_limit = 1000;
3764 if (compare_opcodes(end, utf, cd, list, end, &rec_limit))
3766 switch (c)
3768 case OP_CRSTAR:
3769 case OP_CRMINSTAR:
3770 *repeat_opcode = OP_CRPOSSTAR;
3771 break;
3773 case OP_CRPLUS:
3774 case OP_CRMINPLUS:
3775 *repeat_opcode = OP_CRPOSPLUS;
3776 break;
3778 case OP_CRQUERY:
3779 case OP_CRMINQUERY:
3780 *repeat_opcode = OP_CRPOSQUERY;
3781 break;
3783 case OP_CRRANGE:
3784 case OP_CRMINRANGE:
3785 *repeat_opcode = OP_CRPOSRANGE;
3786 break;
3790 c = *code;
3793 switch(c)
3795 case OP_END:
3796 return;
3798 case OP_TYPESTAR:
3799 case OP_TYPEMINSTAR:
3800 case OP_TYPEPLUS:
3801 case OP_TYPEMINPLUS:
3802 case OP_TYPEQUERY:
3803 case OP_TYPEMINQUERY:
3804 case OP_TYPEPOSSTAR:
3805 case OP_TYPEPOSPLUS:
3806 case OP_TYPEPOSQUERY:
3807 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3808 break;
3810 case OP_TYPEUPTO:
3811 case OP_TYPEMINUPTO:
3812 case OP_TYPEEXACT:
3813 case OP_TYPEPOSUPTO:
3814 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3815 code += 2;
3816 break;
3818 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3819 case OP_XCLASS:
3820 code += GET(code, 1);
3821 break;
3822 #endif
3824 case OP_MARK:
3825 case OP_PRUNE_ARG:
3826 case OP_SKIP_ARG:
3827 case OP_THEN_ARG:
3828 code += code[1];
3829 break;
3832 /* Add in the fixed length from the table */
3834 code += PRIV(OP_lengths)[c];
3836 /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3837 a multi-byte character. The length in the table is a minimum, so we have to
3838 arrange to skip the extra bytes. */
3840 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3841 if (utf) switch(c)
3843 case OP_CHAR:
3844 case OP_CHARI:
3845 case OP_NOT:
3846 case OP_NOTI:
3847 case OP_STAR:
3848 case OP_MINSTAR:
3849 case OP_PLUS:
3850 case OP_MINPLUS:
3851 case OP_QUERY:
3852 case OP_MINQUERY:
3853 case OP_UPTO:
3854 case OP_MINUPTO:
3855 case OP_EXACT:
3856 case OP_POSSTAR:
3857 case OP_POSPLUS:
3858 case OP_POSQUERY:
3859 case OP_POSUPTO:
3860 case OP_STARI:
3861 case OP_MINSTARI:
3862 case OP_PLUSI:
3863 case OP_MINPLUSI:
3864 case OP_QUERYI:
3865 case OP_MINQUERYI:
3866 case OP_UPTOI:
3867 case OP_MINUPTOI:
3868 case OP_EXACTI:
3869 case OP_POSSTARI:
3870 case OP_POSPLUSI:
3871 case OP_POSQUERYI:
3872 case OP_POSUPTOI:
3873 case OP_NOTSTAR:
3874 case OP_NOTMINSTAR:
3875 case OP_NOTPLUS:
3876 case OP_NOTMINPLUS:
3877 case OP_NOTQUERY:
3878 case OP_NOTMINQUERY:
3879 case OP_NOTUPTO:
3880 case OP_NOTMINUPTO:
3881 case OP_NOTEXACT:
3882 case OP_NOTPOSSTAR:
3883 case OP_NOTPOSPLUS:
3884 case OP_NOTPOSQUERY:
3885 case OP_NOTPOSUPTO:
3886 case OP_NOTSTARI:
3887 case OP_NOTMINSTARI:
3888 case OP_NOTPLUSI:
3889 case OP_NOTMINPLUSI:
3890 case OP_NOTQUERYI:
3891 case OP_NOTMINQUERYI:
3892 case OP_NOTUPTOI:
3893 case OP_NOTMINUPTOI:
3894 case OP_NOTEXACTI:
3895 case OP_NOTPOSSTARI:
3896 case OP_NOTPOSPLUSI:
3897 case OP_NOTPOSQUERYI:
3898 case OP_NOTPOSUPTOI:
3899 if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3900 break;
3902 #else
3903 (void)(utf); /* Keep compiler happy by referencing function argument */
3904 #endif
3910 /*************************************************
3911 * Check for POSIX class syntax *
3912 *************************************************/
3914 /* This function is called when the sequence "[:" or "[." or "[=" is
3915 encountered in a character class. It checks whether this is followed by a
3916 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3917 reach an unescaped ']' without the special preceding character, return FALSE.
3919 Originally, this function only recognized a sequence of letters between the
3920 terminators, but it seems that Perl recognizes any sequence of characters,
3921 though of course unknown POSIX names are subsequently rejected. Perl gives an
3922 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3923 didn't consider this to be a POSIX class. Likewise for [:1234:].
3925 The problem in trying to be exactly like Perl is in the handling of escapes. We
3926 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3927 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3928 below handles the special cases \\ and \], but does not try to do any other
3929 escape processing. This makes it different from Perl for cases such as
3930 [:l\ower:] where Perl recognizes it as the POSIX class "lower" but PCRE does
3931 not recognize "l\ower". This is a lesser evil than not diagnosing bad classes
3932 when Perl does, I think.
3934 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3935 It seems that the appearance of a nested POSIX class supersedes an apparent
3936 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3937 a digit.
3939 In Perl, unescaped square brackets may also appear as part of class names. For
3940 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3941 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3942 seem right at all. PCRE does not allow closing square brackets in POSIX class
3943 names.
3945 Arguments:
3946 ptr pointer to the initial [
3947 endptr where to return the end pointer
3949 Returns: TRUE or FALSE
3952 static BOOL
3953 check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3955 pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */
3956 terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
3957 for (++ptr; *ptr != CHAR_NULL; ptr++)
3959 if (*ptr == CHAR_BACKSLASH &&
3960 (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET ||
3961 ptr[1] == CHAR_BACKSLASH))
3962 ptr++;
3963 else if ((*ptr == CHAR_LEFT_SQUARE_BRACKET && ptr[1] == terminator) ||
3964 *ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3965 else if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3967 *endptr = ptr;
3968 return TRUE;
3971 return FALSE;
3977 /*************************************************
3978 * Check POSIX class name *
3979 *************************************************/
3981 /* This function is called to check the name given in a POSIX-style class entry
3982 such as [:alnum:].
3984 Arguments:
3985 ptr points to the first letter
3986 len the length of the name
3988 Returns: a value representing the name, or -1 if unknown
3991 static int
3992 check_posix_name(const pcre_uchar *ptr, int len)
3994 const char *pn = posix_names;
3995 register int yield = 0;
3996 while (posix_name_lengths[yield] != 0)
3998 if (len == posix_name_lengths[yield] &&
3999 STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
4000 pn += posix_name_lengths[yield] + 1;
4001 yield++;
4003 return -1;
4007 /*************************************************
4008 * Adjust OP_RECURSE items in repeated group *
4009 *************************************************/
4011 /* OP_RECURSE items contain an offset from the start of the regex to the group
4012 that is referenced. This means that groups can be replicated for fixed
4013 repetition simply by copying (because the recursion is allowed to refer to
4014 earlier groups that are outside the current group). However, when a group is
4015 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
4016 inserted before it, after it has been compiled. This means that any OP_RECURSE
4017 items within it that refer to the group itself or any contained groups have to
4018 have their offsets adjusted. That one of the jobs of this function. Before it
4019 is called, the partially compiled regex must be temporarily terminated with
4020 OP_END.
4022 This function has been extended to cope with forward references for recursions
4023 and subroutine calls. It must check the list of such references for the
4024 group we are dealing with. If it finds that one of the recursions in the
4025 current group is on this list, it does not adjust the value in the reference
4026 (which is a group number). After the group has been scanned, all the offsets in
4027 the forward reference list for the group are adjusted.
4029 Arguments:
4030 group points to the start of the group
4031 adjust the amount by which the group is to be moved
4032 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
4033 cd contains pointers to tables etc.
4034 save_hwm_offset the hwm forward reference offset at the start of the group
4036 Returns: nothing
4039 static void
4040 adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
4041 size_t save_hwm_offset)
4043 int offset;
4044 pcre_uchar *hc;
4045 pcre_uchar *ptr = group;
4047 while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
4049 for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
4050 hc += LINK_SIZE)
4052 offset = (int)GET(hc, 0);
4053 if (cd->start_code + offset == ptr + 1) break;
4056 /* If we have not found this recursion on the forward reference list, adjust
4057 the recursion's offset if it's after the start of this group. */
4059 if (hc >= cd->hwm)
4061 offset = (int)GET(ptr, 1);
4062 if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
4065 ptr += 1 + LINK_SIZE;
4068 /* Now adjust all forward reference offsets for the group. */
4070 for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
4071 hc += LINK_SIZE)
4073 offset = (int)GET(hc, 0);
4074 PUT(hc, 0, offset + adjust);
4080 /*************************************************
4081 * Insert an automatic callout point *
4082 *************************************************/
4084 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
4085 callout points before each pattern item.
4087 Arguments:
4088 code current code pointer
4089 ptr current pattern pointer
4090 cd pointers to tables etc
4092 Returns: new code pointer
4095 static pcre_uchar *
4096 auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
4098 *code++ = OP_CALLOUT;
4099 *code++ = 255;
4100 PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */
4101 PUT(code, LINK_SIZE, 0); /* Default length */
4102 return code + 2 * LINK_SIZE;
4107 /*************************************************
4108 * Complete a callout item *
4109 *************************************************/
4111 /* A callout item contains the length of the next item in the pattern, which
4112 we can't fill in till after we have reached the relevant point. This is used
4113 for both automatic and manual callouts.
4115 Arguments:
4116 previous_callout points to previous callout item
4117 ptr current pattern pointer
4118 cd pointers to tables etc
4120 Returns: nothing
4123 static void
4124 complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
4126 int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
4127 PUT(previous_callout, 2 + LINK_SIZE, length);
4132 #ifdef SUPPORT_UCP
4133 /*************************************************
4134 * Get othercase range *
4135 *************************************************/
4137 /* This function is passed the start and end of a class range, in UTF-8 mode
4138 with UCP support. It searches up the characters, looking for ranges of
4139 characters in the "other" case. Each call returns the next one, updating the
4140 start address. A character with multiple other cases is returned on its own
4141 with a special return value.
4143 Arguments:
4144 cptr points to starting character value; updated
4145 d end value
4146 ocptr where to put start of othercase range
4147 odptr where to put end of othercase range
4149 Yield: -1 when no more
4150 0 when a range is returned
4151 >0 the CASESET offset for char with multiple other cases
4152 in this case, ocptr contains the original
4155 static int
4156 get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
4157 pcre_uint32 *odptr)
4159 pcre_uint32 c, othercase, next;
4160 unsigned int co;
4162 /* Find the first character that has an other case. If it has multiple other
4163 cases, return its case offset value. */
4165 for (c = *cptr; c <= d; c++)
4167 if ((co = UCD_CASESET(c)) != 0)
4169 *ocptr = c++; /* Character that has the set */
4170 *cptr = c; /* Rest of input range */
4171 return (int)co;
4173 if ((othercase = UCD_OTHERCASE(c)) != c) break;
4176 if (c > d) return -1; /* Reached end of range */
4178 /* Found a character that has a single other case. Search for the end of the
4179 range, which is either the end of the input range, or a character that has zero
4180 or more than one other cases. */
4182 *ocptr = othercase;
4183 next = othercase + 1;
4185 for (++c; c <= d; c++)
4187 if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break;
4188 next++;
4191 *odptr = next - 1; /* End of othercase range */
4192 *cptr = c; /* Rest of input range */
4193 return 0;
4195 #endif /* SUPPORT_UCP */
4199 /*************************************************
4200 * Add a character or range to a class *
4201 *************************************************/
4203 /* This function packages up the logic of adding a character or range of
4204 characters to a class. The character values in the arguments will be within the
4205 valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
4206 mutually recursive with the function immediately below.
4208 Arguments:
4209 classbits the bit map for characters < 256
4210 uchardptr points to the pointer for extra data
4211 options the options word
4212 cd contains pointers to tables etc.
4213 start start of range character
4214 end end of range character
4216 Returns: the number of < 256 characters added
4217 the pointer to extra data is updated
4220 static int
4221 add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
4222 compile_data *cd, pcre_uint32 start, pcre_uint32 end)
4224 pcre_uint32 c;
4225 pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff);
4226 int n8 = 0;
4228 /* If caseless matching is required, scan the range and process alternate
4229 cases. In Unicode, there are 8-bit characters that have alternate cases that
4230 are greater than 255 and vice-versa. Sometimes we can just extend the original
4231 range. */
4233 if ((options & PCRE_CASELESS) != 0)
4235 #ifdef SUPPORT_UCP
4236 if ((options & PCRE_UTF8) != 0)
4238 int rc;
4239 pcre_uint32 oc, od;
4241 options &= ~PCRE_CASELESS; /* Remove for recursive calls */
4242 c = start;
4244 while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
4246 /* Handle a single character that has more than one other case. */
4248 if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
4249 PRIV(ucd_caseless_sets) + rc, oc);
4251 /* Do nothing if the other case range is within the original range. */
4253 else if (oc >= start && od <= end) continue;
4255 /* Extend the original range if there is overlap, noting that if oc < c, we
4256 can't have od > end because a subrange is always shorter than the basic
4257 range. Otherwise, use a recursive call to add the additional range. */
4259 else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
4260 else if (od > end && oc <= end + 1)
4262 end = od; /* Extend upwards */
4263 if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff);
4265 else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
4268 else
4269 #endif /* SUPPORT_UCP */
4271 /* Not UTF-mode, or no UCP */
4273 for (c = start; c <= classbits_end; c++)
4275 SETBIT(classbits, cd->fcc[c]);
4276 n8++;
4280 /* Now handle the original range. Adjust the final value according to the bit
4281 length - this means that the same lists of (e.g.) horizontal spaces can be used
4282 in all cases. */
4284 #if defined COMPILE_PCRE8
4285 #ifdef SUPPORT_UTF
4286 if ((options & PCRE_UTF8) == 0)
4287 #endif
4288 if (end > 0xff) end = 0xff;
4290 #elif defined COMPILE_PCRE16
4291 #ifdef SUPPORT_UTF
4292 if ((options & PCRE_UTF16) == 0)
4293 #endif
4294 if (end > 0xffff) end = 0xffff;
4296 #endif /* COMPILE_PCRE[8|16] */
4298 /* Use the bitmap for characters < 256. Otherwise use extra data.*/
4300 for (c = start; c <= classbits_end; c++)
4302 /* Regardless of start, c will always be <= 255. */
4303 SETBIT(classbits, c);
4304 n8++;
4307 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4308 if (start <= 0xff) start = 0xff + 1;
4310 if (end >= start)
4312 pcre_uchar *uchardata = *uchardptr;
4313 #ifdef SUPPORT_UTF
4314 if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */
4316 if (start < end)
4318 *uchardata++ = XCL_RANGE;
4319 uchardata += PRIV(ord2utf)(start, uchardata);
4320 uchardata += PRIV(ord2utf)(end, uchardata);
4322 else if (start == end)
4324 *uchardata++ = XCL_SINGLE;
4325 uchardata += PRIV(ord2utf)(start, uchardata);
4328 else
4329 #endif /* SUPPORT_UTF */
4331 /* Without UTF support, character values are constrained by the bit length,
4332 and can only be > 256 for 16-bit and 32-bit libraries. */
4334 #ifdef COMPILE_PCRE8
4336 #else
4337 if (start < end)
4339 *uchardata++ = XCL_RANGE;
4340 *uchardata++ = start;
4341 *uchardata++ = end;
4343 else if (start == end)
4345 *uchardata++ = XCL_SINGLE;
4346 *uchardata++ = start;
4348 #endif
4350 *uchardptr = uchardata; /* Updata extra data pointer */
4352 #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
4354 return n8; /* Number of 8-bit characters */
4360 /*************************************************
4361 * Add a list of characters to a class *
4362 *************************************************/
4364 /* This function is used for adding a list of case-equivalent characters to a
4365 class, and also for adding a list of horizontal or vertical whitespace. If the
4366 list is in order (which it should be), ranges of characters are detected and
4367 handled appropriately. This function is mutually recursive with the function
4368 above.
4370 Arguments:
4371 classbits the bit map for characters < 256
4372 uchardptr points to the pointer for extra data
4373 options the options word
4374 cd contains pointers to tables etc.
4375 p points to row of 32-bit values, terminated by NOTACHAR
4376 except character to omit; this is used when adding lists of
4377 case-equivalent characters to avoid including the one we
4378 already know about
4380 Returns: the number of < 256 characters added
4381 the pointer to extra data is updated
4384 static int
4385 add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
4386 compile_data *cd, const pcre_uint32 *p, unsigned int except)
4388 int n8 = 0;
4389 while (p[0] < NOTACHAR)
4391 int n = 0;
4392 if (p[0] != except)
4394 while(p[n+1] == p[0] + n + 1) n++;
4395 n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
4397 p += n + 1;
4399 return n8;
4404 /*************************************************
4405 * Add characters not in a list to a class *
4406 *************************************************/
4408 /* This function is used for adding the complement of a list of horizontal or
4409 vertical whitespace to a class. The list must be in order.
4411 Arguments:
4412 classbits the bit map for characters < 256
4413 uchardptr points to the pointer for extra data
4414 options the options word
4415 cd contains pointers to tables etc.
4416 p points to row of 32-bit values, terminated by NOTACHAR
4418 Returns: the number of < 256 characters added
4419 the pointer to extra data is updated
4422 static int
4423 add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
4424 int options, compile_data *cd, const pcre_uint32 *p)
4426 BOOL utf = (options & PCRE_UTF8) != 0;
4427 int n8 = 0;
4428 if (p[0] > 0)
4429 n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
4430 while (p[0] < NOTACHAR)
4432 while (p[1] == p[0] + 1) p++;
4433 n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
4434 (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
4435 p++;
4437 return n8;
4442 /*************************************************
4443 * Compile one branch *
4444 *************************************************/
4446 /* Scan the pattern, compiling it into the a vector. If the options are
4447 changed during the branch, the pointer is used to change the external options
4448 bits. This function is used during the pre-compile phase when we are trying
4449 to find out the amount of memory needed, as well as during the real compile
4450 phase. The value of lengthptr distinguishes the two phases.
4452 Arguments:
4453 optionsptr pointer to the option bits
4454 codeptr points to the pointer to the current code point
4455 ptrptr points to the current pattern pointer
4456 errorcodeptr points to error code variable
4457 firstcharptr place to put the first required character
4458 firstcharflagsptr place to put the first character flags, or a negative number
4459 reqcharptr place to put the last required character
4460 reqcharflagsptr place to put the last required character flags, or a negative number
4461 bcptr points to current branch chain
4462 cond_depth conditional nesting depth
4463 cd contains pointers to tables etc.
4464 lengthptr NULL during the real compile phase
4465 points to length accumulator during pre-compile phase
4467 Returns: TRUE on success
4468 FALSE, with *errorcodeptr set non-zero on error
4471 static BOOL
4472 compile_branch(int *optionsptr, pcre_uchar **codeptr,
4473 const pcre_uchar **ptrptr, int *errorcodeptr,
4474 pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
4475 pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
4476 branch_chain *bcptr, int cond_depth,
4477 compile_data *cd, int *lengthptr)
4479 int repeat_type, op_type;
4480 int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
4481 int bravalue = 0;
4482 int greedy_default, greedy_non_default;
4483 pcre_uint32 firstchar, reqchar;
4484 pcre_int32 firstcharflags, reqcharflags;
4485 pcre_uint32 zeroreqchar, zerofirstchar;
4486 pcre_int32 zeroreqcharflags, zerofirstcharflags;
4487 pcre_int32 req_caseopt, reqvary, tempreqvary;
4488 int options = *optionsptr; /* May change dynamically */
4489 int after_manual_callout = 0;
4490 int length_prevgroup = 0;
4491 register pcre_uint32 c;
4492 int escape;
4493 register pcre_uchar *code = *codeptr;
4494 pcre_uchar *last_code = code;
4495 pcre_uchar *orig_code = code;
4496 pcre_uchar *tempcode;
4497 BOOL inescq = (cd->extended_options & PCRE_VERBATIM_BIT) != 0;
4498 BOOL basicre = (cd->extended_options & PCRE_POSIX_BASIC_ESC_BIT) != 0;
4499 BOOL posixre = (cd->extended_options & PCRE_POSIX_RE_BITS) != 0;
4500 BOOL embednul = (cd->extended_options & PCRE_ALLOW_EMBEDDED_NUL_BIT) != 0;
4501 BOOL groupsetfirstchar = FALSE;
4502 const pcre_uchar *ptr = *ptrptr;
4503 const pcre_uchar *tempptr;
4504 const pcre_uchar *nestptr = NULL;
4505 pcre_uchar *previous = NULL;
4506 pcre_uchar *previous_callout = NULL;
4507 size_t item_hwm_offset = 0;
4508 pcre_uint8 classbits[32];
4509 pcre_uchar sub_basic_esc[3];
4511 /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
4512 must not do this for other options (e.g. PCRE_EXTENDED) because they may change
4513 dynamically as we process the pattern. */
4515 #ifdef SUPPORT_UTF
4516 /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
4517 BOOL utf = (options & PCRE_UTF8) != 0;
4518 #ifndef COMPILE_PCRE32
4519 pcre_uchar utf_chars[6];
4520 #endif
4521 #else
4522 BOOL utf = FALSE;
4523 #endif
4525 /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
4526 class_uchardata always so that it can be passed to add_to_class() always,
4527 though it will not be used in non-UTF 8-bit cases. This avoids having to supply
4528 alternative calls for the different cases. */
4530 pcre_uchar *class_uchardata;
4531 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4532 BOOL xclass;
4533 pcre_uchar *class_uchardata_base;
4534 #endif
4536 #ifdef PCRE_DEBUG
4537 if (lengthptr != NULL) DPRINTF((">> start branch\n"));
4538 #endif
4540 /* Set up the default and non-default settings for greediness */
4542 greedy_default = ((options & PCRE_UNGREEDY) != 0);
4543 greedy_non_default = greedy_default ^ 1;
4545 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
4546 matching encountered yet". It gets changed to REQ_NONE if we hit something that
4547 matches a non-fixed char first char; reqchar just remains unset if we never
4548 find one.
4550 When we hit a repeat whose minimum is zero, we may have to adjust these values
4551 to take the zero repeat into account. This is implemented by setting them to
4552 zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
4553 item types that can be repeated set these backoff variables appropriately. */
4555 firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
4556 firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
4558 /* The variable req_caseopt contains either the REQ_CASELESS value
4559 or zero, according to the current setting of the caseless flag. The
4560 REQ_CASELESS leaves the lower 28 bit empty. It is added into the
4561 firstchar or reqchar variables to record the case status of the
4562 value. This is used only for ASCII characters. */
4564 req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
4566 /* Switch on next character until the end of the branch */
4568 for (;; ptr++)
4570 BOOL negate_class;
4571 BOOL should_flip_negation;
4572 BOOL possessive_quantifier;
4573 BOOL is_quantifier;
4574 BOOL is_recurse;
4575 BOOL reset_bracount;
4576 int class_has_8bitchar;
4577 int class_one_char;
4578 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4579 BOOL xclass_has_prop;
4580 #endif
4581 int newoptions;
4582 int recno;
4583 int refsign;
4584 int skipbytes;
4585 pcre_uint32 subreqchar, subfirstchar;
4586 pcre_int32 subreqcharflags, subfirstcharflags;
4587 int terminator;
4588 unsigned int mclength;
4589 unsigned int tempbracount;
4590 pcre_uint32 ec;
4591 pcre_uchar mcbuffer[8];
4593 /* Come here to restart the loop without advancing the pointer. */
4595 REDO_LOOP:
4597 /* Get next character in the pattern */
4599 c = *ptr;
4601 /* If we are at the end of a nested substitution, revert to the outer level
4602 string. Nesting only happens one level deep. */
4604 if (c == CHAR_NULL && nestptr != NULL)
4606 ptr = nestptr;
4607 nestptr = NULL;
4608 c = *ptr;
4611 if (c == CHAR_NULL && !inescq && embednul && ptr < cd->end_pattern)
4613 nestptr = ptr + 1;
4614 ptr = sub_embedded_nul;
4615 c = *ptr;
4618 if (nestptr == NULL && !inescq && basicre)
4620 if (c == CHAR_LEFT_PARENTHESIS || c == CHAR_RIGHT_PARENTHESIS ||
4621 c == CHAR_LEFT_CURLY_BRACKET || c == CHAR_RIGHT_CURLY_BRACKET ||
4622 c == CHAR_QUESTION_MARK || c == CHAR_PLUS || c == CHAR_VERTICAL_LINE)
4624 sub_basic_esc[2] = CHAR_NULL;
4625 sub_basic_esc[1] = c;
4626 sub_basic_esc[0] = c = CHAR_BACKSLASH;
4627 nestptr = ptr + 1;
4628 ptr = sub_basic_esc;
4630 else if (c == CHAR_BACKSLASH &&
4631 !((CHAR_1 <= ptr[1] && ptr[1] <= CHAR_9) || ptr[1] == CHAR_DOT ||
4632 ptr[1] == CHAR_BACKSLASH ||
4633 ptr[1] == CHAR_LEFT_SQUARE_BRACKET || ptr[1] == CHAR_ASTERISK ||
4634 ptr[1] == CHAR_CIRCUMFLEX_ACCENT || ptr[1] == CHAR_DOLLAR_SIGN))
4636 c = *(++ptr);
4640 /* If we are in the pre-compile phase, accumulate the length used for the
4641 previous cycle of this loop. */
4643 if (lengthptr != NULL)
4645 #ifdef PCRE_DEBUG
4646 if (code > cd->hwm) cd->hwm = code; /* High water info */
4647 #endif
4648 if (code > cd->start_workspace + cd->workspace_size -
4649 WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */
4651 *errorcodeptr = (code >= cd->start_workspace + cd->workspace_size)?
4652 ERR52 : ERR87;
4653 goto FAILED;
4656 /* There is at least one situation where code goes backwards: this is the
4657 case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
4658 the class is simply eliminated. However, it is created first, so we have to
4659 allow memory for it. Therefore, don't ever reduce the length at this point.
4662 if (code < last_code) code = last_code;
4664 /* Paranoid check for integer overflow */
4666 if (OFLOW_MAX - *lengthptr < code - last_code)
4668 *errorcodeptr = ERR20;
4669 goto FAILED;
4672 *lengthptr += (int)(code - last_code);
4673 DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
4674 (int)(code - last_code), c, c));
4676 /* If "previous" is set and it is not at the start of the work space, move
4677 it back to there, in order to avoid filling up the work space. Otherwise,
4678 if "previous" is NULL, reset the current code pointer to the start. */
4680 if (previous != NULL)
4682 if (previous > orig_code)
4684 memmove(orig_code, previous, IN_UCHARS(code - previous));
4685 code -= previous - orig_code;
4686 previous = orig_code;
4689 else code = orig_code;
4691 /* Remember where this code item starts so we can pick up the length
4692 next time round. */
4694 last_code = code;
4697 /* In the real compile phase, just check the workspace used by the forward
4698 reference list. */
4700 else if (cd->hwm > cd->start_workspace + cd->workspace_size)
4702 *errorcodeptr = ERR52;
4703 goto FAILED;
4706 /* If in \Q...\E, check for the end; if not, we have a literal. Otherwise an
4707 isolated \E is ignored. */
4709 if (c != CHAR_NULL ||
4710 (inescq && nestptr == NULL && embednul && ptr < cd->end_pattern))
4712 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E &&
4713 (cd->extended_options & PCRE_VERBATIM_BIT) == 0)
4715 inescq = FALSE;
4716 ptr++;
4717 continue;
4719 else if (inescq)
4721 if (previous_callout != NULL)
4723 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
4724 complete_callout(previous_callout, ptr, cd);
4725 previous_callout = NULL;
4727 if ((options & PCRE_AUTO_CALLOUT) != 0)
4729 previous_callout = code;
4730 code = auto_callout(code, ptr, cd);
4732 goto NORMAL_CHAR;
4735 /* Check for the start of a \Q...\E sequence. We must do this here rather
4736 than later in case it is immediately followed by \E, which turns it into a
4737 "do nothing" sequence. */
4739 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
4741 inescq = TRUE;
4742 ptr++;
4743 continue;
4747 /* In extended mode, skip white space and comments. */
4749 if ((options & PCRE_EXTENDED) != 0)
4751 const pcre_uchar *wscptr = ptr;
4752 while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr);
4753 if (c == CHAR_NUMBER_SIGN)
4755 ptr++;
4756 while (*ptr != CHAR_NULL || (nestptr == NULL && embednul && ptr < cd->end_pattern))
4758 if (IS_NEWLINE(ptr)) /* For non-fixed-length newline cases, */
4759 { /* IS_NEWLINE sets cd->nllen. */
4760 ptr += cd->nllen;
4761 break;
4763 ptr++;
4764 #ifdef SUPPORT_UTF
4765 if (utf) FORWARDCHAR(ptr);
4766 #endif
4770 /* If we skipped any characters, restart the loop. Otherwise, we didn't see
4771 a comment. */
4773 if (ptr > wscptr) goto REDO_LOOP;
4776 /* Skip over (?# comments. We need to do this here because we want to know if
4777 the next thing is a quantifier, and these comments may come between an item
4778 and its quantifier. */
4780 if (c == CHAR_LEFT_PARENTHESIS && ptr[1] == CHAR_QUESTION_MARK &&
4781 ptr[2] == CHAR_NUMBER_SIGN && (!basicre || nestptr))
4783 ptr += 3;
4784 while ((*ptr != CHAR_NULL ||
4785 (nestptr == NULL && embednul && ptr < cd->end_pattern)) &&
4786 *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
4787 if (*ptr == CHAR_NULL)
4789 *errorcodeptr = ERR18;
4790 goto FAILED;
4792 continue;
4795 /* See if the next thing is a quantifier. */
4797 is_quantifier =
4798 c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4799 (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1, basicre && !nestptr));
4801 /* Fill in length of a previous callout, except when the next thing is a
4802 quantifier or when processing a property substitution string in UCP mode. */
4804 if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4805 after_manual_callout-- <= 0)
4807 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
4808 complete_callout(previous_callout, ptr, cd);
4809 previous_callout = NULL;
4812 /* Create auto callout, except for quantifiers, or while processing property
4813 strings that are substituted for \w etc in UCP mode. */
4815 if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4817 previous_callout = code;
4818 code = auto_callout(code, ptr, cd);
4821 /* Process the next pattern item. */
4823 switch(c)
4825 /* ===================================================================*/
4826 case CHAR_NULL: /* The branch terminates at string end */
4827 case CHAR_VERTICAL_LINE: /* or | or ) */
4828 case CHAR_RIGHT_PARENTHESIS:
4829 *firstcharptr = firstchar;
4830 *firstcharflagsptr = firstcharflags;
4831 *reqcharptr = reqchar;
4832 *reqcharflagsptr = reqcharflags;
4833 *codeptr = code;
4834 *ptrptr = ptr;
4835 if (lengthptr != NULL)
4837 if (OFLOW_MAX - *lengthptr < code - last_code)
4839 *errorcodeptr = ERR20;
4840 goto FAILED;
4842 *lengthptr += (int)(code - last_code); /* To include callout length */
4843 DPRINTF((">> end branch\n"));
4845 return TRUE;
4848 /* ===================================================================*/
4849 /* Handle single-character metacharacters. In multiline mode, ^ disables
4850 the setting of any following char as a first character. */
4852 case CHAR_CIRCUMFLEX_ACCENT:
4853 previous = NULL;
4854 if ((options & PCRE_MULTILINE) != 0)
4856 if (firstcharflags == REQ_UNSET)
4857 zerofirstcharflags = firstcharflags = REQ_NONE;
4858 *code++ = OP_CIRCM;
4860 else *code++ = OP_CIRC;
4861 break;
4863 case CHAR_DOLLAR_SIGN:
4864 previous = NULL;
4865 *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
4866 break;
4868 /* There can never be a first char if '.' is first, whatever happens about
4869 repeats. The value of reqchar doesn't change either. */
4871 case CHAR_DOT:
4872 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4873 zerofirstchar = firstchar;
4874 zerofirstcharflags = firstcharflags;
4875 zeroreqchar = reqchar;
4876 zeroreqcharflags = reqcharflags;
4877 previous = code;
4878 item_hwm_offset = cd->hwm - cd->start_workspace;
4879 *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4880 break;
4883 /* ===================================================================*/
4884 /* Character classes. If the included characters are all < 256, we build a
4885 32-byte bitmap of the permitted characters, except in the special case
4886 where there is only one such character. For negated classes, we build the
4887 map as usual, then invert it at the end. However, we use a different opcode
4888 so that data characters > 255 can be handled correctly.
4890 If the class contains characters outside the 0-255 range, a different
4891 opcode is compiled. It may optionally have a bit map for characters < 256,
4892 but those above are are explicitly listed afterwards. A flag byte tells
4893 whether the bitmap is present, and whether this is a negated class or not.
4895 In JavaScript compatibility mode, an isolated ']' causes an error. In
4896 default (Perl) mode, it is treated as a data character. */
4898 case CHAR_RIGHT_SQUARE_BRACKET:
4899 if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4901 *errorcodeptr = ERR64;
4902 goto FAILED;
4904 goto NORMAL_CHAR;
4906 /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
4907 used for "start of word" and "end of word". As these are otherwise illegal
4908 sequences, we don't break anything by recognizing them. They are replaced
4909 by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
4910 erroneous and are handled by the normal code below. */
4912 case CHAR_LEFT_SQUARE_BRACKET:
4913 if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0)
4915 nestptr = ptr + 7;
4916 ptr = sub_start_of_word;
4917 goto REDO_LOOP;
4920 if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0)
4922 nestptr = ptr + 7;
4923 ptr = sub_end_of_word;
4924 goto REDO_LOOP;
4927 /* Handle a real character class. */
4929 previous = code;
4930 item_hwm_offset = cd->hwm - cd->start_workspace;
4932 /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
4933 they are encountered at the top level, so we'll do that too. */
4935 if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
4936 ptr[1] == CHAR_EQUALS_SIGN) &&
4937 check_posix_syntax(ptr, &tempptr))
4939 *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
4940 goto FAILED;
4943 /* If the first character is '^', set the negation flag and skip it. Also,
4944 if the first few characters (either before or after ^) are \Q\E or \E we
4945 skip them too. This makes for compatibility with Perl. */
4947 negate_class = FALSE;
4948 for (;;)
4950 c = *(++ptr);
4951 if (c == CHAR_BACKSLASH && (!posixre || nestptr))
4953 if (ptr[1] == CHAR_E)
4954 ptr++;
4955 else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
4956 ptr += 3;
4957 else
4958 break;
4960 else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
4961 negate_class = TRUE;
4962 else break;
4965 /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
4966 an initial ']' is taken as a data character -- the code below handles
4967 that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
4968 [^] must match any character, so generate OP_ALLANY. */
4970 if (c == CHAR_RIGHT_SQUARE_BRACKET &&
4971 (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4973 *code++ = negate_class? OP_ALLANY : OP_FAIL;
4974 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4975 zerofirstchar = firstchar;
4976 zerofirstcharflags = firstcharflags;
4977 break;
4980 /* If a class contains a negative special such as \S, we need to flip the
4981 negation flag at the end, so that support for characters > 255 works
4982 correctly (they are all included in the class). */
4984 should_flip_negation = FALSE;
4986 /* Extended class (xclass) will be used when characters > 255
4987 might match. */
4989 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4990 xclass = FALSE;
4991 class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */
4992 class_uchardata_base = class_uchardata; /* Save the start */
4993 #endif
4995 /* For optimization purposes, we track some properties of the class:
4996 class_has_8bitchar will be non-zero if the class contains at least one <
4997 256 character; class_one_char will be 1 if the class contains just one
4998 character; xclass_has_prop will be TRUE if unicode property checks
4999 are present in the class. */
5001 class_has_8bitchar = 0;
5002 class_one_char = 0;
5003 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5004 xclass_has_prop = FALSE;
5005 #endif
5007 /* Initialize the 32-char bit map to all zeros. We build the map in a
5008 temporary bit of memory, in case the class contains fewer than two
5009 8-bit characters because in that case the compiled code doesn't use the bit
5010 map. */
5012 memset(classbits, 0, 32 * sizeof(pcre_uint8));
5014 /* If the PCRE_NOT_EXCLUDES_NL option is set AND the class started with '^',
5015 stuff an ESC_n into the character class then resume. However, [^] means
5016 match any character in JS but not in Perl/PCRE so for JS [^] still becomes
5017 [^\n] but [^]] in Perl/PCRE must become [^]\n] instead. */
5019 if (negate_class && nestptr == NULL && (cd->extended_options & PCRE_NOT_EXCLUDES_NL_BIT) != 0)
5021 if (c != CHAR_RIGHT_SQUARE_BRACKET ||
5022 (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
5024 nestptr = ptr - 1;
5025 ptr = sub_implicit_rsb_newline + 1;
5027 else
5029 nestptr = ptr;
5030 ptr = sub_implicit_rsb_newline;
5032 c = *ptr;
5035 if (c == CHAR_NULL && nestptr == NULL && embednul && ptr < cd->end_pattern)
5037 nestptr = ptr + 1;
5038 ptr = sub_embedded_nul;
5039 c = *ptr;
5042 /* Process characters until ] is reached. By writing this as a "do" it
5043 means that an initial ] is taken as a data character. At the start of the
5044 loop, c contains the first byte of the character. */
5046 if (c != CHAR_NULL) do
5048 const pcre_uchar *oldptr;
5050 #ifdef SUPPORT_UTF
5051 if (utf && HAS_EXTRALEN(c))
5052 { /* Braces are required because the */
5053 GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
5055 #endif
5057 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5058 /* In the pre-compile phase, accumulate the length of any extra
5059 data and reset the pointer. This is so that very large classes that
5060 contain a zillion > 255 characters no longer overwrite the work space
5061 (which is on the stack). We have to remember that there was XCLASS data,
5062 however. */
5064 if (class_uchardata > class_uchardata_base) xclass = TRUE;
5066 if (lengthptr != NULL && class_uchardata > class_uchardata_base)
5068 *lengthptr += (int)(class_uchardata - class_uchardata_base);
5069 class_uchardata = class_uchardata_base;
5071 #endif
5073 /* Inside \Q...\E everything is literal except \E */
5075 if (inescq)
5077 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */
5079 inescq = FALSE; /* Reset literal state */
5080 ptr++; /* Skip the 'E' */
5081 continue; /* Carry on with next */
5083 goto CHECK_RANGE; /* Could be range if \E follows */
5086 /* Handle POSIX class names. Perl allows a negation extension of the
5087 form [:^name:]. A square bracket that doesn't match the syntax is
5088 treated as a literal. We also recognize the POSIX constructions
5089 [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
5090 5.6 and 5.8 do. */
5092 if (c == CHAR_LEFT_SQUARE_BRACKET &&
5093 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
5094 ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
5096 BOOL local_negate = FALSE;
5097 int posix_class, taboffset, tabopt;
5098 register const pcre_uint8 *cbits = cd->cbits;
5099 pcre_uint8 pbits[32];
5101 if (ptr[1] != CHAR_COLON)
5103 *errorcodeptr = ERR31;
5104 goto FAILED;
5107 ptr += 2;
5108 if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
5110 local_negate = TRUE;
5111 should_flip_negation = TRUE; /* Note negative special */
5112 ptr++;
5115 posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
5116 if (posix_class < 0)
5118 *errorcodeptr = ERR30;
5119 goto FAILED;
5122 /* If matching is caseless, upper and lower are converted to
5123 alpha. This relies on the fact that the class table starts with
5124 alpha, lower, upper as the first 3 entries. */
5126 if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
5127 posix_class = 0;
5129 /* When PCRE_UCP is set, some of the POSIX classes are converted to
5130 different escape sequences that use Unicode properties \p or \P. Others
5131 that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP
5132 directly. */
5134 #ifdef SUPPORT_UCP
5135 if ((options & PCRE_UCP) != 0)
5137 unsigned int ptype = 0;
5138 int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
5140 /* The posix_substitutes table specifies which POSIX classes can be
5141 converted to \p or \P items. */
5143 if (posix_substitutes[pc] != NULL)
5145 nestptr = tempptr + 1;
5146 ptr = posix_substitutes[pc] - 1;
5147 continue;
5150 /* There are three other classes that generate special property calls
5151 that are recognized only in an XCLASS. */
5153 else switch(posix_class)
5155 case PC_GRAPH:
5156 ptype = PT_PXGRAPH;
5157 /* Fall through */
5158 case PC_PRINT:
5159 if (ptype == 0) ptype = PT_PXPRINT;
5160 /* Fall through */
5161 case PC_PUNCT:
5162 if (ptype == 0) ptype = PT_PXPUNCT;
5163 *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP;
5164 *class_uchardata++ = ptype;
5165 *class_uchardata++ = 0;
5166 xclass_has_prop = TRUE;
5167 ptr = tempptr + 1;
5168 continue;
5170 /* For the other POSIX classes (ascii, cntrl, xdigit) we are going
5171 to fall through to the non-UCP case and build a bit map for
5172 characters with code points less than 256. If we are in a negated
5173 POSIX class, characters with code points greater than 255 must
5174 either all match or all not match. In the special case where we
5175 have not yet generated any xclass data, and this is the final item
5176 in the overall class, we need do nothing: later on, the opcode
5177 OP_NCLASS will be used to indicate that characters greater than 255
5178 are acceptable. If we have already seen an xclass item or one may
5179 follow (we have to assume that it might if this is not the end of
5180 the class), explicitly list all wide codepoints, which will then
5181 either not match or match, depending on whether the class is or is
5182 not negated. */
5184 default:
5185 if (local_negate &&
5186 (xclass || tempptr[2] != CHAR_RIGHT_SQUARE_BRACKET))
5188 *class_uchardata++ = XCL_RANGE;
5189 class_uchardata += PRIV(ord2utf)(0x100, class_uchardata);
5190 class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
5192 break;
5195 #endif
5196 /* In the non-UCP case, or when UCP makes no difference, we build the
5197 bit map for the POSIX class in a chunk of local store because we may be
5198 adding and subtracting from it, and we don't want to subtract bits that
5199 may be in the main map already. At the end we or the result into the
5200 bit map that is being built. */
5202 posix_class *= 3;
5204 /* Copy in the first table (always present) */
5206 memcpy(pbits, cbits + posix_class_maps[posix_class],
5207 32 * sizeof(pcre_uint8));
5209 /* If there is a second table, add or remove it as required. */
5211 taboffset = posix_class_maps[posix_class + 1];
5212 tabopt = posix_class_maps[posix_class + 2];
5214 if (taboffset >= 0)
5216 if (tabopt >= 0)
5217 for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
5218 else
5219 for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
5222 /* Now see if we need to remove any special characters. An option
5223 value of 1 removes vertical space and 2 removes underscore. */
5225 if (tabopt < 0) tabopt = -tabopt;
5226 if (tabopt == 1) pbits[1] &= ~0x3c;
5227 else if (tabopt == 2) pbits[11] &= 0x7f;
5229 /* Add the POSIX table or its complement into the main table that is
5230 being built and we are done. */
5232 if (local_negate)
5233 for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
5234 else
5235 for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
5237 ptr = tempptr + 1;
5238 /* Every class contains at least one < 256 character. */
5239 class_has_8bitchar = 1;
5240 /* Every class contains at least two characters. */
5241 class_one_char = 2;
5242 continue; /* End of POSIX syntax handling */
5245 /* Backslash may introduce a single character, or it may introduce one
5246 of the specials, which just set a flag. The sequence \b is a special
5247 case. Inside a class (and only there) it is treated as backspace. We
5248 assume that other escapes have more than one character in them, so
5249 speculatively set both class_has_8bitchar and class_one_char bigger
5250 than one. Unrecognized escapes fall through and are either treated
5251 as literal characters (by default), or are faulted if
5252 PCRE_EXTRA is set. */
5254 if (c == CHAR_BACKSLASH && (!posixre || nestptr))
5256 escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
5257 TRUE);
5258 if (*errorcodeptr != 0) goto FAILED;
5259 if (escape == 0) c = ec;
5260 else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
5261 else if (escape == ESC_N) /* \N is not supported in a class */
5263 *errorcodeptr = ERR71;
5264 goto FAILED;
5266 else if (escape == ESC_Q) /* Handle start of quoted string */
5268 if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
5270 ptr += 2; /* avoid empty string */
5272 else inescq = TRUE;
5273 continue;
5275 else if (escape == ESC_E) continue; /* Ignore orphan \E */
5277 else
5279 register const pcre_uint8 *cbits = cd->cbits;
5280 /* Every class contains at least two < 256 characters. */
5281 class_has_8bitchar++;
5282 /* Every class contains at least two characters. */
5283 class_one_char += 2;
5285 switch (escape)
5287 #ifdef SUPPORT_UCP
5288 case ESC_du: /* These are the values given for \d etc */
5289 case ESC_DU: /* when PCRE_UCP is set. We replace the */
5290 case ESC_wu: /* escape sequence with an appropriate \p */
5291 case ESC_WU: /* or \P to test Unicode properties instead */
5292 case ESC_su: /* of the default ASCII testing. */
5293 case ESC_SU:
5294 nestptr = ptr;
5295 ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
5296 class_has_8bitchar--; /* Undo! */
5297 continue;
5298 #endif
5299 case ESC_d:
5300 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
5301 continue;
5303 case ESC_D:
5304 should_flip_negation = TRUE;
5305 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
5306 continue;
5308 case ESC_w:
5309 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
5310 continue;
5312 case ESC_W:
5313 should_flip_negation = TRUE;
5314 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
5315 continue;
5317 /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
5318 5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
5319 previously set by something earlier in the character class.
5320 Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
5321 we could just adjust the appropriate bit. From PCRE 8.34 we no
5322 longer treat \s and \S specially. */
5324 case ESC_s:
5325 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
5326 continue;
5328 case ESC_S:
5329 should_flip_negation = TRUE;
5330 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
5331 continue;
5333 /* The rest apply in both UCP and non-UCP cases. */
5335 case ESC_h:
5336 (void)add_list_to_class(classbits, &class_uchardata, options, cd,
5337 PRIV(hspace_list), NOTACHAR);
5338 continue;
5340 case ESC_H:
5341 (void)add_not_list_to_class(classbits, &class_uchardata, options,
5342 cd, PRIV(hspace_list));
5343 continue;
5345 case ESC_v:
5346 (void)add_list_to_class(classbits, &class_uchardata, options, cd,
5347 PRIV(vspace_list), NOTACHAR);
5348 continue;
5350 case ESC_V:
5351 (void)add_not_list_to_class(classbits, &class_uchardata, options,
5352 cd, PRIV(vspace_list));
5353 continue;
5355 case ESC_p:
5356 case ESC_P:
5357 #ifdef SUPPORT_UCP
5359 BOOL negated;
5360 unsigned int ptype = 0, pdata = 0;
5361 if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
5362 goto FAILED;
5363 *class_uchardata++ = ((escape == ESC_p) != negated)?
5364 XCL_PROP : XCL_NOTPROP;
5365 *class_uchardata++ = ptype;
5366 *class_uchardata++ = pdata;
5367 xclass_has_prop = TRUE;
5368 class_has_8bitchar--; /* Undo! */
5369 continue;
5371 #else
5372 *errorcodeptr = ERR45;
5373 goto FAILED;
5374 #endif
5375 /* Unrecognized escapes are faulted if PCRE is running in its
5376 strict mode. By default, for compatibility with Perl, they are
5377 treated as literals. */
5379 default:
5380 if ((options & PCRE_EXTRA) != 0)
5382 *errorcodeptr = ERR7;
5383 goto FAILED;
5385 class_has_8bitchar--; /* Undo the speculative increase. */
5386 class_one_char -= 2; /* Undo the speculative increase. */
5387 c = *ptr; /* Get the final character and fall through */
5388 break;
5392 /* Fall through if the escape just defined a single character (c >= 0).
5393 This may be greater than 256. */
5395 escape = 0;
5397 } /* End of backslash handling */
5399 /* A character may be followed by '-' to form a range. However, Perl does
5400 not permit ']' to be the end of the range. A '-' character at the end is
5401 treated as a literal. Perl ignores orphaned \E sequences entirely. The
5402 code for handling \Q and \E is messy. */
5404 CHECK_RANGE:
5405 while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E && (!posixre || nestptr))
5407 inescq = FALSE;
5408 ptr += 2;
5410 oldptr = ptr;
5412 /* Remember if \r or \n were explicitly used */
5414 if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
5416 /* Check for range */
5418 if (!inescq && ptr[1] == CHAR_MINUS)
5420 pcre_uint32 d;
5421 ptr += 2;
5422 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E && (!posixre || nestptr)) ptr += 2;
5424 /* If we hit \Q (not followed by \E) at this point, go into escaped
5425 mode. */
5427 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q && (!posixre || nestptr))
5429 ptr += 2;
5430 if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E)
5431 { ptr += 2; continue; }
5432 inescq = TRUE;
5433 break;
5436 /* Minus (hyphen) at the end of a class is treated as a literal, so put
5437 back the pointer and jump to handle the character that preceded it. */
5439 if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
5441 ptr = oldptr;
5442 goto CLASS_SINGLE_CHARACTER;
5445 /* Otherwise, we have a potential range; pick up the next character */
5447 #ifdef SUPPORT_UTF
5448 if (utf)
5449 { /* Braces are required because the */
5450 GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
5452 else
5453 #endif
5454 d = *ptr; /* Not UTF-8 mode */
5456 /* The second part of a range can be a single-character escape
5457 sequence, but not any of the other escapes. Perl treats a hyphen as a
5458 literal in such circumstances. However, in Perl's warning mode, a
5459 warning is given, so PCRE now faults it as it is almost certainly a
5460 mistake on the user's part. */
5462 if (!inescq)
5464 if (d == CHAR_BACKSLASH && (!posixre || nestptr))
5466 int descape;
5467 descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
5468 if (*errorcodeptr != 0) goto FAILED;
5470 /* 0 means a character was put into d; \b is backspace; any other
5471 special causes an error. */
5473 if (descape != 0)
5475 if (descape == ESC_b) d = CHAR_BS; else
5477 *errorcodeptr = ERR83;
5478 goto FAILED;
5483 /* A hyphen followed by a POSIX class is treated in the same way. */
5485 else if (d == CHAR_LEFT_SQUARE_BRACKET &&
5486 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
5487 ptr[1] == CHAR_EQUALS_SIGN) &&
5488 check_posix_syntax(ptr, &tempptr))
5490 *errorcodeptr = ERR83;
5491 goto FAILED;
5495 /* Check that the two values are in the correct order. Optimize
5496 one-character ranges. */
5498 if (d < c)
5500 *errorcodeptr = ERR8;
5501 goto FAILED;
5503 if (d == c) goto CLASS_SINGLE_CHARACTER; /* A few lines below */
5505 /* We have found a character range, so single character optimizations
5506 cannot be done anymore. Any value greater than 1 indicates that there
5507 is more than one character. */
5509 class_one_char = 2;
5511 /* Remember an explicit \r or \n, and add the range to the class. */
5513 if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
5515 class_has_8bitchar +=
5516 add_to_class(classbits, &class_uchardata, options, cd, c, d);
5518 continue; /* Go get the next char in the class */
5521 /* Handle a single character - we can get here for a normal non-escape
5522 char, or after \ that introduces a single character or for an apparent
5523 range that isn't. Only the value 1 matters for class_one_char, so don't
5524 increase it if it is already 2 or more ... just in case there's a class
5525 with a zillion characters in it. */
5527 CLASS_SINGLE_CHARACTER:
5528 if (class_one_char < 2) class_one_char++;
5530 /* If xclass_has_prop is false and class_one_char is 1, we have the first
5531 single character in the class, and there have been no prior ranges, or
5532 XCLASS items generated by escapes. If this is the final character in the
5533 class, we can optimize by turning the item into a 1-character OP_CHAR[I]
5534 if it's positive, or OP_NOT[I] if it's negative. In the positive case, it
5535 can cause firstchar to be set. Otherwise, there can be no first char if
5536 this item is first, whatever repeat count may follow. In the case of
5537 reqchar, save the previous value for reinstating. */
5539 if (!inescq &&
5540 #ifdef SUPPORT_UCP
5541 !xclass_has_prop &&
5542 #endif
5543 class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
5545 ptr++;
5546 zeroreqchar = reqchar;
5547 zeroreqcharflags = reqcharflags;
5549 if (negate_class)
5551 #ifdef SUPPORT_UCP
5552 int d;
5553 #endif
5554 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5555 zerofirstchar = firstchar;
5556 zerofirstcharflags = firstcharflags;
5558 /* For caseless UTF-8 mode when UCP support is available, check
5559 whether this character has more than one other case. If so, generate
5560 a special OP_NOTPROP item instead of OP_NOTI. */
5562 #ifdef SUPPORT_UCP
5563 if (utf && (options & PCRE_CASELESS) != 0 &&
5564 (d = UCD_CASESET(c)) != 0)
5566 *code++ = OP_NOTPROP;
5567 *code++ = PT_CLIST;
5568 *code++ = d;
5570 else
5571 #endif
5572 /* Char has only one other case, or UCP not available */
5575 *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
5576 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5577 if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
5578 code += PRIV(ord2utf)(c, code);
5579 else
5580 #endif
5581 *code++ = c;
5584 /* We are finished with this character class */
5586 goto END_CLASS;
5589 /* For a single, positive character, get the value into mcbuffer, and
5590 then we can handle this with the normal one-character code. */
5592 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5593 if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
5594 mclength = PRIV(ord2utf)(c, mcbuffer);
5595 else
5596 #endif
5598 mcbuffer[0] = c;
5599 mclength = 1;
5601 goto ONE_CHAR;
5602 } /* End of 1-char optimization */
5604 /* There is more than one character in the class, or an XCLASS item
5605 has been generated. Add this character to the class. */
5607 class_has_8bitchar +=
5608 add_to_class(classbits, &class_uchardata, options, cd, c, c);
5611 /* Loop until ']' reached. This "while" is the end of the "do" far above.
5612 If we are at the end of an internal nested string, revert to the outer
5613 string. */
5615 while (((c = *(++ptr)) != CHAR_NULL ||
5616 (nestptr != NULL &&
5617 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL) ||
5618 (inescq && embednul && ptr < cd->end_pattern) ||
5619 (embednul && ptr < cd->end_pattern &&
5620 (nestptr = ptr + 1, ptr = sub_embedded_nul, c = *(++ptr)) != CHAR_NULL)) &&
5621 (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
5623 /* Check for missing terminating ']' */
5625 if (c == CHAR_NULL)
5627 *errorcodeptr = ERR6;
5628 goto FAILED;
5631 /* We will need an XCLASS if data has been placed in class_uchardata. In
5632 the second phase this is a sufficient test. However, in the pre-compile
5633 phase, class_uchardata gets emptied to prevent workspace overflow, so it
5634 only if the very last character in the class needs XCLASS will it contain
5635 anything at this point. For this reason, xclass gets set TRUE above when
5636 uchar_classdata is emptied, and that's why this code is the way it is here
5637 instead of just doing a test on class_uchardata below. */
5639 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5640 if (class_uchardata > class_uchardata_base) xclass = TRUE;
5641 #endif
5643 /* If this is the first thing in the branch, there can be no first char
5644 setting, whatever the repeat count. Any reqchar setting must remain
5645 unchanged after any kind of repeat. */
5647 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5648 zerofirstchar = firstchar;
5649 zerofirstcharflags = firstcharflags;
5650 zeroreqchar = reqchar;
5651 zeroreqcharflags = reqcharflags;
5653 /* If there are characters with values > 255, we have to compile an
5654 extended class, with its own opcode, unless there was a negated special
5655 such as \S in the class, and PCRE_UCP is not set, because in that case all
5656 characters > 255 are in the class, so any that were explicitly given as
5657 well can be ignored. If (when there are explicit characters > 255 that must
5658 be listed) there are no characters < 256, we can omit the bitmap in the
5659 actual compiled code. */
5661 #ifdef SUPPORT_UTF
5662 if (xclass && (xclass_has_prop || !should_flip_negation ||
5663 (options & PCRE_UCP) != 0))
5664 #elif !defined COMPILE_PCRE8
5665 if (xclass && (xclass_has_prop || !should_flip_negation))
5666 #endif
5667 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5669 /* For non-UCP wide characters, in a non-negative class containing \S or
5670 similar (should_flip_negation is set), all characters greater than 255
5671 must be in the class. */
5673 if (
5674 #if defined COMPILE_PCRE8
5675 utf &&
5676 #endif
5677 should_flip_negation && !negate_class && (options & PCRE_UCP) == 0)
5679 *class_uchardata++ = XCL_RANGE;
5680 if (utf) /* Will always be utf in the 8-bit library */
5682 class_uchardata += PRIV(ord2utf)(0x100, class_uchardata);
5683 class_uchardata += PRIV(ord2utf)(0x10ffff, class_uchardata);
5685 else /* Can only happen for the 16-bit & 32-bit libraries */
5687 #if defined COMPILE_PCRE16
5688 *class_uchardata++ = 0x100;
5689 *class_uchardata++ = 0xffffu;
5690 #elif defined COMPILE_PCRE32
5691 *class_uchardata++ = 0x100;
5692 *class_uchardata++ = 0xffffffffu;
5693 #endif
5697 *class_uchardata++ = XCL_END; /* Marks the end of extra data */
5698 *code++ = OP_XCLASS;
5699 code += LINK_SIZE;
5700 *code = negate_class? XCL_NOT:0;
5701 if (xclass_has_prop) *code |= XCL_HASPROP;
5703 /* If the map is required, move up the extra data to make room for it;
5704 otherwise just move the code pointer to the end of the extra data. */
5706 if (class_has_8bitchar > 0)
5708 *code++ |= XCL_MAP;
5709 memmove(code + (32 / sizeof(pcre_uchar)), code,
5710 IN_UCHARS(class_uchardata - code));
5711 if (negate_class && !xclass_has_prop)
5712 for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
5713 memcpy(code, classbits, 32);
5714 code = class_uchardata + (32 / sizeof(pcre_uchar));
5716 else code = class_uchardata;
5718 /* Now fill in the complete length of the item */
5720 PUT(previous, 1, (int)(code - previous));
5721 break; /* End of class handling */
5724 /* Even though any XCLASS list is now discarded, we must allow for
5725 its memory. */
5727 if (lengthptr != NULL)
5728 *lengthptr += (int)(class_uchardata - class_uchardata_base);
5729 #endif
5731 /* If there are no characters > 255, or they are all to be included or
5732 excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
5733 whole class was negated and whether there were negative specials such as \S
5734 (non-UCP) in the class. Then copy the 32-byte map into the code vector,
5735 negating it if necessary. */
5737 *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
5738 if (lengthptr == NULL) /* Save time in the pre-compile phase */
5740 if (negate_class)
5741 for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
5742 memcpy(code, classbits, 32);
5744 code += 32 / sizeof(pcre_uchar);
5746 END_CLASS:
5747 break;
5750 /* ===================================================================*/
5751 /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
5752 has been tested above. */
5754 case CHAR_LEFT_CURLY_BRACKET:
5755 if (!is_quantifier) goto NORMAL_CHAR;
5756 ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
5757 if (*errorcodeptr != 0) goto FAILED;
5758 goto REPEAT;
5760 case CHAR_ASTERISK:
5761 repeat_min = 0;
5762 repeat_max = -1;
5763 goto REPEAT;
5765 case CHAR_PLUS:
5766 repeat_min = 1;
5767 repeat_max = -1;
5768 goto REPEAT;
5770 case CHAR_QUESTION_MARK:
5771 repeat_min = 0;
5772 repeat_max = 1;
5774 REPEAT:
5775 if (previous == NULL)
5777 *errorcodeptr = ERR9;
5778 goto FAILED;
5781 if (repeat_min == 0)
5783 firstchar = zerofirstchar; /* Adjust for zero repeat */
5784 firstcharflags = zerofirstcharflags;
5785 reqchar = zeroreqchar; /* Ditto */
5786 reqcharflags = zeroreqcharflags;
5789 /* Remember whether this is a variable length repeat */
5791 reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
5793 op_type = 0; /* Default single-char op codes */
5794 possessive_quantifier = FALSE; /* Default not possessive quantifier */
5796 /* Save start of previous item, in case we have to move it up in order to
5797 insert something before it. */
5799 tempcode = previous;
5801 /* Before checking for a possessive quantifier, we must skip over
5802 whitespace and comments in extended mode because Perl allows white space at
5803 this point. */
5805 if ((options & PCRE_EXTENDED) != 0)
5807 const pcre_uchar *p = ptr + 1;
5808 for (;;)
5810 while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++;
5811 if (*p != CHAR_NUMBER_SIGN) break;
5812 p++;
5813 while (*p != CHAR_NULL || (nestptr == NULL && embednul && p < cd->end_pattern))
5815 if (IS_NEWLINE(p)) /* For non-fixed-length newline cases, */
5816 { /* IS_NEWLINE sets cd->nllen. */
5817 p += cd->nllen;
5818 break;
5820 p++;
5821 #ifdef SUPPORT_UTF
5822 if (utf) FORWARDCHAR(p);
5823 #endif
5824 } /* Loop for comment characters */
5825 } /* Loop for multiple comments */
5826 ptr = p - 1; /* Character before the next significant one. */
5829 /* We also need to skip over (?# comments, which are not dependent on
5830 extended mode. */
5832 if (ptr[1] == CHAR_LEFT_PARENTHESIS && ptr[2] == CHAR_QUESTION_MARK &&
5833 ptr[3] == CHAR_NUMBER_SIGN && (!basicre || nestptr))
5835 ptr += 4;
5836 while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5837 if (*ptr == CHAR_NULL)
5839 *errorcodeptr = ERR18;
5840 goto FAILED;
5844 /* If the next character is '+', we have a possessive quantifier. This
5845 implies greediness, whatever the setting of the PCRE_UNGREEDY option.
5846 If the next character is '?' this is a minimizing repeat, by default,
5847 but if PCRE_UNGREEDY is set, it works the other way round. We change the
5848 repeat type to the non-default. */
5850 if (ptr[1] == CHAR_PLUS && (!basicre || nestptr))
5852 repeat_type = 0; /* Force greedy */
5853 possessive_quantifier = TRUE;
5854 ptr++;
5856 else if (ptr[1] == CHAR_QUESTION_MARK && (!basicre || nestptr))
5858 repeat_type = greedy_non_default;
5859 ptr++;
5861 else repeat_type = greedy_default;
5863 /* If previous was a recursion call, wrap it in atomic brackets so that
5864 previous becomes the atomic group. All recursions were so wrapped in the
5865 past, but it no longer happens for non-repeated recursions. In fact, the
5866 repeated ones could be re-implemented independently so as not to need this,
5867 but for the moment we rely on the code for repeating groups. */
5869 if (*previous == OP_RECURSE)
5871 memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
5872 *previous = OP_ONCE;
5873 PUT(previous, 1, 2 + 2*LINK_SIZE);
5874 previous[2 + 2*LINK_SIZE] = OP_KET;
5875 PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
5876 code += 2 + 2 * LINK_SIZE;
5877 length_prevgroup = 3 + 3*LINK_SIZE;
5879 /* When actually compiling, we need to check whether this was a forward
5880 reference, and if so, adjust the offset. */
5882 if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
5884 int offset = GET(cd->hwm, -LINK_SIZE);
5885 if (offset == previous + 1 - cd->start_code)
5886 PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
5890 /* Now handle repetition for the different types of item. */
5892 /* If previous was a character or negated character match, abolish the item
5893 and generate a repeat item instead. If a char item has a minimum of more
5894 than one, ensure that it is set in reqchar - it might not be if a sequence
5895 such as x{3} is the first thing in a branch because the x will have gone
5896 into firstchar instead. */
5898 if (*previous == OP_CHAR || *previous == OP_CHARI
5899 || *previous == OP_NOT || *previous == OP_NOTI)
5901 switch (*previous)
5903 default: /* Make compiler happy. */
5904 case OP_CHAR: op_type = OP_STAR - OP_STAR; break;
5905 case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
5906 case OP_NOT: op_type = OP_NOTSTAR - OP_STAR; break;
5907 case OP_NOTI: op_type = OP_NOTSTARI - OP_STAR; break;
5910 /* Deal with UTF characters that take up more than one character. It's
5911 easier to write this out separately than try to macrify it. Use c to
5912 hold the length of the character in bytes, plus UTF_LENGTH to flag that
5913 it's a length rather than a small character. */
5915 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5916 if (utf && NOT_FIRSTCHAR(code[-1]))
5918 pcre_uchar *lastchar = code - 1;
5919 BACKCHAR(lastchar);
5920 c = (int)(code - lastchar); /* Length of UTF-8 character */
5921 memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
5922 c |= UTF_LENGTH; /* Flag c as a length */
5924 else
5925 #endif /* SUPPORT_UTF */
5927 /* Handle the case of a single charater - either with no UTF support, or
5928 with UTF disabled, or for a single character UTF character. */
5930 c = code[-1];
5931 if (*previous <= OP_CHARI && repeat_min > 1)
5933 reqchar = c;
5934 reqcharflags = req_caseopt | cd->req_varyopt;
5938 goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
5941 /* If previous was a character type match (\d or similar), abolish it and
5942 create a suitable repeat item. The code is shared with single-character
5943 repeats by setting op_type to add a suitable offset into repeat_type. Note
5944 the the Unicode property types will be present only when SUPPORT_UCP is
5945 defined, but we don't wrap the little bits of code here because it just
5946 makes it horribly messy. */
5948 else if (*previous < OP_EODN)
5950 pcre_uchar *oldcode;
5951 int prop_type, prop_value;
5952 op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
5953 c = *previous;
5955 OUTPUT_SINGLE_REPEAT:
5956 if (*previous == OP_PROP || *previous == OP_NOTPROP)
5958 prop_type = previous[1];
5959 prop_value = previous[2];
5961 else prop_type = prop_value = -1;
5963 oldcode = code;
5964 code = previous; /* Usually overwrite previous item */
5966 /* If the maximum is zero then the minimum must also be zero; Perl allows
5967 this case, so we do too - by simply omitting the item altogether. */
5969 if (repeat_max == 0) goto END_REPEAT;
5971 /* Combine the op_type with the repeat_type */
5973 repeat_type += op_type;
5975 /* A minimum of zero is handled either as the special case * or ?, or as
5976 an UPTO, with the maximum given. */
5978 if (repeat_min == 0)
5980 if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
5981 else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
5982 else
5984 *code++ = OP_UPTO + repeat_type;
5985 PUT2INC(code, 0, repeat_max);
5989 /* A repeat minimum of 1 is optimized into some special cases. If the
5990 maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
5991 left in place and, if the maximum is greater than 1, we use OP_UPTO with
5992 one less than the maximum. */
5994 else if (repeat_min == 1)
5996 if (repeat_max == -1)
5997 *code++ = OP_PLUS + repeat_type;
5998 else
6000 code = oldcode; /* leave previous item in place */
6001 if (repeat_max == 1) goto END_REPEAT;
6002 *code++ = OP_UPTO + repeat_type;
6003 PUT2INC(code, 0, repeat_max - 1);
6007 /* The case {n,n} is just an EXACT, while the general case {n,m} is
6008 handled as an EXACT followed by an UPTO. */
6010 else
6012 *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
6013 PUT2INC(code, 0, repeat_min);
6015 /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
6016 we have to insert the character for the previous code. For a repeated
6017 Unicode property match, there are two extra bytes that define the
6018 required property. In UTF-8 mode, long characters have their length in
6019 c, with the UTF_LENGTH bit as a flag. */
6021 if (repeat_max < 0)
6023 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6024 if (utf && (c & UTF_LENGTH) != 0)
6026 memcpy(code, utf_chars, IN_UCHARS(c & 7));
6027 code += c & 7;
6029 else
6030 #endif
6032 *code++ = c;
6033 if (prop_type >= 0)
6035 *code++ = prop_type;
6036 *code++ = prop_value;
6039 *code++ = OP_STAR + repeat_type;
6042 /* Else insert an UPTO if the max is greater than the min, again
6043 preceded by the character, for the previously inserted code. If the
6044 UPTO is just for 1 instance, we can use QUERY instead. */
6046 else if (repeat_max != repeat_min)
6048 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6049 if (utf && (c & UTF_LENGTH) != 0)
6051 memcpy(code, utf_chars, IN_UCHARS(c & 7));
6052 code += c & 7;
6054 else
6055 #endif
6056 *code++ = c;
6057 if (prop_type >= 0)
6059 *code++ = prop_type;
6060 *code++ = prop_value;
6062 repeat_max -= repeat_min;
6064 if (repeat_max == 1)
6066 *code++ = OP_QUERY + repeat_type;
6068 else
6070 *code++ = OP_UPTO + repeat_type;
6071 PUT2INC(code, 0, repeat_max);
6076 /* The character or character type itself comes last in all cases. */
6078 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
6079 if (utf && (c & UTF_LENGTH) != 0)
6081 memcpy(code, utf_chars, IN_UCHARS(c & 7));
6082 code += c & 7;
6084 else
6085 #endif
6086 *code++ = c;
6088 /* For a repeated Unicode property match, there are two extra bytes that
6089 define the required property. */
6091 #ifdef SUPPORT_UCP
6092 if (prop_type >= 0)
6094 *code++ = prop_type;
6095 *code++ = prop_value;
6097 #endif
6100 /* If previous was a character class or a back reference, we put the repeat
6101 stuff after it, but just skip the item if the repeat was {0,0}. */
6103 else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
6104 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6105 *previous == OP_XCLASS ||
6106 #endif
6107 *previous == OP_REF || *previous == OP_REFI ||
6108 *previous == OP_DNREF || *previous == OP_DNREFI)
6110 if (repeat_max == 0)
6112 code = previous;
6113 goto END_REPEAT;
6116 if (repeat_min == 0 && repeat_max == -1)
6117 *code++ = OP_CRSTAR + repeat_type;
6118 else if (repeat_min == 1 && repeat_max == -1)
6119 *code++ = OP_CRPLUS + repeat_type;
6120 else if (repeat_min == 0 && repeat_max == 1)
6121 *code++ = OP_CRQUERY + repeat_type;
6122 else
6124 *code++ = OP_CRRANGE + repeat_type;
6125 PUT2INC(code, 0, repeat_min);
6126 if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */
6127 PUT2INC(code, 0, repeat_max);
6131 /* If previous was a bracket group, we may have to replicate it in certain
6132 cases. Note that at this point we can encounter only the "basic" bracket
6133 opcodes such as BRA and CBRA, as this is the place where they get converted
6134 into the more special varieties such as BRAPOS and SBRA. A test for >=
6135 OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
6136 ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND.
6137 Originally, PCRE did not allow repetition of assertions, but now it does,
6138 for Perl compatibility. */
6140 else if (*previous >= OP_ASSERT && *previous <= OP_COND)
6142 register int i;
6143 int len = (int)(code - previous);
6144 size_t base_hwm_offset = item_hwm_offset;
6145 pcre_uchar *bralink = NULL;
6146 pcre_uchar *brazeroptr = NULL;
6148 /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
6149 we just ignore the repeat. */
6151 if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
6152 goto END_REPEAT;
6154 /* There is no sense in actually repeating assertions. The only potential
6155 use of repetition is in cases when the assertion is optional. Therefore,
6156 if the minimum is greater than zero, just ignore the repeat. If the
6157 maximum is not zero or one, set it to 1. */
6159 if (*previous < OP_ONCE) /* Assertion */
6161 if (repeat_min > 0) goto END_REPEAT;
6162 if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
6165 /* The case of a zero minimum is special because of the need to stick
6166 OP_BRAZERO in front of it, and because the group appears once in the
6167 data, whereas in other cases it appears the minimum number of times. For
6168 this reason, it is simplest to treat this case separately, as otherwise
6169 the code gets far too messy. There are several special subcases when the
6170 minimum is zero. */
6172 if (repeat_min == 0)
6174 /* If the maximum is also zero, we used to just omit the group from the
6175 output altogether, like this:
6177 ** if (repeat_max == 0)
6178 ** {
6179 ** code = previous;
6180 ** goto END_REPEAT;
6181 ** }
6183 However, that fails when a group or a subgroup within it is referenced
6184 as a subroutine from elsewhere in the pattern, so now we stick in
6185 OP_SKIPZERO in front of it so that it is skipped on execution. As we
6186 don't have a list of which groups are referenced, we cannot do this
6187 selectively.
6189 If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
6190 and do no more at this point. However, we do need to adjust any
6191 OP_RECURSE calls inside the group that refer to the group itself or any
6192 internal or forward referenced group, because the offset is from the
6193 start of the whole regex. Temporarily terminate the pattern while doing
6194 this. */
6196 if (repeat_max <= 1) /* Covers 0, 1, and unlimited */
6198 *code = OP_END;
6199 adjust_recurse(previous, 1, utf, cd, item_hwm_offset);
6200 memmove(previous + 1, previous, IN_UCHARS(len));
6201 code++;
6202 if (repeat_max == 0)
6204 *previous++ = OP_SKIPZERO;
6205 goto END_REPEAT;
6207 brazeroptr = previous; /* Save for possessive optimizing */
6208 *previous++ = OP_BRAZERO + repeat_type;
6211 /* If the maximum is greater than 1 and limited, we have to replicate
6212 in a nested fashion, sticking OP_BRAZERO before each set of brackets.
6213 The first one has to be handled carefully because it's the original
6214 copy, which has to be moved up. The remainder can be handled by code
6215 that is common with the non-zero minimum case below. We have to
6216 adjust the value or repeat_max, since one less copy is required. Once
6217 again, we may have to adjust any OP_RECURSE calls inside the group. */
6219 else
6221 int offset;
6222 *code = OP_END;
6223 adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, item_hwm_offset);
6224 memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
6225 code += 2 + LINK_SIZE;
6226 *previous++ = OP_BRAZERO + repeat_type;
6227 *previous++ = OP_BRA;
6229 /* We chain together the bracket offset fields that have to be
6230 filled in later when the ends of the brackets are reached. */
6232 offset = (bralink == NULL)? 0 : (int)(previous - bralink);
6233 bralink = previous;
6234 PUTINC(previous, 0, offset);
6237 repeat_max--;
6240 /* If the minimum is greater than zero, replicate the group as many
6241 times as necessary, and adjust the maximum to the number of subsequent
6242 copies that we need. If we set a first char from the group, and didn't
6243 set a required char, copy the latter from the former. If there are any
6244 forward reference subroutine calls in the group, there will be entries on
6245 the workspace list; replicate these with an appropriate increment. */
6247 else
6249 if (repeat_min > 1)
6251 /* In the pre-compile phase, we don't actually do the replication. We
6252 just adjust the length as if we had. Do some paranoid checks for
6253 potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
6254 integer type when available, otherwise double. */
6256 if (lengthptr != NULL)
6258 int delta = (repeat_min - 1)*length_prevgroup;
6259 if ((INT64_OR_DOUBLE)(repeat_min - 1)*
6260 (INT64_OR_DOUBLE)length_prevgroup >
6261 (INT64_OR_DOUBLE)INT_MAX ||
6262 OFLOW_MAX - *lengthptr < delta)
6264 *errorcodeptr = ERR20;
6265 goto FAILED;
6267 *lengthptr += delta;
6270 /* This is compiling for real. If there is a set first byte for
6271 the group, and we have not yet set a "required byte", set it. Make
6272 sure there is enough workspace for copying forward references before
6273 doing the copy. */
6275 else
6277 if (groupsetfirstchar && reqcharflags < 0)
6279 reqchar = firstchar;
6280 reqcharflags = firstcharflags;
6283 for (i = 1; i < repeat_min; i++)
6285 pcre_uchar *hc;
6286 size_t this_hwm_offset = cd->hwm - cd->start_workspace;
6287 memcpy(code, previous, IN_UCHARS(len));
6289 while (cd->hwm > cd->start_workspace + cd->workspace_size -
6290 WORK_SIZE_SAFETY_MARGIN -
6291 (this_hwm_offset - base_hwm_offset))
6293 *errorcodeptr = expand_workspace(cd);
6294 if (*errorcodeptr != 0) goto FAILED;
6297 for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset;
6298 hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
6299 hc += LINK_SIZE)
6301 PUT(cd->hwm, 0, GET(hc, 0) + len);
6302 cd->hwm += LINK_SIZE;
6304 base_hwm_offset = this_hwm_offset;
6305 code += len;
6310 if (repeat_max > 0) repeat_max -= repeat_min;
6313 /* This code is common to both the zero and non-zero minimum cases. If
6314 the maximum is limited, it replicates the group in a nested fashion,
6315 remembering the bracket starts on a stack. In the case of a zero minimum,
6316 the first one was set up above. In all cases the repeat_max now specifies
6317 the number of additional copies needed. Again, we must remember to
6318 replicate entries on the forward reference list. */
6320 if (repeat_max >= 0)
6322 /* In the pre-compile phase, we don't actually do the replication. We
6323 just adjust the length as if we had. For each repetition we must add 1
6324 to the length for BRAZERO and for all but the last repetition we must
6325 add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
6326 paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
6327 a 64-bit integer type when available, otherwise double. */
6329 if (lengthptr != NULL && repeat_max > 0)
6331 int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
6332 2 - 2*LINK_SIZE; /* Last one doesn't nest */
6333 if ((INT64_OR_DOUBLE)repeat_max *
6334 (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
6335 > (INT64_OR_DOUBLE)INT_MAX ||
6336 OFLOW_MAX - *lengthptr < delta)
6338 *errorcodeptr = ERR20;
6339 goto FAILED;
6341 *lengthptr += delta;
6344 /* This is compiling for real */
6346 else for (i = repeat_max - 1; i >= 0; i--)
6348 pcre_uchar *hc;
6349 size_t this_hwm_offset = cd->hwm - cd->start_workspace;
6351 *code++ = OP_BRAZERO + repeat_type;
6353 /* All but the final copy start a new nesting, maintaining the
6354 chain of brackets outstanding. */
6356 if (i != 0)
6358 int offset;
6359 *code++ = OP_BRA;
6360 offset = (bralink == NULL)? 0 : (int)(code - bralink);
6361 bralink = code;
6362 PUTINC(code, 0, offset);
6365 memcpy(code, previous, IN_UCHARS(len));
6367 /* Ensure there is enough workspace for forward references before
6368 copying them. */
6370 while (cd->hwm > cd->start_workspace + cd->workspace_size -
6371 WORK_SIZE_SAFETY_MARGIN -
6372 (this_hwm_offset - base_hwm_offset))
6374 *errorcodeptr = expand_workspace(cd);
6375 if (*errorcodeptr != 0) goto FAILED;
6378 for (hc = (pcre_uchar *)cd->start_workspace + base_hwm_offset;
6379 hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
6380 hc += LINK_SIZE)
6382 PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
6383 cd->hwm += LINK_SIZE;
6385 base_hwm_offset = this_hwm_offset;
6386 code += len;
6389 /* Now chain through the pending brackets, and fill in their length
6390 fields (which are holding the chain links pro tem). */
6392 while (bralink != NULL)
6394 int oldlinkoffset;
6395 int offset = (int)(code - bralink + 1);
6396 pcre_uchar *bra = code - offset;
6397 oldlinkoffset = GET(bra, 1);
6398 bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
6399 *code++ = OP_KET;
6400 PUTINC(code, 0, offset);
6401 PUT(bra, 1, offset);
6405 /* If the maximum is unlimited, set a repeater in the final copy. For
6406 ONCE brackets, that's all we need to do. However, possessively repeated
6407 ONCE brackets can be converted into non-capturing brackets, as the
6408 behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
6409 deal with possessive ONCEs specially.
6411 Otherwise, when we are doing the actual compile phase, check to see
6412 whether this group is one that could match an empty string. If so,
6413 convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
6414 that runtime checking can be done. [This check is also applied to ONCE
6415 groups at runtime, but in a different way.]
6417 Then, if the quantifier was possessive and the bracket is not a
6418 conditional, we convert the BRA code to the POS form, and the KET code to
6419 KETRPOS. (It turns out to be convenient at runtime to detect this kind of
6420 subpattern at both the start and at the end.) The use of special opcodes
6421 makes it possible to reduce greatly the stack usage in pcre_exec(). If
6422 the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
6424 Then, if the minimum number of matches is 1 or 0, cancel the possessive
6425 flag so that the default action below, of wrapping everything inside
6426 atomic brackets, does not happen. When the minimum is greater than 1,
6427 there will be earlier copies of the group, and so we still have to wrap
6428 the whole thing. */
6430 else
6432 pcre_uchar *ketcode = code - 1 - LINK_SIZE;
6433 pcre_uchar *bracode = ketcode - GET(ketcode, 1);
6435 /* Convert possessive ONCE brackets to non-capturing */
6437 if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
6438 possessive_quantifier) *bracode = OP_BRA;
6440 /* For non-possessive ONCE brackets, all we need to do is to
6441 set the KET. */
6443 if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
6444 *ketcode = OP_KETRMAX + repeat_type;
6446 /* Handle non-ONCE brackets and possessive ONCEs (which have been
6447 converted to non-capturing above). */
6449 else
6451 /* In the compile phase, check for empty string matching. */
6453 if (lengthptr == NULL)
6455 pcre_uchar *scode = bracode;
6458 if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
6460 *bracode += OP_SBRA - OP_BRA;
6461 break;
6463 scode += GET(scode, 1);
6465 while (*scode == OP_ALT);
6468 /* A conditional group with only one branch has an implicit empty
6469 alternative branch. */
6471 if (*bracode == OP_COND && bracode[GET(bracode,1)] != OP_ALT)
6472 *bracode = OP_SCOND;
6474 /* Handle possessive quantifiers. */
6476 if (possessive_quantifier)
6478 /* For COND brackets, we wrap the whole thing in a possessively
6479 repeated non-capturing bracket, because we have not invented POS
6480 versions of the COND opcodes. Because we are moving code along, we
6481 must ensure that any pending recursive references are updated. */
6483 if (*bracode == OP_COND || *bracode == OP_SCOND)
6485 int nlen = (int)(code - bracode);
6486 *code = OP_END;
6487 adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
6488 memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
6489 code += 1 + LINK_SIZE;
6490 nlen += 1 + LINK_SIZE;
6491 *bracode = (*bracode == OP_COND)? OP_BRAPOS : OP_SBRAPOS;
6492 *code++ = OP_KETRPOS;
6493 PUTINC(code, 0, nlen);
6494 PUT(bracode, 1, nlen);
6497 /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
6499 else
6501 *bracode += 1; /* Switch to xxxPOS opcodes */
6502 *ketcode = OP_KETRPOS;
6505 /* If the minimum is zero, mark it as possessive, then unset the
6506 possessive flag when the minimum is 0 or 1. */
6508 if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
6509 if (repeat_min < 2) possessive_quantifier = FALSE;
6512 /* Non-possessive quantifier */
6514 else *ketcode = OP_KETRMAX + repeat_type;
6519 /* If previous is OP_FAIL, it was generated by an empty class [] in
6520 JavaScript mode. The other ways in which OP_FAIL can be generated, that is
6521 by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
6522 error above. We can just ignore the repeat in JS case. */
6524 else if (*previous == OP_FAIL) goto END_REPEAT;
6526 /* Else there's some kind of shambles */
6528 else
6530 *errorcodeptr = ERR11;
6531 goto FAILED;
6534 /* If the character following a repeat is '+', possessive_quantifier is
6535 TRUE. For some opcodes, there are special alternative opcodes for this
6536 case. For anything else, we wrap the entire repeated item inside OP_ONCE
6537 brackets. Logically, the '+' notation is just syntactic sugar, taken from
6538 Sun's Java package, but the special opcodes can optimize it.
6540 Some (but not all) possessively repeated subpatterns have already been
6541 completely handled in the code just above. For them, possessive_quantifier
6542 is always FALSE at this stage. Note that the repeated item starts at
6543 tempcode, not at previous, which might be the first part of a string whose
6544 (former) last char we repeated. */
6546 if (possessive_quantifier)
6548 int len;
6550 /* Possessifying an EXACT quantifier has no effect, so we can ignore it.
6551 However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6},
6552 {5,}, or {5,10}). We skip over an EXACT item; if the length of what
6553 remains is greater than zero, there's a further opcode that can be
6554 handled. If not, do nothing, leaving the EXACT alone. */
6556 switch(*tempcode)
6558 case OP_TYPEEXACT:
6559 tempcode += PRIV(OP_lengths)[*tempcode] +
6560 ((tempcode[1 + IMM2_SIZE] == OP_PROP
6561 || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
6562 break;
6564 /* CHAR opcodes are used for exacts whose count is 1. */
6566 case OP_CHAR:
6567 case OP_CHARI:
6568 case OP_NOT:
6569 case OP_NOTI:
6570 case OP_EXACT:
6571 case OP_EXACTI:
6572 case OP_NOTEXACT:
6573 case OP_NOTEXACTI:
6574 tempcode += PRIV(OP_lengths)[*tempcode];
6575 #ifdef SUPPORT_UTF
6576 if (utf && HAS_EXTRALEN(tempcode[-1]))
6577 tempcode += GET_EXTRALEN(tempcode[-1]);
6578 #endif
6579 break;
6581 /* For the class opcodes, the repeat operator appears at the end;
6582 adjust tempcode to point to it. */
6584 case OP_CLASS:
6585 case OP_NCLASS:
6586 tempcode += 1 + 32/sizeof(pcre_uchar);
6587 break;
6589 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6590 case OP_XCLASS:
6591 tempcode += GET(tempcode, 1);
6592 break;
6593 #endif
6596 /* If tempcode is equal to code (which points to the end of the repeated
6597 item), it means we have skipped an EXACT item but there is no following
6598 QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In
6599 all other cases, tempcode will be pointing to the repeat opcode, and will
6600 be less than code, so the value of len will be greater than 0. */
6602 len = (int)(code - tempcode);
6603 if (len > 0)
6605 unsigned int repcode = *tempcode;
6607 /* There is a table for possessifying opcodes, all of which are less
6608 than OP_CALLOUT. A zero entry means there is no possessified version.
6611 if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0)
6612 *tempcode = opcode_possessify[repcode];
6614 /* For opcode without a special possessified version, wrap the item in
6615 ONCE brackets. Because we are moving code along, we must ensure that any
6616 pending recursive references are updated. */
6618 else
6620 *code = OP_END;
6621 adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
6622 memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
6623 code += 1 + LINK_SIZE;
6624 len += 1 + LINK_SIZE;
6625 tempcode[0] = OP_ONCE;
6626 *code++ = OP_KET;
6627 PUTINC(code, 0, len);
6628 PUT(tempcode, 1, len);
6632 #ifdef NEVER
6633 if (len > 0) switch (*tempcode)
6635 case OP_STAR: *tempcode = OP_POSSTAR; break;
6636 case OP_PLUS: *tempcode = OP_POSPLUS; break;
6637 case OP_QUERY: *tempcode = OP_POSQUERY; break;
6638 case OP_UPTO: *tempcode = OP_POSUPTO; break;
6640 case OP_STARI: *tempcode = OP_POSSTARI; break;
6641 case OP_PLUSI: *tempcode = OP_POSPLUSI; break;
6642 case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
6643 case OP_UPTOI: *tempcode = OP_POSUPTOI; break;
6645 case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break;
6646 case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break;
6647 case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
6648 case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break;
6650 case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break;
6651 case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break;
6652 case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
6653 case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break;
6655 case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break;
6656 case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break;
6657 case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
6658 case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break;
6660 case OP_CRSTAR: *tempcode = OP_CRPOSSTAR; break;
6661 case OP_CRPLUS: *tempcode = OP_CRPOSPLUS; break;
6662 case OP_CRQUERY: *tempcode = OP_CRPOSQUERY; break;
6663 case OP_CRRANGE: *tempcode = OP_CRPOSRANGE; break;
6665 /* Because we are moving code along, we must ensure that any
6666 pending recursive references are updated. */
6668 default:
6669 *code = OP_END;
6670 adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, item_hwm_offset);
6671 memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
6672 code += 1 + LINK_SIZE;
6673 len += 1 + LINK_SIZE;
6674 tempcode[0] = OP_ONCE;
6675 *code++ = OP_KET;
6676 PUTINC(code, 0, len);
6677 PUT(tempcode, 1, len);
6678 break;
6680 #endif
6683 /* In all case we no longer have a previous item. We also set the
6684 "follows varying string" flag for subsequently encountered reqchars if
6685 it isn't already set and we have just passed a varying length item. */
6687 END_REPEAT:
6688 previous = NULL;
6689 cd->req_varyopt |= reqvary;
6690 break;
6693 /* ===================================================================*/
6694 /* Start of nested parenthesized sub-expression, or comment or lookahead or
6695 lookbehind or option setting or condition or all the other extended
6696 parenthesis forms. */
6698 case CHAR_LEFT_PARENTHESIS:
6699 ptr++;
6701 /* Now deal with various "verbs" that can be introduced by '*'. */
6703 if (ptr[0] == CHAR_ASTERISK && (!basicre || nestptr) && (ptr[1] == ':'
6704 || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
6706 int i, namelen;
6707 int arglen = 0;
6708 const char *vn = verbnames;
6709 const pcre_uchar *name = ptr + 1;
6710 const pcre_uchar *arg = NULL;
6711 previous = NULL;
6712 ptr++;
6713 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
6714 namelen = (int)(ptr - name);
6716 /* It appears that Perl allows any characters whatsoever, other than
6717 a closing parenthesis, to appear in arguments, so we no longer insist on
6718 letters, digits, and underscores. */
6720 if (*ptr == CHAR_COLON)
6722 arg = ++ptr;
6723 while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
6724 arglen = (int)(ptr - arg);
6725 if ((unsigned int)arglen > MAX_MARK)
6727 *errorcodeptr = ERR75;
6728 goto FAILED;
6732 if (*ptr != CHAR_RIGHT_PARENTHESIS)
6734 *errorcodeptr = ERR60;
6735 goto FAILED;
6738 /* Scan the table of verb names */
6740 for (i = 0; i < verbcount; i++)
6742 if (namelen == verbs[i].len &&
6743 STRNCMP_UC_C8(name, vn, namelen) == 0)
6745 int setverb;
6747 /* Check for open captures before ACCEPT and convert it to
6748 ASSERT_ACCEPT if in an assertion. */
6750 if (verbs[i].op == OP_ACCEPT)
6752 open_capitem *oc;
6753 if (arglen != 0)
6755 *errorcodeptr = ERR59;
6756 goto FAILED;
6758 cd->had_accept = TRUE;
6759 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
6761 if (lengthptr != NULL)
6763 #ifdef COMPILE_PCRE8
6764 *lengthptr += 1 + IMM2_SIZE;
6765 #elif defined COMPILE_PCRE16
6766 *lengthptr += 2 + IMM2_SIZE;
6767 #elif defined COMPILE_PCRE32
6768 *lengthptr += 4 + IMM2_SIZE;
6769 #endif
6771 else
6773 *code++ = OP_CLOSE;
6774 PUT2INC(code, 0, oc->number);
6777 setverb = *code++ =
6778 (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
6780 /* Do not set firstchar after *ACCEPT */
6781 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6784 /* Handle other cases with/without an argument */
6786 else if (arglen == 0)
6788 if (verbs[i].op < 0) /* Argument is mandatory */
6790 *errorcodeptr = ERR66;
6791 goto FAILED;
6793 setverb = *code++ = verbs[i].op;
6796 else
6798 if (verbs[i].op_arg < 0) /* Argument is forbidden */
6800 *errorcodeptr = ERR59;
6801 goto FAILED;
6803 setverb = *code++ = verbs[i].op_arg;
6804 if (lengthptr != NULL) /* In pass 1 just add in the length */
6805 { /* to avoid potential workspace */
6806 *lengthptr += arglen; /* overflow. */
6807 *code++ = 0;
6809 else
6811 *code++ = arglen;
6812 memcpy(code, arg, IN_UCHARS(arglen));
6813 code += arglen;
6815 *code++ = 0;
6818 switch (setverb)
6820 case OP_THEN:
6821 case OP_THEN_ARG:
6822 cd->external_flags |= PCRE_HASTHEN;
6823 break;
6825 case OP_PRUNE:
6826 case OP_PRUNE_ARG:
6827 case OP_SKIP:
6828 case OP_SKIP_ARG:
6829 cd->had_pruneorskip = TRUE;
6830 break;
6833 break; /* Found verb, exit loop */
6836 vn += verbs[i].len + 1;
6839 if (i < verbcount) continue; /* Successfully handled a verb */
6840 *errorcodeptr = ERR60; /* Verb not recognized */
6841 goto FAILED;
6844 /* Initialize for "real" parentheses */
6846 newoptions = options;
6847 skipbytes = 0;
6848 bravalue = OP_CBRA;
6849 item_hwm_offset = cd->hwm - cd->start_workspace;
6850 reset_bracount = FALSE;
6852 /* Deal with the extended parentheses; all are introduced by '?', and the
6853 appearance of any of them means that this is not a capturing group. */
6855 if (*ptr == CHAR_QUESTION_MARK && (!basicre || nestptr))
6857 int i, set, unset, namelen;
6858 int *optset;
6859 const pcre_uchar *name;
6860 pcre_uchar *slot;
6862 switch (*(++ptr))
6864 /* ------------------------------------------------------------ */
6865 case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */
6866 reset_bracount = TRUE;
6867 cd->dupgroups = TRUE; /* Record (?| encountered */
6868 /* Fall through */
6870 /* ------------------------------------------------------------ */
6871 case CHAR_COLON: /* Non-capturing bracket */
6872 bravalue = OP_BRA;
6873 ptr++;
6874 break;
6877 /* ------------------------------------------------------------ */
6878 case CHAR_LEFT_PARENTHESIS:
6879 bravalue = OP_COND; /* Conditional group */
6880 tempptr = ptr;
6882 /* A condition can be an assertion, a number (referring to a numbered
6883 group's having been set), a name (referring to a named group), or 'R',
6884 referring to recursion. R<digits> and R&name are also permitted for
6885 recursion tests.
6887 There are ways of testing a named group: (?(name)) is used by Python;
6888 Perl 5.10 onwards uses (?(<name>) or (?('name')).
6890 There is one unfortunate ambiguity, caused by history. 'R' can be the
6891 recursive thing or the name 'R' (and similarly for 'R' followed by
6892 digits). We look for a name first; if not found, we try the other case.
6894 For compatibility with auto-callouts, we allow a callout to be
6895 specified before a condition that is an assertion. First, check for the
6896 syntax of a callout; if found, adjust the temporary pointer that is
6897 used to check for an assertion condition. That's all that is needed! */
6899 if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
6901 for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
6902 if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
6903 tempptr += i + 1;
6905 /* tempptr should now be pointing to the opening parenthesis of the
6906 assertion condition. */
6908 if (*tempptr != CHAR_LEFT_PARENTHESIS)
6910 *errorcodeptr = ERR28;
6911 goto FAILED;
6915 /* For conditions that are assertions, check the syntax, and then exit
6916 the switch. This will take control down to where bracketed groups,
6917 including assertions, are processed. */
6919 if (tempptr[1] == CHAR_QUESTION_MARK &&
6920 (tempptr[2] == CHAR_EQUALS_SIGN ||
6921 tempptr[2] == CHAR_EXCLAMATION_MARK ||
6922 (tempptr[2] == CHAR_LESS_THAN_SIGN &&
6923 (tempptr[3] == CHAR_EQUALS_SIGN ||
6924 tempptr[3] == CHAR_EXCLAMATION_MARK))))
6926 cd->iscondassert = TRUE;
6927 break;
6930 /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all
6931 need to skip at least 1+IMM2_SIZE bytes at the start of the group. */
6933 code[1+LINK_SIZE] = OP_CREF;
6934 skipbytes = 1+IMM2_SIZE;
6935 refsign = -1; /* => not a number */
6936 namelen = -1; /* => not a name; must set to avoid warning */
6937 name = NULL; /* Always set to avoid warning */
6938 recno = 0; /* Always set to avoid warning */
6940 /* Check for a test for recursion in a named group. */
6942 ptr++;
6943 if (*ptr == CHAR_R && ptr[1] == CHAR_AMPERSAND)
6945 terminator = -1;
6946 ptr += 2;
6947 code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */
6950 /* Check for a test for a named group's having been set, using the Perl
6951 syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6952 syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */
6954 else if (*ptr == CHAR_LESS_THAN_SIGN)
6956 terminator = CHAR_GREATER_THAN_SIGN;
6957 ptr++;
6959 else if (*ptr == CHAR_APOSTROPHE)
6961 terminator = CHAR_APOSTROPHE;
6962 ptr++;
6964 else
6966 terminator = CHAR_NULL;
6967 if (*ptr == CHAR_MINUS || *ptr == CHAR_PLUS) refsign = *ptr++;
6968 else if (IS_DIGIT(*ptr)) refsign = 0;
6971 /* Handle a number */
6973 if (refsign >= 0)
6975 while (IS_DIGIT(*ptr))
6977 if (recno > INT_MAX / 10 - 1) /* Integer overflow */
6979 while (IS_DIGIT(*ptr)) ptr++;
6980 *errorcodeptr = ERR61;
6981 goto FAILED;
6983 recno = recno * 10 + (int)(*ptr - CHAR_0);
6984 ptr++;
6988 /* Otherwise we expect to read a name; anything else is an error. When
6989 a name is one of a number of duplicates, a different opcode is used and
6990 it needs more memory. Unfortunately we cannot tell whether a name is a
6991 duplicate in the first pass, so we have to allow for more memory. */
6993 else
6995 if (IS_DIGIT(*ptr))
6997 *errorcodeptr = ERR84;
6998 goto FAILED;
7000 if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_word) == 0)
7002 *errorcodeptr = ERR28; /* Assertion expected */
7003 goto FAILED;
7005 name = ptr++;
7006 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
7008 ptr++;
7010 namelen = (int)(ptr - name);
7011 if (lengthptr != NULL) skipbytes += IMM2_SIZE;
7014 /* Check the terminator */
7016 if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
7017 *ptr++ != CHAR_RIGHT_PARENTHESIS)
7019 ptr--; /* Error offset */
7020 *errorcodeptr = ERR26; /* Malformed number or name */
7021 goto FAILED;
7024 /* Do no further checking in the pre-compile phase. */
7026 if (lengthptr != NULL) break;
7028 /* In the real compile we do the work of looking for the actual
7029 reference. If refsign is not negative, it means we have a number in
7030 recno. */
7032 if (refsign >= 0)
7034 if (recno <= 0)
7036 *errorcodeptr = ERR35;
7037 goto FAILED;
7039 if (refsign != 0) recno = (refsign == CHAR_MINUS)?
7040 cd->bracount - recno + 1 : recno + cd->bracount;
7041 if (recno <= 0 || recno > cd->final_bracount)
7043 *errorcodeptr = ERR15;
7044 goto FAILED;
7046 PUT2(code, 2+LINK_SIZE, recno);
7047 if (recno > cd->top_backref) cd->top_backref = recno;
7048 break;
7051 /* Otherwise look for the name. */
7053 slot = cd->name_table;
7054 for (i = 0; i < cd->names_found; i++)
7056 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
7057 slot[IMM2_SIZE+namelen] == 0) break;
7058 slot += cd->name_entry_size;
7061 /* Found the named subpattern. If the name is duplicated, add one to
7062 the opcode to change CREF/RREF into DNCREF/DNRREF and insert
7063 appropriate data values. Otherwise, just insert the unique subpattern
7064 number. */
7066 if (i < cd->names_found)
7068 int offset = i++;
7069 int count = 1;
7070 recno = GET2(slot, 0); /* Number from first found */
7071 if (recno > cd->top_backref) cd->top_backref = recno;
7072 for (; i < cd->names_found; i++)
7074 slot += cd->name_entry_size;
7075 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0 ||
7076 (slot+IMM2_SIZE)[namelen] != 0) break;
7077 count++;
7080 if (count > 1)
7082 PUT2(code, 2+LINK_SIZE, offset);
7083 PUT2(code, 2+LINK_SIZE+IMM2_SIZE, count);
7084 skipbytes += IMM2_SIZE;
7085 code[1+LINK_SIZE]++;
7087 else /* Not a duplicated name */
7089 PUT2(code, 2+LINK_SIZE, recno);
7093 /* If terminator == CHAR_NULL it means that the name followed directly
7094 after the opening parenthesis [e.g. (?(abc)...] and in this case there
7095 are some further alternatives to try. For the cases where terminator !=
7096 CHAR_NULL [things like (?(<name>... or (?('name')... or (?(R&name)... ]
7097 we have now checked all the possibilities, so give an error. */
7099 else if (terminator != CHAR_NULL)
7101 *errorcodeptr = ERR15;
7102 goto FAILED;
7105 /* Check for (?(R) for recursion. Allow digits after R to specify a
7106 specific group number. */
7108 else if (*name == CHAR_R)
7110 recno = 0;
7111 for (i = 1; i < namelen; i++)
7113 if (!IS_DIGIT(name[i]))
7115 *errorcodeptr = ERR15;
7116 goto FAILED;
7118 if (recno > INT_MAX / 10 - 1) /* Integer overflow */
7120 *errorcodeptr = ERR61;
7121 goto FAILED;
7123 recno = recno * 10 + name[i] - CHAR_0;
7125 if (recno == 0) recno = RREF_ANY;
7126 code[1+LINK_SIZE] = OP_RREF; /* Change test type */
7127 PUT2(code, 2+LINK_SIZE, recno);
7130 /* Similarly, check for the (?(DEFINE) "condition", which is always
7131 false. */
7133 else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
7135 code[1+LINK_SIZE] = OP_DEF;
7136 skipbytes = 1;
7139 /* Reference to an unidentified subpattern. */
7141 else
7143 *errorcodeptr = ERR15;
7144 goto FAILED;
7146 break;
7149 /* ------------------------------------------------------------ */
7150 case CHAR_EQUALS_SIGN: /* Positive lookahead */
7151 bravalue = OP_ASSERT;
7152 cd->assert_depth += 1;
7153 ptr++;
7154 break;
7156 /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird
7157 thing to do, but Perl allows all assertions to be quantified, and when
7158 they contain capturing parentheses there may be a potential use for
7159 this feature. Not that that applies to a quantified (?!) but we allow
7160 it for uniformity. */
7162 /* ------------------------------------------------------------ */
7163 case CHAR_EXCLAMATION_MARK: /* Negative lookahead */
7164 ptr++;
7165 if (*ptr == CHAR_RIGHT_PARENTHESIS && ptr[1] != CHAR_ASTERISK &&
7166 ptr[1] != CHAR_PLUS && ptr[1] != CHAR_QUESTION_MARK &&
7167 (ptr[1] != CHAR_LEFT_CURLY_BRACKET || !is_counted_repeat(ptr+2, FALSE)))
7169 *code++ = OP_FAIL;
7170 previous = NULL;
7171 continue;
7173 bravalue = OP_ASSERT_NOT;
7174 cd->assert_depth += 1;
7175 break;
7178 /* ------------------------------------------------------------ */
7179 case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */
7180 switch (ptr[1])
7182 case CHAR_EQUALS_SIGN: /* Positive lookbehind */
7183 bravalue = OP_ASSERTBACK;
7184 cd->assert_depth += 1;
7185 ptr += 2;
7186 break;
7188 case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */
7189 bravalue = OP_ASSERTBACK_NOT;
7190 cd->assert_depth += 1;
7191 ptr += 2;
7192 break;
7194 default: /* Could be name define, else bad */
7195 if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0)
7196 goto DEFINE_NAME;
7197 ptr++; /* Correct offset for error */
7198 *errorcodeptr = ERR24;
7199 goto FAILED;
7201 break;
7204 /* ------------------------------------------------------------ */
7205 case CHAR_GREATER_THAN_SIGN: /* One-time brackets */
7206 bravalue = OP_ONCE;
7207 ptr++;
7208 break;
7211 /* ------------------------------------------------------------ */
7212 case CHAR_C: /* Callout - may be followed by digits; */
7213 previous_callout = code; /* Save for later completion */
7214 after_manual_callout = 1; /* Skip one item before completing */
7215 *code++ = OP_CALLOUT;
7217 int n = 0;
7218 ptr++;
7219 while(IS_DIGIT(*ptr))
7221 n = n * 10 + *ptr++ - CHAR_0;
7222 if (n > 255)
7224 *errorcodeptr = ERR38;
7225 goto FAILED;
7228 if (*ptr != CHAR_RIGHT_PARENTHESIS)
7230 *errorcodeptr = ERR39;
7231 goto FAILED;
7233 *code++ = n;
7234 PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
7235 PUT(code, LINK_SIZE, 0); /* Default length */
7236 code += 2 * LINK_SIZE;
7238 previous = NULL;
7239 continue;
7242 /* ------------------------------------------------------------ */
7243 case CHAR_P: /* Python-style named subpattern handling */
7244 if (*(++ptr) == CHAR_EQUALS_SIGN ||
7245 *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */
7247 is_recurse = *ptr == CHAR_GREATER_THAN_SIGN;
7248 terminator = CHAR_RIGHT_PARENTHESIS;
7249 goto NAMED_REF_OR_RECURSE;
7251 else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */
7253 *errorcodeptr = ERR41;
7254 goto FAILED;
7256 /* Fall through to handle (?P< as (?< is handled */
7259 /* ------------------------------------------------------------ */
7260 DEFINE_NAME: /* Come here from (?< handling */
7261 case CHAR_APOSTROPHE:
7262 terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
7263 CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
7264 name = ++ptr;
7265 if (IS_DIGIT(*ptr))
7267 *errorcodeptr = ERR84; /* Group name must start with non-digit */
7268 goto FAILED;
7270 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
7271 namelen = (int)(ptr - name);
7273 /* In the pre-compile phase, do a syntax check, remember the longest
7274 name, and then remember the group in a vector, expanding it if
7275 necessary. Duplicates for the same number are skipped; other duplicates
7276 are checked for validity. In the actual compile, there is nothing to
7277 do. */
7279 if (lengthptr != NULL)
7281 named_group *ng;
7282 pcre_uint32 number = cd->bracount + 1;
7284 if (*ptr != (pcre_uchar)terminator)
7286 *errorcodeptr = ERR42;
7287 goto FAILED;
7290 if (cd->names_found >= MAX_NAME_COUNT)
7292 *errorcodeptr = ERR49;
7293 goto FAILED;
7296 if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
7298 cd->name_entry_size = namelen + IMM2_SIZE + 1;
7299 if (namelen > MAX_NAME_SIZE)
7301 *errorcodeptr = ERR48;
7302 goto FAILED;
7306 /* Scan the list to check for duplicates. For duplicate names, if the
7307 number is the same, break the loop, which causes the name to be
7308 discarded; otherwise, if DUPNAMES is not set, give an error.
7309 If it is set, allow the name with a different number, but continue
7310 scanning in case this is a duplicate with the same number. For
7311 non-duplicate names, give an error if the number is duplicated. */
7313 ng = cd->named_groups;
7314 for (i = 0; i < cd->names_found; i++, ng++)
7316 if (namelen == ng->length &&
7317 STRNCMP_UC_UC(name, ng->name, namelen) == 0)
7319 if (ng->number == number) break;
7320 if ((options & PCRE_DUPNAMES) == 0)
7322 *errorcodeptr = ERR43;
7323 goto FAILED;
7325 cd->dupnames = TRUE; /* Duplicate names exist */
7327 else if (ng->number == number)
7329 *errorcodeptr = ERR65;
7330 goto FAILED;
7334 if (i >= cd->names_found) /* Not a duplicate with same number */
7336 /* Increase the list size if necessary */
7338 if (cd->names_found >= cd->named_group_list_size)
7340 int newsize = cd->named_group_list_size * 2;
7341 named_group *newspace = (PUBL(malloc))
7342 (newsize * sizeof(named_group));
7344 if (newspace == NULL)
7346 *errorcodeptr = ERR21;
7347 goto FAILED;
7350 memcpy(newspace, cd->named_groups,
7351 cd->named_group_list_size * sizeof(named_group));
7352 if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
7353 (PUBL(free))((void *)cd->named_groups);
7354 cd->named_groups = newspace;
7355 cd->named_group_list_size = newsize;
7358 cd->named_groups[cd->names_found].name = name;
7359 cd->named_groups[cd->names_found].length = namelen;
7360 cd->named_groups[cd->names_found].number = number;
7361 cd->names_found++;
7365 ptr++; /* Move past > or ' in both passes. */
7366 goto NUMBERED_GROUP;
7369 /* ------------------------------------------------------------ */
7370 case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */
7371 terminator = CHAR_RIGHT_PARENTHESIS;
7372 is_recurse = TRUE;
7373 /* Fall through */
7375 /* We come here from the Python syntax above that handles both
7376 references (?P=name) and recursion (?P>name), as well as falling
7377 through from the Perl recursion syntax (?&name). We also come here from
7378 the Perl \k<name> or \k'name' back reference syntax and the \k{name}
7379 .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
7381 NAMED_REF_OR_RECURSE:
7382 name = ++ptr;
7383 if (IS_DIGIT(*ptr))
7385 *errorcodeptr = ERR84; /* Group name must start with non-digit */
7386 goto FAILED;
7388 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
7389 namelen = (int)(ptr - name);
7391 /* In the pre-compile phase, do a syntax check. We used to just set
7392 a dummy reference number, because it was not used in the first pass.
7393 However, with the change of recursive back references to be atomic,
7394 we have to look for the number so that this state can be identified, as
7395 otherwise the incorrect length is computed. If it's not a backwards
7396 reference, the dummy number will do. */
7398 if (lengthptr != NULL)
7400 named_group *ng;
7401 recno = 0;
7403 if (namelen == 0)
7405 *errorcodeptr = ERR62;
7406 goto FAILED;
7408 if (*ptr != (pcre_uchar)terminator)
7410 *errorcodeptr = ERR42;
7411 goto FAILED;
7413 if (namelen > MAX_NAME_SIZE)
7415 *errorcodeptr = ERR48;
7416 goto FAILED;
7419 /* Count named back references. */
7421 if (!is_recurse) cd->namedrefcount++;
7423 /* We have to allow for a named reference to a duplicated name (this
7424 cannot be determined until the second pass). This needs an extra
7425 16-bit data item. */
7427 *lengthptr += IMM2_SIZE;
7429 /* If this is a forward reference and we are within a (?|...) group,
7430 the reference may end up as the number of a group which we are
7431 currently inside, that is, it could be a recursive reference. In the
7432 real compile this will be picked up and the reference wrapped with
7433 OP_ONCE to make it atomic, so we must space in case this occurs. */
7435 /* In fact, this can happen for a non-forward reference because
7436 another group with the same number might be created later. This
7437 issue is fixed "properly" in PCRE2. As PCRE1 is now in maintenance
7438 only mode, we finesse the bug by allowing more memory always. */
7440 *lengthptr += 4 + 4*LINK_SIZE;
7442 /* It is even worse than that. The current reference may be to an
7443 existing named group with a different number (so apparently not
7444 recursive) but which later on is also attached to a group with the
7445 current number. This can only happen if $(| has been previous
7446 encountered. In that case, we allow yet more memory, just in case.
7447 (Again, this is fixed "properly" in PCRE2. */
7449 if (cd->dupgroups) *lengthptr += 4 + 4*LINK_SIZE;
7451 /* Otherwise, check for recursion here. The name table does not exist
7452 in the first pass; instead we must scan the list of names encountered
7453 so far in order to get the number. If the name is not found, leave
7454 the value of recno as 0 for a forward reference. */
7456 /* This patch (removing "else") fixes a problem when a reference is
7457 to multiple identically named nested groups from within the nest.
7458 Once again, it is not the "proper" fix, and it results in an
7459 over-allocation of memory. */
7461 /* else */
7463 ng = cd->named_groups;
7464 for (i = 0; i < cd->names_found; i++, ng++)
7466 if (namelen == ng->length &&
7467 STRNCMP_UC_UC(name, ng->name, namelen) == 0)
7469 open_capitem *oc;
7470 recno = ng->number;
7471 if (is_recurse) break;
7472 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
7474 if (oc->number == recno)
7476 oc->flag = TRUE;
7477 break;
7485 /* In the real compile, search the name table. We check the name
7486 first, and then check that we have reached the end of the name in the
7487 table. That way, if the name is longer than any in the table, the
7488 comparison will fail without reading beyond the table entry. */
7490 else
7492 slot = cd->name_table;
7493 for (i = 0; i < cd->names_found; i++)
7495 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
7496 slot[IMM2_SIZE+namelen] == 0)
7497 break;
7498 slot += cd->name_entry_size;
7501 if (i < cd->names_found)
7503 recno = GET2(slot, 0);
7505 else
7507 *errorcodeptr = ERR15;
7508 goto FAILED;
7512 /* In both phases, for recursions, we can now go to the code than
7513 handles numerical recursion. */
7515 if (is_recurse) goto HANDLE_RECURSION;
7517 /* In the second pass we must see if the name is duplicated. If so, we
7518 generate a different opcode. */
7520 if (lengthptr == NULL && cd->dupnames)
7522 int count = 1;
7523 unsigned int index = i;
7524 pcre_uchar *cslot = slot + cd->name_entry_size;
7526 for (i++; i < cd->names_found; i++)
7528 if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
7529 count++;
7530 cslot += cd->name_entry_size;
7533 if (count > 1)
7535 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
7536 previous = code;
7537 item_hwm_offset = cd->hwm - cd->start_workspace;
7538 *code++ = ((options & PCRE_CASELESS) != 0)? OP_DNREFI : OP_DNREF;
7539 PUT2INC(code, 0, index);
7540 PUT2INC(code, 0, count);
7542 /* Process each potentially referenced group. */
7544 for (; slot < cslot; slot += cd->name_entry_size)
7546 open_capitem *oc;
7547 recno = GET2(slot, 0);
7548 cd->backref_map |= (recno < 32)? (1U << recno) : 1;
7549 if (recno > cd->top_backref) cd->top_backref = recno;
7551 /* Check to see if this back reference is recursive, that it, it
7552 is inside the group that it references. A flag is set so that the
7553 group can be made atomic. */
7555 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
7557 if (oc->number == recno)
7559 oc->flag = TRUE;
7560 break;
7565 continue; /* End of back ref handling */
7569 /* First pass, or a non-duplicated name. */
7571 goto HANDLE_REFERENCE;
7574 /* ------------------------------------------------------------ */
7575 case CHAR_R: /* Recursion, same as (?0) */
7576 recno = 0;
7577 if (*(++ptr) != CHAR_RIGHT_PARENTHESIS)
7579 *errorcodeptr = ERR29;
7580 goto FAILED;
7582 goto HANDLE_RECURSION;
7585 /* ------------------------------------------------------------ */
7586 case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */
7587 case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
7588 case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
7590 const pcre_uchar *called;
7591 terminator = CHAR_RIGHT_PARENTHESIS;
7593 /* Come here from the \g<...> and \g'...' code (Oniguruma
7594 compatibility). However, the syntax has been checked to ensure that
7595 the ... are a (signed) number, so that neither ERR63 nor ERR29 will
7596 be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
7597 ever be taken. */
7599 HANDLE_NUMERICAL_RECURSION:
7601 if ((refsign = *ptr) == CHAR_PLUS)
7603 ptr++;
7604 if (!IS_DIGIT(*ptr))
7606 *errorcodeptr = ERR63;
7607 goto FAILED;
7610 else if (refsign == CHAR_MINUS)
7612 if (!IS_DIGIT(ptr[1]))
7613 goto OTHER_CHAR_AFTER_QUERY;
7614 ptr++;
7617 recno = 0;
7618 while(IS_DIGIT(*ptr))
7620 if (recno > INT_MAX / 10 - 1) /* Integer overflow */
7622 while (IS_DIGIT(*ptr)) ptr++;
7623 *errorcodeptr = ERR61;
7624 goto FAILED;
7626 recno = recno * 10 + *ptr++ - CHAR_0;
7629 if (*ptr != (pcre_uchar)terminator)
7631 *errorcodeptr = ERR29;
7632 goto FAILED;
7635 if (refsign == CHAR_MINUS)
7637 if (recno == 0)
7639 *errorcodeptr = ERR58;
7640 goto FAILED;
7642 recno = cd->bracount - recno + 1;
7643 if (recno <= 0)
7645 *errorcodeptr = ERR15;
7646 goto FAILED;
7649 else if (refsign == CHAR_PLUS)
7651 if (recno == 0)
7653 *errorcodeptr = ERR58;
7654 goto FAILED;
7656 recno += cd->bracount;
7659 /* Come here from code above that handles a named recursion */
7661 HANDLE_RECURSION:
7663 previous = code;
7664 item_hwm_offset = cd->hwm - cd->start_workspace;
7665 called = cd->start_code;
7667 /* When we are actually compiling, find the bracket that is being
7668 referenced. Temporarily end the regex in case it doesn't exist before
7669 this point. If we end up with a forward reference, first check that
7670 the bracket does occur later so we can give the error (and position)
7671 now. Then remember this forward reference in the workspace so it can
7672 be filled in at the end. */
7674 if (lengthptr == NULL)
7676 *code = OP_END;
7677 if (recno != 0)
7678 called = PRIV(find_bracket)(cd->start_code, utf, recno);
7680 /* Forward reference */
7682 if (called == NULL)
7684 if (recno > cd->final_bracount)
7686 *errorcodeptr = ERR15;
7687 goto FAILED;
7690 /* Fudge the value of "called" so that when it is inserted as an
7691 offset below, what it actually inserted is the reference number
7692 of the group. Then remember the forward reference. */
7694 called = cd->start_code + recno;
7695 if (cd->hwm >= cd->start_workspace + cd->workspace_size -
7696 WORK_SIZE_SAFETY_MARGIN)
7698 *errorcodeptr = expand_workspace(cd);
7699 if (*errorcodeptr != 0) goto FAILED;
7701 PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
7704 /* If not a forward reference, and the subpattern is still open,
7705 this is a recursive call. We check to see if this is a left
7706 recursion that could loop for ever, and diagnose that case. We
7707 must not, however, do this check if we are in a conditional
7708 subpattern because the condition might be testing for recursion in
7709 a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
7710 Forever loops are also detected at runtime, so those that occur in
7711 conditional subpatterns will be picked up then. */
7713 else if (GET(called, 1) == 0 && cond_depth <= 0 &&
7714 could_be_empty(called, code, bcptr, utf, cd))
7716 *errorcodeptr = ERR40;
7717 goto FAILED;
7721 /* Insert the recursion/subroutine item. It does not have a set first
7722 character (relevant if it is repeated, because it will then be
7723 wrapped with ONCE brackets). */
7725 *code = OP_RECURSE;
7726 PUT(code, 1, (int)(called - cd->start_code));
7727 code += 1 + LINK_SIZE;
7728 groupsetfirstchar = FALSE;
7731 /* Can't determine a first byte now */
7733 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
7734 zerofirstchar = firstchar;
7735 zerofirstcharflags = firstcharflags;
7736 continue;
7739 /* ------------------------------------------------------------ */
7740 default: /* Other characters: check option setting */
7741 OTHER_CHAR_AFTER_QUERY:
7742 set = unset = 0;
7743 optset = &set;
7745 while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON)
7747 switch (*ptr++)
7749 case CHAR_MINUS: optset = &unset; break;
7751 case CHAR_J: /* Record that it changed in the external options */
7752 *optset |= PCRE_DUPNAMES;
7753 cd->external_flags |= PCRE_JCHANGED;
7754 break;
7756 case CHAR_i: *optset |= PCRE_CASELESS; break;
7757 case CHAR_m: *optset |= PCRE_MULTILINE; break;
7758 case CHAR_s: *optset |= PCRE_DOTALL; break;
7759 case CHAR_x: *optset |= PCRE_EXTENDED; break;
7760 case CHAR_U: *optset |= PCRE_UNGREEDY; break;
7761 case CHAR_X: *optset |= PCRE_EXTRA; break;
7763 default: *errorcodeptr = ERR12;
7764 ptr--; /* Correct the offset */
7765 goto FAILED;
7769 /* Set up the changed option bits, but don't change anything yet. */
7771 newoptions = (options | set) & (~unset);
7773 /* If the options ended with ')' this is not the start of a nested
7774 group with option changes, so the options change at this level.
7775 If we are not at the pattern start, reset the greedy defaults and the
7776 case value for firstchar and reqchar. */
7778 if (*ptr == CHAR_RIGHT_PARENTHESIS)
7780 greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
7781 greedy_non_default = greedy_default ^ 1;
7782 req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
7784 /* Change options at this level, and pass them back for use
7785 in subsequent branches. */
7787 *optionsptr = options = newoptions;
7788 previous = NULL; /* This item can't be repeated */
7789 continue; /* It is complete */
7792 /* If the options ended with ':' we are heading into a nested group
7793 with possible change of options. Such groups are non-capturing and are
7794 not assertions of any kind. All we need to do is skip over the ':';
7795 the newoptions value is handled below. */
7797 bravalue = OP_BRA;
7798 ptr++;
7799 } /* End of switch for character following (? */
7800 } /* End of (? handling */
7802 /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
7803 is set, all unadorned brackets become non-capturing and behave like (?:...)
7804 brackets. */
7806 else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
7808 bravalue = OP_BRA;
7811 /* Else we have a capturing group. */
7813 else
7815 NUMBERED_GROUP:
7816 cd->bracount += 1;
7817 PUT2(code, 1+LINK_SIZE, cd->bracount);
7818 skipbytes = IMM2_SIZE;
7821 /* Process nested bracketed regex. First check for parentheses nested too
7822 deeply. */
7824 if ((cd->parens_depth += 1) > PARENS_NEST_LIMIT)
7826 *errorcodeptr = ERR82;
7827 goto FAILED;
7830 /* All assertions used not to be repeatable, but this was changed for Perl
7831 compatibility. All kinds can now be repeated except for assertions that are
7832 conditions (Perl also forbids these to be repeated). We copy code into a
7833 non-register variable (tempcode) in order to be able to pass its address
7834 because some compilers complain otherwise. At the start of a conditional
7835 group whose condition is an assertion, cd->iscondassert is set. We unset it
7836 here so as to allow assertions later in the group to be quantified. */
7838 if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT &&
7839 cd->iscondassert)
7841 previous = NULL;
7842 cd->iscondassert = FALSE;
7844 else
7846 previous = code;
7847 item_hwm_offset = cd->hwm - cd->start_workspace;
7850 *code = bravalue;
7851 tempcode = code;
7852 tempreqvary = cd->req_varyopt; /* Save value before bracket */
7853 tempbracount = cd->bracount; /* Save value before bracket */
7854 length_prevgroup = 0; /* Initialize for pre-compile phase */
7856 if (embednul && nestptr != NULL)
7857 cd->extended_options &= ~PCRE_ALLOW_EMBEDDED_NUL_BIT;
7858 if (!compile_regex(
7859 newoptions, /* The complete new option state */
7860 &tempcode, /* Where to put code (updated) */
7861 &ptr, /* Input pointer (updated) */
7862 errorcodeptr, /* Where to put an error message */
7863 (bravalue == OP_ASSERTBACK ||
7864 bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
7865 reset_bracount, /* True if (?| group */
7866 skipbytes, /* Skip over bracket number */
7867 cond_depth +
7868 ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */
7869 &subfirstchar, /* For possible first char */
7870 &subfirstcharflags,
7871 &subreqchar, /* For possible last char */
7872 &subreqcharflags,
7873 bcptr, /* Current branch chain */
7874 cd, /* Tables block */
7875 (lengthptr == NULL)? NULL : /* Actual compile phase */
7876 &length_prevgroup /* Pre-compile phase */
7878 goto FAILED;
7879 if (embednul && nestptr != NULL)
7880 cd->extended_options |= PCRE_ALLOW_EMBEDDED_NUL_BIT;
7882 cd->parens_depth -= 1;
7884 /* If this was an atomic group and there are no capturing groups within it,
7885 generate OP_ONCE_NC instead of OP_ONCE. */
7887 if (bravalue == OP_ONCE && cd->bracount <= tempbracount)
7888 *code = OP_ONCE_NC;
7890 if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT)
7891 cd->assert_depth -= 1;
7893 /* At the end of compiling, code is still pointing to the start of the
7894 group, while tempcode has been updated to point past the end of the group.
7895 The pattern pointer (ptr) is on the bracket.
7897 If this is a conditional bracket, check that there are no more than
7898 two branches in the group, or just one if it's a DEFINE group. We do this
7899 in the real compile phase, not in the pre-pass, where the whole group may
7900 not be available. */
7902 if (bravalue == OP_COND && lengthptr == NULL)
7904 pcre_uchar *tc = code;
7905 int condcount = 0;
7907 do {
7908 condcount++;
7909 tc += GET(tc,1);
7911 while (*tc != OP_KET);
7913 /* A DEFINE group is never obeyed inline (the "condition" is always
7914 false). It must have only one branch. */
7916 if (code[LINK_SIZE+1] == OP_DEF)
7918 if (condcount > 1)
7920 *errorcodeptr = ERR54;
7921 goto FAILED;
7923 bravalue = OP_DEF; /* Just a flag to suppress char handling below */
7926 /* A "normal" conditional group. If there is just one branch, we must not
7927 make use of its firstchar or reqchar, because this is equivalent to an
7928 empty second branch. */
7930 else
7932 if (condcount > 2)
7934 *errorcodeptr = ERR27;
7935 goto FAILED;
7937 if (condcount == 1) subfirstcharflags = subreqcharflags = REQ_NONE;
7941 /* Error if hit end of pattern */
7943 if (*ptr != CHAR_RIGHT_PARENTHESIS)
7945 *errorcodeptr = ERR14;
7946 goto FAILED;
7949 /* In the pre-compile phase, update the length by the length of the group,
7950 less the brackets at either end. Then reduce the compiled code to just a
7951 set of non-capturing brackets so that it doesn't use much memory if it is
7952 duplicated by a quantifier.*/
7954 if (lengthptr != NULL)
7956 if (OFLOW_MAX - *lengthptr < length_prevgroup - 2 - 2*LINK_SIZE)
7958 *errorcodeptr = ERR20;
7959 goto FAILED;
7961 *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
7962 code++; /* This already contains bravalue */
7963 PUTINC(code, 0, 1 + LINK_SIZE);
7964 *code++ = OP_KET;
7965 PUTINC(code, 0, 1 + LINK_SIZE);
7966 break; /* No need to waste time with special character handling */
7969 /* Otherwise update the main code pointer to the end of the group. */
7971 code = tempcode;
7973 /* For a DEFINE group, required and first character settings are not
7974 relevant. */
7976 if (bravalue == OP_DEF) break;
7978 /* Handle updating of the required and first characters for other types of
7979 group. Update for normal brackets of all kinds, and conditions with two
7980 branches (see code above). If the bracket is followed by a quantifier with
7981 zero repeat, we have to back off. Hence the definition of zeroreqchar and
7982 zerofirstchar outside the main loop so that they can be accessed for the
7983 back off. */
7985 zeroreqchar = reqchar;
7986 zeroreqcharflags = reqcharflags;
7987 zerofirstchar = firstchar;
7988 zerofirstcharflags = firstcharflags;
7989 groupsetfirstchar = FALSE;
7991 if (bravalue >= OP_ONCE)
7993 /* If we have not yet set a firstchar in this branch, take it from the
7994 subpattern, remembering that it was set here so that a repeat of more
7995 than one can replicate it as reqchar if necessary. If the subpattern has
7996 no firstchar, set "none" for the whole branch. In both cases, a zero
7997 repeat forces firstchar to "none". */
7999 if (firstcharflags == REQ_UNSET)
8001 if (subfirstcharflags >= 0)
8003 firstchar = subfirstchar;
8004 firstcharflags = subfirstcharflags;
8005 groupsetfirstchar = TRUE;
8007 else firstcharflags = REQ_NONE;
8008 zerofirstcharflags = REQ_NONE;
8011 /* If firstchar was previously set, convert the subpattern's firstchar
8012 into reqchar if there wasn't one, using the vary flag that was in
8013 existence beforehand. */
8015 else if (subfirstcharflags >= 0 && subreqcharflags < 0)
8017 subreqchar = subfirstchar;
8018 subreqcharflags = subfirstcharflags | tempreqvary;
8021 /* If the subpattern set a required byte (or set a first byte that isn't
8022 really the first byte - see above), set it. */
8024 if (subreqcharflags >= 0)
8026 reqchar = subreqchar;
8027 reqcharflags = subreqcharflags;
8031 /* For a forward assertion, we take the reqchar, if set, provided that the
8032 group has also set a first char. This can be helpful if the pattern that
8033 follows the assertion doesn't set a different char. For example, it's
8034 useful for /(?=abcde).+/. We can't set firstchar for an assertion, however
8035 because it leads to incorrect effect for patterns such as /(?=a)a.+/ when
8036 the "real" "a" would then become a reqchar instead of a firstchar. This is
8037 overcome by a scan at the end if there's no firstchar, looking for an
8038 asserted first char. */
8040 else if (bravalue == OP_ASSERT && subreqcharflags >= 0 &&
8041 subfirstcharflags >= 0)
8043 reqchar = subreqchar;
8044 reqcharflags = subreqcharflags;
8046 break; /* End of processing '(' */
8049 /* ===================================================================*/
8050 /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
8051 are arranged to be the negation of the corresponding OP_values in the
8052 default case when PCRE_UCP is not set. For the back references, the values
8053 are negative the reference number. Only back references and those types
8054 that consume a character may be repeated. We can test for values between
8055 ESC_b and ESC_Z for the latter; this may have to change if any new ones are
8056 ever created. */
8058 case CHAR_BACKSLASH:
8059 tempptr = ptr;
8060 escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options, FALSE);
8061 if (*errorcodeptr != 0) goto FAILED;
8063 if (escape == 0) /* The escape coded a single character */
8064 c = ec;
8065 else
8067 /* For metasequences that actually match a character, we disable the
8068 setting of a first character if it hasn't already been set. */
8070 if (firstcharflags == REQ_UNSET && escape > ESC_b && escape < ESC_Z)
8071 firstcharflags = REQ_NONE;
8073 /* Set values to reset to if this is followed by a zero repeat. */
8075 zerofirstchar = firstchar;
8076 zerofirstcharflags = firstcharflags;
8077 zeroreqchar = reqchar;
8078 zeroreqcharflags = reqcharflags;
8080 /* \g<name> or \g'name' is a subroutine call by name and \g<n> or \g'n'
8081 is a subroutine call by number (Oniguruma syntax). In fact, the value
8082 ESC_g is returned only for these cases. So we don't need to check for <
8083 or ' if the value is ESC_g. For the Perl syntax \g{n} the value is
8084 -n, and for the Perl syntax \g{name} the result is ESC_k (as
8085 that is a synonym for a named back reference). */
8087 if (escape == ESC_g)
8089 const pcre_uchar *p;
8090 pcre_uint32 cf;
8092 item_hwm_offset = cd->hwm - cd->start_workspace; /* Normally this is set when '(' is read */
8093 terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
8094 CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
8096 /* These two statements stop the compiler for warning about possibly
8097 unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
8098 fact, because we do the check for a number below, the paths that
8099 would actually be in error are never taken. */
8101 skipbytes = 0;
8102 reset_bracount = FALSE;
8104 /* If it's not a signed or unsigned number, treat it as a name. */
8106 cf = ptr[1];
8107 if (cf != CHAR_PLUS && cf != CHAR_MINUS && !IS_DIGIT(cf))
8109 is_recurse = TRUE;
8110 goto NAMED_REF_OR_RECURSE;
8113 /* Signed or unsigned number (cf = ptr[1]) is known to be plus or minus
8114 or a digit. */
8116 p = ptr + 2;
8117 while (IS_DIGIT(*p)) p++;
8118 if (*p != (pcre_uchar)terminator)
8120 *errorcodeptr = ERR57;
8121 goto FAILED;
8123 ptr++;
8124 goto HANDLE_NUMERICAL_RECURSION;
8127 /* \k<name> or \k'name' is a back reference by name (Perl syntax).
8128 We also support \k{name} (.NET syntax). */
8130 if (escape == ESC_k)
8132 if ((ptr[1] != CHAR_LESS_THAN_SIGN &&
8133 ptr[1] != CHAR_APOSTROPHE && ptr[1] != CHAR_LEFT_CURLY_BRACKET))
8135 *errorcodeptr = ERR69;
8136 goto FAILED;
8138 is_recurse = FALSE;
8139 terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
8140 CHAR_GREATER_THAN_SIGN : (*ptr == CHAR_APOSTROPHE)?
8141 CHAR_APOSTROPHE : CHAR_RIGHT_CURLY_BRACKET;
8142 goto NAMED_REF_OR_RECURSE;
8145 /* Back references are handled specially; must disable firstchar if
8146 not set to cope with cases like (?=(\w+))\1: which would otherwise set
8147 ':' later. */
8149 if (escape < 0)
8151 open_capitem *oc;
8152 recno = -escape;
8154 /* Come here from named backref handling when the reference is to a
8155 single group (i.e. not to a duplicated name. */
8157 HANDLE_REFERENCE:
8158 if (firstcharflags == REQ_UNSET) zerofirstcharflags = firstcharflags = REQ_NONE;
8159 previous = code;
8160 item_hwm_offset = cd->hwm - cd->start_workspace;
8161 *code++ = ((options & PCRE_CASELESS) != 0)? OP_REFI : OP_REF;
8162 PUT2INC(code, 0, recno);
8163 cd->backref_map |= (recno < 32)? (1U << recno) : 1;
8164 if (recno > cd->top_backref) cd->top_backref = recno;
8166 /* Check to see if this back reference is recursive, that it, it
8167 is inside the group that it references. A flag is set so that the
8168 group can be made atomic. */
8170 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
8172 if (oc->number == recno)
8174 oc->flag = TRUE;
8175 break;
8180 /* So are Unicode property matches, if supported. */
8182 #ifdef SUPPORT_UCP
8183 else if (escape == ESC_P || escape == ESC_p)
8185 BOOL negated;
8186 unsigned int ptype = 0, pdata = 0;
8187 if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
8188 goto FAILED;
8189 previous = code;
8190 item_hwm_offset = cd->hwm - cd->start_workspace;
8191 *code++ = ((escape == ESC_p) != negated)? OP_PROP : OP_NOTPROP;
8192 *code++ = ptype;
8193 *code++ = pdata;
8195 #else
8197 /* If Unicode properties are not supported, \X, \P, and \p are not
8198 allowed. */
8200 else if (escape == ESC_X || escape == ESC_P || escape == ESC_p)
8202 *errorcodeptr = ERR45;
8203 goto FAILED;
8205 #endif
8207 /* For the rest (including \X when Unicode properties are supported), we
8208 can obtain the OP value by negating the escape value in the default
8209 situation when PCRE_UCP is not set. When it *is* set, we substitute
8210 Unicode property tests. Note that \b and \B do a one-character
8211 lookbehind, and \A also behaves as if it does. */
8213 else
8215 if ((escape == ESC_b || escape == ESC_B || escape == ESC_A) &&
8216 cd->max_lookbehind == 0)
8217 cd->max_lookbehind = 1;
8218 #ifdef SUPPORT_UCP
8219 if (escape >= ESC_DU && escape <= ESC_wu)
8221 nestptr = ptr + 1; /* Where to resume */
8222 ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
8224 else
8225 #endif
8226 /* In non-UTF-8 mode, we turn \C into OP_ALLANY instead of OP_ANYBYTE
8227 so that it works in DFA mode and in lookbehinds. */
8230 previous = (escape > ESC_b && escape < ESC_Z)? code : NULL;
8231 item_hwm_offset = cd->hwm - cd->start_workspace;
8232 *code++ = (!utf && escape == ESC_C)? OP_ALLANY : escape;
8235 continue;
8238 /* We have a data character whose value is in c. In UTF-8 mode it may have
8239 a value > 127. We set its representation in the length/buffer, and then
8240 handle it as a data character. */
8242 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
8243 if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
8244 mclength = PRIV(ord2utf)(c, mcbuffer);
8245 else
8246 #endif
8249 mcbuffer[0] = c;
8250 mclength = 1;
8252 goto ONE_CHAR;
8255 /* ===================================================================*/
8256 /* Handle a literal character. It is guaranteed not to be whitespace or #
8257 when the extended flag is set. If we are in a UTF mode, it may be a
8258 multi-unit literal character. */
8260 default:
8261 NORMAL_CHAR:
8262 mclength = 1;
8263 mcbuffer[0] = c;
8265 #ifdef SUPPORT_UTF
8266 if (utf && HAS_EXTRALEN(c))
8267 ACROSSCHAR(TRUE, ptr[1], mcbuffer[mclength++] = *(++ptr));
8268 #endif
8270 /* At this point we have the character's bytes in mcbuffer, and the length
8271 in mclength. When not in UTF-8 mode, the length is always 1. */
8273 ONE_CHAR:
8274 previous = code;
8275 item_hwm_offset = cd->hwm - cd->start_workspace;
8277 /* For caseless UTF-8 mode when UCP support is available, check whether
8278 this character has more than one other case. If so, generate a special
8279 OP_PROP item instead of OP_CHARI. */
8281 #ifdef SUPPORT_UCP
8282 if (utf && (options & PCRE_CASELESS) != 0)
8284 GETCHAR(c, mcbuffer);
8285 if ((c = UCD_CASESET(c)) != 0)
8287 *code++ = OP_PROP;
8288 *code++ = PT_CLIST;
8289 *code++ = c;
8290 if (firstcharflags == REQ_UNSET)
8291 firstcharflags = zerofirstcharflags = REQ_NONE;
8292 break;
8295 #endif
8297 /* Caseful matches, or not one of the multicase characters. */
8299 *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARI : OP_CHAR;
8300 for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
8302 /* Remember if \r or \n were seen */
8304 if (mcbuffer[0] == CHAR_CR || mcbuffer[0] == CHAR_NL)
8305 cd->external_flags |= PCRE_HASCRORLF;
8307 /* Set the first and required bytes appropriately. If no previous first
8308 byte, set it from this character, but revert to none on a zero repeat.
8309 Otherwise, leave the firstchar value alone, and don't change it on a zero
8310 repeat. */
8312 if (firstcharflags == REQ_UNSET)
8314 zerofirstcharflags = REQ_NONE;
8315 zeroreqchar = reqchar;
8316 zeroreqcharflags = reqcharflags;
8318 /* If the character is more than one byte long, we can set firstchar
8319 only if it is not to be matched caselessly. */
8321 if (mclength == 1 || req_caseopt == 0)
8323 firstchar = mcbuffer[0];
8324 firstcharflags = req_caseopt;
8326 if (mclength != 1)
8328 reqchar = code[-1];
8329 reqcharflags = cd->req_varyopt;
8332 else firstcharflags = reqcharflags = REQ_NONE;
8335 /* firstchar was previously set; we can set reqchar only if the length is
8336 1 or the matching is caseful. */
8338 else
8340 zerofirstchar = firstchar;
8341 zerofirstcharflags = firstcharflags;
8342 zeroreqchar = reqchar;
8343 zeroreqcharflags = reqcharflags;
8344 if (mclength == 1 || req_caseopt == 0)
8346 reqchar = code[-1];
8347 reqcharflags = req_caseopt | cd->req_varyopt;
8351 break; /* End of literal character handling */
8353 } /* end of big loop */
8356 /* Control never reaches here by falling through, only by a goto for all the
8357 error states. Pass back the position in the pattern so that it can be displayed
8358 to the user for diagnosing the error. */
8360 FAILED:
8361 *ptrptr = ptr;
8362 return FALSE;
8367 /*************************************************
8368 * Compile sequence of alternatives *
8369 *************************************************/
8371 /* On entry, ptr is pointing past the bracket character, but on return it
8372 points to the closing bracket, or vertical bar, or end of string. The code
8373 variable is pointing at the byte into which the BRA operator has been stored.
8374 This function is used during the pre-compile phase when we are trying to find
8375 out the amount of memory needed, as well as during the real compile phase. The
8376 value of lengthptr distinguishes the two phases.
8378 Arguments:
8379 options option bits, including any changes for this subpattern
8380 codeptr -> the address of the current code pointer
8381 ptrptr -> the address of the current pattern pointer
8382 errorcodeptr -> pointer to error code variable
8383 lookbehind TRUE if this is a lookbehind assertion
8384 reset_bracount TRUE to reset the count for each branch
8385 skipbytes skip this many bytes at start (for brackets and OP_COND)
8386 cond_depth depth of nesting for conditional subpatterns
8387 firstcharptr place to put the first required character
8388 firstcharflagsptr place to put the first character flags, or a negative number
8389 reqcharptr place to put the last required character
8390 reqcharflagsptr place to put the last required character flags, or a negative number
8391 bcptr pointer to the chain of currently open branches
8392 cd points to the data block with tables pointers etc.
8393 lengthptr NULL during the real compile phase
8394 points to length accumulator during pre-compile phase
8396 Returns: TRUE on success
8399 static BOOL
8400 compile_regex(int options, pcre_uchar **codeptr, const pcre_uchar **ptrptr,
8401 int *errorcodeptr, BOOL lookbehind, BOOL reset_bracount, int skipbytes,
8402 int cond_depth,
8403 pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
8404 pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
8405 branch_chain *bcptr, compile_data *cd, int *lengthptr)
8407 const pcre_uchar *ptr = *ptrptr;
8408 pcre_uchar *code = *codeptr;
8409 pcre_uchar *last_branch = code;
8410 pcre_uchar *start_bracket = code;
8411 pcre_uchar *reverse_count = NULL;
8412 open_capitem capitem;
8413 int capnumber = 0;
8414 pcre_uint32 firstchar, reqchar;
8415 pcre_int32 firstcharflags, reqcharflags;
8416 pcre_uint32 branchfirstchar, branchreqchar;
8417 pcre_int32 branchfirstcharflags, branchreqcharflags;
8418 int length;
8419 unsigned int orig_bracount;
8420 unsigned int max_bracount;
8421 branch_chain bc;
8422 size_t save_hwm_offset;
8424 /* If set, call the external function that checks for stack availability. */
8426 if (PUBL(stack_guard) != NULL && PUBL(stack_guard)())
8428 *errorcodeptr= ERR85;
8429 return FALSE;
8432 /* Miscellaneous initialization */
8434 bc.outer = bcptr;
8435 bc.current_branch = code;
8437 firstchar = reqchar = 0;
8438 firstcharflags = reqcharflags = REQ_UNSET;
8440 save_hwm_offset = cd->hwm - cd->start_workspace;
8442 /* Accumulate the length for use in the pre-compile phase. Start with the
8443 length of the BRA and KET and any extra bytes that are required at the
8444 beginning. We accumulate in a local variable to save frequent testing of
8445 lenthptr for NULL. We cannot do this by looking at the value of code at the
8446 start and end of each alternative, because compiled items are discarded during
8447 the pre-compile phase so that the work space is not exceeded. */
8449 length = 2 + 2*LINK_SIZE + skipbytes;
8451 /* WARNING: If the above line is changed for any reason, you must also change
8452 the code that abstracts option settings at the start of the pattern and makes
8453 them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
8454 pre-compile phase to find out whether anything has yet been compiled or not. */
8456 /* If this is a capturing subpattern, add to the chain of open capturing items
8457 so that we can detect them if (*ACCEPT) is encountered. This is also used to
8458 detect groups that contain recursive back references to themselves. Note that
8459 only OP_CBRA need be tested here; changing this opcode to one of its variants,
8460 e.g. OP_SCBRAPOS, happens later, after the group has been compiled. */
8462 if (*code == OP_CBRA)
8464 capnumber = GET2(code, 1 + LINK_SIZE);
8465 capitem.number = capnumber;
8466 capitem.next = cd->open_caps;
8467 capitem.flag = FALSE;
8468 cd->open_caps = &capitem;
8471 /* Offset is set zero to mark that this bracket is still open */
8473 PUT(code, 1, 0);
8474 code += 1 + LINK_SIZE + skipbytes;
8476 /* Loop for each alternative branch */
8478 orig_bracount = max_bracount = cd->bracount;
8479 for (;;)
8481 /* For a (?| group, reset the capturing bracket count so that each branch
8482 uses the same numbers. */
8484 if (reset_bracount) cd->bracount = orig_bracount;
8486 /* Set up dummy OP_REVERSE if lookbehind assertion */
8488 if (lookbehind)
8490 *code++ = OP_REVERSE;
8491 reverse_count = code;
8492 PUTINC(code, 0, 0);
8493 length += 1 + LINK_SIZE;
8496 /* Now compile the branch; in the pre-compile phase its length gets added
8497 into the length. */
8499 if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstchar,
8500 &branchfirstcharflags, &branchreqchar, &branchreqcharflags, &bc,
8501 cond_depth, cd, (lengthptr == NULL)? NULL : &length))
8503 *ptrptr = ptr;
8504 return FALSE;
8507 /* Keep the highest bracket count in case (?| was used and some branch
8508 has fewer than the rest. */
8510 if (cd->bracount > max_bracount) max_bracount = cd->bracount;
8512 /* In the real compile phase, there is some post-processing to be done. */
8514 if (lengthptr == NULL)
8516 /* If this is the first branch, the firstchar and reqchar values for the
8517 branch become the values for the regex. */
8519 if (*last_branch != OP_ALT)
8521 firstchar = branchfirstchar;
8522 firstcharflags = branchfirstcharflags;
8523 reqchar = branchreqchar;
8524 reqcharflags = branchreqcharflags;
8527 /* If this is not the first branch, the first char and reqchar have to
8528 match the values from all the previous branches, except that if the
8529 previous value for reqchar didn't have REQ_VARY set, it can still match,
8530 and we set REQ_VARY for the regex. */
8532 else
8534 /* If we previously had a firstchar, but it doesn't match the new branch,
8535 we have to abandon the firstchar for the regex, but if there was
8536 previously no reqchar, it takes on the value of the old firstchar. */
8538 if (firstcharflags >= 0 &&
8539 (firstcharflags != branchfirstcharflags || firstchar != branchfirstchar))
8541 if (reqcharflags < 0)
8543 reqchar = firstchar;
8544 reqcharflags = firstcharflags;
8546 firstcharflags = REQ_NONE;
8549 /* If we (now or from before) have no firstchar, a firstchar from the
8550 branch becomes a reqchar if there isn't a branch reqchar. */
8552 if (firstcharflags < 0 && branchfirstcharflags >= 0 && branchreqcharflags < 0)
8554 branchreqchar = branchfirstchar;
8555 branchreqcharflags = branchfirstcharflags;
8558 /* Now ensure that the reqchars match */
8560 if (((reqcharflags & ~REQ_VARY) != (branchreqcharflags & ~REQ_VARY)) ||
8561 reqchar != branchreqchar)
8562 reqcharflags = REQ_NONE;
8563 else
8565 reqchar = branchreqchar;
8566 reqcharflags |= branchreqcharflags; /* To "or" REQ_VARY */
8570 /* If lookbehind, check that this branch matches a fixed-length string, and
8571 put the length into the OP_REVERSE item. Temporarily mark the end of the
8572 branch with OP_END. If the branch contains OP_RECURSE, the result is -3
8573 because there may be forward references that we can't check here. Set a
8574 flag to cause another lookbehind check at the end. Why not do it all at the
8575 end? Because common, erroneous checks are picked up here and the offset of
8576 the problem can be shown. */
8578 if (lookbehind)
8580 int fixed_length;
8581 *code = OP_END;
8582 fixed_length = find_fixedlength(last_branch, (options & PCRE_UTF8) != 0,
8583 FALSE, cd, NULL);
8584 DPRINTF(("fixed length = %d\n", fixed_length));
8585 if (fixed_length == -3)
8587 cd->check_lookbehind = TRUE;
8589 else if (fixed_length < 0)
8591 *errorcodeptr = (fixed_length == -2)? ERR36 :
8592 (fixed_length == -4)? ERR70: ERR25;
8593 *ptrptr = ptr;
8594 return FALSE;
8596 else
8598 if (fixed_length > cd->max_lookbehind)
8599 cd->max_lookbehind = fixed_length;
8600 PUT(reverse_count, 0, fixed_length);
8605 /* Reached end of expression, either ')' or end of pattern. In the real
8606 compile phase, go back through the alternative branches and reverse the chain
8607 of offsets, with the field in the BRA item now becoming an offset to the
8608 first alternative. If there are no alternatives, it points to the end of the
8609 group. The length in the terminating ket is always the length of the whole
8610 bracketed item. Return leaving the pointer at the terminating char. */
8612 if (*ptr != CHAR_VERTICAL_LINE)
8614 if (lengthptr == NULL)
8616 int branch_length = (int)(code - last_branch);
8619 int prev_length = GET(last_branch, 1);
8620 PUT(last_branch, 1, branch_length);
8621 branch_length = prev_length;
8622 last_branch -= branch_length;
8624 while (branch_length > 0);
8627 /* Fill in the ket */
8629 *code = OP_KET;
8630 PUT(code, 1, (int)(code - start_bracket));
8631 code += 1 + LINK_SIZE;
8633 /* If it was a capturing subpattern, check to see if it contained any
8634 recursive back references. If so, we must wrap it in atomic brackets.
8635 Because we are moving code along, we must ensure that any pending recursive
8636 references are updated. In any event, remove the block from the chain. */
8638 if (capnumber > 0)
8640 if (cd->open_caps->flag)
8642 *code = OP_END;
8643 adjust_recurse(start_bracket, 1 + LINK_SIZE,
8644 (options & PCRE_UTF8) != 0, cd, save_hwm_offset);
8645 memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
8646 IN_UCHARS(code - start_bracket));
8647 *start_bracket = OP_ONCE;
8648 code += 1 + LINK_SIZE;
8649 PUT(start_bracket, 1, (int)(code - start_bracket));
8650 *code = OP_KET;
8651 PUT(code, 1, (int)(code - start_bracket));
8652 code += 1 + LINK_SIZE;
8653 length += 2 + 2*LINK_SIZE;
8655 cd->open_caps = cd->open_caps->next;
8658 /* Retain the highest bracket number, in case resetting was used. */
8660 cd->bracount = max_bracount;
8662 /* Set values to pass back */
8664 *codeptr = code;
8665 *ptrptr = ptr;
8666 *firstcharptr = firstchar;
8667 *firstcharflagsptr = firstcharflags;
8668 *reqcharptr = reqchar;
8669 *reqcharflagsptr = reqcharflags;
8670 if (lengthptr != NULL)
8672 if (OFLOW_MAX - *lengthptr < length)
8674 *errorcodeptr = ERR20;
8675 return FALSE;
8677 *lengthptr += length;
8679 return TRUE;
8682 /* Another branch follows. In the pre-compile phase, we can move the code
8683 pointer back to where it was for the start of the first branch. (That is,
8684 pretend that each branch is the only one.)
8686 In the real compile phase, insert an ALT node. Its length field points back
8687 to the previous branch while the bracket remains open. At the end the chain
8688 is reversed. It's done like this so that the start of the bracket has a
8689 zero offset until it is closed, making it possible to detect recursion. */
8691 if (lengthptr != NULL)
8693 code = *codeptr + 1 + LINK_SIZE + skipbytes;
8694 length += 1 + LINK_SIZE;
8696 else
8698 *code = OP_ALT;
8699 PUT(code, 1, (int)(code - last_branch));
8700 bc.current_branch = last_branch = code;
8701 code += 1 + LINK_SIZE;
8704 ptr++;
8706 /* Control never reaches here */
8712 /*************************************************
8713 * Check for anchored expression *
8714 *************************************************/
8716 /* Try to find out if this is an anchored regular expression. Consider each
8717 alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
8718 all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
8719 it's anchored. However, if this is a multiline pattern, then only OP_SOD will
8720 be found, because ^ generates OP_CIRCM in that mode.
8722 We can also consider a regex to be anchored if OP_SOM starts all its branches.
8723 This is the code for \G, which means "match at start of match position, taking
8724 into account the match offset".
8726 A branch is also implicitly anchored if it starts with .* and DOTALL is set,
8727 because that will try the rest of the pattern at all possible matching points,
8728 so there is no point trying again.... er ....
8730 .... except when the .* appears inside capturing parentheses, and there is a
8731 subsequent back reference to those parentheses. We haven't enough information
8732 to catch that case precisely.
8734 At first, the best we could do was to detect when .* was in capturing brackets
8735 and the highest back reference was greater than or equal to that level.
8736 However, by keeping a bitmap of the first 31 back references, we can catch some
8737 of the more common cases more precisely.
8739 ... A second exception is when the .* appears inside an atomic group, because
8740 this prevents the number of characters it matches from being adjusted.
8742 Arguments:
8743 code points to start of expression (the bracket)
8744 bracket_map a bitmap of which brackets we are inside while testing; this
8745 handles up to substring 31; after that we just have to take
8746 the less precise approach
8747 cd points to the compile data block
8748 atomcount atomic group level
8750 Returns: TRUE or FALSE
8753 static BOOL
8754 is_anchored(register const pcre_uchar *code, unsigned int bracket_map,
8755 compile_data *cd, int atomcount)
8757 do {
8758 const pcre_uchar *scode = first_significant_code(
8759 code + PRIV(OP_lengths)[*code], FALSE);
8760 register int op = *scode;
8762 /* Non-capturing brackets */
8764 if (op == OP_BRA || op == OP_BRAPOS ||
8765 op == OP_SBRA || op == OP_SBRAPOS)
8767 if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
8770 /* Capturing brackets */
8772 else if (op == OP_CBRA || op == OP_CBRAPOS ||
8773 op == OP_SCBRA || op == OP_SCBRAPOS)
8775 int n = GET2(scode, 1+LINK_SIZE);
8776 int new_map = bracket_map | ((n < 32)? (1U << n) : 1);
8777 if (!is_anchored(scode, new_map, cd, atomcount)) return FALSE;
8780 /* Positive forward assertion */
8782 else if (op == OP_ASSERT)
8784 if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
8787 /* Condition; not anchored if no second branch */
8789 else if (op == OP_COND)
8791 if (scode[GET(scode,1)] != OP_ALT) return FALSE;
8792 if (!is_anchored(scode, bracket_map, cd, atomcount)) return FALSE;
8795 /* Atomic groups */
8797 else if (op == OP_ONCE || op == OP_ONCE_NC)
8799 if (!is_anchored(scode, bracket_map, cd, atomcount + 1))
8800 return FALSE;
8803 /* .* is not anchored unless DOTALL is set (which generates OP_ALLANY) and
8804 it isn't in brackets that are or may be referenced or inside an atomic
8805 group. */
8807 else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
8808 op == OP_TYPEPOSSTAR))
8810 if (scode[1] != OP_ALLANY || (bracket_map & cd->backref_map) != 0 ||
8811 atomcount > 0 || cd->had_pruneorskip)
8812 return FALSE;
8815 /* Check for explicit anchoring */
8817 else if (op != OP_SOD && op != OP_SOM && op != OP_CIRC) return FALSE;
8819 code += GET(code, 1);
8821 while (*code == OP_ALT); /* Loop for each alternative */
8822 return TRUE;
8827 /*************************************************
8828 * Check for starting with ^ or .* *
8829 *************************************************/
8831 /* This is called to find out if every branch starts with ^ or .* so that
8832 "first char" processing can be done to speed things up in multiline
8833 matching and for non-DOTALL patterns that start with .* (which must start at
8834 the beginning or after \n). As in the case of is_anchored() (see above), we
8835 have to take account of back references to capturing brackets that contain .*
8836 because in that case we can't make the assumption. Also, the appearance of .*
8837 inside atomic brackets or in an assertion, or in a pattern that contains *PRUNE
8838 or *SKIP does not count, because once again the assumption no longer holds.
8840 Arguments:
8841 code points to start of expression (the bracket)
8842 bracket_map a bitmap of which brackets we are inside while testing; this
8843 handles up to substring 31; after that we just have to take
8844 the less precise approach
8845 cd points to the compile data
8846 atomcount atomic group level
8847 inassert TRUE if in an assertion
8849 Returns: TRUE or FALSE
8852 static BOOL
8853 is_startline(const pcre_uchar *code, unsigned int bracket_map,
8854 compile_data *cd, int atomcount, BOOL inassert)
8856 do {
8857 const pcre_uchar *scode = first_significant_code(
8858 code + PRIV(OP_lengths)[*code], FALSE);
8859 register int op = *scode;
8861 /* If we are at the start of a conditional assertion group, *both* the
8862 conditional assertion *and* what follows the condition must satisfy the test
8863 for start of line. Other kinds of condition fail. Note that there may be an
8864 auto-callout at the start of a condition. */
8866 if (op == OP_COND)
8868 scode += 1 + LINK_SIZE;
8869 if (*scode == OP_CALLOUT) scode += PRIV(OP_lengths)[OP_CALLOUT];
8870 switch (*scode)
8872 case OP_CREF:
8873 case OP_DNCREF:
8874 case OP_RREF:
8875 case OP_DNRREF:
8876 case OP_DEF:
8877 case OP_FAIL:
8878 return FALSE;
8880 default: /* Assertion */
8881 if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE;
8882 do scode += GET(scode, 1); while (*scode == OP_ALT);
8883 scode += 1 + LINK_SIZE;
8884 break;
8886 scode = first_significant_code(scode, FALSE);
8887 op = *scode;
8890 /* Non-capturing brackets */
8892 if (op == OP_BRA || op == OP_BRAPOS ||
8893 op == OP_SBRA || op == OP_SBRAPOS)
8895 if (!is_startline(scode, bracket_map, cd, atomcount, inassert)) return FALSE;
8898 /* Capturing brackets */
8900 else if (op == OP_CBRA || op == OP_CBRAPOS ||
8901 op == OP_SCBRA || op == OP_SCBRAPOS)
8903 int n = GET2(scode, 1+LINK_SIZE);
8904 int new_map = bracket_map | ((n < 32)? (1U << n) : 1);
8905 if (!is_startline(scode, new_map, cd, atomcount, inassert)) return FALSE;
8908 /* Positive forward assertions */
8910 else if (op == OP_ASSERT)
8912 if (!is_startline(scode, bracket_map, cd, atomcount, TRUE)) return FALSE;
8915 /* Atomic brackets */
8917 else if (op == OP_ONCE || op == OP_ONCE_NC)
8919 if (!is_startline(scode, bracket_map, cd, atomcount + 1, inassert)) return FALSE;
8922 /* .* means "start at start or after \n" if it isn't in atomic brackets or
8923 brackets that may be referenced or an assertion, as long as the pattern does
8924 not contain *PRUNE or *SKIP, because these break the feature. Consider, for
8925 example, /.*?a(*PRUNE)b/ with the subject "aab", which matches "ab", i.e.
8926 not at the start of a line. */
8928 else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
8930 if (scode[1] != OP_ANY || (bracket_map & cd->backref_map) != 0 ||
8931 atomcount > 0 || cd->had_pruneorskip || inassert)
8932 return FALSE;
8935 /* Check for explicit circumflex; anything else gives a FALSE result. Note
8936 in particular that this includes atomic brackets OP_ONCE and OP_ONCE_NC
8937 because the number of characters matched by .* cannot be adjusted inside
8938 them. */
8940 else if (op != OP_CIRC && op != OP_CIRCM) return FALSE;
8942 /* Move on to the next alternative */
8944 code += GET(code, 1);
8946 while (*code == OP_ALT); /* Loop for each alternative */
8947 return TRUE;
8952 /*************************************************
8953 * Check for asserted fixed first char *
8954 *************************************************/
8956 /* During compilation, the "first char" settings from forward assertions are
8957 discarded, because they can cause conflicts with actual literals that follow.
8958 However, if we end up without a first char setting for an unanchored pattern,
8959 it is worth scanning the regex to see if there is an initial asserted first
8960 char. If all branches start with the same asserted char, or with a
8961 non-conditional bracket all of whose alternatives start with the same asserted
8962 char (recurse ad lib), then we return that char, with the flags set to zero or
8963 REQ_CASELESS; otherwise return zero with REQ_NONE in the flags.
8965 Arguments:
8966 code points to start of expression (the bracket)
8967 flags points to the first char flags, or to REQ_NONE
8968 inassert TRUE if in an assertion
8970 Returns: the fixed first char, or 0 with REQ_NONE in flags
8973 static pcre_uint32
8974 find_firstassertedchar(const pcre_uchar *code, pcre_int32 *flags,
8975 BOOL inassert)
8977 register pcre_uint32 c = 0;
8978 int cflags = REQ_NONE;
8980 *flags = REQ_NONE;
8981 do {
8982 pcre_uint32 d;
8983 int dflags;
8984 int xl = (*code == OP_CBRA || *code == OP_SCBRA ||
8985 *code == OP_CBRAPOS || *code == OP_SCBRAPOS)? IMM2_SIZE:0;
8986 const pcre_uchar *scode = first_significant_code(code + 1+LINK_SIZE + xl,
8987 TRUE);
8988 register pcre_uchar op = *scode;
8990 switch(op)
8992 default:
8993 return 0;
8995 case OP_BRA:
8996 case OP_BRAPOS:
8997 case OP_CBRA:
8998 case OP_SCBRA:
8999 case OP_CBRAPOS:
9000 case OP_SCBRAPOS:
9001 case OP_ASSERT:
9002 case OP_ONCE:
9003 case OP_ONCE_NC:
9004 d = find_firstassertedchar(scode, &dflags, op == OP_ASSERT);
9005 if (dflags < 0)
9006 return 0;
9007 if (cflags < 0) { c = d; cflags = dflags; } else if (c != d || cflags != dflags) return 0;
9008 break;
9010 case OP_EXACT:
9011 scode += IMM2_SIZE;
9012 /* Fall through */
9014 case OP_CHAR:
9015 case OP_PLUS:
9016 case OP_MINPLUS:
9017 case OP_POSPLUS:
9018 if (!inassert) return 0;
9019 if (cflags < 0) { c = scode[1]; cflags = 0; }
9020 else if (c != scode[1]) return 0;
9021 break;
9023 case OP_EXACTI:
9024 scode += IMM2_SIZE;
9025 /* Fall through */
9027 case OP_CHARI:
9028 case OP_PLUSI:
9029 case OP_MINPLUSI:
9030 case OP_POSPLUSI:
9031 if (!inassert) return 0;
9032 if (cflags < 0) { c = scode[1]; cflags = REQ_CASELESS; }
9033 else if (c != scode[1]) return 0;
9034 break;
9037 code += GET(code, 1);
9039 while (*code == OP_ALT);
9041 *flags = cflags;
9042 return c;
9047 /*************************************************
9048 * Add an entry to the name/number table *
9049 *************************************************/
9051 /* This function is called between compiling passes to add an entry to the
9052 name/number table, maintaining alphabetical order. Checking for permitted
9053 and forbidden duplicates has already been done.
9055 Arguments:
9056 cd the compile data block
9057 name the name to add
9058 length the length of the name
9059 groupno the group number
9061 Returns: nothing
9064 static void
9065 add_name(compile_data *cd, const pcre_uchar *name, int length,
9066 unsigned int groupno)
9068 int i;
9069 pcre_uchar *slot = cd->name_table;
9071 for (i = 0; i < cd->names_found; i++)
9073 int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(length));
9074 if (crc == 0 && slot[IMM2_SIZE+length] != 0)
9075 crc = -1; /* Current name is a substring */
9077 /* Make space in the table and break the loop for an earlier name. For a
9078 duplicate or later name, carry on. We do this for duplicates so that in the
9079 simple case (when ?(| is not used) they are in order of their numbers. In all
9080 cases they are in the order in which they appear in the pattern. */
9082 if (crc < 0)
9084 memmove(slot + cd->name_entry_size, slot,
9085 IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
9086 break;
9089 /* Continue the loop for a later or duplicate name */
9091 slot += cd->name_entry_size;
9094 PUT2(slot, 0, groupno);
9095 memcpy(slot + IMM2_SIZE, name, IN_UCHARS(length));
9096 slot[IMM2_SIZE + length] = 0;
9097 cd->names_found++;
9102 /*************************************************
9103 * Compile a Regular Expression *
9104 *************************************************/
9106 /* This function takes a string and returns a pointer to a block of store
9107 holding a compiled version of the expression. The original API for this
9108 function had no error code return variable; it is retained for backwards
9109 compatibility. The new function is given a new name.
9111 Arguments:
9112 pattern the regular expression
9113 options various option bits
9114 errorcodeptr pointer to error code variable (pcre_compile2() only)
9115 can be NULL if you don't want a code value
9116 errorptr pointer to pointer to error text
9117 erroroffset ptr offset in pattern where error was detected
9118 tables pointer to character tables or NULL
9120 Returns: pointer to compiled data block, or NULL on error,
9121 with errorptr and erroroffset set
9124 #if defined COMPILE_PCRE8
9125 PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
9126 pcre_compile(const char *pattern, int options, const char **errorptr,
9127 int *erroroffset, const unsigned char *tables)
9128 #elif defined COMPILE_PCRE16
9129 PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
9130 pcre16_compile(PCRE_SPTR16 pattern, int options, const char **errorptr,
9131 int *erroroffset, const unsigned char *tables)
9132 #elif defined COMPILE_PCRE32
9133 PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
9134 pcre32_compile(PCRE_SPTR32 pattern, int options, const char **errorptr,
9135 int *erroroffset, const unsigned char *tables)
9136 #endif
9138 #if defined COMPILE_PCRE8
9139 return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
9140 #elif defined COMPILE_PCRE16
9141 return pcre16_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
9142 #elif defined COMPILE_PCRE32
9143 return pcre32_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
9144 #endif
9148 #if defined COMPILE_PCRE8
9149 PCRE_EXP_DEFN pcre * PCRE_CALL_CONVENTION
9150 pcre_compile2(const char *pattern, int options, int *errorcodeptr,
9151 const char **errorptr, int *erroroffset, const unsigned char *tables)
9152 #elif defined COMPILE_PCRE16
9153 PCRE_EXP_DEFN pcre16 * PCRE_CALL_CONVENTION
9154 pcre16_compile2(PCRE_SPTR16 pattern, int options, int *errorcodeptr,
9155 const char **errorptr, int *erroroffset, const unsigned char *tables)
9156 #elif defined COMPILE_PCRE32
9157 PCRE_EXP_DEFN pcre32 * PCRE_CALL_CONVENTION
9158 pcre32_compile2(PCRE_SPTR32 pattern, int options, int *errorcodeptr,
9159 const char **errorptr, int *erroroffset, const unsigned char *tables)
9160 #endif
9162 REAL_PCRE *re;
9163 int length = 1; /* For final END opcode */
9164 pcre_int32 firstcharflags, reqcharflags;
9165 pcre_uint32 firstchar, reqchar;
9166 pcre_uint32 limit_match = PCRE_UINT32_MAX;
9167 pcre_uint32 limit_recursion = PCRE_UINT32_MAX;
9168 int newline;
9169 int options2;
9170 int errorcode = 0;
9171 int skipatstart = 0;
9172 BOOL utf;
9173 BOOL never_utf = FALSE;
9174 size_t size;
9175 pcre_uchar *code;
9176 const pcre_uchar *codestart;
9177 const pcre_uchar *ptr;
9178 const pcre_uchar *endptr;
9179 compile_data compile_block;
9180 compile_data *cd = &compile_block;
9182 /* This space is used for "compiling" into during the first phase, when we are
9183 computing the amount of memory that is needed. Compiled items are thrown away
9184 as soon as possible, so that a fairly large buffer should be sufficient for
9185 this purpose. The same space is used in the second phase for remembering where
9186 to fill in forward references to subpatterns. That may overflow, in which case
9187 new memory is obtained from malloc(). */
9189 pcre_uchar cworkspace[COMPILE_WORK_SIZE];
9191 /* This vector is used for remembering name groups during the pre-compile. In a
9192 similar way to cworkspace, it can be expanded using malloc() if necessary. */
9194 named_group named_groups[NAMED_GROUP_LIST_SIZE];
9196 /* Set this early so that early errors get offset 0. */
9198 ptr = (const pcre_uchar *)pattern;
9200 /* We can't pass back an error message if errorptr is NULL; I guess the best we
9201 can do is just return NULL, but we can set a code value if there is a code
9202 pointer. */
9204 if (errorptr == NULL)
9206 if (errorcodeptr != NULL) *errorcodeptr = 99;
9207 return NULL;
9210 *errorptr = NULL;
9211 if (errorcodeptr != NULL) *errorcodeptr = ERR0;
9213 /* However, we can give a message for this error */
9215 if (erroroffset == NULL)
9217 errorcode = ERR16;
9218 goto PCRE_EARLY_ERROR_RETURN2;
9221 *erroroffset = 0;
9223 /* Set up pointers to the individual character tables */
9225 if (tables == NULL) tables = PRIV(default_tables);
9226 cd->lcc = tables + lcc_offset;
9227 cd->fcc = tables + fcc_offset;
9228 cd->cbits = tables + cbits_offset;
9229 cd->ctypes = tables + ctypes_offset;
9231 /* Check that all undefined public option bits are zero */
9233 if ((options & ~(PUBLIC_COMPILE_OPTIONS | PUBLIC_EXTENDED_COMPILE_OPTIONS)) != 0 ||
9234 (((options & PCRE_XC1OPTIONS) != 0) &&
9235 (options & (PUBLIC_EXTENDED_COMPILE_OPTIONS & ~PCRE_XC1OPTIONS)) == 0) ||
9236 (((options & PCRE_XC1OPTIONS) == 0) &&
9237 (options & (PUBLIC_EXTENDED_COMPILE_OPTIONS & ~PCRE_XC1OPTIONS)) != 0))
9239 BADOPTIONS:
9240 errorcode = ERR17;
9241 goto PCRE_EARLY_ERROR_RETURN;
9244 /* Extract the extended public options (stripping the PCRE_XC1OPTIONS bit) */
9246 options2 = options & (PUBLIC_EXTENDED_COMPILE_OPTIONS & ~PCRE_XC1OPTIONS);
9247 options &= ~PUBLIC_EXTENDED_COMPILE_OPTIONS;
9251 *** LOOK AWAY NOW!!! YOUR EYES WILL BUG OUT OF YOUR HEAD IF YOU DON'T!!! ;)
9254 /* Handle PCRE_ALLOW_EMBEDDED_NUL_BIT here. */
9256 if (options2 & PCRE_ALLOW_EMBEDDED_NUL_BIT)
9258 # define ISXDIG(x) (digitab[x] & ctype_xdigit)
9259 # define XVAL(x) (IS_DIGIT(x)?((x)&0xf):(((x)+0x9)&0xf)) /* ASCII or EBCDIC */
9260 # define XPTR(x) ((const pcre_uchar *)(void *)(uintptr_t)(x))
9261 const unsigned char *uptr = (const unsigned char *)pattern;
9262 const pcre_uchar **vptrs;
9263 uintptr_t vinfo = 0;
9264 int xdigcnt = sizeof(void *) * 2;
9266 while (xdigcnt && ISXDIG(*uptr))
9268 vinfo <<= 4;
9269 vinfo |= XVAL(*uptr);
9270 ++uptr;
9271 --xdigcnt;
9273 if (xdigcnt || *uptr) goto BADOPTIONS;
9274 vptrs = (const pcre_uchar **)XPTR(vinfo);
9275 if (vptrs[0] != XPTR(0x4841434b /* odd magic number ;) */) ||
9276 vptrs[1] != XPTR(0x01010101 /* version number one */))
9277 goto BADOPTIONS;
9279 endptr = vptrs[3];
9280 if (vptrs[2] == endptr)
9282 /* only case where trailing NUL may be omitted */
9283 ptr = sub_embedded_nul + 4; /* sub_embedded_nul is always "\\000\0" */
9284 options2 &= ~PCRE_ALLOW_EMBEDDED_NUL_BIT;
9286 else
9288 if (*endptr) goto BADOPTIONS;
9290 ptr = vptrs[2];
9291 pattern = (const char *)ptr;
9292 # undef ISXDIG
9293 # undef XVAL
9294 # undef XPTR
9297 /* If PCRE_NEVER_UTF is set, remember it. */
9299 if ((options & PCRE_NEVER_UTF) != 0) never_utf = TRUE;
9301 /* Check for global one-time settings at the start of the pattern, and remember
9302 the offset for later. */
9304 cd->external_flags = 0; /* Initialize here for LIMIT_MATCH/RECURSION */
9306 while (ptr[skipatstart] == CHAR_LEFT_PARENTHESIS &&
9307 ptr[skipatstart+1] == CHAR_ASTERISK &&
9308 !(options2 & (PCRE_POSIX_BASIC_ESC_BIT|PCRE_VERBATIM_BIT)))
9310 int newnl = 0;
9311 int newbsr = 0;
9313 /* For completeness and backward compatibility, (*UTFn) is supported in the
9314 relevant libraries, but (*UTF) is generic and always supported. Note that
9315 PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. */
9317 #ifdef COMPILE_PCRE8
9318 if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF8_RIGHTPAR, 5) == 0)
9319 { skipatstart += 7; options |= PCRE_UTF8; continue; }
9320 #endif
9321 #ifdef COMPILE_PCRE16
9322 if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF16_RIGHTPAR, 6) == 0)
9323 { skipatstart += 8; options |= PCRE_UTF16; continue; }
9324 #endif
9325 #ifdef COMPILE_PCRE32
9326 if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF32_RIGHTPAR, 6) == 0)
9327 { skipatstart += 8; options |= PCRE_UTF32; continue; }
9328 #endif
9330 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UTF_RIGHTPAR, 4) == 0)
9331 { skipatstart += 6; options |= PCRE_UTF8; continue; }
9332 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)
9333 { skipatstart += 6; options |= PCRE_UCP; continue; }
9334 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_AUTO_POSSESS_RIGHTPAR, 16) == 0)
9335 { skipatstart += 18; options |= PCRE_NO_AUTO_POSSESS; continue; }
9336 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
9337 { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
9339 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_MATCH_EQ, 12) == 0)
9341 pcre_uint32 c = 0;
9342 int p = skipatstart + 14;
9343 while (isdigit(ptr[p]))
9345 if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow */
9346 c = c*10 + ptr[p++] - CHAR_0;
9348 if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
9349 if (c < limit_match)
9351 limit_match = c;
9352 cd->external_flags |= PCRE_MLSET;
9354 skipatstart = p;
9355 continue;
9358 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LIMIT_RECURSION_EQ, 16) == 0)
9360 pcre_uint32 c = 0;
9361 int p = skipatstart + 18;
9362 while (isdigit(ptr[p]))
9364 if (c > PCRE_UINT32_MAX / 10 - 1) break; /* Integer overflow check */
9365 c = c*10 + ptr[p++] - CHAR_0;
9367 if (ptr[p++] != CHAR_RIGHT_PARENTHESIS) break;
9368 if (c < limit_recursion)
9370 limit_recursion = c;
9371 cd->external_flags |= PCRE_RLSET;
9373 skipatstart = p;
9374 continue;
9377 if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CR_RIGHTPAR, 3) == 0)
9378 { skipatstart += 5; newnl = PCRE_NEWLINE_CR; }
9379 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_LF_RIGHTPAR, 3) == 0)
9380 { skipatstart += 5; newnl = PCRE_NEWLINE_LF; }
9381 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_CRLF_RIGHTPAR, 5) == 0)
9382 { skipatstart += 7; newnl = PCRE_NEWLINE_CR + PCRE_NEWLINE_LF; }
9383 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANY_RIGHTPAR, 4) == 0)
9384 { skipatstart += 6; newnl = PCRE_NEWLINE_ANY; }
9385 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_ANYCRLF_RIGHTPAR, 8) == 0)
9386 { skipatstart += 10; newnl = PCRE_NEWLINE_ANYCRLF; }
9388 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_ANYCRLF_RIGHTPAR, 12) == 0)
9389 { skipatstart += 14; newbsr = PCRE_BSR_ANYCRLF; }
9390 else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_BSR_UNICODE_RIGHTPAR, 12) == 0)
9391 { skipatstart += 14; newbsr = PCRE_BSR_UNICODE; }
9393 if (newnl != 0)
9394 options = (options & ~PCRE_NEWLINE_BITS) | newnl;
9395 else if (newbsr != 0)
9396 options = (options & ~(PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) | newbsr;
9397 else break;
9400 /* PCRE_UTF(16|32) have the same value as PCRE_UTF8. */
9401 utf = (options & PCRE_UTF8) != 0;
9402 if (utf && never_utf)
9404 errorcode = ERR78;
9405 goto PCRE_EARLY_ERROR_RETURN2;
9408 /* Can't support UTF unless PCRE has been compiled to include the code. The
9409 return of an error code from PRIV(valid_utf)() is a new feature, introduced in
9410 release 8.13. It is passed back from pcre_[dfa_]exec(), but at the moment is
9411 not used here. */
9413 #ifdef SUPPORT_UTF
9414 if (utf && (options & PCRE_NO_UTF8_CHECK) == 0 &&
9415 (errorcode = PRIV(valid_utf)((PCRE_PUCHAR)pattern, -1, erroroffset)) != 0)
9417 #if defined COMPILE_PCRE8
9418 errorcode = ERR44;
9419 #elif defined COMPILE_PCRE16
9420 errorcode = ERR74;
9421 #elif defined COMPILE_PCRE32
9422 errorcode = ERR77;
9423 #endif
9424 goto PCRE_EARLY_ERROR_RETURN2;
9426 #else
9427 if (utf)
9429 errorcode = ERR32;
9430 goto PCRE_EARLY_ERROR_RETURN;
9432 #endif
9434 /* Can't support UCP unless PCRE has been compiled to include the code. */
9436 #ifndef SUPPORT_UCP
9437 if ((options & PCRE_UCP) != 0)
9439 errorcode = ERR67;
9440 goto PCRE_EARLY_ERROR_RETURN;
9442 #endif
9444 /* Check validity of \R options. */
9446 if ((options & (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE)) ==
9447 (PCRE_BSR_ANYCRLF|PCRE_BSR_UNICODE))
9449 errorcode = ERR56;
9450 goto PCRE_EARLY_ERROR_RETURN;
9453 /* Handle different types of newline. The three bits give seven cases. The
9454 current code allows for fixed one- or two-byte sequences, plus "any" and
9455 "anycrlf". */
9457 switch (options & PCRE_NEWLINE_BITS)
9459 case 0: newline = NEWLINE; break; /* Build-time default */
9460 case PCRE_NEWLINE_CR: newline = CHAR_CR; break;
9461 case PCRE_NEWLINE_LF: newline = CHAR_NL; break;
9462 case PCRE_NEWLINE_CR+
9463 PCRE_NEWLINE_LF: newline = (CHAR_CR << 8) | CHAR_NL; break;
9464 case PCRE_NEWLINE_ANY: newline = -1; break;
9465 case PCRE_NEWLINE_ANYCRLF: newline = -2; break;
9466 default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;
9469 if (newline == -2)
9471 cd->nltype = NLTYPE_ANYCRLF;
9473 else if (newline < 0)
9475 cd->nltype = NLTYPE_ANY;
9477 else
9479 cd->nltype = NLTYPE_FIXED;
9480 if (newline > 255)
9482 cd->nllen = 2;
9483 cd->nl[0] = (newline >> 8) & 255;
9484 cd->nl[1] = newline & 255;
9486 else
9488 cd->nllen = 1;
9489 cd->nl[0] = newline;
9493 /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
9494 references to help in deciding whether (.*) can be treated as anchored or not.
9497 cd->top_backref = 0;
9498 cd->backref_map = 0;
9500 /* Reflect pattern for debugging output */
9502 DPRINTF(("------------------------------------------------------------------\n"));
9503 #ifdef PCRE_DEBUG
9504 print_puchar(stdout, (PCRE_PUCHAR)pattern);
9505 #endif
9506 DPRINTF(("\n"));
9508 /* Pretend to compile the pattern while actually just accumulating the length
9509 of memory required. This behaviour is triggered by passing a non-NULL final
9510 argument to compile_regex(). We pass a block of workspace (cworkspace) for it
9511 to compile parts of the pattern into; the compiled code is discarded when it is
9512 no longer needed, so hopefully this workspace will never overflow, though there
9513 is a test for its doing so. */
9515 cd->bracount = cd->final_bracount = 0;
9516 cd->names_found = 0;
9517 cd->name_entry_size = 0;
9518 cd->name_table = NULL;
9519 cd->dupnames = FALSE;
9520 cd->dupgroups = FALSE;
9521 cd->namedrefcount = 0;
9522 cd->start_code = cworkspace;
9523 cd->hwm = cworkspace;
9524 cd->iscondassert = FALSE;
9525 cd->start_workspace = cworkspace;
9526 cd->workspace_size = COMPILE_WORK_SIZE;
9527 cd->named_groups = named_groups;
9528 cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
9529 cd->start_pattern = (const pcre_uchar *)pattern;
9530 if (options2 & PCRE_ALLOW_EMBEDDED_NUL_BIT)
9531 cd->end_pattern = endptr;
9532 else
9533 cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
9534 cd->req_varyopt = 0;
9535 cd->parens_depth = 0;
9536 cd->assert_depth = 0;
9537 cd->max_lookbehind = 0;
9538 cd->external_options = options;
9539 cd->extended_options = options2;
9540 cd->open_caps = NULL;
9542 /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
9543 don't need to look at the result of the function here. The initial options have
9544 been put into the cd block so that they can be changed if an option setting is
9545 found within the regex right at the beginning. Bringing initial option settings
9546 outside can help speed up starting point checks. */
9548 ptr += skipatstart;
9549 code = cworkspace;
9550 *code = OP_BRA;
9552 (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
9553 FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL,
9554 cd, &length);
9555 if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
9557 DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
9558 (int)(cd->hwm - cworkspace)));
9560 if (length > MAX_PATTERN_SIZE)
9562 errorcode = ERR20;
9563 goto PCRE_EARLY_ERROR_RETURN;
9566 /* Compute the size of the data block for storing the compiled pattern. Integer
9567 overflow should no longer be possible because nowadays we limit the maximum
9568 value of cd->names_found and cd->name_entry_size. */
9570 size = sizeof(REAL_PCRE) +
9571 (length + cd->names_found * cd->name_entry_size) * sizeof(pcre_uchar);
9573 /* Get the memory. */
9575 re = (REAL_PCRE *)(PUBL(malloc))(size);
9576 if (re == NULL)
9578 errorcode = ERR21;
9579 goto PCRE_EARLY_ERROR_RETURN;
9582 /* Put in the magic number, and save the sizes, initial options, internal
9583 flags, and character table pointer. NULL is used for the default character
9584 tables. The nullpad field is at the end; it's there to help in the case when a
9585 regex compiled on a system with 4-byte pointers is run on another with 8-byte
9586 pointers. */
9588 re->magic_number = MAGIC_NUMBER;
9589 re->size = (int)size;
9590 re->options = cd->external_options;
9591 re->flags = cd->external_flags;
9592 re->limit_match = limit_match;
9593 re->limit_recursion = limit_recursion;
9594 re->first_char = 0;
9595 re->req_char = 0;
9596 re->name_table_offset = sizeof(REAL_PCRE) / sizeof(pcre_uchar);
9597 re->name_entry_size = cd->name_entry_size;
9598 re->name_count = cd->names_found;
9599 re->ref_count = 0;
9600 re->tables = (tables == PRIV(default_tables))? NULL : tables;
9601 re->nullpad = NULL;
9602 #ifdef COMPILE_PCRE32
9603 re->dummy = 0;
9604 #else
9605 re->dummy1 = re->dummy2 = re->dummy3 = 0;
9606 #endif
9608 /* The starting points of the name/number translation table and of the code are
9609 passed around in the compile data block. The start/end pattern and initial
9610 options are already set from the pre-compile phase, as is the name_entry_size
9611 field. Reset the bracket count and the names_found field. Also reset the hwm
9612 field; this time it's used for remembering forward references to subpatterns.
9615 cd->final_bracount = cd->bracount; /* Save for checking forward references */
9616 cd->parens_depth = 0;
9617 cd->assert_depth = 0;
9618 cd->bracount = 0;
9619 cd->max_lookbehind = 0;
9620 cd->name_table = (pcre_uchar *)re + re->name_table_offset;
9621 codestart = cd->name_table + re->name_entry_size * re->name_count;
9622 cd->start_code = codestart;
9623 cd->hwm = (pcre_uchar *)(cd->start_workspace);
9624 cd->iscondassert = FALSE;
9625 cd->req_varyopt = 0;
9626 cd->had_accept = FALSE;
9627 cd->had_pruneorskip = FALSE;
9628 cd->check_lookbehind = FALSE;
9629 cd->open_caps = NULL;
9631 /* If any named groups were found, create the name/number table from the list
9632 created in the first pass. */
9634 if (cd->names_found > 0)
9636 int i = cd->names_found;
9637 named_group *ng = cd->named_groups;
9638 cd->names_found = 0;
9639 for (; i > 0; i--, ng++)
9640 add_name(cd, ng->name, ng->length, ng->number);
9641 if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
9642 (PUBL(free))((void *)cd->named_groups);
9645 /* Set up a starting, non-extracting bracket, then compile the expression. On
9646 error, errorcode will be set non-zero, so we don't need to look at the result
9647 of the function here. */
9649 ptr = (const pcre_uchar *)pattern + skipatstart;
9650 code = (pcre_uchar *)codestart;
9651 *code = OP_BRA;
9652 (void)compile_regex(re->options, &code, &ptr, &errorcode, FALSE, FALSE, 0, 0,
9653 &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL, cd, NULL);
9654 re->top_bracket = cd->bracount;
9655 re->top_backref = cd->top_backref;
9656 re->max_lookbehind = cd->max_lookbehind;
9657 re->flags = cd->external_flags | PCRE_MODE;
9659 if (cd->had_accept)
9661 reqchar = 0; /* Must disable after (*ACCEPT) */
9662 reqcharflags = REQ_NONE;
9665 /* If not reached end of pattern on success, there's an excess bracket. */
9667 if (errorcode == 0 && *ptr != CHAR_NULL) errorcode = ERR22;
9669 /* Fill in the terminating state and check for disastrous overflow, but
9670 if debugging, leave the test till after things are printed out. */
9672 *code++ = OP_END;
9674 #ifndef PCRE_DEBUG
9675 if (code - codestart > length) errorcode = ERR23;
9676 #endif
9678 #ifdef SUPPORT_VALGRIND
9679 /* If the estimated length exceeds the really used length, mark the extra
9680 allocated memory as unaddressable, so that any out-of-bound reads can be
9681 detected. */
9682 VALGRIND_MAKE_MEM_NOACCESS(code, (length - (code - codestart)) * sizeof(pcre_uchar));
9683 #endif
9685 /* Fill in any forward references that are required. There may be repeated
9686 references; optimize for them, as searching a large regex takes time. */
9688 if (cd->hwm > cd->start_workspace)
9690 int prev_recno = -1;
9691 const pcre_uchar *groupptr = NULL;
9692 while (errorcode == 0 && cd->hwm > cd->start_workspace)
9694 int offset, recno;
9695 cd->hwm -= LINK_SIZE;
9696 offset = GET(cd->hwm, 0);
9698 /* Check that the hwm handling hasn't gone wrong. This whole area is
9699 rewritten in PCRE2 because there are some obscure cases. */
9701 if (offset == 0 || codestart[offset-1] != OP_RECURSE)
9703 errorcode = ERR10;
9704 break;
9707 recno = GET(codestart, offset);
9708 if (recno != prev_recno)
9710 groupptr = PRIV(find_bracket)(codestart, utf, recno);
9711 prev_recno = recno;
9713 if (groupptr == NULL) errorcode = ERR53;
9714 else PUT(((pcre_uchar *)codestart), offset, (int)(groupptr - codestart));
9718 /* If the workspace had to be expanded, free the new memory. Set the pointer to
9719 NULL to indicate that forward references have been filled in. */
9721 if (cd->workspace_size > COMPILE_WORK_SIZE)
9722 (PUBL(free))((void *)cd->start_workspace);
9723 cd->start_workspace = NULL;
9725 /* Give an error if there's back reference to a non-existent capturing
9726 subpattern. */
9728 if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
9730 /* Unless disabled, check whether any single character iterators can be
9731 auto-possessified. The function overwrites the appropriate opcode values, so
9732 the type of the pointer must be cast. NOTE: the intermediate variable "temp" is
9733 used in this code because at least one compiler gives a warning about loss of
9734 "const" attribute if the cast (pcre_uchar *)codestart is used directly in the
9735 function call. */
9737 if (errorcode == 0 && (options & PCRE_NO_AUTO_POSSESS) == 0)
9739 pcre_uchar *temp = (pcre_uchar *)codestart;
9740 auto_possessify(temp, utf, cd);
9743 /* If there were any lookbehind assertions that contained OP_RECURSE
9744 (recursions or subroutine calls), a flag is set for them to be checked here,
9745 because they may contain forward references. Actual recursions cannot be fixed
9746 length, but subroutine calls can. It is done like this so that those without
9747 OP_RECURSE that are not fixed length get a diagnosic with a useful offset. The
9748 exceptional ones forgo this. We scan the pattern to check that they are fixed
9749 length, and set their lengths. */
9751 if (errorcode == 0 && cd->check_lookbehind)
9753 pcre_uchar *cc = (pcre_uchar *)codestart;
9755 /* Loop, searching for OP_REVERSE items, and process those that do not have
9756 their length set. (Actually, it will also re-process any that have a length
9757 of zero, but that is a pathological case, and it does no harm.) When we find
9758 one, we temporarily terminate the branch it is in while we scan it. */
9760 for (cc = (pcre_uchar *)PRIV(find_bracket)(codestart, utf, -1);
9761 cc != NULL;
9762 cc = (pcre_uchar *)PRIV(find_bracket)(cc, utf, -1))
9764 if (GET(cc, 1) == 0)
9766 int fixed_length;
9767 pcre_uchar *be = cc - 1 - LINK_SIZE + GET(cc, -LINK_SIZE);
9768 int end_op = *be;
9769 *be = OP_END;
9770 fixed_length = find_fixedlength(cc, (re->options & PCRE_UTF8) != 0, TRUE,
9771 cd, NULL);
9772 *be = end_op;
9773 DPRINTF(("fixed length = %d\n", fixed_length));
9774 if (fixed_length < 0)
9776 errorcode = (fixed_length == -2)? ERR36 :
9777 (fixed_length == -4)? ERR70 : ERR25;
9778 break;
9780 if (fixed_length > cd->max_lookbehind) cd->max_lookbehind = fixed_length;
9781 PUT(cc, 1, fixed_length);
9783 cc += 1 + LINK_SIZE;
9787 /* Failed to compile, or error while post-processing */
9789 if (errorcode != 0)
9791 (PUBL(free))(re);
9792 PCRE_EARLY_ERROR_RETURN:
9793 *erroroffset = (int)(ptr - (const pcre_uchar *)pattern);
9794 PCRE_EARLY_ERROR_RETURN2:
9795 *errorptr = find_error_text(errorcode);
9796 if (errorcodeptr != NULL) *errorcodeptr = errorcode;
9797 return NULL;
9800 /* If the anchored option was not passed, set the flag if we can determine that
9801 the pattern is anchored by virtue of ^ characters or \A or anything else, such
9802 as starting with non-atomic .* when DOTALL is set and there are no occurrences
9803 of *PRUNE or *SKIP.
9805 Otherwise, if we know what the first byte has to be, save it, because that
9806 speeds up unanchored matches no end. If not, see if we can set the
9807 PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
9808 start with ^. and also when all branches start with non-atomic .* for
9809 non-DOTALL matches when *PRUNE and SKIP are not present. */
9811 if ((re->options & PCRE_ANCHORED) == 0)
9813 if (is_anchored(codestart, 0, cd, 0)) re->options |= PCRE_ANCHORED;
9814 else
9816 if (firstcharflags < 0)
9817 firstchar = find_firstassertedchar(codestart, &firstcharflags, FALSE);
9818 if (firstcharflags >= 0) /* Remove caseless flag for non-caseable chars */
9820 #if defined COMPILE_PCRE8
9821 re->first_char = firstchar & 0xff;
9822 #elif defined COMPILE_PCRE16
9823 re->first_char = firstchar & 0xffff;
9824 #elif defined COMPILE_PCRE32
9825 re->first_char = firstchar;
9826 #endif
9827 if ((firstcharflags & REQ_CASELESS) != 0)
9829 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
9830 /* We ignore non-ASCII first chars in 8 bit mode. */
9831 if (utf)
9833 if (re->first_char < 128)
9835 if (cd->fcc[re->first_char] != re->first_char)
9836 re->flags |= PCRE_FCH_CASELESS;
9838 else if (UCD_OTHERCASE(re->first_char) != re->first_char)
9839 re->flags |= PCRE_FCH_CASELESS;
9841 else
9842 #endif
9843 if (MAX_255(re->first_char)
9844 && cd->fcc[re->first_char] != re->first_char)
9845 re->flags |= PCRE_FCH_CASELESS;
9848 re->flags |= PCRE_FIRSTSET;
9851 else if (is_startline(codestart, 0, cd, 0, FALSE)) re->flags |= PCRE_STARTLINE;
9855 /* For an anchored pattern, we use the "required byte" only if it follows a
9856 variable length item in the regex. Remove the caseless flag for non-caseable
9857 bytes. */
9859 if (reqcharflags >= 0 &&
9860 ((re->options & PCRE_ANCHORED) == 0 || (reqcharflags & REQ_VARY) != 0))
9862 #if defined COMPILE_PCRE8
9863 re->req_char = reqchar & 0xff;
9864 #elif defined COMPILE_PCRE16
9865 re->req_char = reqchar & 0xffff;
9866 #elif defined COMPILE_PCRE32
9867 re->req_char = reqchar;
9868 #endif
9869 if ((reqcharflags & REQ_CASELESS) != 0)
9871 #if defined SUPPORT_UCP && !(defined COMPILE_PCRE8)
9872 /* We ignore non-ASCII first chars in 8 bit mode. */
9873 if (utf)
9875 if (re->req_char < 128)
9877 if (cd->fcc[re->req_char] != re->req_char)
9878 re->flags |= PCRE_RCH_CASELESS;
9880 else if (UCD_OTHERCASE(re->req_char) != re->req_char)
9881 re->flags |= PCRE_RCH_CASELESS;
9883 else
9884 #endif
9885 if (MAX_255(re->req_char) && cd->fcc[re->req_char] != re->req_char)
9886 re->flags |= PCRE_RCH_CASELESS;
9889 re->flags |= PCRE_REQCHSET;
9892 /* Print out the compiled data if debugging is enabled. This is never the
9893 case when building a production library. */
9895 #ifdef PCRE_DEBUG
9896 printf("Length = %d top_bracket = %d top_backref = %d\n",
9897 length, re->top_bracket, re->top_backref);
9899 printf("Options=%08x\n", re->options);
9901 if ((re->flags & PCRE_FIRSTSET) != 0)
9903 pcre_uchar ch = re->first_char;
9904 const char *caseless =
9905 ((re->flags & PCRE_FCH_CASELESS) == 0)? "" : " (caseless)";
9906 if (PRINTABLE(ch)) printf("First char = %c%s\n", ch, caseless);
9907 else printf("First char = \\x%02x%s\n", ch, caseless);
9910 if ((re->flags & PCRE_REQCHSET) != 0)
9912 pcre_uchar ch = re->req_char;
9913 const char *caseless =
9914 ((re->flags & PCRE_RCH_CASELESS) == 0)? "" : " (caseless)";
9915 if (PRINTABLE(ch)) printf("Req char = %c%s\n", ch, caseless);
9916 else printf("Req char = \\x%02x%s\n", ch, caseless);
9919 #if defined COMPILE_PCRE8
9920 pcre_printint((pcre *)re, stdout, TRUE);
9921 #elif defined COMPILE_PCRE16
9922 pcre16_printint((pcre *)re, stdout, TRUE);
9923 #elif defined COMPILE_PCRE32
9924 pcre32_printint((pcre *)re, stdout, TRUE);
9925 #endif
9927 /* This check is done here in the debugging case so that the code that
9928 was compiled can be seen. */
9930 if (code - codestart > length)
9932 (PUBL(free))(re);
9933 *errorptr = find_error_text(ERR23);
9934 *erroroffset = ptr - (pcre_uchar *)pattern;
9935 if (errorcodeptr != NULL) *errorcodeptr = ERR23;
9936 return NULL;
9938 #endif /* PCRE_DEBUG */
9940 /* Check for a pattern than can match an empty string, so that this information
9941 can be provided to applications. */
9945 if (could_be_empty_branch(codestart, code, utf, cd, NULL))
9947 re->flags |= PCRE_MATCH_EMPTY;
9948 break;
9950 codestart += GET(codestart, 1);
9952 while (*codestart == OP_ALT);
9954 #if defined COMPILE_PCRE8
9955 return (pcre *)re;
9956 #elif defined COMPILE_PCRE16
9957 return (pcre16 *)re;
9958 #elif defined COMPILE_PCRE32
9959 return (pcre32 *)re;
9960 #endif
9963 /* End of pcre_compile.c */