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

Contents of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


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