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

Contents of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


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