/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Contents of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1055 - (show annotations)
Tue Oct 16 15:53:30 2012 UTC (6 years, 11 months ago) by chpe
File MIME type: text/plain
File size: 271552 byte(s)
Error occurred while calculating annotation data.
pcre32: Add 32-bit library

Create libpcre32 that operates on 32-bit characters (UTF-32).

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