Add GRegex for regular expression matching. (#50075)
[platform/upstream/glib.git] / glib / pcre / pcre_compile.c
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-2006 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 #define NLBLOCK cd             /* Block containing newline information */
46 #define PSSTART start_pattern  /* Field containing processed string start */
47 #define PSEND   end_pattern    /* Field containing processed string end */
48
49
50 #include "pcre_internal.h"
51
52
53 /* When DEBUG is defined, we need the pcre_printint() function, which is also
54 used by pcretest. DEBUG is not defined when building a production library. */
55
56 #ifdef DEBUG
57 #include "pcre_printint.src"
58 #endif
59
60
61 /*************************************************
62 *      Code parameters and static tables         *
63 *************************************************/
64
65 /* This value specifies the size of stack workspace that is used during the
66 first pre-compile phase that determines how much memory is required. The regex
67 is partly compiled into this space, but the compiled parts are discarded as
68 soon as they can be, so that hopefully there will never be an overrun. The code
69 does, however, check for an overrun. The largest amount I've seen used is 218,
70 so this number is very generous.
71
72 The same workspace is used during the second, actual compile phase for
73 remembering forward references to groups so that they can be filled in at the
74 end. Each entry in this list occupies LINK_SIZE bytes, so even when LINK_SIZE
75 is 4 there is plenty of room. */
76
77 #define COMPILE_WORK_SIZE (4096)
78
79
80 /* Table for handling escaped characters in the range '0'-'z'. Positive returns
81 are simple data values; negative values are for special things like \d and so
82 on. Zero means further processing is needed (for things like \x), or the escape
83 is invalid. */
84
85 #if !EBCDIC   /* This is the "normal" table for ASCII systems */
86 static const short int escapes[] = {
87      0,      0,      0,      0,      0,      0,      0,      0,   /* 0 - 7 */
88      0,      0,    ':',    ';',    '<',    '=',    '>',    '?',   /* 8 - ? */
89    '@', -ESC_A, -ESC_B, -ESC_C, -ESC_D, -ESC_E,      0, -ESC_G,   /* @ - G */
90      0,      0,      0,      0,      0,      0,      0,      0,   /* H - O */
91 -ESC_P, -ESC_Q, -ESC_R, -ESC_S,      0,      0,      0, -ESC_W,   /* P - W */
92 -ESC_X,      0, -ESC_Z,    '[',   '\\',    ']',    '^',    '_',   /* X - _ */
93    '`',      7, -ESC_b,      0, -ESC_d,  ESC_e,  ESC_f,      0,   /* ` - g */
94      0,      0,      0, -ESC_k,      0,      0,  ESC_n,      0,   /* h - o */
95 -ESC_p,      0,  ESC_r, -ESC_s,  ESC_tee,    0,      0, -ESC_w,   /* p - w */
96      0,      0, -ESC_z                                            /* x - z */
97 };
98
99 #else         /* This is the "abnormal" table for EBCDIC systems */
100 static const short int escapes[] = {
101 /*  48 */     0,     0,      0,     '.',    '<',   '(',    '+',    '|',
102 /*  50 */   '&',     0,      0,       0,      0,     0,      0,      0,
103 /*  58 */     0,     0,    '!',     '$',    '*',   ')',    ';',    '~',
104 /*  60 */   '-',   '/',      0,       0,      0,     0,      0,      0,
105 /*  68 */     0,     0,    '|',     ',',    '%',   '_',    '>',    '?',
106 /*  70 */     0,     0,      0,       0,      0,     0,      0,      0,
107 /*  78 */     0,   '`',    ':',     '#',    '@',  '\'',    '=',    '"',
108 /*  80 */     0,     7, -ESC_b,       0, -ESC_d, ESC_e,  ESC_f,      0,
109 /*  88 */     0,     0,      0,     '{',      0,     0,      0,      0,
110 /*  90 */     0,     0, -ESC_k,     'l',      0, ESC_n,      0, -ESC_p,
111 /*  98 */     0, ESC_r,      0,     '}',      0,     0,      0,      0,
112 /*  A0 */     0,   '~', -ESC_s, ESC_tee,      0,     0, -ESC_w,      0,
113 /*  A8 */     0,-ESC_z,      0,       0,      0,   '[',      0,      0,
114 /*  B0 */     0,     0,      0,       0,      0,     0,      0,      0,
115 /*  B8 */     0,     0,      0,       0,      0,   ']',    '=',    '-',
116 /*  C0 */   '{',-ESC_A, -ESC_B,  -ESC_C, -ESC_D,-ESC_E,      0, -ESC_G,
117 /*  C8 */     0,     0,      0,       0,      0,     0,      0,      0,
118 /*  D0 */   '}',     0,      0,       0,      0,     0,      0, -ESC_P,
119 /*  D8 */-ESC_Q,-ESC_R,      0,       0,      0,     0,      0,      0,
120 /*  E0 */  '\\',     0, -ESC_S,       0,      0,     0, -ESC_W, -ESC_X,
121 /*  E8 */     0,-ESC_Z,      0,       0,      0,     0,      0,      0,
122 /*  F0 */     0,     0,      0,       0,      0,     0,      0,      0,
123 /*  F8 */     0,     0,      0,       0,      0,     0,      0,      0
124 };
125 #endif
126
127
128 /* Tables of names of POSIX character classes and their lengths. The list is
129 terminated by a zero length entry. The first three must be alpha, lower, upper,
130 as this is assumed for handling case independence. */
131
132 static const char posix_names[] =
133   "alpha\0" 
134   "lower\0" 
135   "upper\0"
136   "alnum\0" 
137   "ascii\0" 
138   "blank\0" 
139   "cntrl\0" 
140   "digit\0" 
141   "graph\0"
142   "print\0" 
143   "punct\0" 
144   "space\0" 
145   "word\0"  
146   "xdigit";
147
148 static const uschar posix_name_lengths[] = {
149   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
150
151 /* Table of class bit maps for each POSIX class. Each class is formed from a
152 base map, with an optional addition or removal of another map. Then, for some
153 classes, there is some additional tweaking: for [:blank:] the vertical space
154 characters are removed, and for [:alpha:] and [:alnum:] the underscore
155 character is removed. The triples in the table consist of the base map offset,
156 second map offset or -1 if no second map, and a non-negative value for map
157 addition or a negative value for map subtraction (if there are two maps). The
158 absolute value of the third field has these meanings: 0 => no tweaking, 1 =>
159 remove vertical space characters, 2 => remove underscore. */
160
161 static const int posix_class_maps[] = {
162   cbit_word,  cbit_digit, -2,             /* alpha */
163   cbit_lower, -1,          0,             /* lower */
164   cbit_upper, -1,          0,             /* upper */
165   cbit_word,  -1,          2,             /* alnum - word without underscore */
166   cbit_print, cbit_cntrl,  0,             /* ascii */
167   cbit_space, -1,          1,             /* blank - a GNU extension */
168   cbit_cntrl, -1,          0,             /* cntrl */
169   cbit_digit, -1,          0,             /* digit */
170   cbit_graph, -1,          0,             /* graph */
171   cbit_print, -1,          0,             /* print */
172   cbit_punct, -1,          0,             /* punct */
173   cbit_space, -1,          0,             /* space */
174   cbit_word,  -1,          0,             /* word - a Perl extension */
175   cbit_xdigit,-1,          0              /* xdigit */
176 };
177
178
179 #define STRING(a)  # a
180 #define XSTRING(s) STRING(s)
181
182 /* The texts of compile-time error messages. These are "char *" because they
183 are passed to the outside world. Do not ever re-use any error number, because
184 they are documented. Always add a new error instead. Messages marked DEAD below
185 are no longer used. */
186
187 #define DEAD(s) "\0"
188
189 static const char error_texts[] =
190   "no error\0"
191   "\\ at end of pattern\0"
192   "\\c at end of pattern\0"
193   "unrecognized character follows \\\0"
194   "numbers out of order in {} quantifier\0"
195   /* 5 */
196   "number too big in {} quantifier\0"
197   "missing terminating ] for character class\0"
198   "invalid escape sequence in character class\0"
199   "range out of order in character class\0"
200   "nothing to repeat\0"
201   /* 10 */
202   DEAD("operand of unlimited repeat could match the empty string")
203   "internal error: unexpected repeat\0"
204   "unrecognized character after (?\0"
205   "POSIX named classes are supported only within a class\0"
206   "missing )\0"
207   /* 15 */
208   "reference to non-existent subpattern\0"
209   "erroffset passed as NULL\0"
210   "unknown option bit(s) set\0"
211   "missing ) after comment\0"
212   DEAD("parentheses nested too deeply")
213   /* 20 */
214   "regular expression too large\0"
215   "failed to get memory\0"
216   "unmatched parentheses\0"
217   "internal error: code overflow\0"
218   "unrecognized character after (?<\0"
219   /* 25 */
220   "lookbehind assertion is not fixed length\0"
221   "malformed number or name after (?(\0"
222   "conditional group contains more than two branches\0"
223   "assertion expected after (?(\0"
224   "(?R or (?digits must be followed by )\0"
225   /* 30 */
226   "unknown POSIX class name\0"
227   "POSIX collating elements are not supported\0"
228   "this version of PCRE is not compiled with PCRE_UTF8 support\0"
229   DEAD("spare error")
230   "character value in \\x{...} sequence is too large\0"
231   /* 35 */
232   "invalid condition (?(0)\0"
233   "\\C not allowed in lookbehind assertion\0"
234   "PCRE does not support \\L, \\l, \\N, \\U, or \\u\0"
235   "number after (?C is > 255\0"
236   "closing ) for (?C expected\0"
237   /* 40 */
238   "recursive call could loop indefinitely\0"
239   "unrecognized character after (?P\0"
240   "syntax error in subpattern name (missing terminator)\0"
241   "two named subpatterns have the same name\0"
242   "invalid UTF-8 string\0"
243   /* 45 */
244   "support for \\P, \\p, and \\X has not been compiled\0"
245   "malformed \\P or \\p sequence\0"
246   "unknown property name after \\P or \\p\0"
247   "subpattern name is too long (maximum " XSTRING(MAX_NAME_SIZE) " characters)\0"
248   "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
249   /* 50 */
250   "repeated subpattern is too long\0"
251   "octal value is greater than \\377 (not in UTF-8 mode)\0"
252   "internal error: overran compiling workspace\0"
253   "internal error: previously-checked referenced subpattern not found\0"
254   "DEFINE group contains more than one branch\0"
255   /* 55 */
256   "repeating a DEFINE group is not allowed\0"
257   "inconsistent NEWLINE options\0"
258   "\\g is not followed by an (optionally braced) non-zero number";
259
260 static const int error_texts_offsets[] = {
261   0,
262   9,
263   29,
264   50,
265   83,
266   121,
267   153,
268   195,
269   238,
270   276,
271   294,
272   295,
273   329,
274   361,
275   415,
276   425,
277   462,
278   487,
279   513,
280   537,
281   538,
282   567,
283   588,
284   610,
285   640,
286   673,
287   714,
288   749,
289   799,
290   828,
291   866,
292   891,
293   934,
294   994,
295   995,
296   1044,
297   1068,
298   1107,
299   1151,
300   1177,
301   1204,
302   1243,
303   1276,
304   1329,
305   1370,
306   1391,
307   1440,
308   1468,
309   1505,
310   1557,
311   1600,
312   1632,
313   1685,
314   1729,
315   1796,
316   1839,
317   1879,
318   1908
319 };
320
321
322 /* Definition to allow mutual recursion */
323
324 static BOOL
325   compile_regex(int, int, uschar **, const uschar **, int *, BOOL, int, int *,
326     int *, branch_chain *, compile_data *, int *);
327
328
329
330 /*************************************************
331 *            Handle escapes                      *
332 *************************************************/
333
334 /* This function is called when a \ has been encountered. It either returns a
335 positive value for a simple escape such as \n, or a negative value which
336 encodes one of the more complicated things such as \d. A backreference to group
337 n is returned as -(ESC_REF + n); ESC_REF is the highest ESC_xxx macro. When
338 UTF-8 is enabled, a positive value greater than 255 may be returned. On entry,
339 ptr is pointing at the \. On exit, it is on the final character of the escape
340 sequence.
341
342 Arguments:
343   ptrptr         points to the pattern position pointer
344   errorcodeptr   points to the errorcode variable
345   bracount       number of previous extracting brackets
346   options        the options bits
347   isclass        TRUE if inside a character class
348
349 Returns:         zero or positive => a data character
350                  negative => a special escape sequence
351                  on error, errorptr is set
352 */
353
354 static int
355 check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,
356   int options, BOOL isclass)
357 {
358 BOOL utf8 = (options & PCRE_UTF8) != 0;
359 const uschar *ptr = *ptrptr + 1;
360 int c, i;
361
362 GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
363 ptr--;                            /* Set pointer back to the last byte */
364
365 /* If backslash is at the end of the pattern, it's an error. */
366
367 if (c == 0) *errorcodeptr = ERR1;
368
369 /* Non-alphamerics are literals. For digits or letters, do an initial lookup in
370 a table. A non-zero result is something that can be returned immediately.
371 Otherwise further processing may be required. */
372
373 #if !EBCDIC    /* ASCII coding */
374 else if (c < '0' || c > 'z') {}                           /* Not alphameric */
375 else if ((i = escapes[c - '0']) != 0) c = i;
376
377 #else          /* EBCDIC coding */
378 else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphameric */
379 else if ((i = escapes[c - 0x48]) != 0)  c = i;
380 #endif
381
382 /* Escapes that need further processing, or are illegal. */
383
384 else
385   {
386   const uschar *oldptr;
387   BOOL braced, negated;
388
389   switch (c)
390     {
391     /* A number of Perl escapes are not handled by PCRE. We give an explicit
392     error. */
393
394     case 'l':
395     case 'L':
396     case 'N':
397     case 'u':
398     case 'U':
399     *errorcodeptr = ERR37;
400     break;
401
402     /* \g must be followed by a number, either plain or braced. If positive, it
403     is an absolute backreference. If negative, it is a relative backreference.
404     This is a Perl 5.10 feature. */
405
406     case 'g':
407     if (ptr[1] == '{')
408       {
409       braced = TRUE;
410       ptr++;
411       }
412     else braced = FALSE;
413
414     if (ptr[1] == '-')
415       {
416       negated = TRUE;
417       ptr++;
418       }
419     else negated = FALSE;
420
421     c = 0;
422     while (g_ascii_isdigit(ptr[1]) != 0)
423       c = c * 10 + *(++ptr) - '0';
424
425     if (c == 0 || (braced && *(++ptr) != '}'))
426       {
427       *errorcodeptr = ERR57;
428       return 0;
429       }
430
431     if (negated)
432       {
433       if (c > bracount)
434         {
435         *errorcodeptr = ERR15;
436         return 0;
437         }
438       c = bracount - (c - 1);
439       }
440
441     c = -(ESC_REF + c);
442     break;
443
444     /* The handling of escape sequences consisting of a string of digits
445     starting with one that is not zero is not straightforward. By experiment,
446     the way Perl works seems to be as follows:
447
448     Outside a character class, the digits are read as a decimal number. If the
449     number is less than 10, or if there are that many previous extracting
450     left brackets, then it is a back reference. Otherwise, up to three octal
451     digits are read to form an escaped byte. Thus \123 is likely to be octal
452     123 (cf \0123, which is octal 012 followed by the literal 3). If the octal
453     value is greater than 377, the least significant 8 bits are taken. Inside a
454     character class, \ followed by a digit is always an octal number. */
455
456     case '1': case '2': case '3': case '4': case '5':
457     case '6': case '7': case '8': case '9':
458
459     if (!isclass)
460       {
461       oldptr = ptr;
462       c -= '0';
463       while (g_ascii_isdigit(ptr[1]) != 0)
464         c = c * 10 + *(++ptr) - '0';
465       if (c < 10 || c <= bracount)
466         {
467         c = -(ESC_REF + c);
468         break;
469         }
470       ptr = oldptr;      /* Put the pointer back and fall through */
471       }
472
473     /* Handle an octal number following \. If the first digit is 8 or 9, Perl
474     generates a binary zero byte and treats the digit as a following literal.
475     Thus we have to pull back the pointer by one. */
476
477     if ((c = *ptr) >= '8')
478       {
479       ptr--;
480       c = 0;
481       break;
482       }
483
484     /* \0 always starts an octal number, but we may drop through to here with a
485     larger first octal digit. The original code used just to take the least
486     significant 8 bits of octal numbers (I think this is what early Perls used
487     to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more
488     than 3 octal digits. */
489
490     case '0':
491     c -= '0';
492     while(i++ < 2 && ptr[1] >= '0' && ptr[1] <= '7')
493         c = c * 8 + *(++ptr) - '0';
494     if (!utf8 && c > 255) *errorcodeptr = ERR51;
495     break;
496
497     /* \x is complicated. \x{ddd} is a character number which can be greater
498     than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is
499     treated as a data character. */
500
501     case 'x':
502     if (ptr[1] == '{')
503       {
504       const uschar *pt = ptr + 2;
505       int count = 0;
506
507       c = 0;
508       while (g_ascii_isxdigit(*pt) != 0)
509         {
510         register int cc = *pt++;
511         if (c == 0 && cc == '0') continue;     /* Leading zeroes */
512         count++;
513
514 #if !EBCDIC    /* ASCII coding */
515         if (cc >= 'a') cc -= 32;               /* Convert to upper case */
516         c = (c << 4) + cc - ((cc < 'A')? '0' : ('A' - 10));
517 #else          /* EBCDIC coding */
518         if (cc >= 'a' && cc <= 'z') cc += 64;  /* Convert to upper case */
519         c = (c << 4) + cc - ((cc >= '0')? '0' : ('A' - 10));
520 #endif
521         }
522
523       if (*pt == '}')
524         {
525         if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;
526         ptr = pt;
527         break;
528         }
529
530       /* If the sequence of hex digits does not end with '}', then we don't
531       recognize this construct; fall through to the normal \x handling. */
532       }
533
534     /* Read just a single-byte hex-defined char */
535
536     c = 0;
537     while (i++ < 2 && g_ascii_isxdigit(ptr[1]) != 0)
538       {
539       int cc;                               /* Some compilers don't like ++ */
540       cc = *(++ptr);                        /* in initializers */
541 #if !EBCDIC    /* ASCII coding */
542       if (cc >= 'a') cc -= 32;              /* Convert to upper case */
543       c = c * 16 + cc - ((cc < 'A')? '0' : ('A' - 10));
544 #else          /* EBCDIC coding */
545       if (cc <= 'z') cc += 64;              /* Convert to upper case */
546       c = c * 16 + cc - ((cc >= '0')? '0' : ('A' - 10));
547 #endif
548       }
549     break;
550
551     /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
552     This coding is ASCII-specific, but then the whole concept of \cx is
553     ASCII-specific. (However, an EBCDIC equivalent has now been added.) */
554
555     case 'c':
556     c = *(++ptr);
557     if (c == 0)
558       {
559       *errorcodeptr = ERR2;
560       return 0;
561       }
562
563 #if !EBCDIC    /* ASCII coding */
564     if (c >= 'a' && c <= 'z') c -= 32;
565     c ^= 0x40;
566 #else          /* EBCDIC coding */
567     if (c >= 'a' && c <= 'z') c += 64;
568     c ^= 0xC0;
569 #endif
570     break;
571
572     /* PCRE_EXTRA enables extensions to Perl in the matter of escapes. Any
573     other alphameric following \ is an error if PCRE_EXTRA was set; otherwise,
574     for Perl compatibility, it is a literal. This code looks a bit odd, but
575     there used to be some cases other than the default, and there may be again
576     in future, so I haven't "optimized" it. */
577
578     default:
579     if ((options & PCRE_EXTRA) != 0) switch(c)
580       {
581       default:
582       *errorcodeptr = ERR3;
583       break;
584       }
585     break;
586     }
587   }
588
589 *ptrptr = ptr;
590 return c;
591 }
592
593
594
595 #ifdef SUPPORT_UCP
596 /*************************************************
597 *               Handle \P and \p                 *
598 *************************************************/
599
600 /* This function is called after \P or \p has been encountered, provided that
601 PCRE is compiled with support for Unicode properties. On entry, ptrptr is
602 pointing at the P or p. On exit, it is pointing at the final character of the
603 escape sequence.
604
605 Argument:
606   ptrptr         points to the pattern position pointer
607   negptr         points to a boolean that is set TRUE for negation else FALSE
608   dptr           points to an int that is set to the detailed property value
609   errorcodeptr   points to the error code variable
610
611 Returns:         type value from ucp_type_table, or -1 for an invalid type
612 */
613
614 static int
615 get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
616 {
617 int c, i, bot, top;
618 const uschar *ptr = *ptrptr;
619 char name[32];
620
621 c = *(++ptr);
622 if (c == 0) goto ERROR_RETURN;
623
624 *negptr = FALSE;
625
626 /* \P or \p can be followed by a name in {}, optionally preceded by ^ for
627 negation. */
628
629 if (c == '{')
630   {
631   if (ptr[1] == '^')
632     {
633     *negptr = TRUE;
634     ptr++;
635     }
636   for (i = 0; i < sizeof(name) - 1; i++)
637     {
638     c = *(++ptr);
639     if (c == 0) goto ERROR_RETURN;
640     if (c == '}') break;
641     name[i] = c;
642     }
643   if (c !='}') goto ERROR_RETURN;
644   name[i] = 0;
645   }
646
647 /* Otherwise there is just one following character */
648
649 else
650   {
651   name[0] = c;
652   name[1] = 0;
653   }
654
655 *ptrptr = ptr;
656
657 /* Search for a recognized property name using binary chop */
658
659 bot = 0;
660 top = _pcre_utt_size;
661
662 while (bot < top)
663   {
664   i = (bot + top) >> 1;
665   c = strcmp(name, &_pcre_ucp_names[_pcre_utt[i].offset]);
666   if (c == 0)
667     {
668     *dptr = _pcre_utt[i].value;
669     return _pcre_utt[i].type;
670     }
671   if (c > 0) bot = i + 1; else top = i;
672   }
673
674 *errorcodeptr = ERR47;
675 *ptrptr = ptr;
676 return -1;
677
678 ERROR_RETURN:
679 *errorcodeptr = ERR46;
680 *ptrptr = ptr;
681 return -1;
682 }
683 #endif
684
685
686
687
688 /*************************************************
689 *            Check for counted repeat            *
690 *************************************************/
691
692 /* This function is called when a '{' is encountered in a place where it might
693 start a quantifier. It looks ahead to see if it really is a quantifier or not.
694 It is only a quantifier if it is one of the forms {ddd} {ddd,} or {ddd,ddd}
695 where the ddds are digits.
696
697 Arguments:
698   p         pointer to the first char after '{'
699
700 Returns:    TRUE or FALSE
701 */
702
703 static BOOL
704 is_counted_repeat(const uschar *p)
705 {
706 if (g_ascii_isdigit(*p++) == 0) return FALSE;
707 while (g_ascii_isdigit(*p) != 0) p++;
708 if (*p == '}') return TRUE;
709
710 if (*p++ != ',') return FALSE;
711 if (*p == '}') return TRUE;
712
713 if (g_ascii_isdigit(*p++) == 0) return FALSE;
714 while (g_ascii_isdigit(*p) != 0) p++;
715
716 return (*p == '}');
717 }
718
719
720
721 /*************************************************
722 *         Read repeat counts                     *
723 *************************************************/
724
725 /* Read an item of the form {n,m} and return the values. This is called only
726 after is_counted_repeat() has confirmed that a repeat-count quantifier exists,
727 so the syntax is guaranteed to be correct, but we need to check the values.
728
729 Arguments:
730   p              pointer to first char after '{'
731   minp           pointer to int for min
732   maxp           pointer to int for max
733                  returned as -1 if no max
734   errorcodeptr   points to error code variable
735
736 Returns:         pointer to '}' on success;
737                  current ptr on error, with errorcodeptr set non-zero
738 */
739
740 static const uschar *
741 read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)
742 {
743 int min = 0;
744 int max = -1;
745
746 /* Read the minimum value and do a paranoid check: a negative value indicates
747 an integer overflow. */
748
749 while (g_ascii_isdigit(*p) != 0) min = min * 10 + *p++ - '0';
750 if (min < 0 || min > 65535)
751   {
752   *errorcodeptr = ERR5;
753   return p;
754   }
755
756 /* Read the maximum value if there is one, and again do a paranoid on its size.
757 Also, max must not be less than min. */
758
759 if (*p == '}') max = min; else
760   {
761   if (*(++p) != '}')
762     {
763     max = 0;
764     while(g_ascii_isdigit(*p) != 0) max = max * 10 + *p++ - '0';
765     if (max < 0 || max > 65535)
766       {
767       *errorcodeptr = ERR5;
768       return p;
769       }
770     if (max < min)
771       {
772       *errorcodeptr = ERR4;
773       return p;
774       }
775     }
776   }
777
778 /* Fill in the required variables, and pass back the pointer to the terminating
779 '}'. */
780
781 *minp = min;
782 *maxp = max;
783 return p;
784 }
785
786
787
788 /*************************************************
789 *       Find forward referenced subpattern       *
790 *************************************************/
791
792 /* This function scans along a pattern's text looking for capturing
793 subpatterns, and counting them. If it finds a named pattern that matches the
794 name it is given, it returns its number. Alternatively, if the name is NULL, it
795 returns when it reaches a given numbered subpattern. This is used for forward
796 references to subpatterns. We know that if (?P< is encountered, the name will
797 be terminated by '>' because that is checked in the first pass.
798
799 Arguments:
800   ptr          current position in the pattern
801   count        current count of capturing parens so far encountered
802   name         name to seek, or NULL if seeking a numbered subpattern
803   lorn         name length, or subpattern number if name is NULL
804   xmode        TRUE if we are in /x mode
805
806 Returns:       the number of the named subpattern, or -1 if not found
807 */
808
809 static int
810 find_parens(const uschar *ptr, int count, const uschar *name, int lorn,
811   BOOL xmode)
812 {
813 const uschar *thisname;
814
815 for (; *ptr != 0; ptr++)
816   {
817   int term;
818
819   /* Skip over backslashed characters and also entire \Q...\E */
820
821   if (*ptr == '\\')
822     {
823     if (*(++ptr) == 0) return -1;
824     if (*ptr == 'Q') for (;;)
825       {
826       while (*(++ptr) != 0 && *ptr != '\\');
827       if (*ptr == 0) return -1;
828       if (*(++ptr) == 'E') break;
829       }
830     continue;
831     }
832
833   /* Skip over character classes */
834
835   if (*ptr == '[')
836     {
837     while (*(++ptr) != ']')
838       {
839       if (*ptr == '\\')
840         {
841         if (*(++ptr) == 0) return -1;
842         if (*ptr == 'Q') for (;;)
843           {
844           while (*(++ptr) != 0 && *ptr != '\\');
845           if (*ptr == 0) return -1;
846           if (*(++ptr) == 'E') break;
847           }
848         continue;
849         }
850       }
851     continue;
852     }
853
854   /* Skip comments in /x mode */
855
856   if (xmode && *ptr == '#')
857     {
858     while (*(++ptr) != 0 && *ptr != '\n');
859     if (*ptr == 0) return -1;
860     continue;
861     }
862
863   /* An opening parens must now be a real metacharacter */
864
865   if (*ptr != '(') continue;
866   if (ptr[1] != '?')
867     {
868     count++;
869     if (name == NULL && count == lorn) return count;
870     continue;
871     }
872
873   ptr += 2;
874   if (*ptr == 'P') ptr++;                      /* Allow optional P */
875
876   /* We have to disambiguate (?<! and (?<= from (?<name> */
877
878   if ((*ptr != '<' || ptr[1] == '!' || ptr[1] == '=') &&
879        *ptr != '\'')
880     continue;
881
882   count++;
883
884   if (name == NULL && count == lorn) return count;
885   term = *ptr++;
886   if (term == '<') term = '>';
887   thisname = ptr;
888   while (*ptr != term) ptr++;
889   if (name != NULL && lorn == ptr - thisname &&
890       strncmp((const char *)name, (const char *)thisname, lorn) == 0)
891     return count;
892   }
893
894 return -1;
895 }
896
897
898
899 /*************************************************
900 *      Find first significant op code            *
901 *************************************************/
902
903 /* This is called by several functions that scan a compiled expression looking
904 for a fixed first character, or an anchoring op code etc. It skips over things
905 that do not influence this. For some calls, a change of option is important.
906 For some calls, it makes sense to skip negative forward and all backward
907 assertions, and also the \b assertion; for others it does not.
908
909 Arguments:
910   code         pointer to the start of the group
911   options      pointer to external options
912   optbit       the option bit whose changing is significant, or
913                  zero if none are
914   skipassert   TRUE if certain assertions are to be skipped
915
916 Returns:       pointer to the first significant opcode
917 */
918
919 static const uschar*
920 first_significant_code(const uschar *code, int *options, int optbit,
921   BOOL skipassert)
922 {
923 for (;;)
924   {
925   switch ((int)*code)
926     {
927     case OP_OPT:
928     if (optbit > 0 && ((int)code[1] & optbit) != (*options & optbit))
929       *options = (int)code[1];
930     code += 2;
931     break;
932
933     case OP_ASSERT_NOT:
934     case OP_ASSERTBACK:
935     case OP_ASSERTBACK_NOT:
936     if (!skipassert) return code;
937     do code += GET(code, 1); while (*code == OP_ALT);
938     code += _pcre_OP_lengths[*code];
939     break;
940
941     case OP_WORD_BOUNDARY:
942     case OP_NOT_WORD_BOUNDARY:
943     if (!skipassert) return code;
944     /* Fall through */
945
946     case OP_CALLOUT:
947     case OP_CREF:
948     case OP_RREF:
949     case OP_DEF:
950     code += _pcre_OP_lengths[*code];
951     break;
952
953     default:
954     return code;
955     }
956   }
957 /* Control never reaches here */
958 }
959
960
961
962
963 /*************************************************
964 *        Find the fixed length of a pattern      *
965 *************************************************/
966
967 /* Scan a pattern and compute the fixed length of subject that will match it,
968 if the length is fixed. This is needed for dealing with backward assertions.
969 In UTF8 mode, the result is in characters rather than bytes.
970
971 Arguments:
972   code     points to the start of the pattern (the bracket)
973   options  the compiling options
974
975 Returns:   the fixed length, or -1 if there is no fixed length,
976              or -2 if \C was encountered
977 */
978
979 static int
980 find_fixedlength(uschar *code, int options)
981 {
982 int length = -1;
983
984 register int branchlength = 0;
985 register uschar *cc = code + 1 + LINK_SIZE;
986
987 /* Scan along the opcodes for this branch. If we get to the end of the
988 branch, check the length against that of the other branches. */
989
990 for (;;)
991   {
992   int d;
993   register int op = *cc;
994
995   switch (op)
996     {
997     case OP_CBRA:
998     case OP_BRA:
999     case OP_ONCE:
1000     case OP_COND:
1001     d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), options);
1002     if (d < 0) return d;
1003     branchlength += d;
1004     do cc += GET(cc, 1); while (*cc == OP_ALT);
1005     cc += 1 + LINK_SIZE;
1006     break;
1007
1008     /* Reached end of a branch; if it's a ket it is the end of a nested
1009     call. If it's ALT it is an alternation in a nested call. If it is
1010     END it's the end of the outer call. All can be handled by the same code. */
1011
1012     case OP_ALT:
1013     case OP_KET:
1014     case OP_KETRMAX:
1015     case OP_KETRMIN:
1016     case OP_END:
1017     if (length < 0) length = branchlength;
1018       else if (length != branchlength) return -1;
1019     if (*cc != OP_ALT) return length;
1020     cc += 1 + LINK_SIZE;
1021     branchlength = 0;
1022     break;
1023
1024     /* Skip over assertive subpatterns */
1025
1026     case OP_ASSERT:
1027     case OP_ASSERT_NOT:
1028     case OP_ASSERTBACK:
1029     case OP_ASSERTBACK_NOT:
1030     do cc += GET(cc, 1); while (*cc == OP_ALT);
1031     /* Fall through */
1032
1033     /* Skip over things that don't match chars */
1034
1035     case OP_REVERSE:
1036     case OP_CREF:
1037     case OP_RREF:
1038     case OP_DEF:
1039     case OP_OPT:
1040     case OP_CALLOUT:
1041     case OP_SOD:
1042     case OP_SOM:
1043     case OP_EOD:
1044     case OP_EODN:
1045     case OP_CIRC:
1046     case OP_DOLL:
1047     case OP_NOT_WORD_BOUNDARY:
1048     case OP_WORD_BOUNDARY:
1049     cc += _pcre_OP_lengths[*cc];
1050     break;
1051
1052     /* Handle literal characters */
1053
1054     case OP_CHAR:
1055     case OP_CHARNC:
1056     case OP_NOT:
1057     branchlength++;
1058     cc += 2;
1059 #ifdef SUPPORT_UTF8
1060     if ((options & PCRE_UTF8) != 0)
1061       {
1062       while ((*cc & 0xc0) == 0x80) cc++;
1063       }
1064 #endif
1065     break;
1066
1067     /* Handle exact repetitions. The count is already in characters, but we
1068     need to skip over a multibyte character in UTF8 mode.  */
1069
1070     case OP_EXACT:
1071     branchlength += GET2(cc,1);
1072     cc += 4;
1073 #ifdef SUPPORT_UTF8
1074     if ((options & PCRE_UTF8) != 0)
1075       {
1076       while((*cc & 0x80) == 0x80) cc++;
1077       }
1078 #endif
1079     break;
1080
1081     case OP_TYPEEXACT:
1082     branchlength += GET2(cc,1);
1083     cc += 4;
1084     break;
1085
1086     /* Handle single-char matchers */
1087
1088     case OP_PROP:
1089     case OP_NOTPROP:
1090     cc += 2;
1091     /* Fall through */
1092
1093     case OP_NOT_DIGIT:
1094     case OP_DIGIT:
1095     case OP_NOT_WHITESPACE:
1096     case OP_WHITESPACE:
1097     case OP_NOT_WORDCHAR:
1098     case OP_WORDCHAR:
1099     case OP_ANY:
1100     branchlength++;
1101     cc++;
1102     break;
1103
1104     /* The single-byte matcher isn't allowed */
1105
1106     case OP_ANYBYTE:
1107     return -2;
1108
1109     /* Check a class for variable quantification */
1110
1111 #ifdef SUPPORT_UTF8
1112     case OP_XCLASS:
1113     cc += GET(cc, 1) - 33;
1114     /* Fall through */
1115 #endif
1116
1117     case OP_CLASS:
1118     case OP_NCLASS:
1119     cc += 33;
1120
1121     switch (*cc)
1122       {
1123       case OP_CRSTAR:
1124       case OP_CRMINSTAR:
1125       case OP_CRQUERY:
1126       case OP_CRMINQUERY:
1127       return -1;
1128
1129       case OP_CRRANGE:
1130       case OP_CRMINRANGE:
1131       if (GET2(cc,1) != GET2(cc,3)) return -1;
1132       branchlength += GET2(cc,1);
1133       cc += 5;
1134       break;
1135
1136       default:
1137       branchlength++;
1138       }
1139     break;
1140
1141     /* Anything else is variable length */
1142
1143     default:
1144     return -1;
1145     }
1146   }
1147 /* Control never gets here */
1148 }
1149
1150
1151
1152
1153 /*************************************************
1154 *    Scan compiled regex for numbered bracket    *
1155 *************************************************/
1156
1157 /* This little function scans through a compiled pattern until it finds a
1158 capturing bracket with the given number.
1159
1160 Arguments:
1161   code        points to start of expression
1162   utf8        TRUE in UTF-8 mode
1163   number      the required bracket number
1164
1165 Returns:      pointer to the opcode for the bracket, or NULL if not found
1166 */
1167
1168 static const uschar *
1169 find_bracket(const uschar *code, BOOL utf8, int number)
1170 {
1171 for (;;)
1172   {
1173   register int c = *code;
1174   if (c == OP_END) return NULL;
1175
1176   /* XCLASS is used for classes that cannot be represented just by a bit
1177   map. This includes negated single high-valued characters. The length in
1178   the table is zero; the actual length is stored in the compiled code. */
1179
1180   if (c == OP_XCLASS) code += GET(code, 1);
1181
1182   /* Handle capturing bracket */
1183
1184   else if (c == OP_CBRA)
1185     {
1186     int n = GET2(code, 1+LINK_SIZE);
1187     if (n == number) return (uschar *)code;
1188     code += _pcre_OP_lengths[c];
1189     }
1190
1191   /* In UTF-8 mode, opcodes that are followed by a character may be followed by
1192   a multi-byte character. The length in the table is a minimum, so we have to
1193   arrange to skip the extra bytes. */
1194
1195   else
1196     {
1197     code += _pcre_OP_lengths[c];
1198     if (utf8) switch(c)
1199       {
1200       case OP_CHAR:
1201       case OP_CHARNC:
1202       case OP_EXACT:
1203       case OP_UPTO:
1204       case OP_MINUPTO:
1205       case OP_POSUPTO:
1206       case OP_STAR:
1207       case OP_MINSTAR:
1208       case OP_POSSTAR:
1209       case OP_PLUS:
1210       case OP_MINPLUS:
1211       case OP_POSPLUS:
1212       case OP_QUERY:
1213       case OP_MINQUERY:
1214       case OP_POSQUERY:
1215       if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1216       break;
1217       }
1218     }
1219   }
1220 }
1221
1222
1223
1224 /*************************************************
1225 *   Scan compiled regex for recursion reference  *
1226 *************************************************/
1227
1228 /* This little function scans through a compiled pattern until it finds an
1229 instance of OP_RECURSE.
1230
1231 Arguments:
1232   code        points to start of expression
1233   utf8        TRUE in UTF-8 mode
1234
1235 Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
1236 */
1237
1238 static const uschar *
1239 find_recurse(const uschar *code, BOOL utf8)
1240 {
1241 for (;;)
1242   {
1243   register int c = *code;
1244   if (c == OP_END) return NULL;
1245   if (c == OP_RECURSE) return code;
1246
1247   /* XCLASS is used for classes that cannot be represented just by a bit
1248   map. This includes negated single high-valued characters. The length in
1249   the table is zero; the actual length is stored in the compiled code. */
1250
1251   if (c == OP_XCLASS) code += GET(code, 1);
1252
1253   /* Otherwise, we get the item's length from the table. In UTF-8 mode, opcodes
1254   that are followed by a character may be followed by a multi-byte character.
1255   The length in the table is a minimum, so we have to arrange to skip the extra
1256   bytes. */
1257
1258   else
1259     {
1260     code += _pcre_OP_lengths[c];
1261     if (utf8) switch(c)
1262       {
1263       case OP_CHAR:
1264       case OP_CHARNC:
1265       case OP_EXACT:
1266       case OP_UPTO:
1267       case OP_MINUPTO:
1268       case OP_POSUPTO:
1269       case OP_STAR:
1270       case OP_MINSTAR:
1271       case OP_POSSTAR:
1272       case OP_PLUS:
1273       case OP_MINPLUS:
1274       case OP_POSPLUS:
1275       case OP_QUERY:
1276       case OP_MINQUERY:
1277       case OP_POSQUERY:
1278       if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];
1279       break;
1280       }
1281     }
1282   }
1283 }
1284
1285
1286
1287 /*************************************************
1288 *    Scan compiled branch for non-emptiness      *
1289 *************************************************/
1290
1291 /* This function scans through a branch of a compiled pattern to see whether it
1292 can match the empty string or not. It is called from could_be_empty()
1293 below and from compile_branch() when checking for an unlimited repeat of a
1294 group that can match nothing. Note that first_significant_code() skips over
1295 assertions. If we hit an unclosed bracket, we return "empty" - this means we've
1296 struck an inner bracket whose current branch will already have been scanned.
1297
1298 Arguments:
1299   code        points to start of search
1300   endcode     points to where to stop
1301   utf8        TRUE if in UTF8 mode
1302
1303 Returns:      TRUE if what is matched could be empty
1304 */
1305
1306 static BOOL
1307 could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8)
1308 {
1309 register int c;
1310 for (code = first_significant_code(code + _pcre_OP_lengths[*code], NULL, 0, TRUE);
1311      code < endcode;
1312      code = first_significant_code(code + _pcre_OP_lengths[c], NULL, 0, TRUE))
1313   {
1314   const uschar *ccode;
1315
1316   c = *code;
1317
1318   if (c == OP_BRA || c == OP_CBRA || c == OP_ONCE)
1319     {
1320     BOOL empty_branch;
1321     if (GET(code, 1) == 0) return TRUE;    /* Hit unclosed bracket */
1322
1323     /* Scan a closed bracket */
1324
1325     empty_branch = FALSE;
1326     do
1327       {
1328       if (!empty_branch && could_be_empty_branch(code, endcode, utf8))
1329         empty_branch = TRUE;
1330       code += GET(code, 1);
1331       }
1332     while (*code == OP_ALT);
1333     if (!empty_branch) return FALSE;   /* All branches are non-empty */
1334
1335     /* Move past the KET and fudge things so that the increment in the "for"
1336     above has no effect. */
1337
1338     c = OP_END;
1339     code += 1 + LINK_SIZE - _pcre_OP_lengths[c];
1340     continue;
1341     }
1342
1343   /* Handle the other opcodes */
1344
1345   switch (c)
1346     {
1347     /* Check for quantifiers after a class */
1348
1349 #ifdef SUPPORT_UTF8
1350     case OP_XCLASS:
1351     ccode = code + GET(code, 1);
1352     goto CHECK_CLASS_REPEAT;
1353 #endif
1354
1355     case OP_CLASS:
1356     case OP_NCLASS:
1357     ccode = code + 33;
1358
1359 #ifdef SUPPORT_UTF8
1360     CHECK_CLASS_REPEAT:
1361 #endif
1362
1363     switch (*ccode)
1364       {
1365       case OP_CRSTAR:            /* These could be empty; continue */
1366       case OP_CRMINSTAR:
1367       case OP_CRQUERY:
1368       case OP_CRMINQUERY:
1369       break;
1370
1371       default:                   /* Non-repeat => class must match */
1372       case OP_CRPLUS:            /* These repeats aren't empty */
1373       case OP_CRMINPLUS:
1374       return FALSE;
1375
1376       case OP_CRRANGE:
1377       case OP_CRMINRANGE:
1378       if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
1379       break;
1380       }
1381     break;
1382
1383     /* Opcodes that must match a character */
1384
1385     case OP_PROP:
1386     case OP_NOTPROP:
1387     case OP_EXTUNI:
1388     case OP_NOT_DIGIT:
1389     case OP_DIGIT:
1390     case OP_NOT_WHITESPACE:
1391     case OP_WHITESPACE:
1392     case OP_NOT_WORDCHAR:
1393     case OP_WORDCHAR:
1394     case OP_ANY:
1395     case OP_ANYBYTE:
1396     case OP_CHAR:
1397     case OP_CHARNC:
1398     case OP_NOT:
1399     case OP_PLUS:
1400     case OP_MINPLUS:
1401     case OP_POSPLUS:
1402     case OP_EXACT:
1403     case OP_NOTPLUS:
1404     case OP_NOTMINPLUS:
1405     case OP_NOTPOSPLUS:
1406     case OP_NOTEXACT:
1407     case OP_TYPEPLUS:
1408     case OP_TYPEMINPLUS:
1409     case OP_TYPEPOSPLUS:
1410     case OP_TYPEEXACT:
1411     return FALSE;
1412
1413     /* End of branch */
1414
1415     case OP_KET:
1416     case OP_KETRMAX:
1417     case OP_KETRMIN:
1418     case OP_ALT:
1419     return TRUE;
1420
1421     /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
1422     MINUPTO, and POSUPTO may be followed by a multibyte character */
1423
1424 #ifdef SUPPORT_UTF8
1425     case OP_STAR:
1426     case OP_MINSTAR:
1427     case OP_POSSTAR:
1428     case OP_QUERY:
1429     case OP_MINQUERY:
1430     case OP_POSQUERY:
1431     case OP_UPTO:
1432     case OP_MINUPTO:
1433     case OP_POSUPTO:
1434     if (utf8) while ((code[2] & 0xc0) == 0x80) code++;
1435     break;
1436 #endif
1437     }
1438   }
1439
1440 return TRUE;
1441 }
1442
1443
1444
1445 /*************************************************
1446 *    Scan compiled regex for non-emptiness       *
1447 *************************************************/
1448
1449 /* This function is called to check for left recursive calls. We want to check
1450 the current branch of the current pattern to see if it could match the empty
1451 string. If it could, we must look outwards for branches at other levels,
1452 stopping when we pass beyond the bracket which is the subject of the recursion.
1453
1454 Arguments:
1455   code        points to start of the recursion
1456   endcode     points to where to stop (current RECURSE item)
1457   bcptr       points to the chain of current (unclosed) branch starts
1458   utf8        TRUE if in UTF-8 mode
1459
1460 Returns:      TRUE if what is matched could be empty
1461 */
1462
1463 static BOOL
1464 could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,
1465   BOOL utf8)
1466 {
1467 while (bcptr != NULL && bcptr->current >= code)
1468   {
1469   if (!could_be_empty_branch(bcptr->current, endcode, utf8)) return FALSE;
1470   bcptr = bcptr->outer;
1471   }
1472 return TRUE;
1473 }
1474
1475
1476
1477 /*************************************************
1478 *           Check for POSIX class syntax         *
1479 *************************************************/
1480
1481 /* This function is called when the sequence "[:" or "[." or "[=" is
1482 encountered in a character class. It checks whether this is followed by an
1483 optional ^ and then a sequence of letters, terminated by a matching ":]" or
1484 ".]" or "=]".
1485
1486 Argument:
1487   ptr      pointer to the initial [
1488   endptr   where to return the end pointer
1489   cd       pointer to compile data
1490
1491 Returns:   TRUE or FALSE
1492 */
1493
1494 static BOOL
1495 check_posix_syntax(const uschar *ptr, const uschar **endptr, compile_data *cd)
1496 {
1497 int terminator;          /* Don't combine these lines; the Solaris cc */
1498 terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
1499 if (*(++ptr) == '^') ptr++;
1500 while ((cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
1501 if (*ptr == terminator && ptr[1] == ']')
1502   {
1503   *endptr = ptr;
1504   return TRUE;
1505   }
1506 return FALSE;
1507 }
1508
1509
1510
1511
1512 /*************************************************
1513 *          Check POSIX class name                *
1514 *************************************************/
1515
1516 /* This function is called to check the name given in a POSIX-style class entry
1517 such as [:alnum:].
1518
1519 Arguments:
1520   ptr        points to the first letter
1521   len        the length of the name
1522
1523 Returns:     a value representing the name, or -1 if unknown
1524 */
1525
1526 static int
1527 check_posix_name(const uschar *ptr, int len)
1528 {
1529   int offset = 0;
1530   int yield = 0;
1531   while (posix_name_lengths[yield] != 0)
1532     {
1533       if (len == posix_name_lengths[yield] &&
1534           strcmp((const char *)ptr, posix_names + offset) == 0) return yield;
1535       offset += posix_name_lengths[yield] + 1;
1536       yield++;
1537     }
1538   return -1;
1539 }
1540
1541
1542 /*************************************************
1543 *    Adjust OP_RECURSE items in repeated group   *
1544 *************************************************/
1545
1546 /* OP_RECURSE items contain an offset from the start of the regex to the group
1547 that is referenced. This means that groups can be replicated for fixed
1548 repetition simply by copying (because the recursion is allowed to refer to
1549 earlier groups that are outside the current group). However, when a group is
1550 optional (i.e. the minimum quantifier is zero), OP_BRAZERO is inserted before
1551 it, after it has been compiled. This means that any OP_RECURSE items within it
1552 that refer to the group itself or any contained groups have to have their
1553 offsets adjusted. That one of the jobs of this function. Before it is called,
1554 the partially compiled regex must be temporarily terminated with OP_END.
1555
1556 This function has been extended with the possibility of forward references for
1557 recursions and subroutine calls. It must also check the list of such references
1558 for the group we are dealing with. If it finds that one of the recursions in
1559 the current group is on this list, it adjusts the offset in the list, not the
1560 value in the reference (which is a group number).
1561
1562 Arguments:
1563   group      points to the start of the group
1564   adjust     the amount by which the group is to be moved
1565   utf8       TRUE in UTF-8 mode
1566   cd         contains pointers to tables etc.
1567   save_hwm   the hwm forward reference pointer at the start of the group
1568
1569 Returns:     nothing
1570 */
1571
1572 static void
1573 adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,
1574   uschar *save_hwm)
1575 {
1576 uschar *ptr = group;
1577 while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)
1578   {
1579   int offset;
1580   uschar *hc;
1581
1582   /* See if this recursion is on the forward reference list. If so, adjust the
1583   reference. */
1584
1585   for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)
1586     {
1587     offset = GET(hc, 0);
1588     if (cd->start_code + offset == ptr + 1)
1589       {
1590       PUT(hc, 0, offset + adjust);
1591       break;
1592       }
1593     }
1594
1595   /* Otherwise, adjust the recursion offset if it's after the start of this
1596   group. */
1597
1598   if (hc >= cd->hwm)
1599     {
1600     offset = GET(ptr, 1);
1601     if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
1602     }
1603
1604   ptr += 1 + LINK_SIZE;
1605   }
1606 }
1607
1608
1609
1610 /*************************************************
1611 *        Insert an automatic callout point       *
1612 *************************************************/
1613
1614 /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
1615 callout points before each pattern item.
1616
1617 Arguments:
1618   code           current code pointer
1619   ptr            current pattern pointer
1620   cd             pointers to tables etc
1621
1622 Returns:         new code pointer
1623 */
1624
1625 static uschar *
1626 auto_callout(uschar *code, const uschar *ptr, compile_data *cd)
1627 {
1628 *code++ = OP_CALLOUT;
1629 *code++ = 255;
1630 PUT(code, 0, ptr - cd->start_pattern);  /* Pattern offset */
1631 PUT(code, LINK_SIZE, 0);                /* Default length */
1632 return code + 2*LINK_SIZE;
1633 }
1634
1635
1636
1637 /*************************************************
1638 *         Complete a callout item                *
1639 *************************************************/
1640
1641 /* A callout item contains the length of the next item in the pattern, which
1642 we can't fill in till after we have reached the relevant point. This is used
1643 for both automatic and manual callouts.
1644
1645 Arguments:
1646   previous_callout   points to previous callout item
1647   ptr                current pattern pointer
1648   cd                 pointers to tables etc
1649
1650 Returns:             nothing
1651 */
1652
1653 static void
1654 complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)
1655 {
1656 int length = ptr - cd->start_pattern - GET(previous_callout, 2);
1657 PUT(previous_callout, 2 + LINK_SIZE, length);
1658 }
1659
1660
1661
1662 #ifdef SUPPORT_UCP
1663 /*************************************************
1664 *           Get othercase range                  *
1665 *************************************************/
1666
1667 /* This function is passed the start and end of a class range, in UTF-8 mode
1668 with UCP support. It searches up the characters, looking for internal ranges of
1669 characters in the "other" case. Each call returns the next one, updating the
1670 start address.
1671
1672 Arguments:
1673   cptr        points to starting character value; updated
1674   d           end value
1675   ocptr       where to put start of othercase range
1676   odptr       where to put end of othercase range
1677
1678 Yield:        TRUE when range returned; FALSE when no more
1679 */
1680
1681 static BOOL
1682 get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
1683   unsigned int *odptr)
1684 {
1685 unsigned int c, othercase, next;
1686
1687 for (c = *cptr; c <= d; c++)
1688   { if ((othercase = _pcre_ucp_othercase(c)) != NOTACHAR) break; }
1689
1690 if (c > d) return FALSE;
1691
1692 *ocptr = othercase;
1693 next = othercase + 1;
1694
1695 for (++c; c <= d; c++)
1696   {
1697   if (_pcre_ucp_othercase(c) != next) break;
1698   next++;
1699   }
1700
1701 *odptr = next - 1;
1702 *cptr = c;
1703
1704 return TRUE;
1705 }
1706 #endif  /* SUPPORT_UCP */
1707
1708
1709
1710 /*************************************************
1711 *     Check if auto-possessifying is possible    *
1712 *************************************************/
1713
1714 /* This function is called for unlimited repeats of certain items, to see
1715 whether the next thing could possibly match the repeated item. If not, it makes
1716 sense to automatically possessify the repeated item.
1717
1718 Arguments:
1719   op_code       the repeated op code
1720   this          data for this item, depends on the opcode
1721   utf8          TRUE in UTF-8 mode
1722   utf8_char     used for utf8 character bytes, NULL if not relevant
1723   ptr           next character in pattern
1724   options       options bits
1725   cd            contains pointers to tables etc.
1726
1727 Returns:        TRUE if possessifying is wanted
1728 */
1729
1730 static BOOL
1731 check_auto_possessive(int op_code, int item, BOOL utf8, uschar *utf8_char,
1732   const uschar *ptr, int options, compile_data *cd)
1733 {
1734 int next;
1735
1736 /* Skip whitespace and comments in extended mode */
1737
1738 if ((options & PCRE_EXTENDED) != 0)
1739   {
1740   for (;;)
1741     {
1742     while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
1743     if (*ptr == '#')
1744       {
1745       while (*(++ptr) != 0)
1746         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
1747       }
1748     else break;
1749     }
1750   }
1751
1752 /* If the next item is one that we can handle, get its value. A non-negative
1753 value is a character, a negative value is an escape value. */
1754
1755 if (*ptr == '\\')
1756   {
1757   int temperrorcode = 0;
1758   next = check_escape(&ptr, &temperrorcode, cd->bracount, options, FALSE);
1759   if (temperrorcode != 0) return FALSE;
1760   ptr++;    /* Point after the escape sequence */
1761   }
1762
1763 else if ((cd->ctypes[*ptr] & ctype_meta) == 0)
1764   {
1765 #ifdef SUPPORT_UTF8
1766   if (utf8) { GETCHARINC(next, ptr); } else
1767 #endif
1768   next = *ptr++;
1769   }
1770
1771 else return FALSE;
1772
1773 /* Skip whitespace and comments in extended mode */
1774
1775 if ((options & PCRE_EXTENDED) != 0)
1776   {
1777   for (;;)
1778     {
1779     while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
1780     if (*ptr == '#')
1781       {
1782       while (*(++ptr) != 0)
1783         if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
1784       }
1785     else break;
1786     }
1787   }
1788
1789 /* If the next thing is itself optional, we have to give up. */
1790
1791 if (*ptr == '*' || *ptr == '?' || strncmp((char *)ptr, "{0,", 3) == 0)
1792   return FALSE;
1793
1794 /* Now compare the next item with the previous opcode. If the previous is a
1795 positive single character match, "item" either contains the character or, if
1796 "item" is greater than 127 in utf8 mode, the character's bytes are in
1797 utf8_char. */
1798
1799
1800 /* Handle cases when the next item is a character. */
1801
1802 if (next >= 0) switch(op_code)
1803   {
1804   case OP_CHAR:
1805 #ifdef SUPPORT_UTF8
1806   if (utf8 && item > 127) { GETCHAR(item, utf8_char); }
1807 #endif
1808   return item != next;
1809
1810   /* For CHARNC (caseless character) we must check the other case. If we have
1811   Unicode property support, we can use it to test the other case of
1812   high-valued characters. */
1813
1814   case OP_CHARNC:
1815 #ifdef SUPPORT_UTF8
1816   if (utf8 && item > 127) { GETCHAR(item, utf8_char); }
1817 #endif
1818   if (item == next) return FALSE;
1819 #ifdef SUPPORT_UTF8
1820   if (utf8)
1821     {
1822     unsigned int othercase;
1823     if (next < 128) othercase = cd->fcc[next]; else
1824 #ifdef SUPPORT_UCP
1825     othercase = _pcre_ucp_othercase((unsigned int)next);
1826 #else
1827     othercase = NOTACHAR;
1828 #endif
1829     return (unsigned int)item != othercase;
1830     }
1831   else
1832 #endif  /* SUPPORT_UTF8 */
1833   return (item != cd->fcc[next]);  /* Non-UTF-8 mode */
1834
1835   /* For OP_NOT, "item" must be a single-byte character. */
1836
1837   case OP_NOT:
1838   if (next < 0) return FALSE;  /* Not a character */
1839   if (item == next) return TRUE;
1840   if ((options & PCRE_CASELESS) == 0) return FALSE;
1841 #ifdef SUPPORT_UTF8
1842   if (utf8)
1843     {
1844     unsigned int othercase;
1845     if (next < 128) othercase = cd->fcc[next]; else
1846 #ifdef SUPPORT_UCP
1847     othercase = _pcre_ucp_othercase(next);
1848 #else
1849     othercase = NOTACHAR;
1850 #endif
1851     return (unsigned int)item == othercase;
1852     }
1853   else
1854 #endif  /* SUPPORT_UTF8 */
1855   return (item == cd->fcc[next]);  /* Non-UTF-8 mode */
1856
1857   case OP_DIGIT:
1858   return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;
1859
1860   case OP_NOT_DIGIT:
1861   return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;
1862
1863   case OP_WHITESPACE:
1864   return next > 127 || (cd->ctypes[next] & ctype_space) == 0;
1865
1866   case OP_NOT_WHITESPACE:
1867   return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;
1868
1869   case OP_WORDCHAR:
1870   return next > 127 || (cd->ctypes[next] & ctype_word) == 0;
1871
1872   case OP_NOT_WORDCHAR:
1873   return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;
1874
1875   default:
1876   return FALSE;
1877   }
1878
1879
1880 /* Handle the case when the next item is \d, \s, etc. */
1881
1882 switch(op_code)
1883   {
1884   case OP_CHAR:
1885   case OP_CHARNC:
1886 #ifdef SUPPORT_UTF8
1887   if (utf8 && item > 127) { GETCHAR(item, utf8_char); }
1888 #endif
1889   switch(-next)
1890     {
1891     case ESC_d:
1892     return item > 127 || (cd->ctypes[item] & ctype_digit) == 0;
1893
1894     case ESC_D:
1895     return item <= 127 && (cd->ctypes[item] & ctype_digit) != 0;
1896
1897     case ESC_s:
1898     return item > 127 || (cd->ctypes[item] & ctype_space) == 0;
1899
1900     case ESC_S:
1901     return item <= 127 && (cd->ctypes[item] & ctype_space) != 0;
1902
1903     case ESC_w:
1904     return item > 127 || (cd->ctypes[item] & ctype_word) == 0;
1905
1906     case ESC_W:
1907     return item <= 127 && (cd->ctypes[item] & ctype_word) != 0;
1908
1909     default:
1910     return FALSE;
1911     }
1912
1913   case OP_DIGIT:
1914   return next == -ESC_D || next == -ESC_s || next == -ESC_W;
1915
1916   case OP_NOT_DIGIT:
1917   return next == -ESC_d;
1918
1919   case OP_WHITESPACE:
1920   return next == -ESC_S || next == -ESC_d || next == -ESC_w;
1921
1922   case OP_NOT_WHITESPACE:
1923   return next == -ESC_s;
1924
1925   case OP_WORDCHAR:
1926   return next == -ESC_W || next == -ESC_s;
1927
1928   case OP_NOT_WORDCHAR:
1929   return next == -ESC_w || next == -ESC_d;
1930
1931   default:
1932   return FALSE;
1933   }
1934
1935 /* Control does not reach here */
1936 }
1937
1938
1939
1940 /*************************************************
1941 *           Compile one branch                   *
1942 *************************************************/
1943
1944 /* Scan the pattern, compiling it into the a vector. If the options are
1945 changed during the branch, the pointer is used to change the external options
1946 bits. This function is used during the pre-compile phase when we are trying
1947 to find out the amount of memory needed, as well as during the real compile
1948 phase. The value of lengthptr distinguishes the two phases.
1949
1950 Arguments:
1951   optionsptr     pointer to the option bits
1952   codeptr        points to the pointer to the current code point
1953   ptrptr         points to the current pattern pointer
1954   errorcodeptr   points to error code variable
1955   firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
1956   reqbyteptr     set to the last literal character required, else < 0
1957   bcptr          points to current branch chain
1958   cd             contains pointers to tables etc.
1959   lengthptr      NULL during the real compile phase
1960                  points to length accumulator during pre-compile phase
1961
1962 Returns:         TRUE on success
1963                  FALSE, with *errorcodeptr set non-zero on error
1964 */
1965
1966 static BOOL
1967 compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,
1968   int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,
1969   compile_data *cd, int *lengthptr)
1970 {
1971 int repeat_type, op_type;
1972 int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
1973 int bravalue = 0;
1974 int greedy_default, greedy_non_default;
1975 int firstbyte, reqbyte;
1976 int zeroreqbyte, zerofirstbyte;
1977 int req_caseopt, reqvary, tempreqvary;
1978 int options = *optionsptr;
1979 int after_manual_callout = 0;
1980 int length_prevgroup = 0;
1981 register int c;
1982 register uschar *code = *codeptr;
1983 uschar *last_code = code;
1984 uschar *orig_code = code;
1985 uschar *tempcode;
1986 BOOL inescq = FALSE;
1987 BOOL groupsetfirstbyte = FALSE;
1988 const uschar *ptr = *ptrptr;
1989 const uschar *tempptr;
1990 uschar *previous = NULL;
1991 uschar *previous_callout = NULL;
1992 uschar *save_hwm = NULL;
1993 uschar classbits[32];
1994
1995 #ifdef SUPPORT_UTF8
1996 BOOL class_utf8;
1997 BOOL utf8 = (options & PCRE_UTF8) != 0;
1998 uschar *class_utf8data;
1999 uschar utf8_char[6];
2000 #else
2001 BOOL utf8 = FALSE;
2002 uschar *utf8_char = NULL;
2003 #endif
2004
2005 #ifdef DEBUG
2006 if (lengthptr != NULL) DPRINTF((">> start branch\n"));
2007 #endif
2008
2009 /* Set up the default and non-default settings for greediness */
2010
2011 greedy_default = ((options & PCRE_UNGREEDY) != 0);
2012 greedy_non_default = greedy_default ^ 1;
2013
2014 /* Initialize no first byte, no required byte. REQ_UNSET means "no char
2015 matching encountered yet". It gets changed to REQ_NONE if we hit something that
2016 matches a non-fixed char first char; reqbyte just remains unset if we never
2017 find one.
2018
2019 When we hit a repeat whose minimum is zero, we may have to adjust these values
2020 to take the zero repeat into account. This is implemented by setting them to
2021 zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual
2022 item types that can be repeated set these backoff variables appropriately. */
2023
2024 firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;
2025
2026 /* The variable req_caseopt contains either the REQ_CASELESS value or zero,
2027 according to the current setting of the caseless flag. REQ_CASELESS is a bit
2028 value > 255. It is added into the firstbyte or reqbyte variables to record the
2029 case status of the value. This is used only for ASCII characters. */
2030
2031 req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
2032
2033 /* Switch on next character until the end of the branch */
2034
2035 for (;; ptr++)
2036   {
2037   BOOL negate_class;
2038   BOOL possessive_quantifier;
2039   BOOL is_quantifier;
2040   BOOL is_recurse;
2041   int class_charcount;
2042   int class_lastchar;
2043   int newoptions;
2044   int recno;
2045   int skipbytes;
2046   int subreqbyte;
2047   int subfirstbyte;
2048   int terminator;
2049   int mclength;
2050   uschar mcbuffer[8];
2051
2052   /* Get next byte in the pattern */
2053
2054   c = *ptr;
2055
2056   /* If we are in the pre-compile phase, accumulate the length used for the
2057   previous cycle of this loop. */
2058
2059   if (lengthptr != NULL)
2060     {
2061 #ifdef DEBUG
2062     if (code > cd->hwm) cd->hwm = code;                 /* High water info */
2063 #endif
2064     if (code > cd->start_workspace + COMPILE_WORK_SIZE) /* Check for overrun */
2065       {
2066       *errorcodeptr = ERR52;
2067       goto FAILED;
2068       }
2069
2070     /* There is at least one situation where code goes backwards: this is the
2071     case of a zero quantifier after a class (e.g. [ab]{0}). At compile time,
2072     the class is simply eliminated. However, it is created first, so we have to
2073     allow memory for it. Therefore, don't ever reduce the length at this point.
2074     */
2075
2076     if (code < last_code) code = last_code;
2077     *lengthptr += code - last_code;
2078     DPRINTF(("length=%d added %d c=%c\n", *lengthptr, code - last_code, c));
2079
2080     /* If "previous" is set and it is not at the start of the work space, move
2081     it back to there, in order to avoid filling up the work space. Otherwise,
2082     if "previous" is NULL, reset the current code pointer to the start. */
2083
2084     if (previous != NULL)
2085       {
2086       if (previous > orig_code)
2087         {
2088         memmove(orig_code, previous, code - previous);
2089         code -= previous - orig_code;
2090         previous = orig_code;
2091         }
2092       }
2093     else code = orig_code;
2094
2095     /* Remember where this code item starts so we can pick up the length
2096     next time round. */
2097
2098     last_code = code;
2099     }
2100
2101   /* In the real compile phase, just check the workspace used by the forward
2102   reference list. */
2103
2104   else if (cd->hwm > cd->start_workspace + COMPILE_WORK_SIZE)
2105     {
2106     *errorcodeptr = ERR52;
2107     goto FAILED;
2108     }
2109
2110   /* If in \Q...\E, check for the end; if not, we have a literal */
2111
2112   if (inescq && c != 0)
2113     {
2114     if (c == '\\' && ptr[1] == 'E')
2115       {
2116       inescq = FALSE;
2117       ptr++;
2118       continue;
2119       }
2120     else
2121       {
2122       if (previous_callout != NULL)
2123         {
2124         if (lengthptr == NULL)  /* Don't attempt in pre-compile phase */
2125           complete_callout(previous_callout, ptr, cd);
2126         previous_callout = NULL;
2127         }
2128       if ((options & PCRE_AUTO_CALLOUT) != 0)
2129         {
2130         previous_callout = code;
2131         code = auto_callout(code, ptr, cd);
2132         }
2133       goto NORMAL_CHAR;
2134       }
2135     }
2136
2137   /* Fill in length of a previous callout, except when the next thing is
2138   a quantifier. */
2139
2140   is_quantifier = c == '*' || c == '+' || c == '?' ||
2141     (c == '{' && is_counted_repeat(ptr+1));
2142
2143   if (!is_quantifier && previous_callout != NULL &&
2144        after_manual_callout-- <= 0)
2145     {
2146     if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
2147       complete_callout(previous_callout, ptr, cd);
2148     previous_callout = NULL;
2149     }
2150
2151   /* In extended mode, skip white space and comments */
2152
2153   if ((options & PCRE_EXTENDED) != 0)
2154     {
2155     if ((cd->ctypes[c] & ctype_space) != 0) continue;
2156     if (c == '#')
2157       {
2158       while (*(++ptr) != 0)
2159         {
2160         if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
2161         }
2162       if (*ptr != 0) continue;
2163
2164       /* Else fall through to handle end of string */
2165       c = 0;
2166       }
2167     }
2168
2169   /* No auto callout for quantifiers. */
2170
2171   if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)
2172     {
2173     previous_callout = code;
2174     code = auto_callout(code, ptr, cd);
2175     }
2176
2177   switch(c)
2178     {
2179     /* ===================================================================*/
2180     case 0:                        /* The branch terminates at string end */
2181     case '|':                      /* or | or ) */
2182     case ')':
2183     *firstbyteptr = firstbyte;
2184     *reqbyteptr = reqbyte;
2185     *codeptr = code;
2186     *ptrptr = ptr;
2187     if (lengthptr != NULL)
2188       {
2189       *lengthptr += code - last_code;   /* To include callout length */
2190       DPRINTF((">> end branch\n"));
2191       }
2192     return TRUE;
2193
2194
2195     /* ===================================================================*/
2196     /* Handle single-character metacharacters. In multiline mode, ^ disables
2197     the setting of any following char as a first character. */
2198
2199     case '^':
2200     if ((options & PCRE_MULTILINE) != 0)
2201       {
2202       if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
2203       }
2204     previous = NULL;
2205     *code++ = OP_CIRC;
2206     break;
2207
2208     case '$':
2209     previous = NULL;
2210     *code++ = OP_DOLL;
2211     break;
2212
2213     /* There can never be a first char if '.' is first, whatever happens about
2214     repeats. The value of reqbyte doesn't change either. */
2215
2216     case '.':
2217     if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
2218     zerofirstbyte = firstbyte;
2219     zeroreqbyte = reqbyte;
2220     previous = code;
2221     *code++ = OP_ANY;
2222     break;
2223
2224
2225     /* ===================================================================*/
2226     /* Character classes. If the included characters are all < 256, we build a
2227     32-byte bitmap of the permitted characters, except in the special case
2228     where there is only one such character. For negated classes, we build the
2229     map as usual, then invert it at the end. However, we use a different opcode
2230     so that data characters > 255 can be handled correctly.
2231
2232     If the class contains characters outside the 0-255 range, a different
2233     opcode is compiled. It may optionally have a bit map for characters < 256,
2234     but those above are are explicitly listed afterwards. A flag byte tells
2235     whether the bitmap is present, and whether this is a negated class or not.
2236     */
2237
2238     case '[':
2239     previous = code;
2240
2241     /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
2242     they are encountered at the top level, so we'll do that too. */
2243
2244     if ((ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
2245         check_posix_syntax(ptr, &tempptr, cd))
2246       {
2247       *errorcodeptr = (ptr[1] == ':')? ERR13 : ERR31;
2248       goto FAILED;
2249       }
2250
2251     /* If the first character is '^', set the negation flag and skip it. */
2252
2253     if ((c = *(++ptr)) == '^')
2254       {
2255       negate_class = TRUE;
2256       c = *(++ptr);
2257       }
2258     else
2259       {
2260       negate_class = FALSE;
2261       }
2262
2263     /* Keep a count of chars with values < 256 so that we can optimize the case
2264     of just a single character (as long as it's < 256). However, For higher
2265     valued UTF-8 characters, we don't yet do any optimization. */
2266
2267     class_charcount = 0;
2268     class_lastchar = -1;
2269
2270     /* Initialize the 32-char bit map to all zeros. We build the map in a
2271     temporary bit of memory, in case the class contains only 1 character (less
2272     than 256), because in that case the compiled code doesn't use the bit map.
2273     */
2274
2275     memset(classbits, 0, 32 * sizeof(uschar));
2276
2277 #ifdef SUPPORT_UTF8
2278     class_utf8 = FALSE;                       /* No chars >= 256 */
2279     class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */
2280 #endif
2281
2282     /* Process characters until ] is reached. By writing this as a "do" it
2283     means that an initial ] is taken as a data character. At the start of the
2284     loop, c contains the first byte of the character. */
2285
2286     if (c != 0) do
2287       {
2288       const uschar *oldptr;
2289
2290 #ifdef SUPPORT_UTF8
2291       if (utf8 && c > 127)
2292         {                           /* Braces are required because the */
2293         GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
2294         }
2295 #endif
2296
2297       /* Inside \Q...\E everything is literal except \E */
2298
2299       if (inescq)
2300         {
2301         if (c == '\\' && ptr[1] == 'E')     /* If we are at \E */
2302           {
2303           inescq = FALSE;                   /* Reset literal state */
2304           ptr++;                            /* Skip the 'E' */
2305           continue;                         /* Carry on with next */
2306           }
2307         goto CHECK_RANGE;                   /* Could be range if \E follows */
2308         }
2309
2310       /* Handle POSIX class names. Perl allows a negation extension of the
2311       form [:^name:]. A square bracket that doesn't match the syntax is
2312       treated as a literal. We also recognize the POSIX constructions
2313       [.ch.] and [=ch=] ("collating elements") and fault them, as Perl
2314       5.6 and 5.8 do. */
2315
2316       if (c == '[' &&
2317           (ptr[1] == ':' || ptr[1] == '.' || ptr[1] == '=') &&
2318           check_posix_syntax(ptr, &tempptr, cd))
2319         {
2320         BOOL local_negate = FALSE;
2321         int posix_class, taboffset, tabopt;
2322         register const uschar *cbits = cd->cbits;
2323         uschar pbits[32];
2324
2325         if (ptr[1] != ':')
2326           {
2327           *errorcodeptr = ERR31;
2328           goto FAILED;
2329           }
2330
2331         ptr += 2;
2332         if (*ptr == '^')
2333           {
2334           local_negate = TRUE;
2335           ptr++;
2336           }
2337
2338         posix_class = check_posix_name(ptr, tempptr - ptr);
2339         if (posix_class < 0)
2340           {
2341           *errorcodeptr = ERR30;
2342           goto FAILED;
2343           }
2344
2345         /* If matching is caseless, upper and lower are converted to
2346         alpha. This relies on the fact that the class table starts with
2347         alpha, lower, upper as the first 3 entries. */
2348
2349         if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
2350           posix_class = 0;
2351
2352         /* We build the bit map for the POSIX class in a chunk of local store
2353         because we may be adding and subtracting from it, and we don't want to
2354         subtract bits that may be in the main map already. At the end we or the
2355         result into the bit map that is being built. */
2356
2357         posix_class *= 3;
2358
2359         /* Copy in the first table (always present) */
2360
2361         memcpy(pbits, cbits + posix_class_maps[posix_class],
2362           32 * sizeof(uschar));
2363
2364         /* If there is a second table, add or remove it as required. */
2365
2366         taboffset = posix_class_maps[posix_class + 1];
2367         tabopt = posix_class_maps[posix_class + 2];
2368
2369         if (taboffset >= 0)
2370           {
2371           if (tabopt >= 0)
2372             for (c = 0; c < 32; c++) pbits[c] |= cbits[c + taboffset];
2373           else
2374             for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
2375           }
2376
2377         /* Not see if we need to remove any special characters. An option
2378         value of 1 removes vertical space and 2 removes underscore. */
2379
2380         if (tabopt < 0) tabopt = -tabopt;
2381         if (tabopt == 1) pbits[1] &= ~0x3c;
2382           else if (tabopt == 2) pbits[11] &= 0x7f;
2383
2384         /* Add the POSIX table or its complement into the main table that is
2385         being built and we are done. */
2386
2387         if (local_negate)
2388           for (c = 0; c < 32; c++) classbits[c] |= ~pbits[c];
2389         else
2390           for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
2391
2392         ptr = tempptr + 1;
2393         class_charcount = 10;  /* Set > 1; assumes more than 1 per class */
2394         continue;    /* End of POSIX syntax handling */
2395         }
2396
2397       /* Backslash may introduce a single character, or it may introduce one
2398       of the specials, which just set a flag. The sequence \b is a special
2399       case. Inside a class (and only there) it is treated as backspace.
2400       Elsewhere it marks a word boundary. Other escapes have preset maps ready
2401       to or into the one we are building. We assume they have more than one
2402       character in them, so set class_charcount bigger than one. */
2403
2404       if (c == '\\')
2405         {
2406         c = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
2407         if (*errorcodeptr != 0) goto FAILED;
2408
2409         if (-c == ESC_b) c = '\b';       /* \b is backslash in a class */
2410         else if (-c == ESC_X) c = 'X';   /* \X is literal X in a class */
2411         else if (-c == ESC_R) c = 'R';   /* \R is literal R in a class */
2412         else if (-c == ESC_Q)            /* Handle start of quoted string */
2413           {
2414           if (ptr[1] == '\\' && ptr[2] == 'E')
2415             {
2416             ptr += 2; /* avoid empty string */
2417             }
2418           else inescq = TRUE;
2419           continue;
2420           }
2421
2422         if (c < 0)
2423           {
2424           register const uschar *cbits = cd->cbits;
2425           class_charcount += 2;     /* Greater than 1 is what matters */
2426
2427           /* Save time by not doing this in the pre-compile phase. */
2428
2429           if (lengthptr == NULL) switch (-c)
2430             {
2431             case ESC_d:
2432             for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_digit];
2433             continue;
2434
2435             case ESC_D:
2436             for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_digit];
2437             continue;
2438
2439             case ESC_w:
2440             for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_word];
2441             continue;
2442
2443             case ESC_W:
2444             for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
2445             continue;
2446
2447             case ESC_s:
2448             for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
2449             classbits[1] &= ~0x08;   /* Perl 5.004 onwards omits VT from \s */
2450             continue;
2451
2452             case ESC_S:
2453             for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
2454             classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
2455             continue;
2456
2457             case ESC_E: /* Perl ignores an orphan \E */
2458             continue;
2459
2460             default:    /* Not recognized; fall through */
2461             break;      /* Need "default" setting to stop compiler warning. */
2462             }
2463
2464           /* In the pre-compile phase, just do the recognition. */
2465
2466           else if (c == -ESC_d || c == -ESC_D || c == -ESC_w ||
2467                    c == -ESC_W || c == -ESC_s || c == -ESC_S) continue;
2468
2469           /* We need to deal with \P and \p in both phases. */
2470
2471 #ifdef SUPPORT_UCP
2472           if (-c == ESC_p || -c == ESC_P)
2473             {
2474             BOOL negated;
2475             int pdata;
2476             int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
2477             if (ptype < 0) goto FAILED;
2478             class_utf8 = TRUE;
2479             *class_utf8data++ = ((-c == ESC_p) != negated)?
2480               XCL_PROP : XCL_NOTPROP;
2481             *class_utf8data++ = ptype;
2482             *class_utf8data++ = pdata;
2483             class_charcount -= 2;   /* Not a < 256 character */
2484             continue;
2485             }
2486 #endif
2487           /* Unrecognized escapes are faulted if PCRE is running in its
2488           strict mode. By default, for compatibility with Perl, they are
2489           treated as literals. */
2490
2491           if ((options & PCRE_EXTRA) != 0)
2492             {
2493             *errorcodeptr = ERR7;
2494             goto FAILED;
2495             }
2496
2497           class_charcount -= 2;  /* Undo the default count from above */
2498           c = *ptr;              /* Get the final character and fall through */
2499           }
2500
2501         /* Fall through if we have a single character (c >= 0). This may be
2502         greater than 256 in UTF-8 mode. */
2503
2504         }   /* End of backslash handling */
2505
2506       /* A single character may be followed by '-' to form a range. However,
2507       Perl does not permit ']' to be the end of the range. A '-' character
2508       at the end is treated as a literal. Perl ignores orphaned \E sequences
2509       entirely. The code for handling \Q and \E is messy. */
2510
2511       CHECK_RANGE:
2512       while (ptr[1] == '\\' && ptr[2] == 'E')
2513         {
2514         inescq = FALSE;
2515         ptr += 2;
2516         }
2517
2518       oldptr = ptr;
2519
2520       if (!inescq && ptr[1] == '-')
2521         {
2522         int d;
2523         ptr += 2;
2524         while (*ptr == '\\' && ptr[1] == 'E') ptr += 2;
2525
2526         /* If we hit \Q (not followed by \E) at this point, go into escaped
2527         mode. */
2528
2529         while (*ptr == '\\' && ptr[1] == 'Q')
2530           {
2531           ptr += 2;
2532           if (*ptr == '\\' && ptr[1] == 'E') { ptr += 2; continue; }
2533           inescq = TRUE;
2534           break;
2535           }
2536
2537         if (*ptr == 0 || (!inescq && *ptr == ']'))
2538           {
2539           ptr = oldptr;
2540           goto LONE_SINGLE_CHARACTER;
2541           }
2542
2543 #ifdef SUPPORT_UTF8
2544         if (utf8)
2545           {                           /* Braces are required because the */
2546           GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
2547           }
2548         else
2549 #endif
2550         d = *ptr;  /* Not UTF-8 mode */
2551
2552         /* The second part of a range can be a single-character escape, but
2553         not any of the other escapes. Perl 5.6 treats a hyphen as a literal
2554         in such circumstances. */
2555
2556         if (!inescq && d == '\\')
2557           {
2558           d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
2559           if (*errorcodeptr != 0) goto FAILED;
2560
2561           /* \b is backslash; \X is literal X; \R is literal R; any other
2562           special means the '-' was literal */
2563
2564           if (d < 0)
2565             {
2566             if (d == -ESC_b) d = '\b';
2567             else if (d == -ESC_X) d = 'X';
2568             else if (d == -ESC_R) d = 'R'; else
2569               {
2570               ptr = oldptr;
2571               goto LONE_SINGLE_CHARACTER;  /* A few lines below */
2572               }
2573             }
2574           }
2575
2576         /* Check that the two values are in the correct order. Optimize
2577         one-character ranges */
2578
2579         if (d < c)
2580           {
2581           *errorcodeptr = ERR8;
2582           goto FAILED;
2583           }
2584
2585         if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */
2586
2587         /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless
2588         matching, we have to use an XCLASS with extra data items. Caseless
2589         matching for characters > 127 is available only if UCP support is
2590         available. */
2591
2592 #ifdef SUPPORT_UTF8
2593         if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))
2594           {
2595           class_utf8 = TRUE;
2596
2597           /* With UCP support, we can find the other case equivalents of
2598           the relevant characters. There may be several ranges. Optimize how
2599           they fit with the basic range. */
2600
2601 #ifdef SUPPORT_UCP
2602           if ((options & PCRE_CASELESS) != 0)
2603             {
2604             unsigned int occ, ocd;
2605             unsigned int cc = c;
2606             unsigned int origd = d;
2607             while (get_othercase_range(&cc, origd, &occ, &ocd))
2608               {
2609               if (occ >= c && ocd <= d) continue;  /* Skip embedded ranges */
2610
2611               if (occ < c  && ocd >= c - 1)        /* Extend the basic range */
2612                 {                                  /* if there is overlap,   */
2613                 c = occ;                           /* noting that if occ < c */
2614                 continue;                          /* we can't have ocd > d  */
2615                 }                                  /* because a subrange is  */
2616               if (ocd > d && occ <= d + 1)         /* always shorter than    */
2617                 {                                  /* the basic range.       */
2618                 d = ocd;
2619                 continue;
2620                 }
2621
2622               if (occ == ocd)
2623                 {
2624                 *class_utf8data++ = XCL_SINGLE;
2625                 }
2626               else
2627                 {
2628                 *class_utf8data++ = XCL_RANGE;
2629                 class_utf8data += _pcre_ord2utf8(occ, class_utf8data);
2630                 }
2631               class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);
2632               }
2633             }
2634 #endif  /* SUPPORT_UCP */
2635
2636           /* Now record the original range, possibly modified for UCP caseless
2637           overlapping ranges. */
2638
2639           *class_utf8data++ = XCL_RANGE;
2640           class_utf8data += _pcre_ord2utf8(c, class_utf8data);
2641           class_utf8data += _pcre_ord2utf8(d, class_utf8data);
2642
2643           /* With UCP support, we are done. Without UCP support, there is no
2644           caseless matching for UTF-8 characters > 127; we can use the bit map
2645           for the smaller ones. */
2646
2647 #ifdef SUPPORT_UCP
2648           continue;    /* With next character in the class */
2649 #else
2650           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;
2651
2652           /* Adjust upper limit and fall through to set up the map */
2653
2654           d = 127;
2655
2656 #endif  /* SUPPORT_UCP */
2657           }
2658 #endif  /* SUPPORT_UTF8 */
2659
2660         /* We use the bit map for all cases when not in UTF-8 mode; else
2661         ranges that lie entirely within 0-127 when there is UCP support; else
2662         for partial ranges without UCP support. */
2663
2664         class_charcount += d - c + 1;
2665         class_lastchar = d;
2666
2667         /* We can save a bit of time by skipping this in the pre-compile. */
2668
2669         if (lengthptr == NULL) for (; c <= d; c++)
2670           {
2671           classbits[c/8] |= (1 << (c&7));
2672           if ((options & PCRE_CASELESS) != 0)
2673             {
2674             int uc = cd->fcc[c];           /* flip case */
2675             classbits[uc/8] |= (1 << (uc&7));
2676             }
2677           }
2678
2679         continue;   /* Go get the next char in the class */
2680         }
2681
2682       /* Handle a lone single character - we can get here for a normal
2683       non-escape char, or after \ that introduces a single character or for an
2684       apparent range that isn't. */
2685
2686       LONE_SINGLE_CHARACTER:
2687
2688       /* Handle a character that cannot go in the bit map */
2689
2690 #ifdef SUPPORT_UTF8
2691       if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))
2692         {
2693         class_utf8 = TRUE;
2694         *class_utf8data++ = XCL_SINGLE;
2695         class_utf8data += _pcre_ord2utf8(c, class_utf8data);
2696
2697 #ifdef SUPPORT_UCP
2698         if ((options & PCRE_CASELESS) != 0)
2699           {
2700           unsigned int othercase;
2701           if ((othercase = _pcre_ucp_othercase(c)) != NOTACHAR)
2702             {
2703             *class_utf8data++ = XCL_SINGLE;
2704             class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);
2705             }
2706           }
2707 #endif  /* SUPPORT_UCP */
2708
2709         }
2710       else
2711 #endif  /* SUPPORT_UTF8 */
2712
2713       /* Handle a single-byte character */
2714         {
2715         classbits[c/8] |= (1 << (c&7));
2716         if ((options & PCRE_CASELESS) != 0)
2717           {
2718           c = cd->fcc[c];   /* flip case */
2719           classbits[c/8] |= (1 << (c&7));
2720           }
2721         class_charcount++;
2722         class_lastchar = c;
2723         }
2724       }
2725
2726     /* Loop until ']' reached. This "while" is the end of the "do" above. */
2727
2728     while ((c = *(++ptr)) != 0 && (c != ']' || inescq));
2729
2730     if (c == 0)                          /* Missing terminating ']' */
2731       {
2732       *errorcodeptr = ERR6;
2733       goto FAILED;
2734       }
2735
2736     /* If class_charcount is 1, we saw precisely one character whose value is
2737     less than 256. In non-UTF-8 mode we can always optimize. In UTF-8 mode, we
2738     can optimize the negative case only if there were no characters >= 128
2739     because OP_NOT and the related opcodes like OP_NOTSTAR operate on
2740     single-bytes only. This is an historical hangover. Maybe one day we can
2741     tidy these opcodes to handle multi-byte characters.
2742
2743     The optimization throws away the bit map. We turn the item into a
2744     1-character OP_CHAR[NC] if it's positive, or OP_NOT if it's negative. Note
2745     that OP_NOT does not support multibyte characters. In the positive case, it
2746     can cause firstbyte to be set. Otherwise, there can be no first char if
2747     this item is first, whatever repeat count may follow. In the case of
2748     reqbyte, save the previous value for reinstating. */
2749
2750 #ifdef SUPPORT_UTF8
2751     if (class_charcount == 1 &&
2752           (!utf8 ||
2753           (!class_utf8 && (!negate_class || class_lastchar < 128))))
2754
2755 #else
2756     if (class_charcount == 1)
2757 #endif
2758       {
2759       zeroreqbyte = reqbyte;
2760
2761       /* The OP_NOT opcode works on one-byte characters only. */
2762
2763       if (negate_class)
2764         {
2765         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
2766         zerofirstbyte = firstbyte;
2767         *code++ = OP_NOT;
2768         *code++ = class_lastchar;
2769         break;
2770         }
2771
2772       /* For a single, positive character, get the value into mcbuffer, and
2773       then we can handle this with the normal one-character code. */
2774
2775 #ifdef SUPPORT_UTF8
2776       if (utf8 && class_lastchar > 127)
2777         mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);
2778       else
2779 #endif
2780         {
2781         mcbuffer[0] = class_lastchar;
2782         mclength = 1;
2783         }
2784       goto ONE_CHAR;
2785       }       /* End of 1-char optimization */
2786
2787     /* The general case - not the one-char optimization. If this is the first
2788     thing in the branch, there can be no first char setting, whatever the
2789     repeat count. Any reqbyte setting must remain unchanged after any kind of
2790     repeat. */
2791
2792     if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
2793     zerofirstbyte = firstbyte;
2794     zeroreqbyte = reqbyte;
2795
2796     /* If there are characters with values > 255, we have to compile an
2797     extended class, with its own opcode. If there are no characters < 256,
2798     we can omit the bitmap in the actual compiled code. */
2799
2800 #ifdef SUPPORT_UTF8
2801     if (class_utf8)
2802       {
2803       *class_utf8data++ = XCL_END;    /* Marks the end of extra data */
2804       *code++ = OP_XCLASS;
2805       code += LINK_SIZE;
2806       *code = negate_class? XCL_NOT : 0;
2807
2808       /* If the map is required, move up the extra data to make room for it;
2809       otherwise just move the code pointer to the end of the extra data. */
2810
2811       if (class_charcount > 0)
2812         {
2813         *code++ |= XCL_MAP;
2814         memmove(code + 32, code, class_utf8data - code);
2815         memcpy(code, classbits, 32);
2816         code = class_utf8data + 32;
2817         }
2818       else code = class_utf8data;
2819
2820       /* Now fill in the complete length of the item */
2821
2822       PUT(previous, 1, code - previous);
2823       break;   /* End of class handling */
2824       }
2825 #endif
2826
2827     /* If there are no characters > 255, negate the 32-byte map if necessary,
2828     and copy it into the code vector. If this is the first thing in the branch,
2829     there can be no first char setting, whatever the repeat count. Any reqbyte
2830     setting must remain unchanged after any kind of repeat. */
2831
2832     if (negate_class)
2833       {
2834       *code++ = OP_NCLASS;
2835       if (lengthptr == NULL)    /* Save time in the pre-compile phase */
2836         for (c = 0; c < 32; c++) code[c] = ~classbits[c];
2837       }
2838     else
2839       {
2840       *code++ = OP_CLASS;
2841       memcpy(code, classbits, 32);
2842       }
2843     code += 32;
2844     break;
2845
2846
2847     /* ===================================================================*/
2848     /* Various kinds of repeat; '{' is not necessarily a quantifier, but this
2849     has been tested above. */
2850
2851     case '{':
2852     if (!is_quantifier) goto NORMAL_CHAR;
2853     ptr = read_repeat_counts(ptr+1, &repeat_min, &repeat_max, errorcodeptr);
2854     if (*errorcodeptr != 0) goto FAILED;
2855     goto REPEAT;
2856
2857     case '*':
2858     repeat_min = 0;
2859     repeat_max = -1;
2860     goto REPEAT;
2861
2862     case '+':
2863     repeat_min = 1;
2864     repeat_max = -1;
2865     goto REPEAT;
2866
2867     case '?':
2868     repeat_min = 0;
2869     repeat_max = 1;
2870
2871     REPEAT:
2872     if (previous == NULL)
2873       {
2874       *errorcodeptr = ERR9;
2875       goto FAILED;
2876       }
2877
2878     if (repeat_min == 0)
2879       {
2880       firstbyte = zerofirstbyte;    /* Adjust for zero repeat */
2881       reqbyte = zeroreqbyte;        /* Ditto */
2882       }
2883
2884     /* Remember whether this is a variable length repeat */
2885
2886     reqvary = (repeat_min == repeat_max)? 0 : REQ_VARY;
2887
2888     op_type = 0;                    /* Default single-char op codes */
2889     possessive_quantifier = FALSE;  /* Default not possessive quantifier */
2890
2891     /* Save start of previous item, in case we have to move it up to make space
2892     for an inserted OP_ONCE for the additional '+' extension. */
2893
2894     tempcode = previous;
2895
2896     /* If the next character is '+', we have a possessive quantifier. This
2897     implies greediness, whatever the setting of the PCRE_UNGREEDY option.
2898     If the next character is '?' this is a minimizing repeat, by default,
2899     but if PCRE_UNGREEDY is set, it works the other way round. We change the
2900     repeat type to the non-default. */
2901
2902     if (ptr[1] == '+')
2903       {
2904       repeat_type = 0;                  /* Force greedy */
2905       possessive_quantifier = TRUE;
2906       ptr++;
2907       }
2908     else if (ptr[1] == '?')
2909       {
2910       repeat_type = greedy_non_default;
2911       ptr++;
2912       }
2913     else repeat_type = greedy_default;
2914
2915     /* If previous was a character match, abolish the item and generate a
2916     repeat item instead. If a char item has a minumum of more than one, ensure
2917     that it is set in reqbyte - it might not be if a sequence such as x{3} is
2918     the first thing in a branch because the x will have gone into firstbyte
2919     instead.  */
2920
2921     if (*previous == OP_CHAR || *previous == OP_CHARNC)
2922       {
2923       /* Deal with UTF-8 characters that take up more than one byte. It's
2924       easier to write this out separately than try to macrify it. Use c to
2925       hold the length of the character in bytes, plus 0x80 to flag that it's a
2926       length rather than a small character. */
2927
2928 #ifdef SUPPORT_UTF8
2929       if (utf8 && (code[-1] & 0x80) != 0)
2930         {
2931         uschar *lastchar = code - 1;
2932         while((*lastchar & 0xc0) == 0x80) lastchar--;
2933         c = code - lastchar;            /* Length of UTF-8 character */
2934         memcpy(utf8_char, lastchar, c); /* Save the char */
2935         c |= 0x80;                      /* Flag c as a length */
2936         }
2937       else
2938 #endif
2939
2940       /* Handle the case of a single byte - either with no UTF8 support, or
2941       with UTF-8 disabled, or for a UTF-8 character < 128. */
2942
2943         {
2944         c = code[-1];
2945         if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt;
2946         }
2947
2948       /* If the repetition is unlimited, it pays to see if the next thing on
2949       the line is something that cannot possibly match this character. If so,
2950       automatically possessifying this item gains some performance in the case
2951       where the match fails. */
2952
2953       if (!possessive_quantifier &&
2954           repeat_max < 0 &&
2955           check_auto_possessive(*previous, c, utf8, utf8_char, ptr + 1,
2956             options, cd))
2957         {
2958         repeat_type = 0;    /* Force greedy */
2959         possessive_quantifier = TRUE;
2960         }
2961
2962       goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
2963       }
2964
2965     /* If previous was a single negated character ([^a] or similar), we use
2966     one of the special opcodes, replacing it. The code is shared with single-
2967     character repeats by setting opt_type to add a suitable offset into
2968     repeat_type. We can also test for auto-possessification. OP_NOT is
2969     currently used only for single-byte chars. */
2970
2971     else if (*previous == OP_NOT)
2972       {
2973       op_type = OP_NOTSTAR - OP_STAR;  /* Use "not" opcodes */
2974       c = previous[1];
2975       if (!possessive_quantifier &&
2976           repeat_max < 0 &&
2977           check_auto_possessive(OP_NOT, c, utf8, NULL, ptr + 1, options, cd))
2978         {
2979         repeat_type = 0;    /* Force greedy */
2980         possessive_quantifier = TRUE;
2981         }
2982       goto OUTPUT_SINGLE_REPEAT;
2983       }
2984
2985     /* If previous was a character type match (\d or similar), abolish it and
2986     create a suitable repeat item. The code is shared with single-character
2987     repeats by setting op_type to add a suitable offset into repeat_type. Note
2988     the the Unicode property types will be present only when SUPPORT_UCP is
2989     defined, but we don't wrap the little bits of code here because it just
2990     makes it horribly messy. */
2991
2992     else if (*previous < OP_EODN)
2993       {
2994       uschar *oldcode;
2995       int prop_type, prop_value;
2996       op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
2997       c = *previous;
2998
2999       if (!possessive_quantifier &&
3000           repeat_max < 0 &&
3001           check_auto_possessive(c, 0, utf8, NULL, ptr + 1, options, cd))
3002         {
3003         repeat_type = 0;    /* Force greedy */
3004         possessive_quantifier = TRUE;
3005         }
3006
3007       OUTPUT_SINGLE_REPEAT:
3008       if (*previous == OP_PROP || *previous == OP_NOTPROP)
3009         {
3010         prop_type = previous[1];
3011         prop_value = previous[2];
3012         }
3013       else prop_type = prop_value = -1;
3014
3015       oldcode = code;
3016       code = previous;                  /* Usually overwrite previous item */
3017
3018       /* If the maximum is zero then the minimum must also be zero; Perl allows
3019       this case, so we do too - by simply omitting the item altogether. */
3020
3021       if (repeat_max == 0) goto END_REPEAT;
3022
3023       /* All real repeats make it impossible to handle partial matching (maybe
3024       one day we will be able to remove this restriction). */
3025
3026       if (repeat_max != 1) cd->nopartial = TRUE;
3027
3028       /* Combine the op_type with the repeat_type */
3029
3030       repeat_type += op_type;
3031
3032       /* A minimum of zero is handled either as the special case * or ?, or as
3033       an UPTO, with the maximum given. */
3034
3035       if (repeat_min == 0)
3036         {
3037         if (repeat_max == -1) *code++ = OP_STAR + repeat_type;
3038           else if (repeat_max == 1) *code++ = OP_QUERY + repeat_type;
3039         else
3040           {
3041           *code++ = OP_UPTO + repeat_type;
3042           PUT2INC(code, 0, repeat_max);
3043           }
3044         }
3045
3046       /* A repeat minimum of 1 is optimized into some special cases. If the
3047       maximum is unlimited, we use OP_PLUS. Otherwise, the original item is
3048       left in place and, if the maximum is greater than 1, we use OP_UPTO with
3049       one less than the maximum. */
3050
3051       else if (repeat_min == 1)
3052         {
3053         if (repeat_max == -1)
3054           *code++ = OP_PLUS + repeat_type;
3055         else
3056           {
3057           code = oldcode;                 /* leave previous item in place */
3058           if (repeat_max == 1) goto END_REPEAT;
3059           *code++ = OP_UPTO + repeat_type;
3060           PUT2INC(code, 0, repeat_max - 1);
3061           }
3062         }
3063
3064       /* The case {n,n} is just an EXACT, while the general case {n,m} is
3065       handled as an EXACT followed by an UPTO. */
3066
3067       else
3068         {
3069         *code++ = OP_EXACT + op_type;  /* NB EXACT doesn't have repeat_type */
3070         PUT2INC(code, 0, repeat_min);
3071
3072         /* If the maximum is unlimited, insert an OP_STAR. Before doing so,
3073         we have to insert the character for the previous code. For a repeated
3074         Unicode property match, there are two extra bytes that define the
3075         required property. In UTF-8 mode, long characters have their length in
3076         c, with the 0x80 bit as a flag. */
3077
3078         if (repeat_max < 0)
3079           {
3080 #ifdef SUPPORT_UTF8
3081           if (utf8 && c >= 128)
3082             {
3083             memcpy(code, utf8_char, c & 7);
3084             code += c & 7;
3085             }
3086           else
3087 #endif
3088             {
3089             *code++ = c;
3090             if (prop_type >= 0)
3091               {
3092               *code++ = prop_type;
3093               *code++ = prop_value;
3094               }
3095             }
3096           *code++ = OP_STAR + repeat_type;
3097           }
3098
3099         /* Else insert an UPTO if the max is greater than the min, again
3100         preceded by the character, for the previously inserted code. If the
3101         UPTO is just for 1 instance, we can use QUERY instead. */
3102
3103         else if (repeat_max != repeat_min)
3104           {
3105 #ifdef SUPPORT_UTF8
3106           if (utf8 && c >= 128)
3107             {
3108             memcpy(code, utf8_char, c & 7);
3109             code += c & 7;
3110             }
3111           else
3112 #endif
3113           *code++ = c;
3114           if (prop_type >= 0)
3115             {
3116             *code++ = prop_type;
3117             *code++ = prop_value;
3118             }
3119           repeat_max -= repeat_min;
3120
3121           if (repeat_max == 1)
3122             {
3123             *code++ = OP_QUERY + repeat_type;
3124             }
3125           else
3126             {
3127             *code++ = OP_UPTO + repeat_type;
3128             PUT2INC(code, 0, repeat_max);
3129             }
3130           }
3131         }
3132
3133       /* The character or character type itself comes last in all cases. */
3134
3135 #ifdef SUPPORT_UTF8
3136       if (utf8 && c >= 128)
3137         {
3138         memcpy(code, utf8_char, c & 7);
3139         code += c & 7;
3140         }
3141       else
3142 #endif
3143       *code++ = c;
3144
3145       /* For a repeated Unicode property match, there are two extra bytes that
3146       define the required property. */
3147
3148 #ifdef SUPPORT_UCP
3149       if (prop_type >= 0)
3150         {
3151         *code++ = prop_type;
3152         *code++ = prop_value;
3153         }
3154 #endif
3155       }
3156
3157     /* If previous was a character class or a back reference, we put the repeat
3158     stuff after it, but just skip the item if the repeat was {0,0}. */
3159
3160     else if (*previous == OP_CLASS ||
3161              *previous == OP_NCLASS ||
3162 #ifdef SUPPORT_UTF8
3163              *previous == OP_XCLASS ||
3164 #endif
3165              *previous == OP_REF)
3166       {
3167       if (repeat_max == 0)
3168         {
3169         code = previous;
3170         goto END_REPEAT;
3171         }
3172
3173       /* All real repeats make it impossible to handle partial matching (maybe
3174       one day we will be able to remove this restriction). */
3175
3176       if (repeat_max != 1) cd->nopartial = TRUE;
3177
3178       if (repeat_min == 0 && repeat_max == -1)
3179         *code++ = OP_CRSTAR + repeat_type;
3180       else if (repeat_min == 1 && repeat_max == -1)
3181         *code++ = OP_CRPLUS + repeat_type;
3182       else if (repeat_min == 0 && repeat_max == 1)
3183         *code++ = OP_CRQUERY + repeat_type;
3184       else
3185         {
3186         *code++ = OP_CRRANGE + repeat_type;
3187         PUT2INC(code, 0, repeat_min);
3188         if (repeat_max == -1) repeat_max = 0;  /* 2-byte encoding for max */
3189         PUT2INC(code, 0, repeat_max);
3190         }
3191       }
3192
3193     /* If previous was a bracket group, we may have to replicate it in certain
3194     cases. */
3195
3196     else if (*previous == OP_BRA  || *previous == OP_CBRA ||
3197              *previous == OP_ONCE || *previous == OP_COND)
3198       {
3199       register int i;
3200       int ketoffset = 0;
3201       int len = code - previous;
3202       uschar *bralink = NULL;
3203
3204       /* Repeating a DEFINE group is pointless */
3205
3206       if (*previous == OP_COND && previous[LINK_SIZE+1] == OP_DEF)
3207         {
3208         *errorcodeptr = ERR55;
3209         goto FAILED;
3210         }
3211
3212       /* This is a paranoid check to stop integer overflow later on */
3213
3214       if (len > MAX_DUPLENGTH)
3215         {
3216         *errorcodeptr = ERR50;
3217         goto FAILED;
3218         }
3219
3220       /* If the maximum repeat count is unlimited, find the end of the bracket
3221       by scanning through from the start, and compute the offset back to it
3222       from the current code pointer. There may be an OP_OPT setting following
3223       the final KET, so we can't find the end just by going back from the code
3224       pointer. */
3225
3226       if (repeat_max == -1)
3227         {
3228         register uschar *ket = previous;
3229         do ket += GET(ket, 1); while (*ket != OP_KET);
3230         ketoffset = code - ket;
3231         }
3232
3233       /* The case of a zero minimum is special because of the need to stick
3234       OP_BRAZERO in front of it, and because the group appears once in the
3235       data, whereas in other cases it appears the minimum number of times. For
3236       this reason, it is simplest to treat this case separately, as otherwise
3237       the code gets far too messy. There are several special subcases when the
3238       minimum is zero. */
3239
3240       if (repeat_min == 0)
3241         {
3242         /* If the maximum is also zero, we just omit the group from the output
3243         altogether. */
3244
3245         if (repeat_max == 0)
3246           {
3247           code = previous;
3248           goto END_REPEAT;
3249           }
3250
3251         /* If the maximum is 1 or unlimited, we just have to stick in the
3252         BRAZERO and do no more at this point. However, we do need to adjust
3253         any OP_RECURSE calls inside the group that refer to the group itself or
3254         any internal or forward referenced group, because the offset is from
3255         the start of the whole regex. Temporarily terminate the pattern while
3256         doing this. */
3257
3258         if (repeat_max <= 1)
3259           {
3260           *code = OP_END;
3261           adjust_recurse(previous, 1, utf8, cd, save_hwm);
3262           memmove(previous+1, previous, len);
3263           code++;
3264           *previous++ = OP_BRAZERO + repeat_type;
3265           }
3266
3267         /* If the maximum is greater than 1 and limited, we have to replicate
3268         in a nested fashion, sticking OP_BRAZERO before each set of brackets.
3269         The first one has to be handled carefully because it's the original
3270         copy, which has to be moved up. The remainder can be handled by code
3271         that is common with the non-zero minimum case below. We have to
3272         adjust the value or repeat_max, since one less copy is required. Once
3273         again, we may have to adjust any OP_RECURSE calls inside the group. */
3274
3275         else
3276           {
3277           int offset;
3278           *code = OP_END;
3279           adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm);
3280           memmove(previous + 2 + LINK_SIZE, previous, len);
3281           code += 2 + LINK_SIZE;
3282           *previous++ = OP_BRAZERO + repeat_type;
3283           *previous++ = OP_BRA;
3284
3285           /* We chain together the bracket offset fields that have to be
3286           filled in later when the ends of the brackets are reached. */
3287
3288           offset = (bralink == NULL)? 0 : previous - bralink;
3289           bralink = previous;
3290           PUTINC(previous, 0, offset);
3291           }
3292
3293         repeat_max--;
3294         }
3295
3296       /* If the minimum is greater than zero, replicate the group as many
3297       times as necessary, and adjust the maximum to the number of subsequent
3298       copies that we need. If we set a first char from the group, and didn't
3299       set a required char, copy the latter from the former. If there are any
3300       forward reference subroutine calls in the group, there will be entries on
3301       the workspace list; replicate these with an appropriate increment. */
3302
3303       else
3304         {
3305         if (repeat_min > 1)
3306           {
3307           /* In the pre-compile phase, we don't actually do the replication. We
3308           just adjust the length as if we had. */
3309
3310           if (lengthptr != NULL)
3311             *lengthptr += (repeat_min - 1)*length_prevgroup;
3312
3313           /* This is compiling for real */
3314
3315           else
3316             {
3317             if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;
3318             for (i = 1; i < repeat_min; i++)
3319               {
3320               uschar *hc;
3321               uschar *this_hwm = cd->hwm;
3322               memcpy(code, previous, len);
3323               for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
3324                 {
3325                 PUT(cd->hwm, 0, GET(hc, 0) + len);
3326                 cd->hwm += LINK_SIZE;
3327                 }
3328               save_hwm = this_hwm;
3329               code += len;
3330               }
3331             }
3332           }
3333
3334         if (repeat_max > 0) repeat_max -= repeat_min;
3335         }
3336
3337       /* This code is common to both the zero and non-zero minimum cases. If
3338       the maximum is limited, it replicates the group in a nested fashion,
3339       remembering the bracket starts on a stack. In the case of a zero minimum,
3340       the first one was set up above. In all cases the repeat_max now specifies
3341       the number of additional copies needed. Again, we must remember to
3342       replicate entries on the forward reference list. */
3343
3344       if (repeat_max >= 0)
3345         {
3346         /* In the pre-compile phase, we don't actually do the replication. We
3347         just adjust the length as if we had. For each repetition we must add 1
3348         to the length for BRAZERO and for all but the last repetition we must
3349         add 2 + 2*LINKSIZE to allow for the nesting that occurs. */
3350
3351         if (lengthptr != NULL && repeat_max > 0)
3352           *lengthptr += repeat_max * (length_prevgroup + 1 + 2 + 2*LINK_SIZE) -
3353             2 - 2*LINK_SIZE;  /* Last one doesn't nest */
3354
3355         /* This is compiling for real */
3356
3357         else for (i = repeat_max - 1; i >= 0; i--)
3358           {
3359           uschar *hc;
3360           uschar *this_hwm = cd->hwm;
3361
3362           *code++ = OP_BRAZERO + repeat_type;
3363
3364           /* All but the final copy start a new nesting, maintaining the
3365           chain of brackets outstanding. */
3366
3367           if (i != 0)
3368             {
3369             int offset;
3370             *code++ = OP_BRA;
3371             offset = (bralink == NULL)? 0 : code - bralink;
3372             bralink = code;
3373             PUTINC(code, 0, offset);
3374             }
3375
3376           memcpy(code, previous, len);
3377           for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
3378             {
3379             PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
3380             cd->hwm += LINK_SIZE;
3381             }
3382           save_hwm = this_hwm;
3383           code += len;
3384           }
3385
3386         /* Now chain through the pending brackets, and fill in their length
3387         fields (which are holding the chain links pro tem). */
3388
3389         while (bralink != NULL)
3390           {
3391           int oldlinkoffset;
3392           int offset = code - bralink + 1;
3393           uschar *bra = code - offset;
3394           oldlinkoffset = GET(bra, 1);
3395           bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
3396           *code++ = OP_KET;
3397           PUTINC(code, 0, offset);
3398           PUT(bra, 1, offset);
3399           }
3400         }
3401
3402       /* If the maximum is unlimited, set a repeater in the final copy. We
3403       can't just offset backwards from the current code point, because we
3404       don't know if there's been an options resetting after the ket. The
3405       correct offset was computed above.
3406
3407       Then, when we are doing the actual compile phase, check to see whether
3408       this group is a non-atomic one that could match an empty string. If so,
3409       convert the initial operator to the S form (e.g. OP_BRA -> OP_SBRA) so
3410       that runtime checking can be done. [This check is also applied to
3411       atomic groups at runtime, but in a different way.] */
3412
3413       else
3414         {
3415         uschar *ketcode = code - ketoffset;
3416         uschar *bracode = ketcode - GET(ketcode, 1);
3417         *ketcode = OP_KETRMAX + repeat_type;
3418         if (lengthptr == NULL && *bracode != OP_ONCE)
3419           {
3420           uschar *scode = bracode;
3421           do
3422             {
3423             if (could_be_empty_branch(scode, ketcode, utf8))
3424               {
3425               *bracode += OP_SBRA - OP_BRA;
3426               break;
3427               }
3428             scode += GET(scode, 1);
3429             }
3430           while (*scode == OP_ALT);
3431           }
3432         }
3433       }
3434
3435     /* Else there's some kind of shambles */
3436
3437     else
3438       {
3439       *errorcodeptr = ERR11;
3440       goto FAILED;
3441       }
3442
3443     /* If the character following a repeat is '+', or if certain optimization
3444     tests above succeeded, possessive_quantifier is TRUE. For some of the
3445     simpler opcodes, there is an special alternative opcode for this. For
3446     anything else, we wrap the entire repeated item inside OP_ONCE brackets.
3447     The '+' notation is just syntactic sugar, taken from Sun's Java package,
3448     but the special opcodes can optimize it a bit. The repeated item starts at
3449     tempcode, not at previous, which might be the first part of a string whose
3450     (former) last char we repeated.
3451
3452     Possessifying an 'exact' quantifier has no effect, so we can ignore it. But
3453     an 'upto' may follow. We skip over an 'exact' item, and then test the
3454     length of what remains before proceeding. */
3455
3456     if (possessive_quantifier)
3457       {
3458       int len;
3459       if (*tempcode == OP_EXACT || *tempcode == OP_TYPEEXACT ||
3460           *tempcode == OP_NOTEXACT)
3461         tempcode += _pcre_OP_lengths[*tempcode];
3462       len = code - tempcode;
3463       if (len > 0) switch (*tempcode)
3464         {
3465         case OP_STAR:  *tempcode = OP_POSSTAR; break;
3466         case OP_PLUS:  *tempcode = OP_POSPLUS; break;
3467         case OP_QUERY: *tempcode = OP_POSQUERY; break;
3468         case OP_UPTO:  *tempcode = OP_POSUPTO; break;
3469
3470         case OP_TYPESTAR:  *tempcode = OP_TYPEPOSSTAR; break;
3471         case OP_TYPEPLUS:  *tempcode = OP_TYPEPOSPLUS; break;
3472         case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
3473         case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
3474
3475         case OP_NOTSTAR:  *tempcode = OP_NOTPOSSTAR; break;
3476         case OP_NOTPLUS:  *tempcode = OP_NOTPOSPLUS; break;
3477         case OP_NOTQUERY: *tempcode = OP_NOTPOSQUERY; break;
3478         case OP_NOTUPTO:  *tempcode = OP_NOTPOSUPTO; break;
3479
3480         default:
3481         memmove(tempcode + 1+LINK_SIZE, tempcode, len);
3482         code += 1 + LINK_SIZE;
3483         len += 1 + LINK_SIZE;
3484         tempcode[0] = OP_ONCE;
3485         *code++ = OP_KET;
3486         PUTINC(code, 0, len);
3487         PUT(tempcode, 1, len);
3488         break;
3489         }
3490       }
3491
3492     /* In all case we no longer have a previous item. We also set the
3493     "follows varying string" flag for subsequently encountered reqbytes if
3494     it isn't already set and we have just passed a varying length item. */
3495
3496     END_REPEAT:
3497     previous = NULL;
3498     cd->req_varyopt |= reqvary;
3499     break;
3500
3501
3502     /* ===================================================================*/
3503     /* Start of nested parenthesized sub-expression, or comment or lookahead or
3504     lookbehind or option setting or condition or all the other extended
3505     parenthesis forms. First deal with the specials; all are introduced by ?,
3506     and the appearance of any of them means that this is not a capturing
3507     group. */
3508
3509     case '(':
3510     newoptions = options;
3511     skipbytes = 0;
3512     bravalue = OP_CBRA;
3513     save_hwm = cd->hwm;
3514
3515     if (*(++ptr) == '?')
3516       {
3517       int i, set, unset, namelen;
3518       int *optset;
3519       const uschar *name;
3520       uschar *slot;
3521
3522       switch (*(++ptr))
3523         {
3524         case '#':                 /* Comment; skip to ket */
3525         ptr++;
3526         while (*ptr != 0 && *ptr != ')') ptr++;
3527         if (*ptr == 0)
3528           {
3529           *errorcodeptr = ERR18;
3530           goto FAILED;
3531           }
3532         continue;
3533
3534
3535         /* ------------------------------------------------------------ */
3536         case ':':                 /* Non-capturing bracket */
3537         bravalue = OP_BRA;
3538         ptr++;
3539         break;
3540
3541
3542         /* ------------------------------------------------------------ */
3543         case '(':
3544         bravalue = OP_COND;       /* Conditional group */
3545
3546         /* A condition can be an assertion, a number (referring to a numbered
3547         group), a name (referring to a named group), or 'R', referring to
3548         recursion. R<digits> and R&name are also permitted for recursion tests.
3549
3550         There are several syntaxes for testing a named group: (?(name)) is used
3551         by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).
3552
3553         There are two unfortunate ambiguities, caused by history. (a) 'R' can
3554         be the recursive thing or the name 'R' (and similarly for 'R' followed
3555         by digits), and (b) a number could be a name that consists of digits.
3556         In both cases, we look for a name first; if not found, we try the other
3557         cases. */
3558
3559         /* For conditions that are assertions, check the syntax, and then exit
3560         the switch. This will take control down to where bracketed groups,
3561         including assertions, are processed. */
3562
3563         if (ptr[1] == '?' && (ptr[2] == '=' || ptr[2] == '!' || ptr[2] == '<'))
3564           break;
3565
3566         /* Most other conditions use OP_CREF (a couple change to OP_RREF
3567         below), and all need to skip 3 bytes at the start of the group. */
3568
3569         code[1+LINK_SIZE] = OP_CREF;
3570         skipbytes = 3;
3571
3572         /* Check for a test for recursion in a named group. */
3573
3574         if (ptr[1] == 'R' && ptr[2] == '&')
3575           {
3576           terminator = -1;
3577           ptr += 2;
3578           code[1+LINK_SIZE] = OP_RREF;    /* Change the type of test */
3579           }
3580
3581         /* Check for a test for a named group's having been set, using the Perl
3582         syntax (?(<name>) or (?('name') */
3583
3584         else if (ptr[1] == '<')
3585           {
3586           terminator = '>';
3587           ptr++;
3588           }
3589         else if (ptr[1] == '\'')
3590           {
3591           terminator = '\'';
3592           ptr++;
3593           }
3594         else terminator = 0;
3595
3596         /* We now expect to read a name; any thing else is an error */
3597
3598         if ((cd->ctypes[ptr[1]] & ctype_word) == 0)
3599           {
3600           ptr += 1;  /* To get the right offset */
3601           *errorcodeptr = ERR28;
3602           goto FAILED;
3603           }
3604
3605         /* Read the name, but also get it as a number if it's all digits */
3606
3607         recno = 0;
3608         name = ++ptr;
3609         while ((cd->ctypes[*ptr] & ctype_word) != 0)
3610           {
3611           if (recno >= 0)
3612             recno = (g_ascii_isdigit(*ptr) != 0)?
3613               recno * 10 + *ptr - '0' : -1;
3614           ptr++;
3615           }
3616         namelen = ptr - name;
3617
3618         if ((terminator > 0 && *ptr++ != terminator) || *ptr++ != ')')
3619           {
3620           ptr--;      /* Error offset */
3621           *errorcodeptr = ERR26;
3622           goto FAILED;
3623           }
3624
3625         /* Do no further checking in the pre-compile phase. */
3626
3627         if (lengthptr != NULL) break;
3628
3629         /* In the real compile we do the work of looking for the actual
3630         reference. */
3631
3632         slot = cd->name_table;
3633         for (i = 0; i < cd->names_found; i++)
3634           {
3635           if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;
3636           slot += cd->name_entry_size;
3637           }
3638
3639         /* Found a previous named subpattern */
3640
3641         if (i < cd->names_found)
3642           {
3643           recno = GET2(slot, 0);
3644           PUT2(code, 2+LINK_SIZE, recno);
3645           }
3646
3647         /* Search the pattern for a forward reference */
3648
3649         else if ((i = find_parens(ptr, cd->bracount, name, namelen,
3650                         (options & PCRE_EXTENDED) != 0)) > 0)
3651           {
3652           PUT2(code, 2+LINK_SIZE, i);
3653           }
3654
3655         /* If terminator == 0 it means that the name followed directly after
3656         the opening parenthesis [e.g. (?(abc)...] and in this case there are
3657         some further alternatives to try. For the cases where terminator != 0
3658         [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have
3659         now checked all the possibilities, so give an error. */
3660
3661         else if (terminator != 0)
3662           {
3663           *errorcodeptr = ERR15;
3664           goto FAILED;
3665           }
3666
3667         /* Check for (?(R) for recursion. Allow digits after R to specify a
3668         specific group number. */
3669
3670         else if (*name == 'R')
3671           {
3672           recno = 0;
3673           for (i = 1; i < namelen; i++)
3674             {
3675             if (g_ascii_isdigit(name[i]) == 0)
3676               {
3677               *errorcodeptr = ERR15;
3678               goto FAILED;
3679               }
3680             recno = recno * 10 + name[i] - '0';
3681             }
3682           if (recno == 0) recno = RREF_ANY;
3683           code[1+LINK_SIZE] = OP_RREF;      /* Change test type */
3684           PUT2(code, 2+LINK_SIZE, recno);
3685           }
3686
3687         /* Similarly, check for the (?(DEFINE) "condition", which is always
3688         false. */
3689
3690         else if (namelen == 6 && strncmp((char *)name, "DEFINE", 6) == 0)
3691           {
3692           code[1+LINK_SIZE] = OP_DEF;
3693           skipbytes = 1;
3694           }
3695
3696         /* Check for the "name" actually being a subpattern number. */
3697
3698         else if (recno > 0)
3699           {
3700           PUT2(code, 2+LINK_SIZE, recno);
3701           }
3702
3703         /* Either an unidentified subpattern, or a reference to (?(0) */
3704
3705         else
3706           {
3707           *errorcodeptr = (recno == 0)? ERR35: ERR15;
3708           goto FAILED;
3709           }
3710         break;
3711
3712
3713         /* ------------------------------------------------------------ */
3714         case '=':                 /* Positive lookahead */
3715         bravalue = OP_ASSERT;
3716         ptr++;
3717         break;
3718
3719
3720         /* ------------------------------------------------------------ */
3721         case '!':                 /* Negative lookahead */
3722         bravalue = OP_ASSERT_NOT;
3723         ptr++;
3724         break;
3725
3726
3727         /* ------------------------------------------------------------ */
3728         case '<':                 /* Lookbehind or named define */
3729         switch (ptr[1])
3730           {
3731           case '=':               /* Positive lookbehind */
3732           bravalue = OP_ASSERTBACK;
3733           ptr += 2;
3734           break;
3735
3736           case '!':               /* Negative lookbehind */
3737           bravalue = OP_ASSERTBACK_NOT;
3738           ptr += 2;
3739           break;
3740
3741           default:                /* Could be name define, else bad */
3742           if ((cd->ctypes[ptr[1]] & ctype_word) != 0) goto DEFINE_NAME;
3743           ptr++;                  /* Correct offset for error */
3744           *errorcodeptr = ERR24;
3745           goto FAILED;
3746           }
3747         break;
3748
3749
3750         /* ------------------------------------------------------------ */
3751         case '>':                 /* One-time brackets */
3752         bravalue = OP_ONCE;
3753         ptr++;
3754         break;
3755
3756
3757         /* ------------------------------------------------------------ */
3758         case 'C':                 /* Callout - may be followed by digits; */
3759         previous_callout = code;  /* Save for later completion */
3760         after_manual_callout = 1; /* Skip one item before completing */
3761         *code++ = OP_CALLOUT;
3762           {
3763           int n = 0;
3764           while (g_ascii_isdigit(*(++ptr)) != 0)
3765             n = n * 10 + *ptr - '0';
3766           if (*ptr != ')')
3767             {
3768             *errorcodeptr = ERR39;
3769             goto FAILED;
3770             }
3771           if (n > 255)
3772             {
3773             *errorcodeptr = ERR38;
3774             goto FAILED;
3775             }
3776           *code++ = n;
3777           PUT(code, 0, ptr - cd->start_pattern + 1);  /* Pattern offset */
3778           PUT(code, LINK_SIZE, 0);                    /* Default length */
3779           code += 2 * LINK_SIZE;
3780           }
3781         previous = NULL;
3782         continue;
3783
3784
3785         /* ------------------------------------------------------------ */
3786         case 'P':                 /* Python-style named subpattern handling */
3787         if (*(++ptr) == '=' || *ptr == '>')  /* Reference or recursion */
3788           {
3789           is_recurse = *ptr == '>';
3790           terminator = ')';
3791           goto NAMED_REF_OR_RECURSE;
3792           }
3793         else if (*ptr != '<')    /* Test for Python-style definition */
3794           {
3795           *errorcodeptr = ERR41;
3796           goto FAILED;
3797           }
3798         /* Fall through to handle (?P< as (?< is handled */
3799
3800
3801         /* ------------------------------------------------------------ */
3802         DEFINE_NAME:    /* Come here from (?< handling */
3803         case '\'':
3804           {
3805           terminator = (*ptr == '<')? '>' : '\'';
3806           name = ++ptr;
3807
3808           while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
3809           namelen = ptr - name;
3810
3811           /* In the pre-compile phase, just do a syntax check. */
3812
3813           if (lengthptr != NULL)
3814             {
3815             if (*ptr != terminator)
3816               {
3817               *errorcodeptr = ERR42;
3818               goto FAILED;
3819               }
3820             if (cd->names_found >= MAX_NAME_COUNT)
3821               {
3822               *errorcodeptr = ERR49;
3823               goto FAILED;
3824               }
3825             if (namelen + 3 > cd->name_entry_size)
3826               {
3827               cd->name_entry_size = namelen + 3;
3828               if (namelen > MAX_NAME_SIZE)
3829                 {
3830                 *errorcodeptr = ERR48;
3831                 goto FAILED;
3832                 }
3833               }
3834             }
3835
3836           /* In the real compile, create the entry in the table */
3837
3838           else
3839             {
3840             slot = cd->name_table;
3841             for (i = 0; i < cd->names_found; i++)
3842               {
3843               int crc = memcmp(name, slot+2, namelen);
3844               if (crc == 0)
3845                 {
3846                 if (slot[2+namelen] == 0)
3847                   {
3848                   if ((options & PCRE_DUPNAMES) == 0)
3849                     {
3850                     *errorcodeptr = ERR43;
3851                     goto FAILED;
3852                     }
3853                   }
3854                 else crc = -1;      /* Current name is substring */
3855                 }
3856               if (crc < 0)
3857                 {
3858                 memmove(slot + cd->name_entry_size, slot,
3859                   (cd->names_found - i) * cd->name_entry_size);
3860                 break;
3861                 }
3862               slot += cd->name_entry_size;
3863               }
3864
3865             PUT2(slot, 0, cd->bracount + 1);
3866             memcpy(slot + 2, name, namelen);
3867             slot[2+namelen] = 0;
3868             }
3869           }
3870
3871         /* In both cases, count the number of names we've encountered. */
3872
3873         ptr++;                    /* Move past > or ' */
3874         cd->names_found++;
3875         goto NUMBERED_GROUP;
3876
3877
3878         /* ------------------------------------------------------------ */
3879         case '&':                 /* Perl recursion/subroutine syntax */
3880         terminator = ')';
3881         is_recurse = TRUE;
3882         /* Fall through */
3883
3884         /* We come here from the Python syntax above that handles both
3885         references (?P=name) and recursion (?P>name), as well as falling
3886         through from the Perl recursion syntax (?&name). */
3887
3888         NAMED_REF_OR_RECURSE:
3889         name = ++ptr;
3890         while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
3891         namelen = ptr - name;
3892
3893         /* In the pre-compile phase, do a syntax check and set a dummy
3894         reference number. */
3895
3896         if (lengthptr != NULL)
3897           {
3898           if (*ptr != terminator)
3899             {
3900             *errorcodeptr = ERR42;
3901             goto FAILED;
3902             }
3903           if (namelen > MAX_NAME_SIZE)
3904             {
3905             *errorcodeptr = ERR48;
3906             goto FAILED;
3907             }
3908           recno = 0;
3909           }
3910
3911         /* In the real compile, seek the name in the table */
3912
3913         else
3914           {
3915           slot = cd->name_table;
3916           for (i = 0; i < cd->names_found; i++)
3917             {
3918             if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;
3919             slot += cd->name_entry_size;
3920             }
3921
3922           if (i < cd->names_found)         /* Back reference */
3923             {
3924             recno = GET2(slot, 0);
3925             }
3926           else if ((recno =                /* Forward back reference */
3927                     find_parens(ptr, cd->bracount, name, namelen,
3928                       (options & PCRE_EXTENDED) != 0)) <= 0)
3929             {
3930             *errorcodeptr = ERR15;
3931             goto FAILED;
3932             }
3933           }
3934
3935         /* In both phases, we can now go to the code than handles numerical
3936         recursion or backreferences. */
3937
3938         if (is_recurse) goto HANDLE_RECURSION;
3939           else goto HANDLE_REFERENCE;
3940
3941
3942         /* ------------------------------------------------------------ */
3943         case 'R':                 /* Recursion */
3944         ptr++;                    /* Same as (?0)      */
3945         /* Fall through */
3946
3947
3948         /* ------------------------------------------------------------ */
3949         case '0': case '1': case '2': case '3': case '4':   /* Recursion or */
3950         case '5': case '6': case '7': case '8': case '9':   /* subroutine */
3951           {
3952           const uschar *called;
3953           recno = 0;
3954           while(g_ascii_isdigit(*ptr) != 0)
3955             recno = recno * 10 + *ptr++ - '0';
3956           if (*ptr != ')')
3957             {
3958             *errorcodeptr = ERR29;
3959             goto FAILED;
3960             }
3961
3962           /* Come here from code above that handles a named recursion */
3963
3964           HANDLE_RECURSION:
3965
3966           previous = code;
3967           called = cd->start_code;
3968
3969           /* When we are actually compiling, find the bracket that is being
3970           referenced. Temporarily end the regex in case it doesn't exist before
3971           this point. If we end up with a forward reference, first check that
3972           the bracket does occur later so we can give the error (and position)
3973           now. Then remember this forward reference in the workspace so it can
3974           be filled in at the end. */
3975
3976           if (lengthptr == NULL)
3977             {
3978             *code = OP_END;
3979             if (recno != 0) called = find_bracket(cd->start_code, utf8, recno);
3980
3981             /* Forward reference */
3982
3983             if (called == NULL)
3984               {
3985               if (find_parens(ptr, cd->bracount, NULL, recno,
3986                    (options & PCRE_EXTENDED) != 0) < 0)
3987                 {
3988                 *errorcodeptr = ERR15;
3989                 goto FAILED;
3990                 }
3991               called = cd->start_code + recno;
3992               PUTINC(cd->hwm, 0, code + 2 + LINK_SIZE - cd->start_code);
3993               }
3994
3995             /* If not a forward reference, and the subpattern is still open,
3996             this is a recursive call. We check to see if this is a left
3997             recursion that could loop for ever, and diagnose that case. */
3998
3999             else if (GET(called, 1) == 0 &&
4000                      could_be_empty(called, code, bcptr, utf8))
4001               {
4002               *errorcodeptr = ERR40;
4003               goto FAILED;
4004               }
4005             }
4006
4007           /* Insert the recursion/subroutine item, automatically wrapped inside
4008           "once" brackets. Set up a "previous group" length so that a
4009           subsequent quantifier will work. */
4010
4011           *code = OP_ONCE;
4012           PUT(code, 1, 2 + 2*LINK_SIZE);
4013           code += 1 + LINK_SIZE;
4014
4015           *code = OP_RECURSE;
4016           PUT(code, 1, called - cd->start_code);
4017           code += 1 + LINK_SIZE;
4018
4019           *code = OP_KET;
4020           PUT(code, 1, 2 + 2*LINK_SIZE);
4021           code += 1 + LINK_SIZE;
4022
4023           length_prevgroup = 3 + 3*LINK_SIZE;
4024           }
4025
4026         /* Can't determine a first byte now */
4027
4028         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4029         continue;
4030
4031
4032         /* ------------------------------------------------------------ */
4033         default:              /* Other characters: check option setting */
4034         set = unset = 0;
4035         optset = &set;
4036
4037         while (*ptr != ')' && *ptr != ':')
4038           {
4039           switch (*ptr++)
4040             {
4041             case '-': optset = &unset; break;
4042
4043             case 'J':    /* Record that it changed in the external options */
4044             *optset |= PCRE_DUPNAMES;
4045             cd->external_options |= PCRE_JCHANGED;
4046             break;
4047
4048             case 'i': *optset |= PCRE_CASELESS; break;
4049             case 'm': *optset |= PCRE_MULTILINE; break;
4050             case 's': *optset |= PCRE_DOTALL; break;
4051             case 'x': *optset |= PCRE_EXTENDED; break;
4052             case 'U': *optset |= PCRE_UNGREEDY; break;
4053             case 'X': *optset |= PCRE_EXTRA; break;
4054
4055             default:  *errorcodeptr = ERR12;
4056                       ptr--;    /* Correct the offset */
4057                       goto FAILED;
4058             }
4059           }
4060
4061         /* Set up the changed option bits, but don't change anything yet. */
4062
4063         newoptions = (options | set) & (~unset);
4064
4065         /* If the options ended with ')' this is not the start of a nested
4066         group with option changes, so the options change at this level. If this
4067         item is right at the start of the pattern, the options can be
4068         abstracted and made external in the pre-compile phase, and ignored in
4069         the compile phase. This can be helpful when matching -- for instance in
4070         caseless checking of required bytes.
4071
4072         If the code pointer is not (cd->start_code + 1 + LINK_SIZE), we are
4073         definitely *not* at the start of the pattern because something has been
4074         compiled. In the pre-compile phase, however, the code pointer can have
4075         that value after the start, because it gets reset as code is discarded
4076         during the pre-compile. However, this can happen only at top level - if
4077         we are within parentheses, the starting BRA will still be present. At
4078         any parenthesis level, the length value can be used to test if anything
4079         has been compiled at that level. Thus, a test for both these conditions
4080         is necessary to ensure we correctly detect the start of the pattern in
4081         both phases.
4082
4083         If we are not at the pattern start, compile code to change the ims
4084         options if this setting actually changes any of them. We also pass the
4085         new setting back so that it can be put at the start of any following
4086         branches, and when this group ends (if we are in a group), a resetting
4087         item can be compiled. */
4088
4089         if (*ptr == ')')
4090           {
4091           if (code == cd->start_code + 1 + LINK_SIZE &&
4092                (lengthptr == NULL || *lengthptr == 2 + 2*LINK_SIZE))
4093             {
4094             cd->external_options = newoptions;
4095             options = newoptions;
4096             }
4097          else
4098             {
4099             if ((options & PCRE_IMS) != (newoptions & PCRE_IMS))
4100               {
4101               *code++ = OP_OPT;
4102               *code++ = newoptions & PCRE_IMS;
4103               }
4104
4105             /* Change options at this level, and pass them back for use
4106             in subsequent branches. Reset the greedy defaults and the case
4107             value for firstbyte and reqbyte. */
4108
4109             *optionsptr = options = newoptions;
4110             greedy_default = ((newoptions & PCRE_UNGREEDY) != 0);
4111             greedy_non_default = greedy_default ^ 1;
4112             req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;
4113             }
4114
4115           previous = NULL;       /* This item can't be repeated */
4116           continue;              /* It is complete */
4117           }
4118
4119         /* If the options ended with ':' we are heading into a nested group
4120         with possible change of options. Such groups are non-capturing and are
4121         not assertions of any kind. All we need to do is skip over the ':';
4122         the newoptions value is handled below. */
4123
4124         bravalue = OP_BRA;
4125         ptr++;
4126         }     /* End of switch for character following (? */
4127       }       /* End of (? handling */
4128
4129     /* Opening parenthesis not followed by '?'. If PCRE_NO_AUTO_CAPTURE is set,
4130     all unadorned brackets become non-capturing and behave like (?:...)
4131     brackets. */
4132
4133     else if ((options & PCRE_NO_AUTO_CAPTURE) != 0)
4134       {
4135       bravalue = OP_BRA;
4136       }
4137
4138     /* Else we have a capturing group. */
4139
4140     else
4141       {
4142       NUMBERED_GROUP:
4143       cd->bracount += 1;
4144       PUT2(code, 1+LINK_SIZE, cd->bracount);
4145       skipbytes = 2;
4146       }
4147
4148     /* Process nested bracketed regex. Assertions may not be repeated, but
4149     other kinds can be. All their opcodes are >= OP_ONCE. We copy code into a
4150     non-register variable in order to be able to pass its address because some
4151     compilers complain otherwise. Pass in a new setting for the ims options if
4152     they have changed. */
4153
4154     previous = (bravalue >= OP_ONCE)? code : NULL;
4155     *code = bravalue;
4156     tempcode = code;
4157     tempreqvary = cd->req_varyopt;     /* Save value before bracket */
4158     length_prevgroup = 0;              /* Initialize for pre-compile phase */
4159
4160     if (!compile_regex(
4161          newoptions,                   /* The complete new option state */
4162          options & PCRE_IMS,           /* The previous ims option state */
4163          &tempcode,                    /* Where to put code (updated) */
4164          &ptr,                         /* Input pointer (updated) */
4165          errorcodeptr,                 /* Where to put an error message */
4166          (bravalue == OP_ASSERTBACK ||
4167           bravalue == OP_ASSERTBACK_NOT), /* TRUE if back assert */
4168          skipbytes,                    /* Skip over bracket number */
4169          &subfirstbyte,                /* For possible first char */
4170          &subreqbyte,                  /* For possible last char */
4171          bcptr,                        /* Current branch chain */
4172          cd,                           /* Tables block */
4173          (lengthptr == NULL)? NULL :   /* Actual compile phase */
4174            &length_prevgroup           /* Pre-compile phase */
4175          ))
4176       goto FAILED;
4177
4178     /* At the end of compiling, code is still pointing to the start of the
4179     group, while tempcode has been updated to point past the end of the group
4180     and any option resetting that may follow it. The pattern pointer (ptr)
4181     is on the bracket. */
4182
4183     /* If this is a conditional bracket, check that there are no more than
4184     two branches in the group, or just one if it's a DEFINE group. */
4185
4186     if (bravalue == OP_COND)
4187       {
4188       uschar *tc = code;
4189       int condcount = 0;
4190
4191       do {
4192          condcount++;
4193          tc += GET(tc,1);
4194          }
4195       while (*tc != OP_KET);
4196
4197       /* A DEFINE group is never obeyed inline (the "condition" is always
4198       false). It must have only one branch. */
4199
4200       if (code[LINK_SIZE+1] == OP_DEF)
4201         {
4202         if (condcount > 1)
4203           {
4204           *errorcodeptr = ERR54;
4205           goto FAILED;
4206           }
4207         bravalue = OP_DEF;   /* Just a flag to suppress char handling below */
4208         }
4209
4210       /* A "normal" conditional group. If there is just one branch, we must not
4211       make use of its firstbyte or reqbyte, because this is equivalent to an
4212       empty second branch. */
4213
4214       else
4215         {
4216         if (condcount > 2)
4217           {
4218           *errorcodeptr = ERR27;
4219           goto FAILED;
4220           }
4221         if (condcount == 1) subfirstbyte = subreqbyte = REQ_NONE;
4222         }
4223       }
4224
4225     /* Error if hit end of pattern */
4226
4227     if (*ptr != ')')
4228       {
4229       *errorcodeptr = ERR14;
4230       goto FAILED;
4231       }
4232
4233     /* In the pre-compile phase, update the length by the length of the nested
4234     group, less the brackets at either end. Then reduce the compiled code to
4235     just the brackets so that it doesn't use much memory if it is duplicated by
4236     a quantifier. */
4237
4238     if (lengthptr != NULL)
4239       {
4240       *lengthptr += length_prevgroup - 2 - 2*LINK_SIZE;
4241       code++;
4242       PUTINC(code, 0, 1 + LINK_SIZE);
4243       *code++ = OP_KET;
4244       PUTINC(code, 0, 1 + LINK_SIZE);
4245       }
4246
4247     /* Otherwise update the main code pointer to the end of the group. */
4248
4249     else code = tempcode;
4250
4251     /* For a DEFINE group, required and first character settings are not
4252     relevant. */
4253
4254     if (bravalue == OP_DEF) break;
4255
4256     /* Handle updating of the required and first characters for other types of
4257     group. Update for normal brackets of all kinds, and conditions with two
4258     branches (see code above). If the bracket is followed by a quantifier with
4259     zero repeat, we have to back off. Hence the definition of zeroreqbyte and
4260     zerofirstbyte outside the main loop so that they can be accessed for the
4261     back off. */
4262
4263     zeroreqbyte = reqbyte;
4264     zerofirstbyte = firstbyte;
4265     groupsetfirstbyte = FALSE;
4266
4267     if (bravalue >= OP_ONCE)
4268       {
4269       /* If we have not yet set a firstbyte in this branch, take it from the
4270       subpattern, remembering that it was set here so that a repeat of more
4271       than one can replicate it as reqbyte if necessary. If the subpattern has
4272       no firstbyte, set "none" for the whole branch. In both cases, a zero
4273       repeat forces firstbyte to "none". */
4274
4275       if (firstbyte == REQ_UNSET)
4276         {
4277         if (subfirstbyte >= 0)
4278           {
4279           firstbyte = subfirstbyte;
4280           groupsetfirstbyte = TRUE;
4281           }
4282         else firstbyte = REQ_NONE;
4283         zerofirstbyte = REQ_NONE;
4284         }
4285
4286       /* If firstbyte was previously set, convert the subpattern's firstbyte
4287       into reqbyte if there wasn't one, using the vary flag that was in
4288       existence beforehand. */
4289
4290       else if (subfirstbyte >= 0 && subreqbyte < 0)
4291         subreqbyte = subfirstbyte | tempreqvary;
4292
4293       /* If the subpattern set a required byte (or set a first byte that isn't
4294       really the first byte - see above), set it. */
4295
4296       if (subreqbyte >= 0) reqbyte = subreqbyte;
4297       }
4298
4299     /* For a forward assertion, we take the reqbyte, if set. This can be
4300     helpful if the pattern that follows the assertion doesn't set a different
4301     char. For example, it's useful for /(?=abcde).+/. We can't set firstbyte
4302     for an assertion, however because it leads to incorrect effect for patterns
4303     such as /(?=a)a.+/ when the "real" "a" would then become a reqbyte instead
4304     of a firstbyte. This is overcome by a scan at the end if there's no
4305     firstbyte, looking for an asserted first char. */
4306
4307     else if (bravalue == OP_ASSERT && subreqbyte >= 0) reqbyte = subreqbyte;
4308     break;     /* End of processing '(' */
4309
4310
4311     /* ===================================================================*/
4312     /* Handle metasequences introduced by \. For ones like \d, the ESC_ values
4313     are arranged to be the negation of the corresponding OP_values. For the
4314     back references, the values are ESC_REF plus the reference number. Only
4315     back references and those types that consume a character may be repeated.
4316     We can test for values between ESC_b and ESC_Z for the latter; this may
4317     have to change if any new ones are ever created. */
4318
4319     case '\\':
4320     tempptr = ptr;
4321     c = check_escape(&ptr, errorcodeptr, cd->bracount, options, FALSE);
4322     if (*errorcodeptr != 0) goto FAILED;
4323
4324     if (c < 0)
4325       {
4326       if (-c == ESC_Q)            /* Handle start of quoted string */
4327         {
4328         if (ptr[1] == '\\' && ptr[2] == 'E') ptr += 2; /* avoid empty string */
4329           else inescq = TRUE;
4330         continue;
4331         }
4332
4333       if (-c == ESC_E) continue;  /* Perl ignores an orphan \E */
4334
4335       /* For metasequences that actually match a character, we disable the
4336       setting of a first character if it hasn't already been set. */
4337
4338       if (firstbyte == REQ_UNSET && -c > ESC_b && -c < ESC_Z)
4339         firstbyte = REQ_NONE;
4340
4341       /* Set values to reset to if this is followed by a zero repeat. */
4342
4343       zerofirstbyte = firstbyte;
4344       zeroreqbyte = reqbyte;
4345
4346       /* \k<name> or \k'name' is a back reference by name (Perl syntax) */
4347
4348       if (-c == ESC_k && (ptr[1] == '<' || ptr[1] == '\''))
4349         {
4350         is_recurse = FALSE;
4351         terminator = (*(++ptr) == '<')? '>' : '\'';
4352         goto NAMED_REF_OR_RECURSE;
4353         }
4354
4355       /* Back references are handled specially; must disable firstbyte if
4356       not set to cope with cases like (?=(\w+))\1: which would otherwise set
4357       ':' later. */
4358
4359       if (-c >= ESC_REF)
4360         {
4361         recno = -c - ESC_REF;
4362
4363         HANDLE_REFERENCE:    /* Come here from named backref handling */
4364         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;
4365         previous = code;
4366         *code++ = OP_REF;
4367         PUT2INC(code, 0, recno);
4368         cd->backref_map |= (recno < 32)? (1 << recno) : 1;
4369         if (recno > cd->top_backref) cd->top_backref = recno;
4370         }
4371
4372       /* So are Unicode property matches, if supported. */
4373
4374 #ifdef SUPPORT_UCP
4375       else if (-c == ESC_P || -c == ESC_p)
4376         {
4377         BOOL negated;
4378         int pdata;
4379         int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4380         if (ptype < 0) goto FAILED;
4381         previous = code;
4382         *code++ = ((-c == ESC_p) != negated)? OP_PROP : OP_NOTPROP;
4383         *code++ = ptype;
4384         *code++ = pdata;
4385         }
4386 #else
4387
4388       /* If Unicode properties are not supported, \X, \P, and \p are not
4389       allowed. */
4390
4391       else if (-c == ESC_X || -c == ESC_P || -c == ESC_p)
4392         {
4393         *errorcodeptr = ERR45;
4394         goto FAILED;
4395         }
4396 #endif
4397
4398       /* For the rest (including \X when Unicode properties are supported), we
4399       can obtain the OP value by negating the escape value. */
4400
4401       else
4402         {
4403         previous = (-c > ESC_b && -c < ESC_Z)? code : NULL;
4404         *code++ = -c;
4405         }
4406       continue;
4407       }
4408
4409     /* We have a data character whose value is in c. In UTF-8 mode it may have
4410     a value > 127. We set its representation in the length/buffer, and then
4411     handle it as a data character. */
4412
4413 #ifdef SUPPORT_UTF8
4414     if (utf8 && c > 127)
4415       mclength = _pcre_ord2utf8(c, mcbuffer);
4416     else
4417 #endif
4418
4419      {
4420      mcbuffer[0] = c;
4421      mclength = 1;
4422      }
4423     goto ONE_CHAR;
4424
4425
4426     /* ===================================================================*/
4427     /* Handle a literal character. It is guaranteed not to be whitespace or #
4428     when the extended flag is set. If we are in UTF-8 mode, it may be a
4429     multi-byte literal character. */
4430
4431     default:
4432     NORMAL_CHAR:
4433     mclength = 1;
4434     mcbuffer[0] = c;
4435
4436 #ifdef SUPPORT_UTF8
4437     if (utf8 && c >= 0xc0)
4438       {
4439       while ((ptr[1] & 0xc0) == 0x80)
4440         mcbuffer[mclength++] = *(++ptr);
4441       }
4442 #endif
4443
4444     /* At this point we have the character's bytes in mcbuffer, and the length
4445     in mclength. When not in UTF-8 mode, the length is always 1. */
4446
4447     ONE_CHAR:
4448     previous = code;
4449     *code++ = ((options & PCRE_CASELESS) != 0)? OP_CHARNC : OP_CHAR;
4450     for (c = 0; c < mclength; c++) *code++ = mcbuffer[c];
4451
4452     /* Set the first and required bytes appropriately. If no previous first
4453     byte, set it from this character, but revert to none on a zero repeat.
4454     Otherwise, leave the firstbyte value alone, and don't change it on a zero
4455     repeat. */
4456
4457     if (firstbyte == REQ_UNSET)
4458       {
4459       zerofirstbyte = REQ_NONE;
4460       zeroreqbyte = reqbyte;
4461
4462       /* If the character is more than one byte long, we can set firstbyte
4463       only if it is not to be matched caselessly. */
4464
4465       if (mclength == 1 || req_caseopt == 0)
4466         {
4467         firstbyte = mcbuffer[0] | req_caseopt;
4468         if (mclength != 1) reqbyte = code[-1] | cd->req_varyopt;
4469         }
4470       else firstbyte = reqbyte = REQ_NONE;
4471       }
4472
4473     /* firstbyte was previously set; we can set reqbyte only the length is
4474     1 or the matching is caseful. */
4475
4476     else
4477       {
4478       zerofirstbyte = firstbyte;
4479       zeroreqbyte = reqbyte;
4480       if (mclength == 1 || req_caseopt == 0)
4481         reqbyte = code[-1] | req_caseopt | cd->req_varyopt;
4482       }
4483
4484     break;            /* End of literal character handling */
4485     }
4486   }                   /* end of big loop */
4487
4488
4489 /* Control never reaches here by falling through, only by a goto for all the
4490 error states. Pass back the position in the pattern so that it can be displayed
4491 to the user for diagnosing the error. */
4492
4493 FAILED:
4494 *ptrptr = ptr;
4495 return FALSE;
4496 }
4497
4498
4499
4500
4501 /*************************************************
4502 *     Compile sequence of alternatives           *
4503 *************************************************/
4504
4505 /* On entry, ptr is pointing past the bracket character, but on return it
4506 points to the closing bracket, or vertical bar, or end of string. The code
4507 variable is pointing at the byte into which the BRA operator has been stored.
4508 If the ims options are changed at the start (for a (?ims: group) or during any
4509 branch, we need to insert an OP_OPT item at the start of every following branch
4510 to ensure they get set correctly at run time, and also pass the new options
4511 into every subsequent branch compile.
4512
4513 This function is used during the pre-compile phase when we are trying to find
4514 out the amount of memory needed, as well as during the real compile phase. The
4515 value of lengthptr distinguishes the two phases.
4516
4517 Argument:
4518   options        option bits, including any changes for this subpattern
4519   oldims         previous settings of ims option bits
4520   codeptr        -> the address of the current code pointer
4521   ptrptr         -> the address of the current pattern pointer
4522   errorcodeptr   -> pointer to error code variable
4523   lookbehind     TRUE if this is a lookbehind assertion
4524   skipbytes      skip this many bytes at start (for brackets and OP_COND)
4525   firstbyteptr   place to put the first required character, or a negative number
4526   reqbyteptr     place to put the last required character, or a negative number
4527   bcptr          pointer to the chain of currently open branches
4528   cd             points to the data block with tables pointers etc.
4529   lengthptr      NULL during the real compile phase
4530                  points to length accumulator during pre-compile phase
4531
4532 Returns:         TRUE on success
4533 */
4534
4535 static BOOL
4536 compile_regex(int options, int oldims, uschar **codeptr, const uschar **ptrptr,
4537   int *errorcodeptr, BOOL lookbehind, int skipbytes, int *firstbyteptr,
4538   int *reqbyteptr, branch_chain *bcptr, compile_data *cd, int *lengthptr)
4539 {
4540 const uschar *ptr = *ptrptr;
4541 uschar *code = *codeptr;
4542 uschar *last_branch = code;
4543 uschar *start_bracket = code;
4544 uschar *reverse_count = NULL;
4545 int firstbyte, reqbyte;
4546 int branchfirstbyte, branchreqbyte;
4547 int length;
4548 branch_chain bc;
4549
4550 bc.outer = bcptr;
4551 bc.current = code;
4552
4553 firstbyte = reqbyte = REQ_UNSET;
4554
4555 /* Accumulate the length for use in the pre-compile phase. Start with the
4556 length of the BRA and KET and any extra bytes that are required at the
4557 beginning. We accumulate in a local variable to save frequent testing of
4558 lenthptr for NULL. We cannot do this by looking at the value of code at the
4559 start and end of each alternative, because compiled items are discarded during
4560 the pre-compile phase so that the work space is not exceeded. */
4561
4562 length = 2 + 2*LINK_SIZE + skipbytes;
4563
4564 /* WARNING: If the above line is changed for any reason, you must also change
4565 the code that abstracts option settings at the start of the pattern and makes
4566 them global. It tests the value of length for (2 + 2*LINK_SIZE) in the
4567 pre-compile phase to find out whether anything has yet been compiled or not. */
4568
4569 /* Offset is set zero to mark that this bracket is still open */
4570
4571 PUT(code, 1, 0);
4572 code += 1 + LINK_SIZE + skipbytes;
4573
4574 /* Loop for each alternative branch */
4575
4576 for (;;)
4577   {
4578   /* Handle a change of ims options at the start of the branch */
4579
4580   if ((options & PCRE_IMS) != oldims)
4581     {
4582     *code++ = OP_OPT;
4583     *code++ = options & PCRE_IMS;
4584     length += 2;
4585     }
4586
4587   /* Set up dummy OP_REVERSE if lookbehind assertion */
4588
4589   if (lookbehind)
4590     {
4591     *code++ = OP_REVERSE;
4592     reverse_count = code;
4593     PUTINC(code, 0, 0);
4594     length += 1 + LINK_SIZE;
4595     }
4596
4597   /* Now compile the branch; in the pre-compile phase its length gets added
4598   into the length. */
4599
4600   if (!compile_branch(&options, &code, &ptr, errorcodeptr, &branchfirstbyte,
4601         &branchreqbyte, &bc, cd, (lengthptr == NULL)? NULL : &length))
4602     {
4603     *ptrptr = ptr;
4604     return FALSE;
4605     }
4606
4607   /* In the real compile phase, there is some post-processing to be done. */
4608
4609   if (lengthptr == NULL)
4610     {
4611     /* If this is the first branch, the firstbyte and reqbyte values for the
4612     branch become the values for the regex. */
4613
4614     if (*last_branch != OP_ALT)
4615       {
4616       firstbyte = branchfirstbyte;
4617       reqbyte = branchreqbyte;
4618       }
4619
4620     /* If this is not the first branch, the first char and reqbyte have to
4621     match the values from all the previous branches, except that if the
4622     previous value for reqbyte didn't have REQ_VARY set, it can still match,
4623     and we set REQ_VARY for the regex. */
4624
4625     else
4626       {
4627       /* If we previously had a firstbyte, but it doesn't match the new branch,
4628       we have to abandon the firstbyte for the regex, but if there was
4629       previously no reqbyte, it takes on the value of the old firstbyte. */
4630
4631       if (firstbyte >= 0 && firstbyte != branchfirstbyte)
4632         {
4633         if (reqbyte < 0) reqbyte = firstbyte;
4634         firstbyte = REQ_NONE;
4635         }
4636
4637       /* If we (now or from before) have no firstbyte, a firstbyte from the
4638       branch becomes a reqbyte if there isn't a branch reqbyte. */
4639
4640       if (firstbyte < 0 && branchfirstbyte >= 0 && branchreqbyte < 0)
4641           branchreqbyte = branchfirstbyte;
4642
4643       /* Now ensure that the reqbytes match */
4644
4645       if ((reqbyte & ~REQ_VARY) != (branchreqbyte & ~REQ_VARY))
4646         reqbyte = REQ_NONE;
4647       else reqbyte |= branchreqbyte;   /* To "or" REQ_VARY */
4648       }
4649
4650     /* If lookbehind, check that this branch matches a fixed-length string, and
4651     put the length into the OP_REVERSE item. Temporarily mark the end of the
4652     branch with OP_END. */
4653
4654     if (lookbehind)
4655       {
4656       int fixed_length;
4657       *code = OP_END;
4658       fixed_length = find_fixedlength(last_branch, options);
4659       DPRINTF(("fixed length = %d\n", fixed_length));
4660       if (fixed_length < 0)
4661         {
4662         *errorcodeptr = (fixed_length == -2)? ERR36 : ERR25;
4663         *ptrptr = ptr;
4664         return FALSE;
4665         }
4666       PUT(reverse_count, 0, fixed_length);
4667       }
4668     }
4669
4670   /* Reached end of expression, either ')' or end of pattern. Go back through
4671   the alternative branches and reverse the chain of offsets, with the field in
4672   the BRA item now becoming an offset to the first alternative. If there are
4673   no alternatives, it points to the end of the group. The length in the
4674   terminating ket is always the length of the whole bracketed item. If any of
4675   the ims options were changed inside the group, compile a resetting op-code
4676   following, except at the very end of the pattern. Return leaving the pointer
4677   at the terminating char. */
4678
4679   if (*ptr != '|')
4680     {
4681     int branch_length = code - last_branch;
4682     do
4683       {
4684       int prev_length = GET(last_branch, 1);
4685       PUT(last_branch, 1, branch_length);
4686       branch_length = prev_length;
4687       last_branch -= branch_length;
4688       }
4689     while (branch_length > 0);
4690
4691     /* Fill in the ket */
4692
4693     *code = OP_KET;
4694     PUT(code, 1, code - start_bracket);
4695     code += 1 + LINK_SIZE;
4696
4697     /* Resetting option if needed */
4698
4699     if ((options & PCRE_IMS) != oldims && *ptr == ')')
4700       {
4701       *code++ = OP_OPT;
4702       *code++ = oldims;
4703       length += 2;
4704       }
4705
4706     /* Set values to pass back */
4707
4708     *codeptr = code;
4709     *ptrptr = ptr;
4710     *firstbyteptr = firstbyte;
4711     *reqbyteptr = reqbyte;
4712     if (lengthptr != NULL) *lengthptr += length;
4713     return TRUE;
4714     }
4715
4716   /* Another branch follows; insert an "or" node. Its length field points back
4717   to the previous branch while the bracket remains open. At the end the chain
4718   is reversed. It's done like this so that the start of the bracket has a
4719   zero offset until it is closed, making it possible to detect recursion. */
4720
4721   *code = OP_ALT;
4722   PUT(code, 1, code - last_branch);
4723   bc.current = last_branch = code;
4724   code += 1 + LINK_SIZE;
4725   ptr++;
4726   length += 1 + LINK_SIZE;
4727   }
4728 /* Control never reaches here */
4729 }
4730
4731
4732
4733
4734 /*************************************************
4735 *          Check for anchored expression         *
4736 *************************************************/
4737
4738 /* Try to find out if this is an anchored regular expression. Consider each
4739 alternative branch. If they all start with OP_SOD or OP_CIRC, or with a bracket
4740 all of whose alternatives start with OP_SOD or OP_CIRC (recurse ad lib), then
4741 it's anchored. However, if this is a multiline pattern, then only OP_SOD
4742 counts, since OP_CIRC can match in the middle.
4743
4744 We can also consider a regex to be anchored if OP_SOM starts all its branches.
4745 This is the code for \G, which means "match at start of match position, taking
4746 into account the match offset".
4747
4748 A branch is also implicitly anchored if it starts with .* and DOTALL is set,
4749 because that will try the rest of the pattern at all possible matching points,
4750 so there is no point trying again.... er ....
4751
4752 .... except when the .* appears inside capturing parentheses, and there is a
4753 subsequent back reference to those parentheses. We haven't enough information
4754 to catch that case precisely.
4755
4756 At first, the best we could do was to detect when .* was in capturing brackets
4757 and the highest back reference was greater than or equal to that level.
4758 However, by keeping a bitmap of the first 31 back references, we can catch some
4759 of the more common cases more precisely.
4760
4761 Arguments:
4762   code           points to start of expression (the bracket)
4763   options        points to the options setting
4764   bracket_map    a bitmap of which brackets we are inside while testing; this
4765                   handles up to substring 31; after that we just have to take
4766                   the less precise approach
4767   backref_map    the back reference bitmap
4768
4769 Returns:     TRUE or FALSE
4770 */
4771
4772 static BOOL
4773 is_anchored(register const uschar *code, int *options, unsigned int bracket_map,
4774   unsigned int backref_map)
4775 {
4776 do {
4777    const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
4778      options, PCRE_MULTILINE, FALSE);
4779    register int op = *scode;
4780
4781    /* Non-capturing brackets */
4782
4783    if (op == OP_BRA)
4784      {
4785      if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;
4786      }
4787
4788    /* Capturing brackets */
4789
4790    else if (op == OP_CBRA)
4791      {
4792      int n = GET2(scode, 1+LINK_SIZE);
4793      int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
4794      if (!is_anchored(scode, options, new_map, backref_map)) return FALSE;
4795      }
4796
4797    /* Other brackets */
4798
4799    else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)
4800      {
4801      if (!is_anchored(scode, options, bracket_map, backref_map)) return FALSE;
4802      }
4803
4804    /* .* is not anchored unless DOTALL is set and it isn't in brackets that
4805    are or may be referenced. */
4806
4807    else if ((op == OP_TYPESTAR || op == OP_TYPEMINSTAR ||
4808              op == OP_TYPEPOSSTAR) &&
4809             (*options & PCRE_DOTALL) != 0)
4810      {
4811      if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE;
4812      }
4813
4814    /* Check for explicit anchoring */
4815
4816    else if (op != OP_SOD && op != OP_SOM &&
4817            ((*options & PCRE_MULTILINE) != 0 || op != OP_CIRC))
4818      return FALSE;
4819    code += GET(code, 1);
4820    }
4821 while (*code == OP_ALT);   /* Loop for each alternative */
4822 return TRUE;
4823 }
4824
4825
4826
4827 /*************************************************
4828 *         Check for starting with ^ or .*        *
4829 *************************************************/
4830
4831 /* This is called to find out if every branch starts with ^ or .* so that
4832 "first char" processing can be done to speed things up in multiline
4833 matching and for non-DOTALL patterns that start with .* (which must start at
4834 the beginning or after \n). As in the case of is_anchored() (see above), we
4835 have to take account of back references to capturing brackets that contain .*
4836 because in that case we can't make the assumption.
4837
4838 Arguments:
4839   code           points to start of expression (the bracket)
4840   bracket_map    a bitmap of which brackets we are inside while testing; this
4841                   handles up to substring 31; after that we just have to take
4842                   the less precise approach
4843   backref_map    the back reference bitmap
4844
4845 Returns:         TRUE or FALSE
4846 */
4847
4848 static BOOL
4849 is_startline(const uschar *code, unsigned int bracket_map,
4850   unsigned int backref_map)
4851 {
4852 do {
4853    const uschar *scode = first_significant_code(code + _pcre_OP_lengths[*code],
4854      NULL, 0, FALSE);
4855    register int op = *scode;
4856
4857    /* Non-capturing brackets */
4858
4859    if (op == OP_BRA)
4860      {
4861      if (!is_startline(scode, bracket_map, backref_map)) return FALSE;
4862      }
4863
4864    /* Capturing brackets */
4865
4866    else if (op == OP_CBRA)
4867      {
4868      int n = GET2(scode, 1+LINK_SIZE);
4869      int new_map = bracket_map | ((n < 32)? (1 << n) : 1);
4870      if (!is_startline(scode, new_map, backref_map)) return FALSE;
4871      }
4872
4873    /* Other brackets */
4874
4875    else if (op == OP_ASSERT || op == OP_ONCE || op == OP_COND)
4876      { if (!is_startline(scode, bracket_map, backref_map)) return FALSE; }
4877
4878    /* .* means "start at start or after \n" if it isn't in brackets that
4879    may be referenced. */
4880
4881    else if (op == OP_TYPESTAR || op == OP_TYPEMINSTAR || op == OP_TYPEPOSSTAR)
4882      {
4883      if (scode[1] != OP_ANY || (bracket_map & backref_map) != 0) return FALSE;
4884      }
4885
4886    /* Check for explicit circumflex */
4887
4888    else if (op != OP_CIRC) return FALSE;
4889
4890    /* Move on to the next alternative */
4891
4892    code += GET(code, 1);
4893    }
4894 while (*code == OP_ALT);  /* Loop for each alternative */
4895 return TRUE;
4896 }
4897
4898
4899
4900 /*************************************************
4901 *       Check for asserted fixed first char      *
4902 *************************************************/
4903
4904 /* During compilation, the "first char" settings from forward assertions are
4905 discarded, because they can cause conflicts with actual literals that follow.
4906 However, if we end up without a first char setting for an unanchored pattern,
4907 it is worth scanning the regex to see if there is an initial asserted first
4908 char. If all branches start with the same asserted char, or with a bracket all
4909 of whose alternatives start with the same asserted char (recurse ad lib), then
4910 we return that char, otherwise -1.
4911
4912 Arguments:
4913   code       points to start of expression (the bracket)
4914   options    pointer to the options (used to check casing changes)
4915   inassert   TRUE if in an assertion
4916
4917 Returns:     -1 or the fixed first char
4918 */
4919
4920 static int
4921 find_firstassertedchar(const uschar *code, int *options, BOOL inassert)
4922 {
4923 register int c = -1;
4924 do {
4925    int d;
4926    const uschar *scode =
4927      first_significant_code(code + 1+LINK_SIZE, options, PCRE_CASELESS, TRUE);
4928    register int op = *scode;
4929
4930    switch(op)
4931      {
4932      default:
4933      return -1;
4934
4935      case OP_BRA:
4936      case OP_CBRA:
4937      case OP_ASSERT:
4938      case OP_ONCE:
4939      case OP_COND:
4940      if ((d = find_firstassertedchar(scode, options, op == OP_ASSERT)) < 0)
4941        return -1;
4942      if (c < 0) c = d; else if (c != d) return -1;
4943      break;
4944
4945      case OP_EXACT:       /* Fall through */
4946      scode += 2;
4947
4948      case OP_CHAR:
4949      case OP_CHARNC:
4950      case OP_PLUS:
4951      case OP_MINPLUS:
4952      case OP_POSPLUS:
4953      if (!inassert) return -1;
4954      if (c < 0)
4955        {
4956        c = scode[1];
4957        if ((*options & PCRE_CASELESS) != 0) c |= REQ_CASELESS;
4958        }
4959      else if (c != scode[1]) return -1;
4960      break;
4961      }
4962
4963    code += GET(code, 1);
4964    }
4965 while (*code == OP_ALT);
4966 return c;
4967 }
4968
4969
4970
4971 /*************************************************
4972 *        Compile a Regular Expression            *
4973 *************************************************/
4974
4975 /* This function takes a string and returns a pointer to a block of store
4976 holding a compiled version of the expression. The original API for this
4977 function had no error code return variable; it is retained for backwards
4978 compatibility. The new function is given a new name.
4979
4980 Arguments:
4981   pattern       the regular expression
4982   options       various option bits
4983   errorcodeptr  pointer to error code variable (pcre_compile2() only)
4984                   can be NULL if you don't want a code value
4985   errorptr      pointer to pointer to error text
4986   erroroffset   ptr offset in pattern where error was detected
4987   tables        pointer to character tables or NULL
4988
4989 Returns:        pointer to compiled data block, or NULL on error,
4990                 with errorptr and erroroffset set
4991 */
4992
4993 PCRE_DATA_SCOPE pcre *
4994 pcre_compile(const char *pattern, int options, const char **errorptr,
4995   int *erroroffset, const unsigned char *tables)
4996 {
4997 return pcre_compile2(pattern, options, NULL, errorptr, erroroffset, tables);
4998 }
4999
5000
5001 PCRE_DATA_SCOPE pcre *
5002 pcre_compile2(const char *pattern, int options, int *errorcodeptr,
5003   const char **errorptr, int *erroroffset, const unsigned char *tables)
5004 {
5005 real_pcre *re;
5006 int length = 1;  /* For final END opcode */
5007 int firstbyte, reqbyte, newline;
5008 int errorcode = 0;
5009 #ifdef SUPPORT_UTF8
5010 BOOL utf8;
5011 #endif
5012 size_t size;
5013 uschar *code;
5014 const uschar *codestart;
5015 const uschar *ptr;
5016 compile_data compile_block;
5017 compile_data *cd = &compile_block;
5018
5019 /* This space is used for "compiling" into during the first phase, when we are
5020 computing the amount of memory that is needed. Compiled items are thrown away
5021 as soon as possible, so that a fairly large buffer should be sufficient for
5022 this purpose. The same space is used in the second phase for remembering where
5023 to fill in forward references to subpatterns. */
5024
5025 uschar cworkspace[COMPILE_WORK_SIZE];
5026
5027
5028 /* Set this early so that early errors get offset 0. */
5029
5030 ptr = (const uschar *)pattern;
5031
5032 /* We can't pass back an error message if errorptr is NULL; I guess the best we
5033 can do is just return NULL, but we can set a code value if there is a code
5034 pointer. */
5035
5036 if (errorptr == NULL)
5037   {
5038   if (errorcodeptr != NULL) *errorcodeptr = 99;
5039   return NULL;
5040   }
5041
5042 *errorptr = NULL;
5043 if (errorcodeptr != NULL) *errorcodeptr = ERR0;
5044
5045 /* However, we can give a message for this error */
5046
5047 if (erroroffset == NULL)
5048   {
5049   errorcode = ERR16;
5050   goto PCRE_EARLY_ERROR_RETURN;
5051   }
5052
5053 *erroroffset = 0;
5054
5055 /* Can't support UTF8 unless PCRE has been compiled to include the code. */
5056
5057 #ifdef SUPPORT_UTF8
5058 utf8 = (options & PCRE_UTF8) != 0;
5059 if (utf8 && (options & PCRE_NO_UTF8_CHECK) == 0 &&
5060      (*erroroffset = _pcre_valid_utf8((uschar *)pattern, -1)) >= 0)
5061   {
5062   errorcode = ERR44;
5063   goto PCRE_UTF8_ERROR_RETURN;
5064   }
5065 #else
5066 if ((options & PCRE_UTF8) != 0)
5067   {
5068   errorcode = ERR32;
5069   goto PCRE_EARLY_ERROR_RETURN;
5070   }
5071 #endif
5072
5073 if ((options & ~PUBLIC_OPTIONS) != 0)
5074   {
5075   errorcode = ERR17;
5076   goto PCRE_EARLY_ERROR_RETURN;
5077   }
5078
5079 /* Set up pointers to the individual character tables */
5080
5081 if (tables == NULL) tables = _pcre_default_tables;
5082 cd->lcc = tables + lcc_offset;
5083 cd->fcc = tables + fcc_offset;
5084 cd->cbits = tables + cbits_offset;
5085 cd->ctypes = tables + ctypes_offset;
5086
5087 /* Handle different types of newline. The three bits give seven cases. The
5088 current code allows for fixed one- or two-byte sequences, plus "any". */
5089
5090 switch (options & (PCRE_NEWLINE_CRLF | PCRE_NEWLINE_ANY))
5091   {
5092   case 0: newline = NEWLINE; break;   /* Compile-time default */
5093   case PCRE_NEWLINE_CR: newline = '\r'; break;
5094   case PCRE_NEWLINE_LF: newline = '\n'; break;
5095   case PCRE_NEWLINE_CR+
5096        PCRE_NEWLINE_LF: newline = ('\r' << 8) | '\n'; break;
5097   case PCRE_NEWLINE_ANY: newline = -1; break;
5098   default: errorcode = ERR56; goto PCRE_EARLY_ERROR_RETURN;
5099   }
5100
5101 if (newline < 0)
5102   {
5103   cd->nltype = NLTYPE_ANY;
5104   }
5105 else
5106   {
5107   cd->nltype = NLTYPE_FIXED;
5108   if (newline > 255)
5109     {
5110     cd->nllen = 2;
5111     cd->nl[0] = (newline >> 8) & 255;
5112     cd->nl[1] = newline & 255;
5113     }
5114   else
5115     {
5116     cd->nllen = 1;
5117     cd->nl[0] = newline;
5118     }
5119   }
5120
5121 /* Maximum back reference and backref bitmap. The bitmap records up to 31 back
5122 references to help in deciding whether (.*) can be treated as anchored or not.
5123 */
5124
5125 cd->top_backref = 0;
5126 cd->backref_map = 0;
5127
5128 /* Reflect pattern for debugging output */
5129
5130 DPRINTF(("------------------------------------------------------------------\n"));
5131 DPRINTF(("%s\n", pattern));
5132
5133 /* Pretend to compile the pattern while actually just accumulating the length
5134 of memory required. This behaviour is triggered by passing a non-NULL final
5135 argument to compile_regex(). We pass a block of workspace (cworkspace) for it
5136 to compile parts of the pattern into; the compiled code is discarded when it is
5137 no longer needed, so hopefully this workspace will never overflow, though there
5138 is a test for its doing so. */
5139
5140 cd->bracount = 0;
5141 cd->names_found = 0;
5142 cd->name_entry_size = 0;
5143 cd->name_table = NULL;
5144 cd->start_workspace = cworkspace;
5145 cd->start_code = cworkspace;
5146 cd->hwm = cworkspace;
5147 cd->start_pattern = (const uschar *)pattern;
5148 cd->end_pattern = (const uschar *)(pattern + strlen(pattern));
5149 cd->req_varyopt = 0;
5150 cd->nopartial = FALSE;
5151 cd->external_options = options;
5152
5153 /* Now do the pre-compile. On error, errorcode will be set non-zero, so we
5154 don't need to look at the result of the function here. The initial options have
5155 been put into the cd block so that they can be changed if an option setting is
5156 found within the regex right at the beginning. Bringing initial option settings
5157 outside can help speed up starting point checks. */
5158
5159 code = cworkspace;
5160 *code = OP_BRA;
5161 (void)compile_regex(cd->external_options, cd->external_options & PCRE_IMS,
5162   &code, &ptr, &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, &length);
5163 if (errorcode != 0) goto PCRE_EARLY_ERROR_RETURN;
5164
5165 DPRINTF(("end pre-compile: length=%d workspace=%d\n", length,
5166   cd->hwm - cworkspace));
5167
5168 if (length > MAX_PATTERN_SIZE)
5169   {
5170   errorcode = ERR20;
5171   goto PCRE_EARLY_ERROR_RETURN;
5172   }
5173
5174 /* Compute the size of data block needed and get it, either from malloc or
5175 externally provided function. Integer overflow should no longer be possible
5176 because nowadays we limit the maximum value of cd->names_found and
5177 cd->name_entry_size. */
5178
5179 size = length + sizeof(real_pcre) + cd->names_found * (cd->name_entry_size + 3);
5180 re = (real_pcre *)(pcre_malloc)(size);
5181
5182 if (re == NULL)
5183   {
5184   errorcode = ERR21;
5185   goto PCRE_EARLY_ERROR_RETURN;
5186   }
5187
5188 /* Put in the magic number, and save the sizes, initial options, and character
5189 table pointer. NULL is used for the default character tables. The nullpad field
5190 is at the end; it's there to help in the case when a regex compiled on a system
5191 with 4-byte pointers is run on another with 8-byte pointers. */
5192
5193 re->magic_number = MAGIC_NUMBER;
5194 re->size = size;
5195 re->options = cd->external_options;
5196 re->dummy1 = 0;
5197 re->first_byte = 0;
5198 re->req_byte = 0;
5199 re->name_table_offset = sizeof(real_pcre);
5200 re->name_entry_size = cd->name_entry_size;
5201 re->name_count = cd->names_found;
5202 re->ref_count = 0;
5203 re->tables = (tables == _pcre_default_tables)? NULL : tables;
5204 re->nullpad = NULL;
5205
5206 /* The starting points of the name/number translation table and of the code are
5207 passed around in the compile data block. The start/end pattern and initial
5208 options are already set from the pre-compile phase, as is the name_entry_size
5209 field. Reset the bracket count and the names_found field. Also reset the hwm
5210 field; this time it's used for remembering forward references to subpatterns.
5211 */
5212
5213 cd->bracount = 0;
5214 cd->names_found = 0;
5215 cd->name_table = (uschar *)re + re->name_table_offset;
5216 codestart = cd->name_table + re->name_entry_size * re->name_count;
5217 cd->start_code = codestart;
5218 cd->hwm = cworkspace;
5219 cd->req_varyopt = 0;
5220 cd->nopartial = FALSE;
5221
5222 /* Set up a starting, non-extracting bracket, then compile the expression. On
5223 error, errorcode will be set non-zero, so we don't need to look at the result
5224 of the function here. */
5225
5226 ptr = (const uschar *)pattern;
5227 code = (uschar *)codestart;
5228 *code = OP_BRA;
5229 (void)compile_regex(re->options, re->options & PCRE_IMS, &code, &ptr,
5230   &errorcode, FALSE, 0, &firstbyte, &reqbyte, NULL, cd, NULL);
5231 re->top_bracket = cd->bracount;
5232 re->top_backref = cd->top_backref;
5233
5234 if (cd->nopartial) re->options |= PCRE_NOPARTIAL;
5235
5236 /* If not reached end of pattern on success, there's an excess bracket. */
5237
5238 if (errorcode == 0 && *ptr != 0) errorcode = ERR22;
5239
5240 /* Fill in the terminating state and check for disastrous overflow, but
5241 if debugging, leave the test till after things are printed out. */
5242
5243 *code++ = OP_END;
5244
5245 #ifndef DEBUG
5246 if (code - codestart > length) errorcode = ERR23;
5247 #endif
5248
5249 /* Fill in any forward references that are required. */
5250
5251 while (errorcode == 0 && cd->hwm > cworkspace)
5252   {
5253   int offset, recno;
5254   const uschar *groupptr;
5255   cd->hwm -= LINK_SIZE;
5256   offset = GET(cd->hwm, 0);
5257   recno = GET(codestart, offset);
5258   groupptr = find_bracket(codestart, (re->options & PCRE_UTF8) != 0, recno);
5259   if (groupptr == NULL) errorcode = ERR53;
5260     else PUT(((uschar *)codestart), offset, groupptr - codestart);
5261   }
5262
5263 /* Give an error if there's back reference to a non-existent capturing
5264 subpattern. */
5265
5266 if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
5267
5268 /* Failed to compile, or error while post-processing */
5269
5270 if (errorcode != 0)
5271   {
5272   (pcre_free)(re);
5273   PCRE_EARLY_ERROR_RETURN:
5274   *erroroffset = ptr - (const uschar *)pattern;
5275 #ifdef SUPPORT_UTF8
5276   PCRE_UTF8_ERROR_RETURN:
5277 #endif
5278   *errorptr = error_texts + error_texts_offsets[errorcode];
5279   if (errorcodeptr != NULL) *errorcodeptr = errorcode;
5280   return NULL;
5281   }
5282
5283 /* If the anchored option was not passed, set the flag if we can determine that
5284 the pattern is anchored by virtue of ^ characters or \A or anything else (such
5285 as starting with .* when DOTALL is set).
5286
5287 Otherwise, if we know what the first byte has to be, save it, because that
5288 speeds up unanchored matches no end. If not, see if we can set the
5289 PCRE_STARTLINE flag. This is helpful for multiline matches when all branches
5290 start with ^. and also when all branches start with .* for non-DOTALL matches.
5291 */
5292
5293 if ((re->options & PCRE_ANCHORED) == 0)
5294   {
5295   int temp_options = re->options;   /* May get changed during these scans */
5296   if (is_anchored(codestart, &temp_options, 0, cd->backref_map))
5297     re->options |= PCRE_ANCHORED;
5298   else
5299     {
5300     if (firstbyte < 0)
5301       firstbyte = find_firstassertedchar(codestart, &temp_options, FALSE);
5302     if (firstbyte >= 0)   /* Remove caseless flag for non-caseable chars */
5303       {
5304       int ch = firstbyte & 255;
5305       re->first_byte = ((firstbyte & REQ_CASELESS) != 0 &&
5306          cd->fcc[ch] == ch)? ch : firstbyte;
5307       re->options |= PCRE_FIRSTSET;
5308       }
5309     else if (is_startline(codestart, 0, cd->backref_map))
5310       re->options |= PCRE_STARTLINE;
5311     }
5312   }
5313
5314 /* For an anchored pattern, we use the "required byte" only if it follows a
5315 variable length item in the regex. Remove the caseless flag for non-caseable
5316 bytes. */
5317
5318 if (reqbyte >= 0 &&
5319      ((re->options & PCRE_ANCHORED) == 0 || (reqbyte & REQ_VARY) != 0))
5320   {
5321   int ch = reqbyte & 255;
5322   re->req_byte = ((reqbyte & REQ_CASELESS) != 0 &&
5323     cd->fcc[ch] == ch)? (reqbyte & ~REQ_CASELESS) : reqbyte;
5324   re->options |= PCRE_REQCHSET;
5325   }
5326
5327 /* Print out the compiled data if debugging is enabled. This is never the
5328 case when building a production library. */
5329
5330 #ifdef DEBUG
5331
5332 printf("Length = %d top_bracket = %d top_backref = %d\n",
5333   length, re->top_bracket, re->top_backref);
5334
5335 if (re->options != 0)
5336   {
5337   printf("%s%s%s%s%s%s%s%s%s\n",
5338     ((re->options & PCRE_NOPARTIAL) != 0)? "nopartial " : "",
5339     ((re->options & PCRE_ANCHORED) != 0)? "anchored " : "",
5340     ((re->options & PCRE_CASELESS) != 0)? "caseless " : "",
5341     ((re->options & PCRE_EXTENDED) != 0)? "extended " : "",
5342     ((re->options & PCRE_MULTILINE) != 0)? "multiline " : "",
5343     ((re->options & PCRE_DOTALL) != 0)? "dotall " : "",
5344     ((re->options & PCRE_DOLLAR_ENDONLY) != 0)? "endonly " : "",
5345     ((re->options & PCRE_EXTRA) != 0)? "extra " : "",
5346     ((re->options & PCRE_UNGREEDY) != 0)? "ungreedy " : "");
5347   }
5348
5349 if ((re->options & PCRE_FIRSTSET) != 0)
5350   {
5351   int ch = re->first_byte & 255;
5352   const char *caseless = ((re->first_byte & REQ_CASELESS) == 0)?
5353     "" : " (caseless)";
5354   if (isprint(ch)) printf("First char = %c%s\n", ch, caseless);
5355     else printf("First char = \\x%02x%s\n", ch, caseless);
5356   }
5357
5358 if ((re->options & PCRE_REQCHSET) != 0)
5359   {
5360   int ch = re->req_byte & 255;
5361   const char *caseless = ((re->req_byte & REQ_CASELESS) == 0)?
5362     "" : " (caseless)";
5363   if (isprint(ch)) printf("Req char = %c%s\n", ch, caseless);
5364     else printf("Req char = \\x%02x%s\n", ch, caseless);
5365   }
5366
5367 pcre_printint(re, stdout);
5368
5369 /* This check is done here in the debugging case so that the code that
5370 was compiled can be seen. */
5371
5372 if (code - codestart > length)
5373   {
5374   (pcre_free)(re);
5375   *errorptr = error_texts + error_texts_offsets[ERR23];
5376   *erroroffset = ptr - (uschar *)pattern;
5377   if (errorcodeptr != NULL) *errorcodeptr = ERR23;
5378   return NULL;
5379   }
5380 #endif   /* DEBUG */
5381
5382 return (pcre *)re;
5383 }
5384
5385 /* End of pcre_compile.c */