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

Contents of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1352 - (show annotations)
Mon Jul 29 15:49:21 2013 UTC (6 years, 3 months ago) by ph10
File MIME type: text/plain
File size: 280546 byte(s)
Error occurred while calculating annotation data.
Fix incorrect "first data item" recorded for an assertion condition.
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 recurses chain of recurse_check to catch mutual recursion
2357
2358 Returns: TRUE if what is matched could be empty
2359 */
2360
2361 typedef struct recurse_check {
2362 struct recurse_check *prev;
2363 const pcre_uchar *group;
2364 } recurse_check;
2365
2366 static BOOL
2367 could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2368 BOOL utf, compile_data *cd, recurse_check *recurses)
2369 {
2370 register pcre_uchar c;
2371 recurse_check this_recurse;
2372
2373 for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2374 code < endcode;
2375 code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2376 {
2377 const pcre_uchar *ccode;
2378
2379 c = *code;
2380
2381 /* Skip over forward assertions; the other assertions are skipped by
2382 first_significant_code() with a TRUE final argument. */
2383
2384 if (c == OP_ASSERT)
2385 {
2386 do code += GET(code, 1); while (*code == OP_ALT);
2387 c = *code;
2388 continue;
2389 }
2390
2391 /* For a recursion/subroutine call, if its end has been reached, which
2392 implies a backward reference subroutine call, we can scan it. If it's a
2393 forward reference subroutine call, we can't. To detect forward reference
2394 we have to scan up the list that is kept in the workspace. This function is
2395 called only when doing the real compile, not during the pre-compile that
2396 measures the size of the compiled pattern. */
2397
2398 if (c == OP_RECURSE)
2399 {
2400 const pcre_uchar *scode = cd->start_code + GET(code, 1);
2401 BOOL empty_branch;
2402
2403 /* Test for forward reference or uncompleted reference. This is disabled
2404 when called to scan a completed pattern by setting cd->start_workspace to
2405 NULL. */
2406
2407 if (cd->start_workspace != NULL)
2408 {
2409 const pcre_uchar *tcode;
2410 for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2411 if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2412 if (GET(scode, 1) == 0) return TRUE; /* Unclosed */
2413 }
2414
2415 /* If we are scanning a completed pattern, there are no forward references
2416 and all groups are complete. We need to detect whether this is a recursive
2417 call, as otherwise there will be an infinite loop. If it is a recursion,
2418 just skip over it. Simple recursions are easily detected. For mutual
2419 recursions we keep a chain on the stack. */
2420
2421 else
2422 {
2423 recurse_check *r = recurses;
2424 const pcre_uchar *endgroup = scode;
2425
2426 do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2427 if (code >= scode && code <= endgroup) continue; /* Simple recursion */
2428
2429 for (r = recurses; r != NULL; r = r->prev)
2430 if (r->group == scode) break;
2431 if (r != NULL) continue; /* Mutual recursion */
2432 }
2433
2434 /* Completed reference; scan the referenced group, remembering it on the
2435 stack chain to detect mutual recursions. */
2436
2437 empty_branch = FALSE;
2438 this_recurse.prev = recurses;
2439 this_recurse.group = scode;
2440
2441 do
2442 {
2443 if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2444 {
2445 empty_branch = TRUE;
2446 break;
2447 }
2448 scode += GET(scode, 1);
2449 }
2450 while (*scode == OP_ALT);
2451
2452 if (!empty_branch) return FALSE; /* All branches are non-empty */
2453 continue;
2454 }
2455
2456 /* Groups with zero repeats can of course be empty; skip them. */
2457
2458 if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2459 c == OP_BRAPOSZERO)
2460 {
2461 code += PRIV(OP_lengths)[c];
2462 do code += GET(code, 1); while (*code == OP_ALT);
2463 c = *code;
2464 continue;
2465 }
2466
2467 /* A nested group that is already marked as "could be empty" can just be
2468 skipped. */
2469
2470 if (c == OP_SBRA || c == OP_SBRAPOS ||
2471 c == OP_SCBRA || c == OP_SCBRAPOS)
2472 {
2473 do code += GET(code, 1); while (*code == OP_ALT);
2474 c = *code;
2475 continue;
2476 }
2477
2478 /* For other groups, scan the branches. */
2479
2480 if (c == OP_BRA || c == OP_BRAPOS ||
2481 c == OP_CBRA || c == OP_CBRAPOS ||
2482 c == OP_ONCE || c == OP_ONCE_NC ||
2483 c == OP_COND)
2484 {
2485 BOOL empty_branch;
2486 if (GET(code, 1) == 0) return TRUE; /* Hit unclosed bracket */
2487
2488 /* If a conditional group has only one branch, there is a second, implied,
2489 empty branch, so just skip over the conditional, because it could be empty.
2490 Otherwise, scan the individual branches of the group. */
2491
2492 if (c == OP_COND && code[GET(code, 1)] != OP_ALT)
2493 code += GET(code, 1);
2494 else
2495 {
2496 empty_branch = FALSE;
2497 do
2498 {
2499 if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))
2500 empty_branch = TRUE;
2501 code += GET(code, 1);
2502 }
2503 while (*code == OP_ALT);
2504 if (!empty_branch) return FALSE; /* All branches are non-empty */
2505 }
2506
2507 c = *code;
2508 continue;
2509 }
2510
2511 /* Handle the other opcodes */
2512
2513 switch (c)
2514 {
2515 /* Check for quantifiers after a class. XCLASS is used for classes that
2516 cannot be represented just by a bit map. This includes negated single
2517 high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2518 actual length is stored in the compiled code, so we must update "code"
2519 here. */
2520
2521 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2522 case OP_XCLASS:
2523 ccode = code += GET(code, 1);
2524 goto CHECK_CLASS_REPEAT;
2525 #endif
2526
2527 case OP_CLASS:
2528 case OP_NCLASS:
2529 ccode = code + PRIV(OP_lengths)[OP_CLASS];
2530
2531 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2532 CHECK_CLASS_REPEAT:
2533 #endif
2534
2535 switch (*ccode)
2536 {
2537 case OP_CRSTAR: /* These could be empty; continue */
2538 case OP_CRMINSTAR:
2539 case OP_CRQUERY:
2540 case OP_CRMINQUERY:
2541 break;
2542
2543 default: /* Non-repeat => class must match */
2544 case OP_CRPLUS: /* These repeats aren't empty */
2545 case OP_CRMINPLUS:
2546 return FALSE;
2547
2548 case OP_CRRANGE:
2549 case OP_CRMINRANGE:
2550 if (GET2(ccode, 1) > 0) return FALSE; /* Minimum > 0 */
2551 break;
2552 }
2553 break;
2554
2555 /* Opcodes that must match a character */
2556
2557 case OP_ANY:
2558 case OP_ALLANY:
2559 case OP_ANYBYTE:
2560
2561 case OP_PROP:
2562 case OP_NOTPROP:
2563 case OP_ANYNL:
2564
2565 case OP_NOT_HSPACE:
2566 case OP_HSPACE:
2567 case OP_NOT_VSPACE:
2568 case OP_VSPACE:
2569 case OP_EXTUNI:
2570
2571 case OP_NOT_DIGIT:
2572 case OP_DIGIT:
2573 case OP_NOT_WHITESPACE:
2574 case OP_WHITESPACE:
2575 case OP_NOT_WORDCHAR:
2576 case OP_WORDCHAR:
2577
2578 case OP_CHAR:
2579 case OP_CHARI:
2580 case OP_NOT:
2581 case OP_NOTI:
2582
2583 case OP_PLUS:
2584 case OP_PLUSI:
2585 case OP_MINPLUS:
2586 case OP_MINPLUSI:
2587
2588 case OP_NOTPLUS:
2589 case OP_NOTPLUSI:
2590 case OP_NOTMINPLUS:
2591 case OP_NOTMINPLUSI:
2592
2593 case OP_POSPLUS:
2594 case OP_POSPLUSI:
2595 case OP_NOTPOSPLUS:
2596 case OP_NOTPOSPLUSI:
2597
2598 case OP_EXACT:
2599 case OP_EXACTI:
2600 case OP_NOTEXACT:
2601 case OP_NOTEXACTI:
2602
2603 case OP_TYPEPLUS:
2604 case OP_TYPEMINPLUS:
2605 case OP_TYPEPOSPLUS:
2606 case OP_TYPEEXACT:
2607
2608 return FALSE;
2609
2610 /* These are going to continue, as they may be empty, but we have to
2611 fudge the length for the \p and \P cases. */
2612
2613 case OP_TYPESTAR:
2614 case OP_TYPEMINSTAR:
2615 case OP_TYPEPOSSTAR:
2616 case OP_TYPEQUERY:
2617 case OP_TYPEMINQUERY:
2618 case OP_TYPEPOSQUERY:
2619 if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
2620 break;
2621
2622 /* Same for these */
2623
2624 case OP_TYPEUPTO:
2625 case OP_TYPEMINUPTO:
2626 case OP_TYPEPOSUPTO:
2627 if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2628 code += 2;
2629 break;
2630
2631 /* End of branch */
2632
2633 case OP_KET:
2634 case OP_KETRMAX:
2635 case OP_KETRMIN:
2636 case OP_KETRPOS:
2637 case OP_ALT:
2638 return TRUE;
2639
2640 /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2641 MINUPTO, and POSUPTO and their caseless and negative versions may be
2642 followed by a multibyte character. */
2643
2644 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2645 case OP_STAR:
2646 case OP_STARI:
2647 case OP_NOTSTAR:
2648 case OP_NOTSTARI:
2649
2650 case OP_MINSTAR:
2651 case OP_MINSTARI:
2652 case OP_NOTMINSTAR:
2653 case OP_NOTMINSTARI:
2654
2655 case OP_POSSTAR:
2656 case OP_POSSTARI:
2657 case OP_NOTPOSSTAR:
2658 case OP_NOTPOSSTARI:
2659
2660 case OP_QUERY:
2661 case OP_QUERYI:
2662 case OP_NOTQUERY:
2663 case OP_NOTQUERYI:
2664
2665 case OP_MINQUERY:
2666 case OP_MINQUERYI:
2667 case OP_NOTMINQUERY:
2668 case OP_NOTMINQUERYI:
2669
2670 case OP_POSQUERY:
2671 case OP_POSQUERYI:
2672 case OP_NOTPOSQUERY:
2673 case OP_NOTPOSQUERYI:
2674
2675 if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2676 break;
2677
2678 case OP_UPTO:
2679 case OP_UPTOI:
2680 case OP_NOTUPTO:
2681 case OP_NOTUPTOI:
2682
2683 case OP_MINUPTO:
2684 case OP_MINUPTOI:
2685 case OP_NOTMINUPTO:
2686 case OP_NOTMINUPTOI:
2687
2688 case OP_POSUPTO:
2689 case OP_POSUPTOI:
2690 case OP_NOTPOSUPTO:
2691 case OP_NOTPOSUPTOI:
2692
2693 if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2694 break;
2695 #endif
2696
2697 /* MARK, and PRUNE/SKIP/THEN with an argument must skip over the argument
2698 string. */
2699
2700 case OP_MARK:
2701 case OP_PRUNE_ARG:
2702 case OP_SKIP_ARG:
2703 case OP_THEN_ARG:
2704 code += code[1];
2705 break;
2706
2707 /* None of the remaining opcodes are required to match a character. */
2708
2709 default:
2710 break;
2711 }
2712 }
2713
2714 return TRUE;
2715 }
2716
2717
2718
2719 /*************************************************
2720 * Scan compiled regex for non-emptiness *
2721 *************************************************/
2722
2723 /* This function is called to check for left recursive calls. We want to check
2724 the current branch of the current pattern to see if it could match the empty
2725 string. If it could, we must look outwards for branches at other levels,
2726 stopping when we pass beyond the bracket which is the subject of the recursion.
2727 This function is called only during the real compile, not during the
2728 pre-compile.
2729
2730 Arguments:
2731 code points to start of the recursion
2732 endcode points to where to stop (current RECURSE item)
2733 bcptr points to the chain of current (unclosed) branch starts
2734 utf TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2735 cd pointers to tables etc
2736
2737 Returns: TRUE if what is matched could be empty
2738 */
2739
2740 static BOOL
2741 could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2742 branch_chain *bcptr, BOOL utf, compile_data *cd)
2743 {
2744 while (bcptr != NULL && bcptr->current_branch >= code)
2745 {
2746 if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2747 return FALSE;
2748 bcptr = bcptr->outer;
2749 }
2750 return TRUE;
2751 }
2752
2753
2754
2755 /*************************************************
2756 * Check for POSIX class syntax *
2757 *************************************************/
2758
2759 /* This function is called when the sequence "[:" or "[." or "[=" is
2760 encountered in a character class. It checks whether this is followed by a
2761 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
2762 reach an unescaped ']' without the special preceding character, return FALSE.
2763
2764 Originally, this function only recognized a sequence of letters between the
2765 terminators, but it seems that Perl recognizes any sequence of characters,
2766 though of course unknown POSIX names are subsequently rejected. Perl gives an
2767 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
2768 didn't consider this to be a POSIX class. Likewise for [:1234:].
2769
2770 The problem in trying to be exactly like Perl is in the handling of escapes. We
2771 have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
2772 class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
2773 below handles the special case of \], but does not try to do any other escape
2774 processing. This makes it different from Perl for cases such as [:l\ower:]
2775 where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
2776 "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,
2777 I think.
2778
2779 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
2780 It seems that the appearance of a nested POSIX class supersedes an apparent
2781 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
2782 a digit.
2783
2784 In Perl, unescaped square brackets may also appear as part of class names. For
2785 example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
2786 [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
2787 seem right at all. PCRE does not allow closing square brackets in POSIX class
2788 names.
2789
2790 Arguments:
2791 ptr pointer to the initial [
2792 endptr where to return the end pointer
2793
2794 Returns: TRUE or FALSE
2795 */
2796
2797 static BOOL
2798 check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2799 {
2800 pcre_uchar terminator; /* Don't combine these lines; the Solaris cc */
2801 terminator = *(++ptr); /* compiler warns about "non-constant" initializer. */
2802 for (++ptr; *ptr != CHAR_NULL; ptr++)
2803 {
2804 if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2805 ptr++;
2806 else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
2807 else
2808 {
2809 if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
2810 {
2811 *endptr = ptr;
2812 return TRUE;
2813 }
2814 if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
2815 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
2816 ptr[1] == CHAR_EQUALS_SIGN) &&
2817 check_posix_syntax(ptr, endptr))
2818 return FALSE;
2819 }
2820 }
2821 return FALSE;
2822 }
2823
2824
2825
2826
2827 /*************************************************
2828 * Check POSIX class name *
2829 *************************************************/
2830
2831 /* This function is called to check the name given in a POSIX-style class entry
2832 such as [:alnum:].
2833
2834 Arguments:
2835 ptr points to the first letter
2836 len the length of the name
2837
2838 Returns: a value representing the name, or -1 if unknown
2839 */
2840
2841 static int
2842 check_posix_name(const pcre_uchar *ptr, int len)
2843 {
2844 const char *pn = posix_names;
2845 register int yield = 0;
2846 while (posix_name_lengths[yield] != 0)
2847 {
2848 if (len == posix_name_lengths[yield] &&
2849 STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
2850 pn += posix_name_lengths[yield] + 1;
2851 yield++;
2852 }
2853 return -1;
2854 }
2855
2856
2857 /*************************************************
2858 * Adjust OP_RECURSE items in repeated group *
2859 *************************************************/
2860
2861 /* OP_RECURSE items contain an offset from the start of the regex to the group
2862 that is referenced. This means that groups can be replicated for fixed
2863 repetition simply by copying (because the recursion is allowed to refer to
2864 earlier groups that are outside the current group). However, when a group is
2865 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
2866 inserted before it, after it has been compiled. This means that any OP_RECURSE
2867 items within it that refer to the group itself or any contained groups have to
2868 have their offsets adjusted. That one of the jobs of this function. Before it
2869 is called, the partially compiled regex must be temporarily terminated with
2870 OP_END.
2871
2872 This function has been extended with the possibility of forward references for
2873 recursions and subroutine calls. It must also check the list of such references
2874 for the group we are dealing with. If it finds that one of the recursions in
2875 the current group is on this list, it adjusts the offset in the list, not the
2876 value in the reference (which is a group number).
2877
2878 Arguments:
2879 group points to the start of the group
2880 adjust the amount by which the group is to be moved
2881 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
2882 cd contains pointers to tables etc.
2883 save_hwm the hwm forward reference pointer at the start of the group
2884
2885 Returns: nothing
2886 */
2887
2888 static void
2889 adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2890 pcre_uchar *save_hwm)
2891 {
2892 pcre_uchar *ptr = group;
2893
2894 while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2895 {
2896 int offset;
2897 pcre_uchar *hc;
2898
2899 /* See if this recursion is on the forward reference list. If so, adjust the
2900 reference. */
2901
2902 for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
2903 {
2904 offset = (int)GET(hc, 0);
2905 if (cd->start_code + offset == ptr + 1)
2906 {
2907 PUT(hc, 0, offset + adjust);
2908 break;
2909 }
2910 }
2911
2912 /* Otherwise, adjust the recursion offset if it's after the start of this
2913 group. */
2914
2915 if (hc >= cd->hwm)
2916 {
2917 offset = (int)GET(ptr, 1);
2918 if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
2919 }
2920
2921 ptr += 1 + LINK_SIZE;
2922 }
2923 }
2924
2925
2926
2927 /*************************************************
2928 * Insert an automatic callout point *
2929 *************************************************/
2930
2931 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
2932 callout points before each pattern item.
2933
2934 Arguments:
2935 code current code pointer
2936 ptr current pattern pointer
2937 cd pointers to tables etc
2938
2939 Returns: new code pointer
2940 */
2941
2942 static pcre_uchar *
2943 auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2944 {
2945 *code++ = OP_CALLOUT;
2946 *code++ = 255;
2947 PUT(code, 0, (int)(ptr - cd->start_pattern)); /* Pattern offset */
2948 PUT(code, LINK_SIZE, 0); /* Default length */
2949 return code + 2 * LINK_SIZE;
2950 }
2951
2952
2953
2954 /*************************************************
2955 * Complete a callout item *
2956 *************************************************/
2957
2958 /* A callout item contains the length of the next item in the pattern, which
2959 we can't fill in till after we have reached the relevant point. This is used
2960 for both automatic and manual callouts.
2961
2962 Arguments:
2963 previous_callout points to previous callout item
2964 ptr current pattern pointer
2965 cd pointers to tables etc
2966
2967 Returns: nothing
2968 */
2969
2970 static void
2971 complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2972 {
2973 int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2974 PUT(previous_callout, 2 + LINK_SIZE, length);
2975 }
2976
2977
2978
2979 #ifdef SUPPORT_UCP
2980 /*************************************************
2981 * Get othercase range *
2982 *************************************************/
2983
2984 /* This function is passed the start and end of a class range, in UTF-8 mode
2985 with UCP support. It searches up the characters, looking for ranges of
2986 characters in the "other" case. Each call returns the next one, updating the
2987 start address. A character with multiple other cases is returned on its own
2988 with a special return value.
2989
2990 Arguments:
2991 cptr points to starting character value; updated
2992 d end value
2993 ocptr where to put start of othercase range
2994 odptr where to put end of othercase range
2995
2996 Yield: -1 when no more
2997 0 when a range is returned
2998 >0 the CASESET offset for char with multiple other cases
2999 in this case, ocptr contains the original
3000 */
3001
3002 static int
3003 get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
3004 pcre_uint32 *odptr)
3005 {
3006 pcre_uint32 c, othercase, next;
3007 unsigned int co;
3008
3009 /* Find the first character that has an other case. If it has multiple other
3010 cases, return its case offset value. */
3011
3012 for (c = *cptr; c <= d; c++)
3013 {
3014 if ((co = UCD_CASESET(c)) != 0)
3015 {
3016 *ocptr = c++; /* Character that has the set */
3017 *cptr = c; /* Rest of input range */
3018 return (int)co;
3019 }
3020 if ((othercase = UCD_OTHERCASE(c)) != c) break;
3021 }
3022
3023 if (c > d) return -1; /* Reached end of range */
3024
3025 *ocptr = othercase;
3026 next = othercase + 1;
3027
3028 for (++c; c <= d; c++)
3029 {
3030 if (UCD_OTHERCASE(c) != next) break;
3031 next++;
3032 }
3033
3034 *odptr = next - 1; /* End of othercase range */
3035 *cptr = c; /* Rest of input range */
3036 return 0;
3037 }
3038
3039
3040
3041 /*************************************************
3042 * Check a character and a property *
3043 *************************************************/
3044
3045 /* This function is called by check_auto_possessive() when a property item
3046 is adjacent to a fixed character.
3047
3048 Arguments:
3049 c the character
3050 ptype the property type
3051 pdata the data for the type
3052 negated TRUE if it's a negated property (\P or \p{^)
3053
3054 Returns: TRUE if auto-possessifying is OK
3055 */
3056
3057 static BOOL
3058 check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)
3059 {
3060 #ifdef SUPPORT_UCP
3061 const pcre_uint32 *p;
3062 #endif
3063
3064 const ucd_record *prop = GET_UCD(c);
3065
3066 switch(ptype)
3067 {
3068 case PT_LAMP:
3069 return (prop->chartype == ucp_Lu ||
3070 prop->chartype == ucp_Ll ||
3071 prop->chartype == ucp_Lt) == negated;
3072
3073 case PT_GC:
3074 return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
3075
3076 case PT_PC:
3077 return (pdata == prop->chartype) == negated;
3078
3079 case PT_SC:
3080 return (pdata == prop->script) == negated;
3081
3082 /* These are specials */
3083
3084 case PT_ALNUM:
3085 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
3086 PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
3087
3088 case PT_SPACE: /* Perl space */
3089 return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
3090 c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
3091 == negated;
3092
3093 case PT_PXSPACE: /* POSIX space */
3094 return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
3095 c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
3096 c == CHAR_FF || c == CHAR_CR)
3097 == negated;
3098
3099 case PT_WORD:
3100 return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
3101 PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
3102 c == CHAR_UNDERSCORE) == negated;
3103
3104 #ifdef SUPPORT_UCP
3105 case PT_CLIST:
3106 p = PRIV(ucd_caseless_sets) + prop->caseset;
3107 for (;;)
3108 {
3109 if (c < *p) return !negated;
3110 if (c == *p++) return negated;
3111 }
3112 break; /* Control never reaches here */
3113 #endif
3114 }
3115
3116 return FALSE;
3117 }
3118 #endif /* SUPPORT_UCP */
3119
3120
3121
3122 /*************************************************
3123 * Check if auto-possessifying is possible *
3124 *************************************************/
3125
3126 /* This function is called for unlimited repeats of certain items, to see
3127 whether the next thing could possibly match the repeated item. If not, it makes
3128 sense to automatically possessify the repeated item.
3129
3130 Arguments:
3131 previous pointer to the repeated opcode
3132 utf TRUE in UTF-8 / UTF-16 / UTF-32 mode
3133 ptr next character in pattern
3134 options options bits
3135 cd contains pointers to tables etc.
3136
3137 Returns: TRUE if possessifying is wanted
3138 */
3139
3140 static BOOL
3141 check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3142 const pcre_uchar *ptr, int options, compile_data *cd)
3143 {
3144 pcre_uint32 c = NOTACHAR;
3145 pcre_uint32 next;
3146 int escape;
3147 pcre_uchar op_code = *previous++;
3148
3149 /* Skip whitespace and comments in extended mode */
3150
3151 if ((options & PCRE_EXTENDED) != 0)
3152 {
3153 for (;;)
3154 {
3155 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3156 if (*ptr == CHAR_NUMBER_SIGN)
3157 {
3158 ptr++;
3159 while (*ptr != CHAR_NULL)
3160 {
3161 if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3162 ptr++;
3163 #ifdef SUPPORT_UTF
3164 if (utf) FORWARDCHAR(ptr);
3165 #endif
3166 }
3167 }
3168 else break;
3169 }
3170 }
3171
3172 /* If the next item is one that we can handle, get its value. A non-negative
3173 value is a character, a negative value is an escape value. */
3174
3175 if (*ptr == CHAR_BACKSLASH)
3176 {
3177 int temperrorcode = 0;
3178 escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,
3179 FALSE);
3180 if (temperrorcode != 0) return FALSE;
3181 ptr++; /* Point after the escape sequence */
3182 }
3183 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
3184 {
3185 escape = 0;
3186 #ifdef SUPPORT_UTF
3187 if (utf) { GETCHARINC(next, ptr); } else
3188 #endif
3189 next = *ptr++;
3190 }
3191 else return FALSE;
3192
3193 /* Skip whitespace and comments in extended mode */
3194
3195 if ((options & PCRE_EXTENDED) != 0)
3196 {
3197 for (;;)
3198 {
3199 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3200 if (*ptr == CHAR_NUMBER_SIGN)
3201 {
3202 ptr++;
3203 while (*ptr != CHAR_NULL)
3204 {
3205 if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3206 ptr++;
3207 #ifdef SUPPORT_UTF
3208 if (utf) FORWARDCHAR(ptr);
3209 #endif
3210 }
3211 }
3212 else break;
3213 }
3214 }
3215
3216 /* If the next thing is itself optional, we have to give up. */
3217
3218 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3219 STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3220 return FALSE;
3221
3222 /* If the previous item is a character, get its value. */
3223
3224 if (op_code == OP_CHAR || op_code == OP_CHARI ||
3225 op_code == OP_NOT || op_code == OP_NOTI)
3226 {
3227 #ifdef SUPPORT_UTF
3228 GETCHARTEST(c, previous);
3229 #else
3230 c = *previous;
3231 #endif
3232 }
3233
3234 /* Now compare the next item with the previous opcode. First, handle cases when
3235 the next item is a character. */
3236
3237 if (escape == 0)
3238 {
3239 /* For a caseless UTF match, the next character may have more than one other
3240 case, which maps to the special PT_CLIST property. Check this first. */
3241
3242 #ifdef SUPPORT_UCP
3243 if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)
3244 {
3245 unsigned int ocs = UCD_CASESET(next);
3246 if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);
3247 }
3248 #endif
3249
3250 switch(op_code)
3251 {
3252 case OP_CHAR:
3253 return c != next;
3254
3255 /* For CHARI (caseless character) we must check the other case. If we have
3256 Unicode property support, we can use it to test the other case of
3257 high-valued characters. We know that next can have only one other case,
3258 because multi-other-case characters are dealt with above. */
3259
3260 case OP_CHARI:
3261 if (c == next) return FALSE;
3262 #ifdef SUPPORT_UTF
3263 if (utf)
3264 {
3265 pcre_uint32 othercase;
3266 if (next < 128) othercase = cd->fcc[next]; else
3267 #ifdef SUPPORT_UCP
3268 othercase = UCD_OTHERCASE(next);
3269 #else
3270 othercase = NOTACHAR;
3271 #endif
3272 return c != othercase;
3273 }
3274 else
3275 #endif /* SUPPORT_UTF */
3276 return (c != TABLE_GET(next, cd->fcc, next)); /* Not UTF */
3277
3278 case OP_NOT:
3279 return c == next;
3280
3281 case OP_NOTI:
3282 if (c == next) return TRUE;
3283 #ifdef SUPPORT_UTF
3284 if (utf)
3285 {
3286 pcre_uint32 othercase;
3287 if (next < 128) othercase = cd->fcc[next]; else
3288 #ifdef SUPPORT_UCP
3289 othercase = UCD_OTHERCASE(next);
3290 #else
3291 othercase = NOTACHAR;
3292 #endif
3293 return c == othercase;
3294 }
3295 else
3296 #endif /* SUPPORT_UTF */
3297 return (c == TABLE_GET(next, cd->fcc, next)); /* Not UTF */
3298
3299 /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3300 When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3301
3302 case OP_DIGIT:
3303 return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3304
3305 case OP_NOT_DIGIT:
3306 return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3307
3308 case OP_WHITESPACE:
3309 return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3310
3311 case OP_NOT_WHITESPACE:
3312 return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3313
3314 case OP_WORDCHAR:
3315 return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3316
3317 case OP_NOT_WORDCHAR:
3318 return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3319
3320 case OP_HSPACE:
3321 case OP_NOT_HSPACE:
3322 switch(next)
3323 {
3324 HSPACE_CASES:
3325 return op_code == OP_NOT_HSPACE;
3326
3327 default:
3328 return op_code != OP_NOT_HSPACE;
3329 }
3330
3331 case OP_ANYNL:
3332 case OP_VSPACE:
3333 case OP_NOT_VSPACE:
3334 switch(next)
3335 {
3336 VSPACE_CASES:
3337 return op_code == OP_NOT_VSPACE;
3338
3339 default:
3340 return op_code != OP_NOT_VSPACE;
3341 }
3342
3343 #ifdef SUPPORT_UCP
3344 case OP_PROP:
3345 return check_char_prop(next, previous[0], previous[1], FALSE);
3346
3347 case OP_NOTPROP:
3348 return check_char_prop(next, previous[0], previous[1], TRUE);
3349 #endif
3350
3351 default:
3352 return FALSE;
3353 }
3354 }
3355
3356 /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3357 is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
3358 generated only when PCRE_UCP is *not* set, that is, when only ASCII
3359 characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are
3360 replaced by OP_PROP codes when PCRE_UCP is set. */
3361
3362 switch(op_code)
3363 {
3364 case OP_CHAR:
3365 case OP_CHARI:
3366 switch(escape)
3367 {
3368 case ESC_d:
3369 return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3370
3371 case ESC_D:
3372 return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3373
3374 case ESC_s:
3375 return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3376
3377 case ESC_S:
3378 return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3379
3380 case ESC_w:
3381 return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3382
3383 case ESC_W:
3384 return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3385
3386 case ESC_h:
3387 case ESC_H:
3388 switch(c)
3389 {
3390 HSPACE_CASES:
3391 return escape != ESC_h;
3392
3393 default:
3394 return escape == ESC_h;
3395 }
3396
3397 case ESC_v:
3398 case ESC_V:
3399 switch(c)
3400 {
3401 VSPACE_CASES:
3402 return escape != ESC_v;
3403
3404 default:
3405 return escape == ESC_v;
3406 }
3407
3408 /* When PCRE_UCP is set, these values get generated for \d etc. Find
3409 their substitutions and process them. The result will always be either
3410 ESC_p or ESC_P. Then fall through to process those values. */
3411
3412 #ifdef SUPPORT_UCP
3413 case ESC_du:
3414 case ESC_DU:
3415 case ESC_wu:
3416 case ESC_WU:
3417 case ESC_su:
3418 case ESC_SU:
3419 {
3420 int temperrorcode = 0;
3421 ptr = substitutes[escape - ESC_DU];
3422 escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);
3423 if (temperrorcode != 0) return FALSE;
3424 ptr++; /* For compatibility */
3425 }
3426 /* Fall through */
3427
3428 case ESC_p:
3429 case ESC_P:
3430 {
3431 unsigned int ptype = 0, pdata = 0;
3432 int errorcodeptr;
3433 BOOL negated;
3434
3435 ptr--; /* Make ptr point at the p or P */
3436 if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))
3437 return FALSE;
3438 ptr++; /* Point past the final curly ket */
3439
3440 /* If the property item is optional, we have to give up. (When generated
3441 from \d etc by PCRE_UCP, this test will have been applied much earlier,
3442 to the original \d etc. At this point, ptr will point to a zero byte. */
3443
3444 if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3445 STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3446 return FALSE;
3447
3448 /* Do the property check. */
3449
3450 return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);
3451 }
3452 #endif
3453
3454 default:
3455 return FALSE;
3456 }
3457
3458 /* In principle, support for Unicode properties should be integrated here as
3459 well. It means re-organizing the above code so as to get hold of the property
3460 values before switching on the op-code. However, I wonder how many patterns
3461 combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,
3462 these op-codes are never generated.) */
3463
3464 case OP_DIGIT:
3465 return escape == ESC_D || escape == ESC_s || escape == ESC_W ||
3466 escape == ESC_h || escape == ESC_v || escape == ESC_R;
3467
3468 case OP_NOT_DIGIT:
3469 return escape == ESC_d;
3470
3471 case OP_WHITESPACE:
3472 return escape == ESC_S || escape == ESC_d || escape == ESC_w;
3473
3474 case OP_NOT_WHITESPACE:
3475 return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;
3476
3477 case OP_HSPACE:
3478 return escape == ESC_S || escape == ESC_H || escape == ESC_d ||
3479 escape == ESC_w || escape == ESC_v || escape == ESC_R;
3480
3481 case OP_NOT_HSPACE:
3482 return escape == ESC_h;
3483
3484 /* Can't have \S in here because VT matches \S (Perl anomaly) */
3485 case OP_ANYNL:
3486 case OP_VSPACE:
3487 return escape == ESC_V || escape == ESC_d || escape == ESC_w;
3488
3489 case OP_NOT_VSPACE:
3490 return escape == ESC_v || escape == ESC_R;
3491
3492 case OP_WORDCHAR:
3493 return escape == ESC_W || escape == ESC_s || escape == ESC_h ||
3494 escape == ESC_v || escape == ESC_R;
3495
3496 case OP_NOT_WORDCHAR:
3497 return escape == ESC_w || escape == ESC_d;
3498
3499 default:
3500 return FALSE;
3501 }
3502
3503 /* Control does not reach here */
3504 }
3505
3506
3507
3508 /*************************************************
3509 * Add a character or range to a class *
3510 *************************************************/
3511
3512 /* This function packages up the logic of adding a character or range of
3513 characters to a class. The character values in the arguments will be within the
3514 valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3515 mutually recursive with the function immediately below.
3516
3517 Arguments:
3518 classbits the bit map for characters < 256
3519 uchardptr points to the pointer for extra data
3520 options the options word
3521 cd contains pointers to tables etc.
3522 start start of range character
3523 end end of range character
3524
3525 Returns: the number of < 256 characters added
3526 the pointer to extra data is updated
3527 */
3528
3529 static int
3530 add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3531 compile_data *cd, pcre_uint32 start, pcre_uint32 end)
3532 {
3533 pcre_uint32 c;
3534 int n8 = 0;
3535
3536 /* If caseless matching is required, scan the range and process alternate
3537 cases. In Unicode, there are 8-bit characters that have alternate cases that
3538 are greater than 255 and vice-versa. Sometimes we can just extend the original
3539 range. */
3540
3541 if ((options & PCRE_CASELESS) != 0)
3542 {
3543 #ifdef SUPPORT_UCP
3544 if ((options & PCRE_UTF8) != 0)
3545 {
3546 int rc;
3547 pcre_uint32 oc, od;
3548
3549 options &= ~PCRE_CASELESS; /* Remove for recursive calls */
3550 c = start;
3551
3552 while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3553 {
3554 /* Handle a single character that has more than one other case. */
3555
3556 if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3557 PRIV(ucd_caseless_sets) + rc, oc);
3558
3559 /* Do nothing if the other case range is within the original range. */
3560
3561 else if (oc >= start && od <= end) continue;
3562
3563 /* Extend the original range if there is overlap, noting that if oc < c, we
3564 can't have od > end because a subrange is always shorter than the basic
3565 range. Otherwise, use a recursive call to add the additional range. */
3566
3567 else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3568 else if (od > end && oc <= end + 1) end = od; /* Extend upwards */
3569 else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3570 }
3571 }
3572 else
3573 #endif /* SUPPORT_UCP */
3574
3575 /* Not UTF-mode, or no UCP */
3576
3577 for (c = start; c <= end && c < 256; c++)
3578 {
3579 SETBIT(classbits, cd->fcc[c]);
3580 n8++;
3581 }
3582 }
3583
3584 /* Now handle the original range. Adjust the final value according to the bit
3585 length - this means that the same lists of (e.g.) horizontal spaces can be used
3586 in all cases. */
3587
3588 #if defined COMPILE_PCRE8
3589 #ifdef SUPPORT_UTF
3590 if ((options & PCRE_UTF8) == 0)
3591 #endif
3592 if (end > 0xff) end = 0xff;
3593
3594 #elif defined COMPILE_PCRE16
3595 #ifdef SUPPORT_UTF
3596 if ((options & PCRE_UTF16) == 0)
3597 #endif
3598 if (end > 0xffff) end = 0xffff;
3599
3600 #endif /* COMPILE_PCRE[8|16] */
3601
3602 /* If all characters are less than 256, use the bit map. Otherwise use extra
3603 data. */
3604
3605 if (end < 0x100)
3606 {
3607 for (c = start; c <= end; c++)
3608 {
3609 n8++;
3610 SETBIT(classbits, c);
3611 }
3612 }
3613
3614 else
3615 {
3616 pcre_uchar *uchardata = *uchardptr;
3617
3618 #ifdef SUPPORT_UTF
3619 if ((options & PCRE_UTF8) != 0) /* All UTFs use the same flag bit */
3620 {
3621 if (start < end)
3622 {
3623 *uchardata++ = XCL_RANGE;
3624 uchardata += PRIV(ord2utf)(start, uchardata);
3625 uchardata += PRIV(ord2utf)(end, uchardata);
3626 }
3627 else if (start == end)
3628 {
3629 *uchardata++ = XCL_SINGLE;
3630 uchardata += PRIV(ord2utf)(start, uchardata);
3631 }
3632 }
3633 else
3634 #endif /* SUPPORT_UTF */
3635
3636 /* Without UTF support, character values are constrained by the bit length,
3637 and can only be > 256 for 16-bit and 32-bit libraries. */
3638
3639 #ifdef COMPILE_PCRE8
3640 {}
3641 #else
3642 if (start < end)
3643 {
3644 *uchardata++ = XCL_RANGE;
3645 *uchardata++ = start;
3646 *uchardata++ = end;
3647 }
3648 else if (start == end)
3649 {
3650 *uchardata++ = XCL_SINGLE;
3651 *uchardata++ = start;
3652 }
3653 #endif
3654
3655 *uchardptr = uchardata; /* Updata extra data pointer */
3656 }
3657
3658 return n8; /* Number of 8-bit characters */
3659 }
3660
3661
3662
3663
3664 /*************************************************
3665 * Add a list of characters to a class *
3666 *************************************************/
3667
3668 /* This function is used for adding a list of case-equivalent characters to a
3669 class, and also for adding a list of horizontal or vertical whitespace. If the
3670 list is in order (which it should be), ranges of characters are detected and
3671 handled appropriately. This function is mutually recursive with the function
3672 above.
3673
3674 Arguments:
3675 classbits the bit map for characters < 256
3676 uchardptr points to the pointer for extra data
3677 options the options word
3678 cd contains pointers to tables etc.
3679 p points to row of 32-bit values, terminated by NOTACHAR
3680 except character to omit; this is used when adding lists of
3681 case-equivalent characters to avoid including the one we
3682 already know about
3683
3684 Returns: the number of < 256 characters added
3685 the pointer to extra data is updated
3686 */
3687
3688 static int
3689 add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3690 compile_data *cd, const pcre_uint32 *p, unsigned int except)
3691 {
3692 int n8 = 0;
3693 while (p[0] < NOTACHAR)
3694 {
3695 int n = 0;
3696 if (p[0] != except)
3697 {
3698 while(p[n+1] == p[0] + n + 1) n++;
3699 n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3700 }
3701 p += n + 1;
3702 }
3703 return n8;
3704 }
3705
3706
3707
3708 /*************************************************
3709 * Add characters not in a list to a class *
3710 *************************************************/
3711
3712 /* This function is used for adding the complement of a list of horizontal or
3713 vertical whitespace to a class. The list must be in order.
3714
3715 Arguments:
3716 classbits the bit map for characters < 256
3717 uchardptr points to the pointer for extra data
3718 options the options word
3719 cd contains pointers to tables etc.
3720 p points to row of 32-bit values, terminated by NOTACHAR
3721
3722 Returns: the number of < 256 characters added
3723 the pointer to extra data is updated
3724 */
3725
3726 static int
3727 add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3728 int options, compile_data *cd, const pcre_uint32 *p)
3729 {
3730 BOOL utf = (options & PCRE_UTF8) != 0;
3731 int n8 = 0;
3732 if (p[0] > 0)
3733 n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3734 while (p[0] < NOTACHAR)
3735 {
3736 while (p[1] == p[0] + 1) p++;
3737 n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3738 (p[1] == NOTACHAR) ? (utf ? 0x10ffffu : 0xffffffffu) : p[1] - 1);
3739 p++;
3740 }
3741 return n8;
3742 }
3743
3744
3745
3746 /*************************************************
3747 * Compile one branch *
3748 *************************************************/
3749
3750 /* Scan the pattern, compiling it into the a vector. If the options are
3751 changed during the branch, the pointer is used to change the external options
3752 bits. This function is used during the pre-compile phase when we are trying
3753 to find out the amount of memory needed, as well as during the real compile
3754 phase. The value of lengthptr distinguishes the two phases.
3755
3756 Arguments:
3757 optionsptr pointer to the option bits
3758 codeptr points to the pointer to the current code point
3759 ptrptr points to the current pattern pointer
3760 errorcodeptr points to error code variable
3761 firstcharptr place to put the first required character
3762 firstcharflagsptr place to put the first character flags, or a negative number
3763 reqcharptr place to put the last required character
3764 reqcharflagsptr place to put the last required character flags, or a negative number
3765 bcptr points to current branch chain
3766 cond_depth conditional nesting depth
3767 cd contains pointers to tables etc.
3768 lengthptr NULL during the real compile phase
3769 points to length accumulator during pre-compile phase
3770
3771 Returns: TRUE on success
3772 FALSE, with *errorcodeptr set non-zero on error
3773 */
3774
3775 static BOOL
3776 compile_branch(int *optionsptr, pcre_uchar **codeptr,
3777 const pcre_uchar **ptrptr, int *errorcodeptr,
3778 pcre_uint32 *firstcharptr, pcre_int32 *firstcharflagsptr,
3779 pcre_uint32 *reqcharptr, pcre_int32 *reqcharflagsptr,
3780 branch_chain *bcptr, int cond_depth,
3781 compile_data *cd, int *lengthptr)
3782 {
3783 int repeat_type, op_type;
3784 int repeat_min = 0, repeat_max = 0; /* To please picky compilers */
3785 int bravalue = 0;
3786 int greedy_default, greedy_non_default;
3787 pcre_uint32 firstchar, reqchar;
3788 pcre_int32 firstcharflags, reqcharflags;
3789 pcre_uint32 zeroreqchar, zerofirstchar;
3790 pcre_int32 zeroreqcharflags, zerofirstcharflags;
3791 pcre_int32 req_caseopt, reqvary, tempreqvary;
3792 int options = *optionsptr; /* May change dynamically */
3793 int after_manual_callout = 0;
3794 int length_prevgroup = 0;
3795 register pcre_uint32 c;
3796 int escape;
3797 register pcre_uchar *code = *codeptr;
3798 pcre_uchar *last_code = code;
3799 pcre_uchar *orig_code = code;
3800 pcre_uchar *tempcode;
3801 BOOL inescq = FALSE;
3802 BOOL groupsetfirstchar = FALSE;
3803 const pcre_uchar *ptr = *ptrptr;
3804 const pcre_uchar *tempptr;
3805 const pcre_uchar *nestptr = NULL;
3806 pcre_uchar *previous = NULL;
3807 pcre_uchar *previous_callout = NULL;
3808 pcre_uchar *save_hwm = NULL;
3809 pcre_uint8 classbits[32];
3810
3811 /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3812 must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3813 dynamically as we process the pattern. */
3814
3815 #ifdef SUPPORT_UTF
3816 /* PCRE_UTF[16|32] have the same value as PCRE_UTF8. */
3817 BOOL utf = (options & PCRE_UTF8) != 0;
3818 #ifndef COMPILE_PCRE32
3819 pcre_uchar utf_chars[6];
3820 #endif
3821 #else
3822 BOOL utf = FALSE;
3823 #endif
3824
3825 /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3826 class_uchardata always so that it can be passed to add_to_class() always,
3827 though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3828 alternative calls for the different cases. */
3829
3830 pcre_uchar *class_uchardata;
3831 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3832 BOOL xclass;
3833 pcre_uchar *class_uchardata_base;
3834 #endif
3835
3836 #ifdef PCRE_DEBUG
3837 if (lengthptr != NULL) DPRINTF((">> start branch\n"));
3838 #endif
3839
3840 /* Set up the default and non-default settings for greediness */
3841
3842 greedy_default = ((options & PCRE_UNGREEDY) != 0);
3843 greedy_non_default = greedy_default ^ 1;
3844
3845 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3846 matching encountered yet". It gets changed to REQ_NONE if we hit something that
3847 matches a non-fixed char first char; reqchar just remains unset if we never
3848 find one.
3849
3850 When we hit a repeat whose minimum is zero, we may have to adjust these values
3851 to take the zero repeat into account. This is implemented by setting them to
3852 zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3853 item types that can be repeated set these backoff variables appropriately. */
3854
3855 firstchar = reqchar = zerofirstchar = zeroreqchar = 0;
3856 firstcharflags = reqcharflags = zerofirstcharflags = zeroreqcharflags = REQ_UNSET;
3857
3858 /* The variable req_caseopt contains either the REQ_CASELESS value
3859 or zero, according to the current setting of the caseless flag. The
3860 REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3861 firstchar or reqchar variables to record the case status of the
3862 value. This is used only for ASCII characters. */
3863
3864 req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3865
3866 /* Switch on next character until the end of the branch */
3867
3868 for (;; ptr++)
3869 {
3870 BOOL negate_class;
3871 BOOL should_flip_negation;
3872 BOOL possessive_quantifier;
3873 BOOL is_quantifier;
3874 BOOL is_recurse;
3875 BOOL reset_bracount;
3876 int class_has_8bitchar;
3877 int class_one_char;
3878 int newoptions;
3879 int recno;
3880 int refsign;
3881 int skipbytes;
3882 pcre_uint32 subreqchar, subfirstchar;
3883 pcre_int32 subreqcharflags, subfirstcharflags;
3884 int terminator;
3885 unsigned int mclength;
3886 unsigned int tempbracount;
3887 pcre_uint32 ec;
3888 pcre_uchar mcbuffer[8];
3889
3890 /* Get next character in the pattern */
3891
3892 c = *ptr;
3893
3894 /* If we are at the end of a nested substitution, revert to the outer level
3895 string. Nesting only happens one level deep. */
3896
3897 if (c == CHAR_NULL && nestptr != NULL)
3898 {
3899 ptr = nestptr;
3900 nestptr = NULL;
3901 c = *ptr;
3902 }
3903
3904 /* If we are in the pre-compile phase, accumulate the length used for the
3905 previous cycle of this loop. */
3906
3907 if (lengthptr != NULL)
3908 {
3909 #ifdef PCRE_DEBUG
3910 if (code > cd->hwm) cd->hwm = code; /* High water info */
3911 #endif
3912 if (code > cd->start_workspace + cd->workspace_size -
3913 WORK_SIZE_SAFETY_MARGIN) /* Check for overrun */
3914 {
3915 *errorcodeptr = ERR52;
3916 goto FAILED;
3917 }
3918
3919 /* There is at least one situation where code goes backwards: this is the
3920 case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
3921 the class is simply eliminated. However, it is created first, so we have to
3922 allow memory for it. Therefore, don't ever reduce the length at this point.
3923 */
3924
3925 if (code < last_code) code = last_code;
3926
3927 /* Paranoid check for integer overflow */
3928
3929 if (OFLOW_MAX - *lengthptr < code - last_code)
3930 {
3931 *errorcodeptr = ERR20;
3932 goto FAILED;
3933 }
3934
3935 *lengthptr += (int)(code - last_code);
3936 DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3937 (int)(code - last_code), c, c));
3938
3939 /* If "previous" is set and it is not at the start of the work space, move
3940 it back to there, in order to avoid filling up the work space. Otherwise,
3941 if "previous" is NULL, reset the current code pointer to the start. */
3942
3943 if (previous != NULL)
3944 {
3945 if (previous > orig_code)
3946 {
3947 memmove(orig_code, previous, IN_UCHARS(code - previous));
3948 code -= previous - orig_code;
3949 previous = orig_code;
3950 }
3951 }
3952 else code = orig_code;
3953
3954 /* Remember where this code item starts so we can pick up the length
3955 next time round. */
3956
3957 last_code = code;
3958 }
3959
3960 /* In the real compile phase, just check the workspace used by the forward
3961 reference list. */
3962
3963 else if (cd->hwm > cd->start_workspace + cd->workspace_size -
3964 WORK_SIZE_SAFETY_MARGIN)
3965 {
3966 *errorcodeptr = ERR52;
3967 goto FAILED;
3968 }
3969
3970 /* If in \Q...\E, check for the end; if not, we have a literal */
3971
3972 if (inescq && c != CHAR_NULL)
3973 {
3974 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E)
3975 {
3976 inescq = FALSE;
3977 ptr++;
3978 continue;
3979 }
3980 else
3981 {
3982 if (previous_callout != NULL)
3983 {
3984 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
3985 complete_callout(previous_callout, ptr, cd);
3986 previous_callout = NULL;
3987 }
3988 if ((options & PCRE_AUTO_CALLOUT) != 0)
3989 {
3990 previous_callout = code;
3991 code = auto_callout(code, ptr, cd);
3992 }
3993 goto NORMAL_CHAR;
3994 }
3995 }
3996
3997 /* Fill in length of a previous callout, except when the next thing is
3998 a quantifier. */
3999
4000 is_quantifier =
4001 c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4002 (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4003
4004 if (!is_quantifier && previous_callout != NULL &&
4005 after_manual_callout-- <= 0)
4006 {
4007 if (lengthptr == NULL) /* Don't attempt in pre-compile phase */
4008 complete_callout(previous_callout, ptr, cd);
4009 previous_callout = NULL;
4010 }
4011
4012 /* In extended mode, skip white space and comments. */
4013
4014 if ((options & PCRE_EXTENDED) != 0)
4015 {
4016 if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
4017 if (c == CHAR_NUMBER_SIGN)
4018 {
4019 ptr++;
4020 while (*ptr != CHAR_NULL)
4021 {
4022 if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
4023 ptr++;
4024 #ifdef SUPPORT_UTF
4025 if (utf) FORWARDCHAR(ptr);
4026 #endif
4027 }
4028 if (*ptr != CHAR_NULL) continue;
4029
4030 /* Else fall through to handle end of string */
4031 c = 0;
4032 }
4033 }
4034
4035 /* No auto callout for quantifiers. */
4036
4037 if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)
4038 {
4039 previous_callout = code;
4040 code = auto_callout(code, ptr, cd);
4041 }
4042
4043 switch(c)
4044 {
4045 /* ===================================================================*/
4046 case 0: /* The branch terminates at string end */
4047 case CHAR_VERTICAL_LINE: /* or | or ) */
4048 case CHAR_RIGHT_PARENTHESIS:
4049 *firstcharptr = firstchar;
4050 *firstcharflagsptr = firstcharflags;
4051 *reqcharptr = reqchar;
4052 *reqcharflagsptr = reqcharflags;
4053 *codeptr = code;
4054 *ptrptr = ptr;
4055 if (lengthptr != NULL)
4056 {
4057 if (OFLOW_MAX - *lengthptr < code - last_code)
4058 {
4059 *errorcodeptr = ERR20;
4060 goto FAILED;
4061 }
4062 *lengthptr += (int)(code - last_code); /* To include callout length */
4063 DPRINTF((">> end branch\n"));
4064 }
4065 return TRUE;
4066
4067
4068 /* ===================================================================*/
4069 /* Handle single-character metacharacters. In multiline mode, ^ disables
4070 the setting of any following char as a first character. */
4071
4072 case CHAR_CIRCUMFLEX_ACCENT:
4073 previous = NULL;
4074 if ((options & PCRE_MULTILINE) != 0)
4075 {
4076 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4077 *code++ = OP_CIRCM;
4078 }
4079 else *code++ = OP_CIRC;
4080 break;
4081
4082 case CHAR_DOLLAR_SIGN:
4083 previous = NULL;
4084 *code++ = ((options & PCRE_MULTILINE) != 0)? OP_DOLLM : OP_DOLL;
4085 break;
4086
4087 /* There can never be a first char if '.' is first, whatever happens about
4088 repeats. The value of reqchar doesn't change either. */
4089
4090 case CHAR_DOT:
4091 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4092 zerofirstchar = firstchar;
4093 zerofirstcharflags = firstcharflags;
4094 zeroreqchar = reqchar;
4095 zeroreqcharflags = reqcharflags;
4096 previous = code;
4097 *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
4098 break;
4099
4100
4101 /* ===================================================================*/
4102 /* Character classes. If the included characters are all < 256, we build a
4103 32-byte bitmap of the permitted characters, except in the special case
4104 where there is only one such character. For negated classes, we build the
4105 map as usual, then invert it at the end. However, we use a different opcode
4106 so that data characters > 255 can be handled correctly.
4107
4108 If the class contains characters outside the 0-255 range, a different
4109 opcode is compiled. It may optionally have a bit map for characters < 256,
4110 but those above are are explicitly listed afterwards. A flag byte tells
4111 whether the bitmap is present, and whether this is a negated class or not.
4112
4113 In JavaScript compatibility mode, an isolated ']' causes an error. In
4114 default (Perl) mode, it is treated as a data character. */
4115
4116 case CHAR_RIGHT_SQUARE_BRACKET:
4117 if ((cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4118 {
4119 *errorcodeptr = ERR64;
4120 goto FAILED;
4121 }
4122 goto NORMAL_CHAR;
4123
4124 case CHAR_LEFT_SQUARE_BRACKET:
4125 previous = code;
4126
4127 /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
4128 they are encountered at the top level, so we'll do that too. */
4129
4130 if ((ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
4131 ptr[1] == CHAR_EQUALS_SIGN) &&
4132 check_posix_syntax(ptr, &tempptr))
4133 {
4134 *errorcodeptr = (ptr[1] == CHAR_COLON)? ERR13 : ERR31;
4135 goto FAILED;
4136 }
4137
4138 /* If the first character is '^', set the negation flag and skip it. Also,
4139 if the first few characters (either before or after ^) are \Q\E or \E we
4140 skip them too. This makes for compatibility with Perl. */
4141
4142 negate_class = FALSE;
4143 for (;;)
4144 {
4145 c = *(++ptr);
4146 if (c == CHAR_BACKSLASH)
4147 {
4148 if (ptr[1] == CHAR_E)
4149 ptr++;
4150 else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
4151 ptr += 3;
4152 else
4153 break;
4154 }
4155 else if (!negate_class && c == CHAR_CIRCUMFLEX_ACCENT)
4156 negate_class = TRUE;
4157 else break;
4158 }
4159
4160 /* Empty classes are allowed in JavaScript compatibility mode. Otherwise,
4161 an initial ']' is taken as a data character -- the code below handles
4162 that. In JS mode, [] must always fail, so generate OP_FAIL, whereas
4163 [^] must match any character, so generate OP_ALLANY. */
4164
4165 if (c == CHAR_RIGHT_SQUARE_BRACKET &&
4166 (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4167 {
4168 *code++ = negate_class? OP_ALLANY : OP_FAIL;
4169 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4170 zerofirstchar = firstchar;
4171 zerofirstcharflags = firstcharflags;
4172 break;
4173 }
4174
4175 /* If a class contains a negative special such as \S, we need to flip the
4176 negation flag at the end, so that support for characters > 255 works
4177 correctly (they are all included in the class). */
4178
4179 should_flip_negation = FALSE;
4180
4181 /* For optimization purposes, we track some properties of the class:
4182 class_has_8bitchar will be non-zero if the class contains at least one <
4183 256 character; class_one_char will be 1 if the class contains just one
4184 character. */
4185
4186 class_has_8bitchar = 0;
4187 class_one_char = 0;
4188
4189 /* Initialize the 32-char bit map to all zeros. We build the map in a
4190 temporary bit of memory, in case the class contains fewer than two
4191 8-bit characters because in that case the compiled code doesn't use the bit
4192 map. */
4193
4194 memset(classbits, 0, 32 * sizeof(pcre_uint8));
4195
4196 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4197 xclass = FALSE;
4198 class_uchardata = code + LINK_SIZE + 2; /* For XCLASS items */
4199 class_uchardata_base = class_uchardata; /* Save the start */
4200 #endif
4201
4202 /* Process characters until ] is reached. By writing this as a "do" it
4203 means that an initial ] is taken as a data character. At the start of the
4204 loop, c contains the first byte of the character. */
4205
4206 if (c != CHAR_NULL) do
4207 {
4208 const pcre_uchar *oldptr;
4209
4210 #ifdef SUPPORT_UTF
4211 if (utf && HAS_EXTRALEN(c))
4212 { /* Braces are required because the */
4213 GETCHARLEN(c, ptr, ptr); /* macro generates multiple statements */
4214 }
4215 #endif
4216
4217 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4218 /* In the pre-compile phase, accumulate the length of any extra
4219 data and reset the pointer. This is so that very large classes that
4220 contain a zillion > 255 characters no longer overwrite the work space
4221 (which is on the stack). We have to remember that there was XCLASS data,
4222 however. */
4223
4224 if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4225 {
4226 xclass = TRUE;
4227 *lengthptr += class_uchardata - class_uchardata_base;
4228 class_uchardata = class_uchardata_base;
4229 }
4230 #endif
4231
4232 /* Inside \Q...\E everything is literal except \E */
4233
4234 if (inescq)
4235 {
4236 if (c == CHAR_BACKSLASH && ptr[1] == CHAR_E) /* If we are at \E */
4237 {
4238 inescq = FALSE; /* Reset literal state */
4239 ptr++; /* Skip the 'E' */
4240 continue; /* Carry on with next */
4241 }
4242 goto CHECK_RANGE; /* Could be range if \E follows */
4243 }
4244
4245 /* Handle POSIX class names. Perl allows a negation extension of the
4246 form [:^name:]. A square bracket that doesn't match the syntax is
4247 treated as a literal. We also recognize the POSIX constructions
4248 [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
4249 5.6 and 5.8 do. */
4250
4251 if (c == CHAR_LEFT_SQUARE_BRACKET &&
4252 (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
4253 ptr[1] == CHAR_EQUALS_SIGN) && check_posix_syntax(ptr, &tempptr))
4254 {
4255 BOOL local_negate = FALSE;
4256 int posix_class, taboffset, tabopt;
4257 register const pcre_uint8 *cbits = cd->cbits;
4258 pcre_uint8 pbits[32];
4259
4260 if (ptr[1] != CHAR_COLON)
4261 {
4262 *errorcodeptr = ERR31;
4263 goto FAILED;
4264 }
4265
4266 ptr += 2;
4267 if (*ptr == CHAR_CIRCUMFLEX_ACCENT)
4268 {
4269 local_negate = TRUE;
4270 should_flip_negation = TRUE; /* Note negative special */
4271 ptr++;
4272 }
4273
4274 posix_class = check_posix_name(ptr, (int)(tempptr - ptr));
4275 if (posix_class < 0)
4276 {
4277 *errorcodeptr = ERR30;
4278 goto FAILED;
4279 }
4280
4281 /* If matching is caseless, upper and lower are converted to
4282 alpha. This relies on the fact that the class table starts with
4283 alpha, lower, upper as the first 3 entries. */
4284
4285 if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4286 posix_class = 0;
4287
4288 /* When PCRE_UCP is set, some of the POSIX classes are converted to
4289 different escape sequences that use Unicode properties. */
4290
4291 #ifdef SUPPORT_UCP
4292 if ((options & PCRE_UCP) != 0)
4293 {
4294 int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
4295 if (posix_substitutes[pc] != NULL)
4296 {
4297 nestptr = tempptr + 1;
4298 ptr = posix_substitutes[pc] - 1;
4299 continue;
4300 }
4301 }
4302 #endif
4303 /* In the non-UCP case, we build the bit map for the POSIX class in a
4304 chunk of local store because we may be adding and subtracting from it,
4305 and we don't want to subtract bits that may be in the main map already.
4306 At the end we or the result into the bit map that is being built. */
4307
4308 posix_class *= 3;
4309
4310 /* Copy in the first table (always present) */
4311
4312 memcpy(pbits, cbits + posix_class_maps[posix_class],
4313 32 * sizeof(pcre_uint8));
4314
4315 /* If there is a second table, add or remove it as required. */
4316
4317 taboffset = posix_class_maps[posix_class + 1];
4318 tabopt = posix_class_maps[posix_class + 2];
4319
4320 if (taboffset >= 0)
4321 {
4322 if (tabopt >= 0)
4323 for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
4324 else
4325 for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4326 }
4327
4328 /* Now see if we need to remove any special characters. An option
4329 value of 1 removes vertical space and 2 removes underscore. */
4330
4331 if (tabopt < 0) tabopt = -tabopt;
4332 if (tabopt == 1) pbits[1] &= ~0x3c;
4333 else if (tabopt == 2) pbits[11] &= 0x7f;
4334
4335 /* Add the POSIX table or its complement into the main table that is
4336 being built and we are done. */
4337
4338 if (local_negate)
4339 for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
4340 else
4341 for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4342
4343 ptr = tempptr + 1;
4344 /* Every class contains at least one < 256 character. */
4345 class_has_8bitchar = 1;
4346 /* Every class contains at least two characters. */
4347 class_one_char = 2;
4348 continue; /* End of POSIX syntax handling */
4349 }
4350
4351 /* Backslash may introduce a single character, or it may introduce one
4352 of the specials, which just set a flag. The sequence \b is a special
4353 case. Inside a class (and only there) it is treated as backspace. We
4354 assume that other escapes have more than one character in them, so
4355 speculatively set both class_has_8bitchar and class_one_char bigger
4356 than one. Unrecognized escapes fall through and are either treated
4357 as literal characters (by default), or are faulted if
4358 PCRE_EXTRA is set. */
4359
4360 if (c == CHAR_BACKSLASH)
4361 {
4362 escape = check_escape(&ptr, &ec, errorcodeptr, cd->bracount, options,
4363 TRUE);
4364 if (*errorcodeptr != 0) goto FAILED;
4365 if (escape == 0) c = ec;
4366 else if (escape == ESC_b) c = CHAR_BS; /* \b is backspace in a class */
4367 else if (escape == ESC_N) /* \N is not supported in a class */
4368 {
4369 *errorcodeptr = ERR71;
4370 goto FAILED;
4371 }
4372 else if (escape == ESC_Q) /* Handle start of quoted string */
4373 {
4374 if (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4375 {
4376 ptr += 2; /* avoid empty string */
4377 }
4378 else inescq = TRUE;
4379 continue;
4380 }
4381 else if (escape == ESC_E) continue; /* Ignore orphan \E */
4382
4383 else
4384 {
4385 register const pcre_uint8 *cbits = cd->cbits;
4386 /* Every class contains at least two < 256 characters. */
4387 class_has_8bitchar++;
4388 /* Every class contains at least two characters. */
4389 class_one_char += 2;
4390
4391 switch (escape)
4392 {
4393 #ifdef SUPPORT_UCP
4394 case ESC_du: /* These are the values given for \d etc */
4395 case ESC_DU: /* when PCRE_UCP is set. We replace the */
4396 case ESC_wu: /* escape sequence with an appropriate \p */
4397 case ESC_WU: /* or \P to test Unicode properties instead */
4398 case ESC_su: /* of the default ASCII testing. */
4399 case ESC_SU:
4400 nestptr = ptr;
4401 ptr = substitutes[escape - ESC_DU] - 1; /* Just before substitute */
4402 class_has_8bitchar--; /* Undo! */
4403 continue;
4404 #endif
4405 case ESC_d:
4406 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
4407 continue;
4408
4409 case ESC_D:
4410 should_flip_negation = TRUE;
4411 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
4412 continue;
4413
4414 case ESC_w:
4415 for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
4416 continue;
4417
4418 case ESC_W:
4419 should_flip_negation = TRUE;
4420 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
4421 continue;
4422
4423 /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4424 if it was previously set by something earlier in the character
4425 class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4426 EBCDIC, so we lazily just adjust the appropriate bit. */
4427
4428 case ESC_s:
4429 classbits[0] |= cbits[cbit_space];
4430 classbits[1] |= cbits[cbit_space+1] & ~0x08;
4431 for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
4432 continue;
4433
4434 case ESC_S:
4435 should_flip_negation = TRUE;
4436 for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4437 classbits[1] |= 0x08; /* Perl 5.004 onwards omits VT from \s */
4438 continue;
4439
4440 /* The rest apply in both UCP and non-UCP cases. */
4441
4442 case ESC_h:
4443 (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4444 PRIV(hspace_list), NOTACHAR);
4445 continue;
4446
4447 case ESC_H:
4448 (void)add_not_list_to_class(classbits, &class_uchardata, options,
4449 cd, PRIV(hspace_list));
4450 continue;
4451
4452 case ESC_v:
4453 (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4454 PRIV(vspace_list), NOTACHAR);
4455 continue;
4456
4457 case ESC_V:
4458 (void)add_not_list_to_class(classbits, &class_uchardata, options,
4459 cd, PRIV(vspace_list));
4460 continue;
4461
4462 #ifdef SUPPORT_UCP
4463 case ESC_p:
4464 case ESC_P:
4465 {
4466 BOOL negated;
4467 unsigned int ptype = 0, pdata = 0;
4468 if (!get_ucp(&ptr, &negated, &ptype, &pdata, errorcodeptr))
4469 goto FAILED;
4470 *class_uchardata++ = ((escape == ESC_p) != negated)?
4471 XCL_PROP : XCL_NOTPROP;
4472 *class_uchardata++ = ptype;
4473 *class_uchardata++ = pdata;
4474 class_has_8bitchar--; /* Undo! */
4475 continue;
4476 }
4477 #endif
4478 /* Unrecognized escapes are faulted if PCRE is running in its
4479 strict mode. By default, for compatibility with Perl, they are
4480 treated as literals. */
4481
4482 default:
4483 if ((options & PCRE_EXTRA) != 0)
4484 {
4485 *errorcodeptr = ERR7;
4486 goto FAILED;
4487 }
4488 class_has_8bitchar--; /* Undo the speculative increase. */
4489 class_one_char -= 2; /* Undo the speculative increase. */
4490 c = *ptr; /* Get the final character and fall through */
4491 break;
4492 }
4493 }
4494
4495 /* Fall through if the escape just defined a single character (c >= 0).
4496 This may be greater than 256. */
4497
4498 escape = 0;
4499
4500 } /* End of backslash handling */
4501
4502 /* A character may be followed by '-' to form a range. However, Perl does
4503 not permit ']' to be the end of the range. A '-' character at the end is
4504 treated as a literal. Perl ignores orphaned \E sequences entirely. The
4505 code for handling \Q and \E is messy. */
4506
4507 CHECK_RANGE:
4508 while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
4509 {
4510 inescq = FALSE;
4511 ptr += 2;
4512 }
4513 oldptr = ptr;
4514
4515 /* Remember if \r or \n were explicitly used */
4516
4517 if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4518
4519 /* Check for range */
4520
4521 if (!inescq && ptr[1] == CHAR_MINUS)
4522 {
4523 pcre_uint32 d;
4524 ptr += 2;
4525 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E) ptr += 2;
4526
4527 /* If we hit \Q (not followed by \E) at this point, go into escaped
4528 mode. */
4529
4530 while (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_Q)
4531 {
4532 ptr += 2;
4533 if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_E)
4534 { ptr += 2; continue; }
4535 inescq = TRUE;
4536 break;
4537 }
4538
4539 /* Minus (hyphen) at the end of a class is treated as a literal, so put
4540 back the pointer and jump to handle the character that preceded it. */
4541
4542 if (*ptr == CHAR_NULL || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4543 {
4544 ptr = oldptr;
4545 goto CLASS_SINGLE_CHARACTER;
4546 }
4547
4548 /* Otherwise, we have a potential range; pick up the next character */
4549
4550 #ifdef SUPPORT_UTF
4551 if (utf)
4552 { /* Braces are required because the */
4553 GETCHARLEN(d, ptr, ptr); /* macro generates multiple statements */
4554 }
4555 else
4556 #endif
4557 d = *ptr; /* Not UTF-8 mode */
4558
4559 /* The second part of a range can be a single-character escape, but
4560 not any of the other escapes. Perl 5.6 treats a hyphen as a literal
4561 in such circumstances. */
4562
4563 if (!inescq && d == CHAR_BACKSLASH)
4564 {
4565 int descape;
4566 descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
4567 if (*errorcodeptr != 0) goto FAILED;
4568
4569 /* \b is backspace; any other special means the '-' was literal. */
4570
4571 if (descape != 0)
4572 {
4573 if (descape == ESC_b) d = CHAR_BS; else
4574 {
4575 ptr = oldptr;
4576 goto CLASS_SINGLE_CHARACTER; /* A few lines below */
4577 }
4578 }
4579 }
4580
4581 /* Check that the two values are in the correct order. Optimize
4582 one-character ranges. */
4583
4584 if (d < c)
4585 {
4586 *errorcodeptr = ERR8;
4587 goto FAILED;
4588 }
4589 if (d == c) goto CLASS_SINGLE_CHARACTER; /* A few lines below */
4590
4591 /* We have found a character range, so single character optimizations
4592 cannot be done anymore. Any value greater than 1 indicates that there
4593 is more than one character. */
4594
4595 class_one_char = 2;
4596
4597 /* Remember an explicit \r or \n, and add the range to the class. */
4598
4599 if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4600
4601 class_has_8bitchar +=
4602 add_to_class(classbits, &class_uchardata, options, cd, c, d);
4603
4604 continue; /* Go get the next char in the class */
4605 }
4606
4607 /* Handle a single character - we can get here for a normal non-escape
4608 char, or after \ that introduces a single character or for an apparent
4609 range that isn't. Only the value 1 matters for class_one_char, so don't
4610 increase it if it is already 2 or more ... just in case there's a class
4611 with a zillion characters in it. */
4612
4613 CLASS_SINGLE_CHARACTER:
4614 if (class_one_char < 2) class_one_char++;
4615
4616 /* If class_one_char is 1, we have the first single character in the
4617 class, and there have been no prior ranges, or XCLASS items generated by
4618 escapes. If this is the final character in the class, we can optimize by
4619 turning the item into a 1-character OP_CHAR[I] if it's positive, or
4620 OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4621 to be set. Otherwise, there can be no first char if this item is first,
4622 whatever repeat count may follow. In the case of reqchar, save the
4623 previous value for reinstating. */
4624
4625 if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
4626 {
4627 ptr++;
4628 zeroreqchar = reqchar;
4629 zeroreqcharflags = reqcharflags;
4630
4631 if (negate_class)
4632 {
4633 #ifdef SUPPORT_UCP
4634 int d;
4635 #endif
4636 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4637 zerofirstchar = firstchar;
4638 zerofirstcharflags = firstcharflags;
4639
4640 /* For caseless UTF-8 mode when UCP support is available, check
4641 whether this character has more than one other case. If so, generate
4642 a special OP_NOTPROP item instead of OP_NOTI. */
4643
4644 #ifdef SUPPORT_UCP
4645 if (utf && (options & PCRE_CASELESS) != 0 &&
4646 (d = UCD_CASESET(c)) != 0)
4647 {
4648 *code++ = OP_NOTPROP;
4649 *code++ = PT_CLIST;
4650 *code++ = d;
4651 }
4652 else
4653 #endif
4654 /* Char has only one other case, or UCP not available */
4655
4656 {
4657 *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4658 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4659 if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4660 code += PRIV(ord2utf)(c, code);
4661 else
4662 #endif
4663 *code++ = c;
4664 }
4665
4666 /* We are finished with this character class */
4667
4668 goto END_CLASS;
4669 }
4670
4671 /* For a single, positive character, get the value into mcbuffer, and
4672 then we can handle this with the normal one-character code. */
4673
4674 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4675 if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4676 mclength = PRIV(ord2utf)(c, mcbuffer);
4677 else
4678 #endif
4679 {
4680 mcbuffer[0] = c;
4681 mclength = 1;
4682 }
4683 goto ONE_CHAR;
4684 } /* End of 1-char optimization */
4685
4686 /* There is more than one character in the class, or an XCLASS item
4687 has been generated. Add this character to the class. */
4688
4689 class_has_8bitchar +=
4690 add_to_class(classbits, &class_uchardata, options, cd, c, c);
4691 }
4692
4693 /* Loop until ']' reached. This "while" is the end of the "do" far above.
4694 If we are at the end of an internal nested string, revert to the outer
4695 string. */
4696
4697 while (((c = *(++ptr)) != CHAR_NULL ||
4698 (nestptr != NULL &&
4699 (ptr = nestptr, nestptr = NULL, c = *(++ptr)) != CHAR_NULL)) &&
4700 (c != CHAR_RIGHT_SQUARE_BRACKET || inescq));
4701
4702 /* Check for missing terminating ']' */
4703
4704 if (c == CHAR_NULL)
4705 {
4706 *errorcodeptr = ERR6;
4707 goto FAILED;
4708 }
4709
4710 /* We will need an XCLASS if data has been placed in class_uchardata. In
4711 the second phase this is a sufficient test. However, in the pre-compile
4712 phase, class_uchardata gets emptied to prevent workspace overflow, so it
4713 only if the very last character in the class needs XCLASS will it contain
4714 anything at this point. For this reason, xclass gets set TRUE above when
4715 uchar_classdata is emptied, and that's why this code is the way it is here
4716 instead of just doing a test on class_uchardata below. */
4717
4718 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4719 if (class_uchardata > class_uchardata_base) xclass = TRUE;
4720 #endif
4721
4722 /* If this is the first thing in the branch, there can be no first char
4723 setting, whatever the repeat count. Any reqchar setting must remain
4724 unchanged after any kind of repeat. */
4725
4726 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
4727 zerofirstchar = firstchar;
4728 zerofirstcharflags = firstcharflags;
4729 zeroreqchar = reqchar;
4730 zeroreqcharflags = reqcharflags;
4731
4732 /* If there are characters with values > 255, we have to compile an
4733 extended class, with its own opcode, unless there was a negated special
4734 such as \S in the class, and PCRE_UCP is not set, because in that case all
4735 characters > 255 are in the class, so any that were explicitly given as
4736 well can be ignored. If (when there are explicit characters > 255 that must
4737 be listed) there are no characters < 256, we can omit the bitmap in the
4738 actual compiled code. */
4739
4740 #ifdef SUPPORT_UTF
4741 if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4742 #elif !defined COMPILE_PCRE8
4743 if (xclass && !should_flip_negation)
4744 #endif
4745 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4746 {
4747 *class_uchardata++ = XCL_END; /* Marks the end of extra data */
4748 *code++ = OP_XCLASS;
4749 code += LINK_SIZE;
4750 *code = negate_class? XCL_NOT:0;
4751
4752 /* If the map is required, move up the extra data to make room for it;
4753 otherwise just move the code pointer to the end of the extra data. */
4754
4755 if (class_has_8bitchar > 0)
4756 {
4757 *code++ |= XCL_MAP;
4758 memmove(code + (32 / sizeof(pcre_uchar)), code,
4759 IN_UCHARS(class_uchardata - code));
4760 memcpy(code, classbits, 32);
4761 code = class_uchardata + (32 / sizeof(pcre_uchar));
4762 }
4763 else code = class_uchardata;
4764
4765 /* Now fill in the complete length of the item */
4766
4767 PUT(previous, 1, (int)(code - previous));
4768 break; /* End of class handling */
4769 }
4770 #endif
4771
4772 /* If there are no characters > 255, or they are all to be included or
4773 excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
4774 whole class was negated and whether there were negative specials such as \S
4775 (non-UCP) in the class. Then copy the 32-byte map into the code vector,
4776 negating it if necessary. */
4777
4778 *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4779 if (lengthptr == NULL) /* Save time in the pre-compile phase */
4780 {
4781 if (negate_class)
4782 for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4783 memcpy(code, classbits, 32);
4784 }
4785 code += 32 / sizeof(pcre_uchar);
4786
4787 END_CLASS:
4788 break;
4789
4790
4791 /* ===================================================================*/
4792 /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
4793 has been tested above. */
4794
4795 case CHAR_LEFT_CURLY_BRACKET:
4796 if (!is_quantifier) goto NORMAL_CHAR;
4797 ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
4798 if (*errorcodeptr != 0) goto FAILED;
4799 goto REPEAT;
4800
4801 case CHAR_ASTERISK:
4802 repeat_min = 0;
4803 repeat_max = -1;
4804 goto REPEAT;
4805
4806 case CHAR_PLUS:
4807 repeat_min = 1;
4808 repeat_max = -1;
4809 goto REPEAT;
4810
4811 case CHAR_QUESTION_MARK:
4812 repeat_min = 0;
4813 repeat_max = 1;
4814
4815 REPEAT:
4816 if (previous == NULL)
4817 {
4818 *errorcodeptr = ERR9;
4819 goto FAILED;
4820 }
4821
4822 if (repeat_min == 0)
4823 {
4824 firstchar = zerofirstchar; /* Adjust for zero repeat */
4825 firstcharflags = zerofirstcharflags;
4826 reqchar = zeroreqchar; /* Ditto */
4827 reqcharflags = zeroreqcharflags;
4828 }
4829
4830 /* Remember whether this is a variable length repeat */
4831
4832 reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
4833
4834 op_type = 0; /* Default single-char op codes */
4835 possessive_quantifier = FALSE; /* Default not possessive quantifier */
4836
4837 /* Save start of previous item, in case we have to move it up in order to
4838 insert something before it. */
4839
4840 tempcode = previous;
4841
4842 /* If the next character is '+', we have a possessive quantifier. This
4843 implies greediness, whatever the setting of the PCRE_UNGREEDY option.
4844 If the next character is '?' this is a minimizing repeat, by default,
4845 but if PCRE_UNGREEDY is set, it works the other way round. We change the
4846 repeat type to the non-default. */
4847
4848 if (ptr[1] == CHAR_PLUS)
4849 {
4850 repeat_type = 0; /* Force greedy */
4851 possessive_quantifier = TRUE;
4852 ptr++;
4853 }
4854 else if (ptr[1] == CHAR_QUESTION_MARK)
4855 {
4856 repeat_type = greedy_non_default;
4857 ptr++;
4858 }
4859 else repeat_type = greedy_default;
4860
4861 /* If previous was a recursion call, wrap it in atomic brackets so that
4862 previous becomes the atomic group. All recursions were so wrapped in the
4863 past, but it no longer happens for non-repeated recursions. In fact, the
4864 repeated ones could be re-implemented independently so as not to need this,
4865 but for the moment we rely on the code for repeating groups. */
4866
4867 if (*previous == OP_RECURSE)
4868 {
4869 memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4870 *previous = OP_ONCE;
4871 PUT(previous, 1, 2 + 2*LINK_SIZE);
4872 previous[2 + 2*LINK_SIZE] = OP_KET;
4873 PUT(previous, 3 + 2*LINK_SIZE, 2 + 2*LINK_SIZE);
4874 code += 2 + 2 * LINK_SIZE;
4875 length_prevgroup = 3 + 3*LINK_SIZE;
4876
4877 /* When actually compiling, we need to check whether this was a forward
4878 reference, and if so, adjust the offset. */
4879
4880 if (lengthptr == NULL && cd->hwm >= cd->start_workspace + LINK_SIZE)
4881 {
4882 int offset = GET(cd->hwm, -LINK_SIZE);
4883 if (offset == previous + 1 - cd->start_code)
4884 PUT(cd->hwm, -LINK_SIZE, offset + 1 + LINK_SIZE);
4885 }
4886 }
4887
4888 /* Now handle repetition for the different types of item. */
4889
4890 /* If previous was a character or negated character match, abolish the item
4891 and generate a repeat item instead. If a char item has a minimum of more
4892 than one, ensure that it is set in reqchar - it might not be if a sequence
4893 such as x{3} is the first thing in a branch because the x will have gone
4894 into firstchar instead. */
4895
4896 if (*previous == OP_CHAR || *previous == OP_CHARI
4897 || *previous == OP_NOT || *previous == OP_NOTI)
4898 {
4899 switch (*previous)
4900 {
4901 default: /* Make compiler happy. */
4902 case OP_CHAR: op_type = OP_STAR - OP_STAR; break;
4903 case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4904 case OP_NOT: op_type = OP_NOTSTAR - OP_STAR; break;
4905 case OP_NOTI: op_type = OP_NOTSTARI - OP_STAR; break;
4906 }
4907
4908 /* Deal with UTF characters that take up more than one character. It's
4909 easier to write this out separately than try to macrify it. Use c to
4910 hold the length of the character in bytes, plus UTF_LENGTH to flag that
4911 it's a length rather than a small character. */
4912
4913 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
4914 if (utf && NOT_FIRSTCHAR(code[-1]))
4915 {
4916 pcre_uchar *lastchar = code - 1;
4917 BACKCHAR(lastchar);
4918 c = (int)(code - lastchar); /* Length of UTF-8 character */
4919 memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
4920 c |= UTF_LENGTH; /* Flag c as a length */
4921 }
4922 else
4923 #endif /* SUPPORT_UTF */
4924
4925 /* Handle the case of a single charater - either with no UTF support, or
4926 with UTF disabled, or for a single character UTF character. */
4927 {
4928 c = code[-1];
4929 if (*previous <= OP_CHARI && repeat_min > 1)
4930 {
4931 reqchar = c;
4932 reqcharflags = req_caseopt | cd->req_varyopt;
4933 }
4934 }
4935
4936 /* If the repetition is unlimited, it pays to see if the next thing on
4937 the line is something that cannot possibly match this character. If so,
4938 automatically possessifying this item gains some performance in the case
4939 where the match fails. */
4940
4941 if (!possessive_quantifier &&
4942 repeat_max < 0 &&
4943 check_auto_possessive(previous, utf, ptr + 1, options, cd))
4944 {
4945 repeat_type = 0; /* Force greedy */
4946 possessive_quantifier = TRUE;
4947 }
4948
4949 goto OUTPUT_SINGLE_REPEAT; /* Code shared with single character types */
4950 }
4951
4952 /* If previous was a character type match (\d or similar), abolish it and
4953 create a suitable repeat item. The code is shared with single-character
4954 repeats by setting op_type to add a suitable offset into repeat_type. Note
4955 the the Unicode property types will be present only when SUPPORT_UCP is
4956 defined, but we don't wrap the little bits of code here because it just
4957 makes it horribly messy. */
4958
4959 else if (*previous < OP_EODN)
4960 {
4961 pcre_uchar *oldcode;
4962 int prop_type, prop_value;
4963 op_type = OP_TYPESTAR - OP_STAR; /* Use type opcodes */
4964 c = *previous;
4965
4966 if (!possessive_quantifier &&
4967 repeat_max < 0 &&
4968 check_auto_possessive(previous, utf, ptr + 1, options, cd))
4969 {
4970 repeat_type = 0; /* Force greedy */
4971 possessive_quantifier = TRUE;
4972 }
4973
4974 OUTPUT_SINGLE_REPEAT:
4975 if (*previous == OP_PROP || *previous == OP_NOTPROP)
4976 {
4977 prop_type = previous[1];
4978 prop_value = previous[2];
4979 }
4980 else prop_type = prop_value = -1;
4981
4982 oldcode = code;
4983 code = previous; /* Usually overwrite previous item */
4984
4985 /* If the maximum is zero then the minimum must also be zero; Perl allows
4986 this case, so we do too - by simply omitting the item altogether. */
4987
4988 if (repeat_max == 0) goto END_REPEAT;
4989
4990 /* Combine the op_type with the repeat_type */
4991
4992 repeat_type += op_type;
4993
4994 /* A minimum of zero is handled either as the special case * or ?, or as
4995 an UPTO, with the maximum given. */
4996
4997 if (repeat_min == 0)
4998 {
4999 if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
5000 else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
5001 else
5002 {
5003 *code++ = OP_UPTO + repeat_type;
5004 PUT2INC(code, 0, repeat_max);
5005 }
5006 }
5007
5008 /* A repeat minimum of 1 is optimized into some special cases. If the
5009 maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
5010 left in place and, if the maximum is greater than 1, we use OP_UPTO with
5011 one less than the maximum. */
5012
5013 else if (repeat_min == 1)
5014 {
5015 if (repeat_max == -1)
5016 *code++ = OP_PLUS + repeat_type;
5017 else
5018 {
5019 code = oldcode; /* leave previous item in place */
5020 if (repeat_max == 1) goto END_REPEAT;
5021 *code++ = OP_UPTO + repeat_type;
5022 PUT2INC(code, 0, repeat_max - 1);
5023 }
5024 }
5025
5026 /* The case {n,n} is just an EXACT, while the general case {n,m} is
5027 handled as an EXACT followed by an UPTO. */
5028
5029 else
5030 {
5031 *code++ = OP_EXACT + op_type; /* NB EXACT doesn't have repeat_type */
5032 PUT2INC(code, 0, repeat_min);
5033
5034 /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
5035 we have to insert the character for the previous code. For a repeated
5036 Unicode property match, there are two extra bytes that define the
5037 required property. In UTF-8 mode, long characters have their length in
5038 c, with the UTF_LENGTH bit as a flag. */
5039
5040 if (repeat_max < 0)
5041 {
5042 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5043 if (utf && (c & UTF_LENGTH) != 0)
5044 {
5045 memcpy(code, utf_chars, IN_UCHARS(c & 7));
5046 code += c & 7;
5047 }
5048 else
5049 #endif
5050 {
5051 *code++ = c;
5052 if (prop_type >= 0)
5053 {
5054 *code++ = prop_type;
5055 *code++ = prop_value;
5056 }
5057 }
5058 *code++ = OP_STAR + repeat_type;
5059 }
5060
5061 /* Else insert an UPTO if the max is greater than the min, again
5062 preceded by the character, for the previously inserted code. If the
5063 UPTO is just for 1 instance, we can use QUERY instead. */
5064
5065 else if (repeat_max != repeat_min)
5066 {
5067 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5068 if (utf && (c & UTF_LENGTH) != 0)
5069 {
5070 memcpy(code, utf_chars, IN_UCHARS(c & 7));
5071 code += c & 7;
5072 }
5073 else
5074 #endif
5075 *code++ = c;
5076 if (prop_type >= 0)
5077 {
5078 *code++ = prop_type;
5079 *code++ = prop_value;
5080 }
5081 repeat_max -= repeat_min;
5082
5083 if (repeat_max == 1)
5084 {
5085 *code++ = OP_QUERY + repeat_type;
5086 }
5087 else
5088 {
5089 *code++ = OP_UPTO + repeat_type;
5090 PUT2INC(code, 0, repeat_max);
5091 }
5092 }
5093 }
5094
5095 /* The character or character type itself comes last in all cases. */
5096
5097 #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
5098 if (utf && (c & UTF_LENGTH) != 0)
5099 {
5100 memcpy(code, utf_chars, IN_UCHARS(c & 7));
5101 code += c & 7;
5102 }
5103 else
5104 #endif
5105 *code++ = c;
5106
5107 /* For a repeated Unicode property match, there are two extra bytes that
5108 define the required property. */
5109
5110 #ifdef SUPPORT_UCP
5111 if (prop_type >= 0)
5112 {
5113 *code++ = prop_type;
5114 *code++ = prop_value;
5115 }
5116 #endif
5117 }
5118
5119 /* If previous was a character class or a back reference, we put the repeat
5120 stuff after it, but just skip the item if the repeat was {0,0}. */
5121
5122 else if (*previous == OP_CLASS ||
5123 *previous == OP_NCLASS ||
5124 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5125 *previous == OP_XCLASS ||
5126 #endif
5127 *previous == OP_REF ||
5128 *previous == OP_REFI)
5129 {
5130 if (repeat_max == 0)
5131 {
5132 code = previous;
5133 goto END_REPEAT;
5134 }
5135
5136 if (repeat_min == 0 && repeat_max == -1)
5137 *code++ = OP_CRSTAR + repeat_type;
5138 else if (repeat_min == 1 && repeat_max == -1)
5139 *code++ = OP_CRPLUS + repeat_type;
5140 else if (repeat_min == 0 && repeat_max == 1)
5141 *code++ = OP_CRQUERY + repeat_type;
5142 else
5143 {
5144 *code++ = OP_CRRANGE + repeat_type;
5145 PUT2INC(code, 0, repeat_min);
5146 if (repeat_max == -1) repeat_max = 0; /* 2-byte encoding for max */
5147 PUT2INC(code, 0, repeat_max);
5148 }
5149 }
5150
5151 /* If previous was a bracket group, we may have to replicate it in certain
5152 cases. Note that at this point we can encounter only the "basic" bracket
5153 opcodes such as BRA and CBRA, as this is the place where they get converted
5154 into the more special varieties such as BRAPOS and SBRA. A test for >=
5155 OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
5156 ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow
5157 repetition of assertions, but now it does, for Perl compatibility. */
5158
5159 else if (*previous >= OP_ASSERT && *previous <= OP_COND)
5160 {
5161 register int i;
5162 int len = (int)(code - previous);
5163 pcre_uchar *bralink = NULL;
5164 pcre_uchar *brazeroptr = NULL;
5165
5166 /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
5167 we just ignore the repeat. */
5168
5169 if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
5170 goto END_REPEAT;
5171
5172 /* There is no sense in actually repeating assertions. The only potential
5173 use of repetition is in cases when the assertion is optional. Therefore,
5174 if the minimum is greater than zero, just ignore the repeat. If the
5175 maximum is not not zero or one, set it to 1. */
5176
5177 if (*previous < OP_ONCE) /* Assertion */
5178 {
5179 if (repeat_min > 0) goto END_REPEAT;
5180 if (repeat_max < 0 || repeat_max > 1) repeat_max = 1;
5181 }
5182
5183 /* The case of a zero minimum is special because of the need to stick
5184 OP_BRAZERO in front of it, and because the group appears once in the
5185 data, whereas in other cases it appears the minimum number of times. For
5186 this reason, it is simplest to treat this case separately, as otherwise
5187 the code gets far too messy. There are several special subcases when the
5188 minimum is zero. */
5189
5190 if (repeat_min == 0)
5191 {
5192 /* If the maximum is also zero, we used to just omit the group from the
5193 output altogether, like this:
5194
5195 ** if (repeat_max == 0)
5196 ** {
5197 ** code = previous;
5198 ** goto END_REPEAT;
5199 ** }
5200
5201 However, that fails when a group or a subgroup within it is referenced
5202 as a subroutine from elsewhere in the pattern, so now we stick in
5203 OP_SKIPZERO in front of it so that it is skipped on execution. As we
5204 don't have a list of which groups are referenced, we cannot do this
5205 selectively.
5206
5207 If the maximum is 1 or unlimited, we just have to stick in the BRAZERO
5208 and do no more at this point. However, we do need to adjust any
5209 OP_RECURSE calls inside the group that refer to the group itself or any
5210 internal or forward referenced group, because the offset is from the
5211 start of the whole regex. Temporarily terminate the pattern while doing
5212 this. */
5213
5214 if (repeat_max <= 1) /* Covers 0, 1, and unlimited */
5215 {
5216 *code = OP_END;
5217 adjust_recurse(previous, 1, utf, cd, save_hwm);
5218 memmove(previous + 1, previous, IN_UCHARS(len));
5219 code++;
5220 if (repeat_max == 0)
5221 {
5222 *previous++ = OP_SKIPZERO;
5223 goto END_REPEAT;
5224 }
5225 brazeroptr = previous; /* Save for possessive optimizing */
5226 *previous++ = OP_BRAZERO + repeat_type;
5227 }
5228
5229 /* If the maximum is greater than 1 and limited, we have to replicate
5230 in a nested fashion, sticking OP_BRAZERO before each set of brackets.
5231 The first one has to be handled carefully because it's the original
5232 copy, which has to be moved up. The remainder can be handled by code
5233 that is common with the non-zero minimum case below. We have to
5234 adjust the value or repeat_max, since one less copy is required. Once
5235 again, we may have to adjust any OP_RECURSE calls inside the group. */
5236
5237 else
5238 {
5239 int offset;
5240 *code = OP_END;
5241 adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);
5242 memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
5243 code += 2 + LINK_SIZE;
5244 *previous++ = OP_BRAZERO + repeat_type;
5245 *previous++ = OP_BRA;
5246
5247 /* We chain together the bracket offset fields that have to be
5248 filled in later when the ends of the brackets are reached. */
5249
5250 offset = (bralink == NULL)? 0 : (int)(previous - bralink);
5251 bralink = previous;
5252 PUTINC(previous, 0, offset);
5253 }
5254
5255 repeat_max--;
5256 }
5257
5258 /* If the minimum is greater than zero, replicate the group as many
5259 times as necessary, and adjust the maximum to the number of subsequent
5260 copies that we need. If we set a first char from the group, and didn't
5261 set a required char, copy the latter from the former. If there are any
5262 forward reference subroutine calls in the group, there will be entries on
5263 the workspace list; replicate these with an appropriate increment. */
5264
5265 else
5266 {
5267 if (repeat_min > 1)
5268 {
5269 /* In the pre-compile phase, we don't actually do the replication. We
5270 just adjust the length as if we had. Do some paranoid checks for
5271 potential integer overflow. The INT64_OR_DOUBLE type is a 64-bit
5272 integer type when available, otherwise double. */
5273
5274 if (lengthptr != NULL)
5275 {
5276 int delta = (repeat_min - 1)*length_prevgroup;
5277 if ((INT64_OR_DOUBLE)(repeat_min - 1)*
5278 (INT64_OR_DOUBLE)length_prevgroup >
5279 (INT64_OR_DOUBLE)INT_MAX ||
5280 OFLOW_MAX - *lengthptr < delta)
5281 {
5282 *errorcodeptr = ERR20;
5283 goto FAILED;
5284 }
5285 *lengthptr += delta;
5286 }
5287
5288 /* This is compiling for real. If there is a set first byte for
5289 the group, and we have not yet set a "required byte", set it. Make
5290 sure there is enough workspace for copying forward references before
5291 doing the copy. */
5292
5293 else
5294 {
5295 if (groupsetfirstchar && reqcharflags < 0)
5296 {
5297 reqchar = firstchar;
5298 reqcharflags = firstcharflags;
5299 }
5300
5301 for (i = 1; i < repeat_min; i++)
5302 {
5303 pcre_uchar *hc;
5304 pcre_uchar *this_hwm = cd->hwm;
5305 memcpy(code, previous, IN_UCHARS(len));
5306
5307 while (cd->hwm > cd->start_workspace + cd->workspace_size -
5308 WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5309 {
5310 int save_offset = save_hwm - cd->start_workspace;
5311 int this_offset = this_hwm - cd->start_workspace;
5312 *errorcodeptr = expand_workspace(cd);
5313 if (*errorcodeptr != 0) goto FAILED;
5314 save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5315 this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5316 }
5317
5318 for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5319 {
5320 PUT(cd->hwm, 0, GET(hc, 0) + len);
5321 cd->hwm += LINK_SIZE;
5322 }
5323 save_hwm = this_hwm;
5324 code += len;
5325 }
5326 }
5327 }
5328
5329 if (repeat_max > 0) repeat_max -= repeat_min;
5330 }
5331
5332 /* This code is common to both the zero and non-zero minimum cases. If
5333 the maximum is limited, it replicates the group in a nested fashion,
5334 remembering the bracket starts on a stack. In the case of a zero minimum,
5335 the first one was set up above. In all cases the repeat_max now specifies
5336 the number of additional copies needed. Again, we must remember to
5337 replicate entries on the forward reference list. */
5338
5339 if (repeat_max >= 0)
5340 {
5341 /* In the pre-compile phase, we don't actually do the replication. We
5342 just adjust the length as if we had. For each repetition we must add 1
5343 to the length for BRAZERO and for all but the last repetition we must
5344 add 2 + 2*LINKSIZE to allow for the nesting that occurs. Do some
5345 paranoid checks to avoid integer overflow. The INT64_OR_DOUBLE type is
5346 a 64-bit integer type when available, otherwise double. */
5347
5348 if (lengthptr != NULL && repeat_max > 0)
5349 {
5350 int delta = repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
5351 2 - 2*LINK_SIZE; /* Last one doesn't nest */
5352 if ((INT64_OR_DOUBLE)repeat_max *
5353 (INT64_OR_DOUBLE)(length_prevgroup + 1 + 2 + 2*LINK_SIZE)
5354 > (INT64_OR_DOUBLE)INT_MAX ||
5355 OFLOW_MAX - *lengthptr < delta)
5356 {
5357 *errorcodeptr = ERR20;
5358 goto FAILED;
5359 }
5360 *lengthptr += delta;
5361 }
5362
5363 /* This is compiling for real */
5364
5365 else for (i = repeat_max - 1; i >= 0; i--)
5366 {
5367 pcre_uchar *hc;
5368 pcre_uchar *this_hwm = cd->hwm;
5369
5370 *code++ = OP_BRAZERO + repeat_type;
5371
5372 /* All but the final copy start a new nesting, maintaining the
5373 chain of brackets outstanding. */
5374
5375 if (i != 0)
5376 {
5377 int offset;
5378 *code++ = OP_BRA;
5379 offset = (bralink == NULL)? 0 : (int)(code - bralink);
5380 bralink = code;
5381 PUTINC(code, 0, offset);
5382 }
5383
5384 memcpy(code, previous, IN_UCHARS(len));
5385
5386 /* Ensure there is enough workspace for forward references before
5387 copying them. */
5388
5389 while (cd->hwm > cd->start_workspace + cd->workspace_size -
5390 WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
5391 {
5392 int save_offset = save_hwm - cd->start_workspace;
5393 int this_offset = this_hwm - cd->start_workspace;
5394 *errorcodeptr = expand_workspace(cd);
5395 if (*errorcodeptr != 0) goto FAILED;
5396 save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5397 this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5398 }
5399
5400 for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
5401 {
5402 PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
5403 cd->hwm += LINK_SIZE;
5404 }
5405 save_hwm = this_hwm;
5406 code += len;
5407 }
5408
5409 /* Now chain through the pending brackets, and fill in their length
5410 fields (which are holding the chain links pro tem). */
5411
5412 while (bralink != NULL)
5413 {
5414 int oldlinkoffset;
5415 int offset = (int)(code - bralink + 1);
5416 pcre_uchar *bra = code - offset;
5417 oldlinkoffset = GET(bra, 1);
5418 bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5419 *code++ = OP_KET;
5420 PUTINC(code, 0, offset);
5421 PUT(bra, 1, offset);
5422 }
5423 }
5424
5425 /* If the maximum is unlimited, set a repeater in the final copy. For
5426 ONCE brackets, that's all we need to do. However, possessively repeated
5427 ONCE brackets can be converted into non-capturing brackets, as the
5428 behaviour of (?:xx)++ is the same as (?>xx)++ and this saves having to
5429 deal with possessive ONCEs specially.
5430
5431 Otherwise, when we are doing the actual compile phase, check to see
5432 whether this group is one that could match an empty string. If so,
5433 convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
5434 that runtime checking can be done. [This check is also applied to ONCE
5435 groups at runtime, but in a different way.]
5436
5437 Then, if the quantifier was possessive and the bracket is not a
5438 conditional, we convert the BRA code to the POS form, and the KET code to
5439 KETRPOS. (It turns out to be convenient at runtime to detect this kind of
5440 subpattern at both the start and at the end.) The use of special opcodes
5441 makes it possible to reduce greatly the stack usage in pcre_exec(). If
5442 the group is preceded by OP_BRAZERO, convert this to OP_BRAPOSZERO.
5443
5444 Then, if the minimum number of matches is 1 or 0, cancel the possessive
5445 flag so that the default action below, of wrapping everything inside
5446 atomic brackets, does not happen. When the minimum is greater than 1,
5447 there will be earlier copies of the group, and so we still have to wrap
5448 the whole thing. */
5449
5450 else
5451 {
5452 pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5453 pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5454
5455 /* Convert possessive ONCE brackets to non-capturing */
5456
5457 if ((*bracode == OP_ONCE || *bracode == OP_ONCE_NC) &&
5458 possessive_quantifier) *bracode = OP_BRA;
5459
5460 /* For non-possessive ONCE brackets, all we need to do is to
5461 set the KET. */
5462
5463 if (*bracode == OP_ONCE || *bracode == OP_ONCE_NC)
5464 *ketcode = OP_KETRMAX + repeat_type;
5465
5466 /* Handle non-ONCE brackets and possessive ONCEs (which have been
5467 converted to non-capturing above). */
5468
5469 else
5470 {
5471 /* In the compile phase, check for empty string matching. */
5472
5473 if (lengthptr == NULL)
5474 {
5475 pcre_uchar *scode = bracode;
5476 do
5477 {
5478 if (could_be_empty_branch(scode, ketcode, utf, cd, NULL))
5479 {
5480 *bracode += OP_SBRA - OP_BRA;
5481 break;
5482 }
5483 scode += GET(scode, 1);
5484 }
5485 while (*scode == OP_ALT);
5486 }
5487
5488 /* Handle possessive quantifiers. */
5489
5490 if (possessive_quantifier)
5491 {
5492 /* For COND brackets, we wrap the whole thing in a possessively
5493 repeated non-capturing bracket, because we have not invented POS
5494 versions of the COND opcodes. Because we are moving code along, we
5495 must ensure that any pending recursive references are updated. */
5496
5497 if (*bracode == OP_COND || *bracode == OP_SCOND)
5498 {
5499 int nlen = (int)(code - bracode);
5500 *code = OP_END;
5501 adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);
5502 memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
5503 code += 1 + LINK_SIZE;
5504 nlen += 1 + LINK_SIZE;
5505 *bracode = OP_BRAPOS;
5506 *code++ = OP_KETRPOS;
5507 PUTINC(code, 0, nlen);
5508 PUT(bracode, 1, nlen);
5509 }
5510
5511 /* For non-COND brackets, we modify the BRA code and use KETRPOS. */
5512
5513 else
5514 {
5515 *bracode += 1; /* Switch to xxxPOS opcodes */
5516 *ketcode = OP_KETRPOS;
5517 }
5518
5519 /* If the minimum is zero, mark it as possessive, then unset the
5520 possessive flag when the minimum is 0 or 1. */
5521
5522 if (brazeroptr != NULL) *brazeroptr = OP_BRAPOSZERO;
5523 if (repeat_min < 2) possessive_quantifier = FALSE;
5524 }
5525
5526 /* Non-possessive quantifier */
5527
5528 else *ketcode = OP_KETRMAX + repeat_type;
5529 }
5530 }
5531 }
5532
5533 /* If previous is OP_FAIL, it was generated by an empty class [] in
5534 JavaScript mode. The other ways in which OP_FAIL can be generated, that is
5535 by (*FAIL) or (?!) set previous to NULL, which gives a "nothing to repeat"
5536 error above. We can just ignore the repeat in JS case. */
5537
5538 else if (*previous == OP_FAIL) goto END_REPEAT;
5539
5540 /* Else there's some kind of shambles */
5541
5542 else
5543 {
5544 *errorcodeptr = ERR11;
5545 goto FAILED;
5546 }
5547
5548 /* If the character following a repeat is '+', or if certain optimization
5549 tests above succeeded, possessive_quantifier is TRUE. For some opcodes,
5550 there are special alternative opcodes for this case. For anything else, we
5551 wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'
5552 notation is just syntactic sugar, taken from Sun's Java package, but the
5553 special opcodes can optimize it.
5554
5555 Some (but not all) possessively repeated subpatterns have already been
5556 completely handled in the code just above. For them, possessive_quantifier
5557 is always FALSE at this stage.
5558
5559 Note that the repeated item starts at tempcode, not at previous, which
5560 might be the first part of a string whose (former) last char we repeated.
5561
5562 Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
5563 an 'upto' may follow. We skip over an 'exact' item, and then test the
5564 length of what remains before proceeding. */
5565
5566 if (possessive_quantifier)
5567 {
5568 int len;
5569
5570 if (*tempcode == OP_TYPEEXACT)
5571 tempcode += PRIV(OP_lengths)[*tempcode] +
5572 ((tempcode[1 + IMM2_SIZE] == OP_PROP
5573 || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
5574
5575 else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5576 {
5577 tempcode += PRIV(OP_lengths)[*tempcode];
5578 #ifdef SUPPORT_UTF
5579 if (utf && HAS_EXTRALEN(tempcode[-1]))
5580 tempcode += GET_EXTRALEN(tempcode[-1]);
5581 #endif
5582 }
5583
5584 len = (int)(code - tempcode);
5585 if (len > 0) switch (*tempcode)
5586 {
5587 case OP_STAR: *tempcode = OP_POSSTAR; break;
5588 case OP_PLUS: *tempcode = OP_POSPLUS; break;
5589 case OP_QUERY: *tempcode = OP_POSQUERY; break;
5590 case OP_UPTO: *tempcode = OP_POSUPTO; break;
5591
5592 case OP_STARI: *tempcode = OP_POSSTARI; break;
5593 case OP_PLUSI: *tempcode = OP_POSPLUSI; break;
5594 case OP_QUERYI: *tempcode = OP_POSQUERYI; break;
5595 case OP_UPTOI: *tempcode = OP_POSUPTOI; break;
5596
5597 case OP_NOTSTAR: *tempcode = OP_NOTPOSSTAR; break;
5598 case OP_NOTPLUS: *tempcode = OP_NOTPOSPLUS; break;
5599 case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
5600 case OP_NOTUPTO: *tempcode = OP_NOTPOSUPTO; break;
5601
5602 case OP_NOTSTARI: *tempcode = OP_NOTPOSSTARI; break;
5603 case OP_NOTPLUSI: *tempcode = OP_NOTPOSPLUSI; break;
5604 case OP_NOTQUERYI: *tempcode = OP_NOTPOSQUERYI; break;
5605 case OP_NOTUPTOI: *tempcode = OP_NOTPOSUPTOI; break;
5606
5607 case OP_TYPESTAR: *tempcode = OP_TYPEPOSSTAR; break;
5608 case OP_TYPEPLUS: *tempcode = OP_TYPEPOSPLUS; break;
5609 case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
5610 case OP_TYPEUPTO: *tempcode = OP_TYPEPOSUPTO; break;
5611
5612 /* Because we are moving code along, we must ensure that any
5613 pending recursive references are updated. */
5614
5615 default:
5616 *code = OP_END;
5617 adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);
5618 memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
5619 code += 1 + LINK_SIZE;
5620 len += 1 + LINK_SIZE;
5621 tempcode[0] = OP_ONCE;
5622 *code++ = OP_KET;
5623 PUTINC(code, 0, len);
5624 PUT(tempcode, 1, len);
5625 break;
5626 }
5627 }
5628
5629 /* In all case we no longer have a previous item. We also set the
5630 "follows varying string" flag for subsequently encountered reqchars if
5631 it isn't already set and we have just passed a varying length item. */
5632
5633 END_REPEAT:
5634 previous = NULL;
5635 cd->req_varyopt |= reqvary;
5636 break;
5637
5638
5639 /* ===================================================================*/
5640 /* Start of nested parenthesized sub-expression, or comment or lookahead or
5641 lookbehind or option setting or condition or all the other extended
5642 parenthesis forms. */
5643
5644 case CHAR_LEFT_PARENTHESIS:
5645 newoptions = options;
5646 skipbytes = 0;
5647 bravalue = OP_CBRA;
5648 save_hwm = cd->hwm;
5649 reset_bracount = FALSE;
5650
5651 /* First deal with various "verbs" that can be introduced by '*'. */
5652
5653 ptr++;
5654 if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
5655 || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
5656 {
5657 int i, namelen;
5658 int arglen = 0;
5659 const char *vn = verbnames;
5660 const pcre_uchar *name = ptr + 1;
5661 const pcre_uchar *arg = NULL;
5662 previous = NULL;
5663 ptr++;
5664 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
5665 namelen = (int)(ptr - name);
5666
5667 /* It appears that Perl allows any characters whatsoever, other than
5668 a closing parenthesis, to appear in arguments, so we no longer insist on
5669 letters, digits, and underscores. */
5670
5671 if (*ptr == CHAR_COLON)
5672 {
5673 arg = ++ptr;
5674 while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5675 arglen = (int)(ptr - arg);
5676 if ((unsigned int)arglen > MAX_MARK)
5677 {
5678 *errorcodeptr = ERR75;
5679 goto FAILED;
5680 }
5681 }
5682
5683 if (*ptr != CHAR_RIGHT_PARENTHESIS)
5684 {
5685 *errorcodeptr = ERR60;
5686 goto FAILED;
5687 }
5688
5689 /* Scan the table of verb names */
5690
5691 for (i = 0; i < verbcount; i++)
5692 {
5693 if (namelen == verbs[i].len &&
5694 STRNCMP_UC_C8(name, vn, namelen) == 0)
5695 {
5696 int setverb;
5697
5698 /* Check for open captures before ACCEPT and convert it to
5699 ASSERT_ACCEPT if in an assertion. */
5700
5701 if (verbs[i].op == OP_ACCEPT)
5702 {
5703 open_capitem *oc;
5704 if (arglen != 0)
5705 {
5706 *errorcodeptr = ERR59;
5707 goto FAILED;
5708 }
5709 cd->had_accept = TRUE;
5710 for (oc = cd->open_caps; oc != NULL; oc = oc->next)
5711 {
5712 *code++ = OP_CLOSE;
5713 PUT2INC(code, 0, oc->number);
5714 }
5715 setverb = *code++ =
5716 (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5717
5718 /* Do not set firstchar after *ACCEPT */
5719 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
5720 }
5721
5722 /* Handle other cases with/without an argument */
5723
5724 else if (arglen == 0)
5725 {
5726 if (verbs[i].op < 0) /* Argument is mandatory */
5727 {
5728 *errorcodeptr = ERR66;
5729 goto FAILED;
5730 }
5731 setverb = *code++ = verbs[i].op;
5732 }
5733
5734 else
5735 {
5736 if (verbs[i].op_arg < 0) /* Argument is forbidden */
5737 {
5738 *errorcodeptr = ERR59;
5739 goto FAILED;
5740 }
5741 setverb = *code++ = verbs[i].op_arg;
5742 *code++ = arglen;
5743 memcpy(code, arg, IN_UCHARS(arglen));
5744 code += arglen;
5745 *code++ = 0;
5746 }
5747
5748 switch (setverb)
5749 {
5750 case OP_THEN:
5751 case OP_THEN_ARG:
5752 cd->external_flags |= PCRE_HASTHEN;
5753 break;
5754
5755 case OP_PRUNE:
5756 case OP_PRUNE_ARG:
5757 case OP_SKIP:
5758 case OP_SKIP_ARG:
5759 cd->had_pruneorskip = TRUE;
5760 break;
5761 }
5762
5763 break; /* Found verb, exit loop */
5764 }
5765
5766 vn += verbs[i].len + 1;
5767 }
5768
5769 if (i < verbcount) continue; /* Successfully handled a verb */
5770 *errorcodeptr = ERR60; /* Verb not recognized */
5771 goto FAILED;
5772 }
5773
5774 /* Deal with the extended parentheses; all are introduced by '?', and the
5775 appearance of any of them means that this is not a capturing group. */
5776
5777 else if (*ptr == CHAR_QUESTION_MARK)
5778 {
5779 int i, set, unset, namelen;
5780 int *optset;
5781 const pcre_uchar *name;
5782 pcre_uchar *slot;
5783
5784 switch (*(++ptr))
5785 {
5786 case CHAR_NUMBER_SIGN: /* Comment; skip to ket */
5787 ptr++;
5788 while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5789 if (*ptr == CHAR_NULL)
5790 {
5791 *errorcodeptr = ERR18;
5792 goto FAILED;
5793 }
5794 continue;
5795
5796
5797 /* ------------------------------------------------------------ */
5798 case CHAR_VERTICAL_LINE: /* Reset capture count for each branch */
5799 reset_bracount = TRUE;
5800 /* Fall through */
5801
5802 /* ------------------------------------------------------------ */
5803 case CHAR_COLON: /* Non-capturing bracket */
5804 bravalue = OP_BRA;
5805 ptr++;
5806 break;
5807
5808
5809 /* ------------------------------------------------------------ */
5810 case CHAR_LEFT_PARENTHESIS:
5811 bravalue = OP_COND; /* Conditional group */
5812 tempptr = ptr;
5813
5814 /* A condition can be an assertion, a number (referring to a numbered
5815 group), a name (referring to a named group), or 'R', referring to
5816 recursion. R<digits> and R&name are also permitted for recursion tests.
5817
5818 There are several syntaxes for testing a named group: (?(name)) is used
5819 by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
5820
5821 There are two unfortunate ambiguities, caused by history. (a) 'R' can
5822 be the recursive thing or the name 'R' (and similarly for 'R' followed
5823 by digits), and (b) a number could be a name that consists of digits.
5824 In both cases, we look for a name first; if not found, we try the other
5825 cases.
5826
5827 For compatibility with auto-callouts, we allow a callout to be
5828 specified before a condition that is an assertion. First, check for the
5829 syntax of a callout; if found, adjust the temporary pointer that is
5830 used to check for an assertion condition. That's all that is needed! */
5831
5832 if (ptr[1] == CHAR_QUESTION_MARK && ptr[2] == CHAR_C)
5833 {
5834 for (i = 3;; i++) if (!IS_DIGIT(ptr[i])) break;
5835 if (ptr[i] == CHAR_RIGHT_PARENTHESIS)
5836 tempptr += i + 1;
5837 }
5838
5839 /* For conditions that are assertions, check the syntax, and then exit
5840 the switch. This will take control down to where bracketed groups,
5841 including assertions, are processed. */
5842
5843 if (tempptr[1] == CHAR_QUESTION_MARK &&
5844 (tempptr[2] == CHAR_EQUALS_SIGN ||
5845 tempptr[2] == CHAR_EXCLAMATION_MARK ||
5846 tempptr[2] == CHAR_LESS_THAN_SIGN))
5847 break;
5848
5849 /* Most other conditions use OP_CREF (a couple change to OP_RREF
5850 below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5851
5852 code[1+LINK_SIZE] = OP_CREF;
5853 skipbytes = 1+IMM2_SIZE;
5854 refsign = -1;
5855
5856 /* Check for a test for recursion in a named group. */
5857
5858 if (ptr[1] == CHAR_R && ptr[2] == CHAR_AMPERSAND)
5859 {
5860 terminator = -1;
5861 ptr += 2;
5862 code[1+LINK_SIZE] = OP_RREF; /* Change the type of test */
5863 }
5864
5865 /* Check for a test for a named group's having been set, using the Perl
5866 syntax (?(<name>) or (?('name') */
5867
5868 else if (ptr[1] == CHAR_LESS_THAN_SIGN)
5869 {
5870 terminator = CHAR_GREATER_THAN_SIGN;
5871 ptr++;
5872 }
5873 else if (ptr[1] == CHAR_APOSTROPHE)
5874 {
5875 terminator = CHAR_APOSTROPHE;
5876 ptr++;
5877 }
5878 else
5879 {
5880 terminator = CHAR_NULL;
5881 if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);
5882 }
5883
5884 /* We now expect to read a name; any thing else is an error */
5885
5886 if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
5887 {
5888 ptr += 1; /* To get the right offset */
5889 *errorcodeptr = ERR28;
5890 goto FAILED;
5891 }
5892
5893 /* Read the name, but also get it as a number if it's all digits */
5894
5895 recno = 0;
5896 name = ++ptr;
5897 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
5898 {
5899 if (recno >= 0)
5900 recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;
5901 ptr++;
5902 }
5903 namelen = (int)(ptr - name);
5904
5905 if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
5906 *ptr++ != CHAR_RIGHT_PARENTHESIS)
5907 {
5908 ptr--; /* Error offset */
5909 *errorcodeptr = ERR26;
5910 goto FAILED;
5911 }
5912
5913 /* Do no further checking in the pre-compile phase. */
5914
5915 if (lengthptr != NULL) break;
5916
5917 /* In the real compile we do the work of looking for the actual
5918 reference. If the string started with "+" or "-" we require the rest to
5919 be digits, in which case recno will be set. */
5920
5921 if (refsign > 0)
5922 {
5923 if (recno <= 0)
5924 {
5925 *errorcodeptr = ERR58;
5926 goto FAILED;
5927 }
5928 recno = (refsign == CHAR_MINUS)?
5929 cd->bracount - recno + 1 : recno +cd->bracount;
5930 if (recno <= 0 || recno > cd->final_bracount)
5931 {
5932 *errorcodeptr = ERR15;
5933 goto FAILED;
5934 }
5935 PUT2(code, 2+LINK_SIZE, recno);
5936 break;
5937 }
5938
5939 /* Otherwise (did not start with "+" or "-"), start by looking for the
5940 name. If we find a name, add one to the opcode to change OP_CREF or
5941 OP_RREF into OP_NCREF or OP_NRREF. These behave exactly the same,
5942 except they record that the reference was originally to a name. The
5943 information is used to check duplicate names. */
5944
5945 slot = cd->name_table;
5946 for (i = 0; i < cd->names_found; i++)
5947 {
5948 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
5949 slot += cd->name_entry_size;
5950 }
5951
5952 /* Found a previous named subpattern */
5953
5954 if (i < cd->names_found)
5955 {
5956 recno = GET2(slot, 0);
5957 PUT2(code, 2+LINK_SIZE, recno);
5958 code[1+LINK_SIZE]++;
5959 }
5960
5961 /* Search the pattern for a forward reference */
5962
5963 else if ((i = find_parens(cd, name, namelen,
5964 (options & PCRE_EXTENDED) != 0, utf)) > 0)
5965 {
5966 PUT2(code, 2+LINK_SIZE, i);
5967 code[1+LINK_SIZE]++;
5968 }
5969
5970 /* If terminator == CHAR_NULL it means that the name followed directly
5971 after the opening parenthesis [e.g. (?(abc)...] and in this case there
5972 are some further alternatives to try. For the cases where terminator !=
5973 0 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
5974 now checked all the possibilities, so give an error. */
5975
5976 else if (terminator != CHAR_NULL)
5977 {
5978 *errorcodeptr = ERR15;
5979 goto FAILED;
5980 }
5981
5982 /* Check for (?(R) for recursion. Allow digits after R to specify a
5983 specific group number. */
5984
5985 else if (*name == CHAR_R)
5986 {
5987 recno = 0;
5988 for (i = 1; i < namelen; i++)
5989 {
5990 if (!IS_DIGIT(name[i]))
5991 {
5992 *errorcodeptr = ERR15;
5993 goto FAILED;
5994 }
5995 recno = recno * 10 + name[i] - CHAR_0;
5996 }
5997 if (recno == 0) recno = RREF_ANY;
5998 code[1+LINK_SIZE] = OP_RREF; /* Change test type */
5999 PUT2(code, 2+LINK_SIZE, recno);
6000 }
6001
6002 /* Similarly, check for the (?(DEFINE) "condition", which is always
6003 false. */
6004
6005 else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
6006 {
6007 code[1+LINK_SIZE] = OP_DEF;
6008 skipbytes = 1;
6009 }
6010
6011 /* Check for the "name" actually being a subpattern number. We are
6012 in the second pass here, so final_bracount is set. */
6013
6014 else if (recno > 0 && recno <= cd->final_bracount)
6015 {
6016 PUT2(code, 2+LINK_SIZE, recno);
6017 }
6018
6019 /* Either an unidentified subpattern, or a reference to (?(0) */
6020
6021 else
6022 {
6023 *errorcodeptr = (recno == 0)? ERR35: ERR15;
6024 goto FAILED;
6025 }
6026 break;
6027
6028
6029 /* ------------------------------------------------------------ */
6030 case CHAR_EQUALS_SIGN: /* Positive lookahead */
6031 bravalue = OP_ASSERT;
6032 cd->assert_depth += 1;
6033 ptr++;
6034 break;
6035
6036
6037 /* ------------------------------------------------------------ */
6038 case CHAR_EXCLAMATION_MARK: /* Negative lookahead */
6039 ptr++;
6040 if (*ptr == CHAR_RIGHT_PARENTHESIS) /* Optimize (?!) */
6041 {
6042 *code++ = OP_FAIL;
6043 previous = NULL;
6044 continue;
6045 }
6046 bravalue = OP_ASSERT_NOT;
6047 cd->assert_depth += 1;
6048 break;
6049
6050
6051 /* ------------------------------------------------------------ */
6052 case CHAR_LESS_THAN_SIGN: /* Lookbehind or named define */
6053 switch (ptr[1])
6054 {
6055 case CHAR_EQUALS_SIGN: /* Positive lookbehind */
6056 bravalue = OP_ASSERTBACK;
6057 cd->assert_depth += 1;
6058 ptr += 2;
6059 break;
6060
6061 case CHAR_EXCLAMATION_MARK: /* Negative lookbehind */
6062 bravalue = OP_ASSERTBACK_NOT;
6063 cd->assert_depth += 1;
6064 ptr += 2;
6065 break;
6066
6067 default: /* Could be name define, else bad */
6068 if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0)
6069 goto DEFINE_NAME;
6070 ptr++; /* Correct offset for error */
6071 *errorcodeptr = ERR24;
6072 goto FAILED;
6073 }
6074 break;
6075
6076
6077 /* ------------------------------------------------------------ */
6078 case CHAR_GREATER_THAN_SIGN: /* One-time brackets */
6079 bravalue = OP_ONCE;
6080 ptr++;
6081 break;
6082
6083
6084 /* ------------------------------------------------------------ */
6085 case CHAR_C: /* Callout - may be followed by digits; */
6086 previous_callout = code; /* Save for later completion */
6087 after_manual_callout = 1; /* Skip one item before completing */
6088 *code++ = OP_CALLOUT;
6089 {
6090 int n = 0;
6091 ptr++;
6092 while(IS_DIGIT(*ptr))
6093 n = n * 10 + *ptr++ - CHAR_0;
6094 if (*ptr != CHAR_RIGHT_PARENTHESIS)
6095 {
6096 *errorcodeptr = ERR39;
6097 goto FAILED;
6098 }
6099 if (n > 255)
6100 {
6101 *errorcodeptr = ERR38;
6102 goto FAILED;
6103 }
6104 *code++ = n;
6105 PUT(code, 0, (int)(ptr - cd->start_pattern + 1)); /* Pattern offset */
6106 PUT(code, LINK_SIZE, 0); /* Default length */
6107 code += 2 * LINK_SIZE;
6108 }
6109 previous = NULL;
6110 continue;
6111
6112
6113 /* ------------------------------------------------------------ */
6114 case CHAR_P: /* Python-style named subpattern handling */
6115 if (*(++ptr) == CHAR_EQUALS_SIGN ||
6116 *ptr == CHAR_GREATER_THAN_SIGN) /* Reference or recursion */
6117 {
6118 is_recurse = *ptr == CHAR_GREATER_THAN_SIGN;
6119 terminator = CHAR_RIGHT_PARENTHESIS;
6120 goto NAMED_REF_OR_RECURSE;
6121 }
6122 else if (*ptr != CHAR_LESS_THAN_SIGN) /* Test for Python-style defn */
6123 {
6124 *errorcodeptr = ERR41;
6125 goto FAILED;
6126 }
6127 /* Fall through to handle (?P< as (?< is handled */
6128
6129
6130 /* ------------------------------------------------------------ */
6131 DEFINE_NAME: /* Come here from (?< handling */
6132 case CHAR_APOSTROPHE:
6133 {
6134 terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
6135 CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
6136 name = ++ptr;
6137
6138 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6139 namelen = (int)(ptr - name);
6140
6141 /* In the pre-compile phase, just do a syntax check. */
6142
6143 if (lengthptr != NULL)
6144 {
6145 if (*ptr != (pcre_uchar)terminator)
6146 {
6147 *errorcodeptr = ERR42;
6148 goto FAILED;
6149 }
6150 if (cd->names_found >= MAX_NAME_COUNT)
6151 {
6152 *errorcodeptr = ERR49;
6153 goto FAILED;
6154 }
6155 if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6156 {
6157 cd->name_entry_size = namelen + IMM2_SIZE + 1;
6158 if (namelen > MAX_NAME_SIZE)
6159 {
6160 *errorcodeptr = ERR48;
6161 goto FAILED;
6162 }
6163 }
6164 }
6165
6166 /* In the real compile, create the entry in the table, maintaining
6167 alphabetical order. Duplicate names for different numbers are
6168 permitted only if PCRE_DUPNAMES is set. Duplicate names for the same
6169 number are always OK. (An existing number can be re-used if (?|
6170 appears in the pattern.) In either event, a duplicate name results in
6171 a duplicate entry in the table, even if the number is the same. This
6172 is because the number of names, and hence the table size, is computed
6173 in the pre-compile, and it affects various numbers and pointers which
6174 would all have to be modified, and the compiled code moved down, if
6175 duplicates with the same number were omitted from the table. This
6176 doesn't seem worth the hassle. However, *different* names for the
6177 same number are not permitted. */
6178
6179 else
6180 {
6181 BOOL dupname = FALSE;
6182 slot = cd->name_table;
6183
6184 for (i = 0; i < cd->names_found; i++)
6185 {
6186 int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));
6187 if (crc == 0)
6188 {
6189 if (slot[IMM2_SIZE+namelen] == 0)
6190 {
6191 if (GET2(slot, 0) != cd->bracount + 1 &&
6192 (options & PCRE_DUPNAMES) == 0)
6193 {
6194 *errorcodeptr = ERR43;
6195 goto FAILED;
6196 }
6197 else dupname = TRUE;
6198 }
6199 else crc = -1; /* Current name is a substring */
6200 }
6201
6202 /* Make space in the table and break the loop for an earlier
6203 name. For a duplicate or later name, carry on. We do this for
6204 duplicates so that in the simple case (when ?(| is not used) they
6205 are in order of their numbers. */
6206
6207 if (crc < 0)
6208 {
6209 memmove(slot + cd->name_entry_size, slot,
6210 IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
6211 break;
6212 }
6213
6214 /* Continue the loop for a later or duplicate name */
6215
6216 slot += cd->name_entry_size;
6217 }
6218
6219 /* For non-duplicate names, check for a duplicate number before
6220 adding the new name. */
6221
6222 if (!dupname)
6223 {
6224 pcre_uchar *cslot = cd->name_table;
6225 for (i = 0; i < cd->names_found; i++)
6226 {
6227 if (cslot != slot)
6228 {
6229 if (GET2(cslot, 0) == cd->bracount + 1)
6230 {
6231 *errorcodeptr = ERR65;
6232 goto FAILED;
6233 }
6234 }
6235 else i--;
6236 cslot += cd->name_entry_size;
6237 }
6238 }
6239
6240 PUT2(slot, 0, cd->bracount + 1);
6241 memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));
6242 slot[IMM2_SIZE + namelen] = 0;
6243 }
6244 }
6245
6246 /* In both pre-compile and compile, count the number of names we've
6247 encountered. */
6248
6249 cd->names_found++;
6250 ptr++; /* Move past > or ' */
6251 goto NUMBERED_GROUP;
6252
6253
6254 /* ------------------------------------------------------------ */
6255 case CHAR_AMPERSAND: /* Perl recursion/subroutine syntax */
6256 terminator = CHAR_RIGHT_PARENTHESIS;
6257 is_recurse = TRUE;
6258 /* Fall through */
6259
6260 /* We come here from the Python syntax above that handles both
6261 references (?P=name) and recursion (?P>name), as well as falling
6262 through from the Perl recursion syntax (?&name). We also come here from
6263 the Perl \k<name> or \k'name' back reference syntax and the \k{name}
6264 .NET syntax, and the Oniguruma \g<...> and \g'...' subroutine syntax. */
6265
6266 NAMED_REF_OR_RECURSE:
6267 name = ++ptr;
6268 while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6269 namelen = (int)(ptr - name);
6270
6271 /* In the pre-compile phase, do a syntax check. We used to just set
6272 a dummy reference number, because it was not used in the first pass.
6273 However, with the change of recursive back references to be atomic,
6274 we have to look for the number so that this state can be identified, as
6275 otherwise the incorrect length is computed. If it's not a backwards
6276 reference, the dummy number will do. */
6277
6278 if (lengthptr != NULL)
6279 {
6280 const pcre_uchar *temp;
6281
6282 if (namelen == 0)
6283 {
6284 *errorcodeptr = ERR62;
6285 goto FAILED;
6286 }
6287 if (*ptr != (pcre_uchar)terminator)
6288 {
6289 *errorcodeptr = ERR42;
6290 goto FAILED;
6291 }
6292 if (namelen > MAX_NAME_SIZE)
6293 {
6294 *errorcodeptr = ERR48;
6295 goto FAILED;
6296 }
6297
6298 /* The name table does not exist in the first pass, so we cannot
6299 do a simple search as in the code below. Instead, we have to scan the
6300 pattern to find the number. It is important that we scan it only as
6301 far as we have got because the syntax of named subpatterns has not
6302 been checked for the rest of the pattern, and find_parens() assumes
6303 correct syntax. In any case, it's a waste of resources to scan
6304 further. We stop the scan at the current point by temporarily
6305 adjusting the value of cd->endpattern. */
6306
6307 temp = cd->end_pattern;
6308 cd->end_pattern = ptr;
6309 recno = find_parens(cd, name, namelen,
6310 (options & PCRE_EXTENDED) != 0, utf);
6311 cd->end_pattern = temp;
6312 if (recno < 0) recno = 0; /* Forward ref; set dummy number */
6313 }
6314
6315 /* In the real compile, seek the name in the table. We check the name
6316 first, and then check that we have reached the end of the name in the
6317 table. That way, if the name that is longer than any in the table,
6318 the comparison will fail without reading beyond the table entry. */
6319
6320 else
6321 {
6322 slot = cd->name_table;
6323 for (i = 0; i < cd->names_found; i++)
6324 {
6325 if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
6326 slot[IMM2_SIZE+namelen] == 0)
6327 break;
6328 slot += cd->name_entry_size;
6329 }
6330
6331 if (i < cd->names_found) /* Back reference */
6332 {
6333 recno = GET2(slot, 0);
6334 }
6335 else if ((recno = /* Forward back reference */
6336 find_parens(cd, name, namelen,
6337 (options & PCRE_EXTENDED) != 0, utf)) <= 0)
6338 {
6339 *errorcodeptr = ERR15;
6340 goto FAILED;
6341 }
6342 }
6343
6344 /* In both phases, we can now go to the code than handles numerical
6345 recursion or backreferences. */
6346
6347 if (is_recurse) goto HANDLE_RECURSION;
6348 else goto HANDLE_REFERENCE;
6349
6350
6351 /* ------------------------------------------------------------ */
6352 case CHAR_R: /* Recursion */
6353 ptr++; /* Same as (?0) */
6354 /* Fall through */
6355
6356
6357 /* ------------------------------------------------------------ */
6358 case CHAR_MINUS: case CHAR_PLUS: /* Recursion or subroutine */
6359 case CHAR_0: case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4:
6360 case CHAR_5: case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
6361 {
6362 const pcre_uchar *called;
6363 terminator = CHAR_RIGHT_PARENTHESIS;
6364
6365 /* Come here from the \g<...> and \g'...' code (Oniguruma
6366 compatibility). However, the syntax has been checked to ensure that
6367 the ... are a (signed) number, so that neither ERR63 nor ERR29 will
6368 be called on this path, nor with the jump to OTHER_CHAR_AFTER_QUERY
6369 ever be taken. */
6370
6371 HANDLE_NUMERICAL_RECURSION:
6372
6373 if ((refsign = *ptr) == CHAR_PLUS)
6374 {
6375 ptr++;
6376 if (!IS_DIGIT(*ptr))
6377 {
6378 *errorcodeptr = ERR63;
6379 goto FAILED;
6380 }
6381 }
6382 else if (refsign == CHAR_MINUS)
6383 {
6384 if (!IS_DIGIT(ptr[1]))
6385 goto OTHER_CHAR_AFTER_QUERY;
6386 ptr++;
6387 }
6388
6389 recno = 0;
6390 while(IS_DIGIT(*ptr))
6391 recno = recno * 10 + *ptr++ - CHAR_0;
6392
6393 if (*ptr != (pcre_uchar)terminator)
6394 {
6395 *errorcodeptr = ERR29;
6396 goto FAILED;
6397 }
6398
6399 if (refsign == CHAR_MINUS)
6400 {
6401 if (recno == 0)
6402 {
6403 *errorcodeptr = ERR58;
6404 goto FAILED;
6405 }
6406 recno = cd->bracount - recno + 1;
6407 if (recno <= 0)
6408 {
6409 *errorcodeptr = ERR15;
6410 goto FAILED;
6411 }
6412 }
6413 else if (refsign == CHAR_PLUS)
6414 {
6415 if (recno == 0)
6416 {
6417 *errorcodeptr = ERR58;
6418 goto FAILED;
6419 }
6420 recno += cd->bracount;
6421 }
6422
6423 /* Come here from code above that handles a named recursion */
6424
6425 HANDLE_RECURSION:
6426
6427 previous = code;
6428 called = cd->start_code;
6429
6430 /* When we are actually compiling, find the bracket that is being
6431 referenced. Temporarily end the regex in case it doesn't exist before
6432 this point. If we end up with a forward reference, first check that
6433 the bracket does occur later so we can give the error (and position)
6434 now. Then remember this forward reference in the workspace so it can
6435 be filled in at the end. */
6436
6437 if (lengthptr == NULL)
6438 {
6439 *code = OP_END;
6440 if (recno != 0)
6441 called = PRIV(find_bracket)(cd->start_code, utf, recno);
6442
6443 /* Forward reference */
6444
6445 if (called == NULL)
6446 {
6447 if (find_parens(cd, NULL, recno,
6448 (options & PCRE_EXTENDED) != 0, utf) < 0)
6449 {
6450 *errorcodeptr = ERR15;
6451 goto FAILED;
6452 }
6453
6454 /* Fudge the value of "called" so that when it is inserted as an
6455 offset below, what it actually inserted is the reference number
6456 of the group. Then remember the forward reference. */
6457
6458 called = cd->start_code + recno;
6459 if (cd->hwm >= cd->start_workspace + cd->workspace_size -
6460 WORK_SIZE_SAFETY_MARGIN)
6461 {
6462 *errorcodeptr = expand_workspace(cd);
6463 if (*errorcodeptr != 0) goto FAILED;
6464 }
6465 PUTINC(cd->hwm, 0, (int)(code + 1 - cd->start_code));
6466 }
6467
6468 /* If not a forward reference, and the subpattern is still open,
6469 this is a recursive call. We check to see if this is a left
6470 recursion that could loop for ever, and diagnose that case. We
6471 must not, however, do this check if we are in a conditional
6472 subpattern because the condition might be testing for recursion in
6473 a pattern such as /(?(R)a+|(?R)b)/, which is perfectly valid.
6474 Forever loops are also detected at runtime, so those that occur in
6475 conditional subpatterns will be picked up then. */
6476
6477 else if (GET(called, 1) == 0 && cond_depth <= 0 &&
6478 could_be_empty(called, code, bcptr, utf, cd))
6479 {
6480 *errorcodeptr = ERR40;
6481 goto FAILED;
6482 }
6483 }
6484
6485 /* Insert the recursion/subroutine item. It does not have a set first
6486 character (relevant if it is repeated, because it will then be
6487 wrapped with ONCE brackets). */
6488
6489 *code = OP_RECURSE;
6490 PUT(code, 1, (int)(called - cd->start_code));
6491 code += 1 + LINK_SIZE;
6492 groupsetfirstchar = FALSE;
6493 }
6494
6495 /* Can't determine a first byte now */
6496
6497 if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;
6498 continue;
6499
6500
6501 /* ------------------------------------------------------------ */
6502 default: /* Other characters: check option setting */
6503 OTHER_CHAR_AFTER_QUERY:
6504 set = unset = 0;
6505 optset = &set;
6506
6507 while (*ptr != CHAR_RIGHT_PARENTHESIS && *ptr != CHAR_COLON)
6508 {
6509 switch (*ptr++)
6510 {
6511 case CHAR_MINUS: optset = &unset; break;
6512
6513 case CHAR_J: /* Record that it changed in the external options */
6514 *optset |= PCRE_DUPNAMES;
6515 cd->external_flags |= PCRE_JCHANGED;
6516 break;
6517
6518 case CHAR_i: *optset |= PCRE_CASELESS; break;
6519 case CHAR_m: *optset |= PCRE_MULTILINE; break;
6520 case CHAR_s: *optset |= PCRE_DOTALL; break;
6521 case CHAR_x: *optset |= PCRE_EXTENDED; break;
6522 case CHAR_U: *optset |= PCRE_UNGREEDY; break;
6523 case CHAR_X: *optset |= PCRE_EXTRA; break;
6524
6525 default: *errorcodeptr = ERR12;
6526 ptr--; /* Correct the offset */
6527 goto FAILED;
6528 }
6529 }
6530
6531 /* Set up the changed option bits, but don't change anything yet. */
6532
6533 newoptions = (options | set) & (~unset);
6534
6535 /* If the options ended with ')' this is not the start of a nested
6536 group with option changes, so the options change at this level. If this
6537 item is right at the start of the pattern, the options can be
6538 abstracted and made external in the pre-compile phase, and ignored in
6539 the compile phase. This can be helpful when matching -- for instance in
6540 caseless checking of required bytes.
6541
6542 If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are
6543 definitely *not* at the start of the pattern because something has been
6544 compiled. In the pre-compile phase, however, the code pointer can have
6545 that value after the start, because it gets reset as code is discarded
6546 during the pre-compile. However, this can happen only at top level - if
6547 we are within parentheses, the starting BRA will still be present. At
6548 any parenthesis level, the length value can be used to test if anything
6549 has been compiled at that level. Thus, a test for both these conditions
6550 is necessary to ensure we correctly detect the start of the pattern in
6551 both phases.
6552
6553 If we are not at the pattern start, reset the greedy defaults and the
6554 case value for firstchar and reqchar. */
6555
6556 if (*ptr == CHAR_RIGHT_PARENTHESIS)
6557 {
6558 if (code == cd->start_code + 1 + LINK_SIZE &&
6559 (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))
6560 {
6561 cd->external_options = newoptions;
6562 }
6563 else
6564 {
6565 greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
6566 greedy_non_default = greedy_default ^ 1;
6567 req_caseopt = ((newoptions & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
6568 }
6569
6570 /* Change options at this level, and pass them back for use
6571 in subsequent branches. */
6572
6573 *optionsptr = options = newoptions;
6574 previous = NULL; /* This item can't be repeated */
6575 continue; /* It is complete */
6576 }
6577
6578 /* If the options ended with ':' we are heading into a nested group
6579 with possible change of options. Such groups are non-capturing and are
6580 not assertions of any kind. All we need to do is skip over the ':';
6581 the newoptions value is handled below. */
6582
6583 bravalue = OP_BRA;
6584 ptr++;
6585 } /* End of switch for character following (? */
6586 } /* End of (? handling */
6587
6588 /* Opening parenthesis not followed by '*' or '?'. If PCRE_NO_AUTO_CAPTURE
6589 is set, all unadorned brackets become non-capturing and behave like (?:...)
6590 brackets. */
6591
6592 else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
6593 {
6594 bravalue = OP_BRA;
6595 }
6596
6597 /* Else we have a capturing group. */
6598
6599 else
6600 {
6601 NUMBERED_GROUP:
6602 cd->bracount += 1;
6603 PUT2(code, 1+LINK_SIZE, cd->bracount);
6604 skipbytes = IMM2_SIZE;
6605 }
6606
6607 /* Process nested bracketed regex. Assertions used not to be repeatable,
6608 but this was changed for Perl compatibility, so all kinds can now be
6609 repeated. We copy code into a non-register variable (tempcode) in order to
6610 be able to pass its address because some compilers complain otherwise. */
6611
6612 previous = code; /* For handling repetition */
6613 *code = bravalue;
6614 tempcode = code;
6615 tempreqvary = cd->req_varyopt; /* Save value before bracket */
6616 tempbracount = cd->bracount; /* Save value before bracket */
6617 length_prevgroup = 0; /* Initialize for pre-compile phase */
6618
6619 if (!compile_regex(
6620 newoptions, /* The complete new option state */
6621 &tempcode, /* Where to put code (updated) */
6622 &ptr, /* Input pointer (updated) */
6623 errorcodeptr, /* Where to put an error message */
6624 (bravalue == OP_ASSERTBACK ||
6625 bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
6626 reset_bracount, /* True if (?| group */
6627 skipbytes, /* Skip over bracket number */
6628 cond_depth +
6629 ((bravalue == OP_COND)?1:0), /* Depth of condition subpatterns */
6630 &subfirstchar, /* For possible first char */
6631 &subfirstcharflags,
6632 &subreqchar, /* For possible last char */
6633 &subreqcharflags,
6634 bcptr, /* Current branch chain */
6635 cd, /* Tables block */
6636 (lengthptr == NULL)? NULL : /* Actual compile phase */
6637 &length_prevgroup /* Pre-compile phase */
6638 ))
6639 goto FAILED;
6640
6641 /* If this was an atomic group and there are no capturing groups within it,
6642 generate OP_ONCE_NC instead of OP_ONCE. */