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

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

Parent Directory Parent Directory | Revision Log Revision Log


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