/[pcre]/code/branches/pcre16/pcre_compile.c
ViewVC logotype

Contents of /code/branches/pcre16/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log


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