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

Contents of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


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