Annotation of win32/pcre/pcre_compile.c, revision 1.4

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

E-mail: