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

Diff of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 793 by ph10, Wed Dec 7 16:52:34 2011 UTC revision 1046 by ph10, Tue Sep 25 16:27:58 2012 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2011 University of Cambridge             Copyright (c) 1997-2012 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 53  supporting internal functions that are n Line 53  supporting internal functions that are n
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
55    
56  /* When PCRE_DEBUG is defined, we need the pcre_printint() function, which is  /* When PCRE_DEBUG is defined, we need the pcre(16)_printint() function, which
57  also used by pcretest. PCRE_DEBUG is not defined when building a production  is also used by pcretest. PCRE_DEBUG is not defined when building a production
58  library. */  library. We do not need to select pcre16_printint.c specially, because the
59    COMPILE_PCREx macro will already be appropriately set. */
60    
61  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
62  #include "pcre_printint.src"  /* pcre_printint.c should not include any headers */
63    #define PCRE_INCLUDED
64    #include "pcre_printint.c"
65    #undef PCRE_INCLUDED
66  #endif  #endif
67    
68    
69  /* Macro for setting individual bits in class bitmaps. */  /* Macro for setting individual bits in class bitmaps. */
70    
71  #define SETBIT(a,b) a[b/8] |= (1 << (b%8))  #define SETBIT(a,b) a[(b)/8] |= (1 << ((b)&7))
72    
73  /* Maximum length value to check against when making sure that the integer that  /* Maximum length value to check against when making sure that the integer that
74  holds the compiled pattern length does not overflow. We make it a bit less than  holds the compiled pattern length does not overflow. We make it a bit less than
# Line 73  to check them every time. */ Line 77  to check them every time. */
77    
78  #define OFLOW_MAX (INT_MAX - 20)  #define OFLOW_MAX (INT_MAX - 20)
79    
80    /* Definitions to allow mutual recursion */
81    
82    static int
83      add_list_to_class(pcre_uint8 *, pcre_uchar **, int, compile_data *,
84        const pcre_uint32 *, unsigned int);
85    
86    static BOOL
87      compile_regex(int, pcre_uchar **, const pcre_uchar **, int *, BOOL, BOOL,
88        int, int, int *, int *, branch_chain *, compile_data *, int *);
89    
90    
91    
92  /*************************************************  /*************************************************
93  *      Code parameters and static tables         *  *      Code parameters and static tables         *
# Line 104  overrun before it actually does run off Line 119  overrun before it actually does run off
119    
120  #define WORK_SIZE_SAFETY_MARGIN (100)  #define WORK_SIZE_SAFETY_MARGIN (100)
121    
122    /* Private flags added to firstchar and reqchar. */
123    
124    #define REQ_CASELESS   0x10000000l      /* Indicates caselessness */
125    #define REQ_VARY       0x20000000l      /* Reqchar followed non-literal item */
126    
127    /* Repeated character flags. */
128    
129    #define UTF_LENGTH     0x10000000l      /* The char contains its length. */
130    
131  /* Table for handling escaped characters in the range '0'-'z'. Positive returns  /* Table for handling escaped characters in the range '0'-'z'. Positive returns
132  are simple data values; negative values are for special things like \d and so  are simple data values; negative values are for special things like \d and so
# Line 238  static const char posix_names[] = Line 261  static const char posix_names[] =
261    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0    STRING_graph0 STRING_print0 STRING_punct0 STRING_space0
262    STRING_word0  STRING_xdigit;    STRING_word0  STRING_xdigit;
263    
264  static const uschar posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
265    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
266    
267  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
# Line 273  substitutes must be in the order of the Line 296  substitutes must be in the order of the
296  both positive and negative cases. NULL means no substitute. */  both positive and negative cases. NULL means no substitute. */
297    
298  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
299  static const uschar *substitutes[] = {  static const pcre_uchar string_PNd[]  = {
300    (uschar *)"\\P{Nd}",    /* \D */    CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
301    (uschar *)"\\p{Nd}",    /* \d */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
302    (uschar *)"\\P{Xsp}",   /* \S */       /* NOTE: Xsp is Perl space */  static const pcre_uchar string_pNd[]  = {
303    (uschar *)"\\p{Xsp}",   /* \s */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
304    (uschar *)"\\P{Xwd}",   /* \W */    CHAR_N, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
305    (uschar *)"\\p{Xwd}"    /* \w */  static const pcre_uchar string_PXsp[] = {
306      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
307      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
308    static const pcre_uchar string_pXsp[] = {
309      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
310      CHAR_X, CHAR_s, CHAR_p, CHAR_RIGHT_CURLY_BRACKET, '\0' };
311    static const pcre_uchar string_PXwd[] = {
312      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
313      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
314    static const pcre_uchar string_pXwd[] = {
315      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
316      CHAR_X, CHAR_w, CHAR_d, CHAR_RIGHT_CURLY_BRACKET, '\0' };
317    
318    static const pcre_uchar *substitutes[] = {
319      string_PNd,           /* \D */
320      string_pNd,           /* \d */
321      string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */
322      string_pXsp,          /* \s */
323      string_PXwd,          /* \W */
324      string_pXwd           /* \w */
325  };  };
326    
327  static const uschar *posix_substitutes[] = {  static const pcre_uchar string_pL[] =   {
328    (uschar *)"\\p{L}",     /* alpha */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
329    (uschar *)"\\p{Ll}",    /* lower */    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
330    (uschar *)"\\p{Lu}",    /* upper */  static const pcre_uchar string_pLl[] =  {
331    (uschar *)"\\p{Xan}",   /* alnum */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
332    NULL,                   /* ascii */    CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
333    (uschar *)"\\h",        /* blank */  static const pcre_uchar string_pLu[] =  {
334    NULL,                   /* cntrl */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
335    (uschar *)"\\p{Nd}",    /* digit */    CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
336    NULL,                   /* graph */  static const pcre_uchar string_pXan[] = {
337    NULL,                   /* print */    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
338    NULL,                   /* punct */    CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
339    (uschar *)"\\p{Xps}",   /* space */    /* NOTE: Xps is POSIX space */  static const pcre_uchar string_h[] =    {
340    (uschar *)"\\p{Xwd}",   /* word */    CHAR_BACKSLASH, CHAR_h, '\0' };
341    NULL,                   /* xdigit */  static const pcre_uchar string_pXps[] = {
342      CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
343      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
344    static const pcre_uchar string_PL[] =   {
345      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
346      CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
347    static const pcre_uchar string_PLl[] =  {
348      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
349      CHAR_L, CHAR_l, CHAR_RIGHT_CURLY_BRACKET, '\0' };
350    static const pcre_uchar string_PLu[] =  {
351      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
352      CHAR_L, CHAR_u, CHAR_RIGHT_CURLY_BRACKET, '\0' };
353    static const pcre_uchar string_PXan[] = {
354      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
355      CHAR_X, CHAR_a, CHAR_n, CHAR_RIGHT_CURLY_BRACKET, '\0' };
356    static const pcre_uchar string_H[] =    {
357      CHAR_BACKSLASH, CHAR_H, '\0' };
358    static const pcre_uchar string_PXps[] = {
359      CHAR_BACKSLASH, CHAR_P, CHAR_LEFT_CURLY_BRACKET,
360      CHAR_X, CHAR_p, CHAR_s, CHAR_RIGHT_CURLY_BRACKET, '\0' };
361    
362    static const pcre_uchar *posix_substitutes[] = {
363      string_pL,            /* alpha */
364      string_pLl,           /* lower */
365      string_pLu,           /* upper */
366      string_pXan,          /* alnum */
367      NULL,                 /* ascii */
368      string_h,             /* blank */
369      NULL,                 /* cntrl */
370      string_pNd,           /* digit */
371      NULL,                 /* graph */
372      NULL,                 /* print */
373      NULL,                 /* punct */
374      string_pXps,          /* space */    /* NOTE: Xps is POSIX space */
375      string_pXwd,          /* word */
376      NULL,                 /* xdigit */
377    /* Negated cases */    /* Negated cases */
378    (uschar *)"\\P{L}",     /* ^alpha */    string_PL,            /* ^alpha */
379    (uschar *)"\\P{Ll}",    /* ^lower */    string_PLl,           /* ^lower */
380    (uschar *)"\\P{Lu}",    /* ^upper */    string_PLu,           /* ^upper */
381    (uschar *)"\\P{Xan}",   /* ^alnum */    string_PXan,          /* ^alnum */
382    NULL,                   /* ^ascii */    NULL,                 /* ^ascii */
383    (uschar *)"\\H",        /* ^blank */    string_H,             /* ^blank */
384    NULL,                   /* ^cntrl */    NULL,                 /* ^cntrl */
385    (uschar *)"\\P{Nd}",    /* ^digit */    string_PNd,           /* ^digit */
386    NULL,                   /* ^graph */    NULL,                 /* ^graph */
387    NULL,                   /* ^print */    NULL,                 /* ^print */
388    NULL,                   /* ^punct */    NULL,                 /* ^punct */
389    (uschar *)"\\P{Xps}",   /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */
390    (uschar *)"\\P{Xwd}",   /* ^word */    string_PXwd,          /* ^word */
391    NULL                    /* ^xdigit */    NULL                  /* ^xdigit */
392  };  };
393  #define POSIX_SUBSIZE (sizeof(posix_substitutes)/sizeof(uschar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
394  #endif  #endif
395    
396  #define STRING(a)  # a  #define STRING(a)  # a
# Line 372  static const char error_texts[] = Line 449  static const char error_texts[] =
449    /* 30 */    /* 30 */
450    "unknown POSIX class name\0"    "unknown POSIX class name\0"
451    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
452    "this version of PCRE is not compiled with PCRE_UTF8 support\0"    "this version of PCRE is compiled without UTF support\0"
453    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
454    "character value in \\x{...} sequence is too large\0"    "character value in \\x{...} sequence is too large\0"
455    /* 35 */    /* 35 */
# Line 395  static const char error_texts[] = Line 472  static const char error_texts[] =
472    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"    "too many named subpatterns (maximum " XSTRING(MAX_NAME_COUNT) ")\0"
473    /* 50 */    /* 50 */
474    "repeated subpattern is too long\0"    /** DEAD **/    "repeated subpattern is too long\0"    /** DEAD **/
475    "octal value is greater than \\377 (not in UTF-8 mode)\0"    "octal value is greater than \\377 in 8-bit non-UTF-8 mode\0"
476    "internal error: overran compiling workspace\0"    "internal error: overran compiling workspace\0"
477    "internal error: previously-checked referenced subpattern not found\0"    "internal error: previously-checked referenced subpattern not found\0"
478    "DEFINE group contains more than one branch\0"    "DEFINE group contains more than one branch\0"
# Line 414  static const char error_texts[] = Line 491  static const char error_texts[] =
491    /* 65 */    /* 65 */
492    "different names for subpatterns of the same number are not allowed\0"    "different names for subpatterns of the same number are not allowed\0"
493    "(*MARK) must have an argument\0"    "(*MARK) must have an argument\0"
494    "this version of PCRE is not compiled with PCRE_UCP support\0"    "this version of PCRE is not compiled with Unicode property support\0"
495    "\\c must be followed by an ASCII character\0"    "\\c must be followed by an ASCII character\0"
496    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"    "\\k is not followed by a braced, angle-bracketed, or quoted name\0"
497    /* 70 */    /* 70 */
498    "internal error: unknown opcode in find_fixedlength()\0"    "internal error: unknown opcode in find_fixedlength()\0"
499    "\\N is not supported in a class\0"    "\\N is not supported in a class\0"
500    "too many forward references\0"    "too many forward references\0"
501      "disallowed Unicode code point (>= 0xd800 && <= 0xdfff)\0"
502      "invalid UTF-16 string\0"
503      /* 75 */
504      "name is too long in (*MARK), (*PRUNE), (*SKIP), or (*THEN)\0"
505      "character value in \\u.... sequence is too large\0"
506    ;    ;
507    
508  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 439  For convenience, we use the same bit def Line 521  For convenience, we use the same bit def
521    
522  Then we can use ctype_digit and ctype_xdigit in the code. */  Then we can use ctype_digit and ctype_xdigit in the code. */
523    
524    /* Using a simple comparison for decimal numbers rather than a memory read
525    is much faster, and the resulting code is simpler (the compiler turns it
526    into a subtraction and unsigned comparison). */
527    
528    #define IS_DIGIT(x) ((x) >= CHAR_0 && (x) <= CHAR_9)
529    
530  #ifndef EBCDIC  #ifndef EBCDIC
531    
532  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in  /* This is the "normal" case, for ASCII systems, and EBCDIC systems running in
533  UTF-8 mode. */  UTF-8 mode. */
534    
535  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
536    {    {
537    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7 */
538    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15 */
# Line 483  static const unsigned char digitab[] = Line 571  static const unsigned char digitab[] =
571    
572  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */  /* This is the "abnormal" case, for EBCDIC systems not running in UTF-8 mode. */
573    
574  static const unsigned char digitab[] =  static const pcre_uint8 digitab[] =
575    {    {
576    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   0-  7  0 */
577    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*   8- 15    */
# Line 518  static const unsigned char digitab[] = Line 606  static const unsigned char digitab[] =
606    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */    0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c, /*  0 - 7  F0 */
607    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */    0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00};/*  8 -255    */
608    
609  static const unsigned char ebcdic_chartab[] = { /* chartable partial dup */  static const pcre_uint8 ebcdic_chartab[] = { /* chartable partial dup */
610    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */    0x80,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*   0-  7 */
611    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */    0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00, /*   8- 15 */
612    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */    0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00, /*  16- 23 */
# Line 554  static const unsigned char ebcdic_charta Line 642  static const unsigned char ebcdic_charta
642  #endif  #endif
643    
644    
 /* Definition to allow mutual recursion */  
   
 static BOOL  
   compile_regex(int, uschar **, const uschar **, int *, BOOL, BOOL, int, int,  
     int *, int *, branch_chain *, compile_data *, int *);  
   
645    
646    
647  /*************************************************  /*************************************************
# Line 604  Returns:  0 if all went well, else an er Line 686  Returns:  0 if all went well, else an er
686  static int  static int
687  expand_workspace(compile_data *cd)  expand_workspace(compile_data *cd)
688  {  {
689  uschar *newspace;  pcre_uchar *newspace;
690  int newsize = cd->workspace_size * 2;  int newsize = cd->workspace_size * 2;
691    
692  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;  if (newsize > COMPILE_WORK_SIZE_MAX) newsize = COMPILE_WORK_SIZE_MAX;
# Line 612  if (cd->workspace_size >= COMPILE_WORK_S Line 694  if (cd->workspace_size >= COMPILE_WORK_S
694      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)      newsize - cd->workspace_size < WORK_SIZE_SAFETY_MARGIN)
695   return ERR72;   return ERR72;
696    
697  newspace = (pcre_malloc)(newsize);  newspace = (PUBL(malloc))(IN_UCHARS(newsize));
698  if (newspace == NULL) return ERR21;  if (newspace == NULL) return ERR21;
699    memcpy(newspace, cd->start_workspace, cd->workspace_size * sizeof(pcre_uchar));
700  memcpy(newspace, cd->start_workspace, cd->workspace_size);  cd->hwm = (pcre_uchar *)newspace + (cd->hwm - cd->start_workspace);
 cd->hwm = (uschar *)newspace + (cd->hwm - cd->start_workspace);  
701  if (cd->workspace_size > COMPILE_WORK_SIZE)  if (cd->workspace_size > COMPILE_WORK_SIZE)
702    (pcre_free)((void *)cd->start_workspace);    (PUBL(free))((void *)cd->start_workspace);
703  cd->start_workspace = newspace;  cd->start_workspace = newspace;
704  cd->workspace_size = newsize;  cd->workspace_size = newsize;
705  return 0;  return 0;
# Line 642  Returns:    TRUE or FALSE Line 723  Returns:    TRUE or FALSE
723  */  */
724    
725  static BOOL  static BOOL
726  is_counted_repeat(const uschar *p)  is_counted_repeat(const pcre_uchar *p)
727  {  {
728  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
729  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
730    while (IS_DIGIT(*p)) p++;
731  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
732    
733  if (*p++ != CHAR_COMMA) return FALSE;  if (*p++ != CHAR_COMMA) return FALSE;
734  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;  if (*p == CHAR_RIGHT_CURLY_BRACKET) return TRUE;
735    
736  if ((digitab[*p++] & ctype_digit) == 0) return FALSE;  if (!IS_DIGIT(*p)) return FALSE;
737  while ((digitab[*p] & ctype_digit) != 0) p++;  p++;
738    while (IS_DIGIT(*p)) p++;
739    
740  return (*p == CHAR_RIGHT_CURLY_BRACKET);  return (*p == CHAR_RIGHT_CURLY_BRACKET);
741  }  }
# Line 684  Returns:         zero or positive => a d Line 767  Returns:         zero or positive => a d
767  */  */
768    
769  static int  static int
770  check_escape(const uschar **ptrptr, int *errorcodeptr, int bracount,  check_escape(const pcre_uchar **ptrptr, int *errorcodeptr, int bracount,
771    int options, BOOL isclass)    int options, BOOL isclass)
772  {  {
773  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
774  const uschar *ptr = *ptrptr + 1;  BOOL utf = (options & PCRE_UTF8) != 0;
775  int c, i;  const pcre_uchar *ptr = *ptrptr + 1;
776    pcre_int32 c;
777    int i;
778    
779  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */  GETCHARINCTEST(c, ptr);           /* Get character value, increment pointer */
780  ptr--;                            /* Set pointer back to the last byte */  ptr--;                            /* Set pointer back to the last byte */
# Line 703  in a table. A non-zero result is somethi Line 788  in a table. A non-zero result is somethi
788  Otherwise further processing may be required. */  Otherwise further processing may be required. */
789    
790  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
791  else if (c < CHAR_0 || c > CHAR_z) {}                     /* Not alphanumeric */  /* Not alphanumeric */
792    else if (c < CHAR_0 || c > CHAR_z) {}
793  else if ((i = escapes[c - CHAR_0]) != 0) c = i;  else if ((i = escapes[c - CHAR_0]) != 0) c = i;
794    
795  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
796  else if (c < 'a' || (ebcdic_chartab[c] & 0x0E) == 0) {}   /* Not alphanumeric */  /* Not alphanumeric */
797    else if (c < CHAR_a || (!MAX_255(c) || (ebcdic_chartab[c] & 0x0E) == 0)) {}
798  else if ((i = escapes[c - 0x48]) != 0)  c = i;  else if ((i = escapes[c - 0x48]) != 0)  c = i;
799  #endif  #endif
800    
# Line 715  else if ((i = escapes[c - 0x48]) != 0) Line 802  else if ((i = escapes[c - 0x48]) != 0)
802    
803  else  else
804    {    {
805    const uschar *oldptr;    const pcre_uchar *oldptr;
806    BOOL braced, negated;    BOOL braced, negated;
807    
808    switch (c)    switch (c)
# Line 733  else Line 820  else
820        {        {
821        /* In JavaScript, \u must be followed by four hexadecimal numbers.        /* In JavaScript, \u must be followed by four hexadecimal numbers.
822        Otherwise it is a lowercase u letter. */        Otherwise it is a lowercase u letter. */
823        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
824             && (digitab[ptr[3]] & ctype_xdigit) != 0 && (digitab[ptr[4]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0
825            && MAX_255(ptr[3]) && (digitab[ptr[3]] & ctype_xdigit) != 0
826            && MAX_255(ptr[4]) && (digitab[ptr[4]] & ctype_xdigit) != 0)
827          {          {
828          c = 0;          c = 0;
829          for (i = 0; i < 4; ++i)          for (i = 0; i < 4; ++i)
# Line 748  else Line 837  else
837            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
838  #endif  #endif
839            }            }
840    
841    #ifdef COMPILE_PCRE8
842            if (c > (utf ? 0x10ffff : 0xff))
843    #else
844    #ifdef COMPILE_PCRE16
845            if (c > (utf ? 0x10ffff : 0xffff))
846    #endif
847    #endif
848              {
849              *errorcodeptr = ERR76;
850              }
851            else if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
852          }          }
853        }        }
854      else      else
# Line 788  else Line 889  else
889    
890      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
891        {        {
892        const uschar *p;        const pcre_uchar *p;
893        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)        for (p = ptr+2; *p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET; p++)
894          if (*p != CHAR_MINUS && (digitab[*p] & ctype_digit) == 0) break;          if (*p != CHAR_MINUS && !IS_DIGIT(*p)) break;
895        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)        if (*p != 0 && *p != CHAR_RIGHT_CURLY_BRACKET)
896          {          {
897          c = -ESC_k;          c = -ESC_k;
# Line 808  else Line 909  else
909        }        }
910      else negated = FALSE;      else negated = FALSE;
911    
912        /* The integer range is limited by the machine's int representation. */
913      c = 0;      c = 0;
914      while ((digitab[ptr[1]] & ctype_digit) != 0)      while (IS_DIGIT(ptr[1]))
915          {
916          if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
917            {
918            c = -1;
919            break;
920            }
921        c = c * 10 + *(++ptr) - CHAR_0;        c = c * 10 + *(++ptr) - CHAR_0;
922          }
923      if (c < 0)   /* Integer overflow */      if (((unsigned int)c) > INT_MAX) /* Integer overflow */
924        {        {
925          while (IS_DIGIT(ptr[1]))
926            ptr++;
927        *errorcodeptr = ERR61;        *errorcodeptr = ERR61;
928        break;        break;
929        }        }
# Line 861  else Line 971  else
971      if (!isclass)      if (!isclass)
972        {        {
973        oldptr = ptr;        oldptr = ptr;
974          /* The integer range is limited by the machine's int representation. */
975        c -= CHAR_0;        c -= CHAR_0;
976        while ((digitab[ptr[1]] & ctype_digit) != 0)        while (IS_DIGIT(ptr[1]))
977            {
978            if (((unsigned int)c) > INT_MAX / 10) /* Integer overflow */
979              {
980              c = -1;
981              break;
982              }
983          c = c * 10 + *(++ptr) - CHAR_0;          c = c * 10 + *(++ptr) - CHAR_0;
984        if (c < 0)    /* Integer overflow */          }
985          if (((unsigned int)c) > INT_MAX) /* Integer overflow */
986          {          {
987            while (IS_DIGIT(ptr[1]))
988              ptr++;
989          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
990          break;          break;
991          }          }
# Line 891  else Line 1011  else
1011      /* \0 always starts an octal number, but we may drop through to here with a      /* \0 always starts an octal number, but we may drop through to here with a
1012      larger first octal digit. The original code used just to take the least      larger first octal digit. The original code used just to take the least
1013      significant 8 bits of octal numbers (I think this is what early Perls used      significant 8 bits of octal numbers (I think this is what early Perls used
1014      to do). Nowadays we allow for larger numbers in UTF-8 mode, but no more      to do). Nowadays we allow for larger numbers in UTF-8 mode and 16-bit mode,
1015      than 3 octal digits. */      but no more than 3 octal digits. */
1016    
1017      case CHAR_0:      case CHAR_0:
1018      c -= CHAR_0;      c -= CHAR_0;
1019      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)      while(i++ < 2 && ptr[1] >= CHAR_0 && ptr[1] <= CHAR_7)
1020          c = c * 8 + *(++ptr) - CHAR_0;          c = c * 8 + *(++ptr) - CHAR_0;
1021      if (!utf8 && c > 255) *errorcodeptr = ERR51;  #ifdef COMPILE_PCRE8
1022        if (!utf && c > 0xff) *errorcodeptr = ERR51;
1023    #endif
1024      break;      break;
1025    
1026      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \x is complicated. \x{ddd} is a character number which can be greater
1027      than 0xff in utf8 mode, but only if the ddd are hex digits. If not, { is      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.
1028      treated as a data character. */      If not, { is treated as a data character. */
1029    
1030      case CHAR_x:      case CHAR_x:
1031      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1032        {        {
1033        /* In JavaScript, \x must be followed by two hexadecimal numbers.        /* In JavaScript, \x must be followed by two hexadecimal numbers.
1034        Otherwise it is a lowercase x letter. */        Otherwise it is a lowercase x letter. */
1035        if ((digitab[ptr[1]] & ctype_xdigit) != 0 && (digitab[ptr[2]] & ctype_xdigit) != 0)        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1036            && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1037          {          {
1038          c = 0;          c = 0;
1039          for (i = 0; i < 2; ++i)          for (i = 0; i < 2; ++i)
# Line 930  else Line 1053  else
1053    
1054      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1055        {        {
1056        const uschar *pt = ptr + 2;        const pcre_uchar *pt = ptr + 2;
       int count = 0;  
1057    
1058        c = 0;        c = 0;
1059        while ((digitab[*pt] & ctype_xdigit) != 0)        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)
1060          {          {
1061          register int cc = *pt++;          register int cc = *pt++;
1062          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
         count++;  
1063    
1064  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1065          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
# Line 947  else Line 1068  else
1068          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */          if (cc >= CHAR_a && cc <= CHAR_z) cc += 64;  /* Convert to upper case */
1069          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1070  #endif  #endif
1071    
1072    #ifdef COMPILE_PCRE8
1073            if (c > (utf ? 0x10ffff : 0xff)) { c = -1; break; }
1074    #else
1075    #ifdef COMPILE_PCRE16
1076            if (c > (utf ? 0x10ffff : 0xffff)) { c = -1; break; }
1077    #endif
1078    #endif
1079            }
1080    
1081          if (c < 0)
1082            {
1083            while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;
1084            *errorcodeptr = ERR34;
1085          }          }
1086    
1087        if (*pt == CHAR_RIGHT_CURLY_BRACKET)        if (*pt == CHAR_RIGHT_CURLY_BRACKET)
1088          {          {
1089          if (c < 0 || count > (utf8? 8 : 2)) *errorcodeptr = ERR34;          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1090          ptr = pt;          ptr = pt;
1091          break;          break;
1092          }          }
# Line 963  else Line 1098  else
1098      /* Read just a single-byte hex-defined char */      /* Read just a single-byte hex-defined char */
1099    
1100      c = 0;      c = 0;
1101      while (i++ < 2 && (digitab[ptr[1]] & ctype_xdigit) != 0)      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1102        {        {
1103        int cc;                                  /* Some compilers don't like */        int cc;                                  /* Some compilers don't like */
1104        cc = *(++ptr);                           /* ++ in initializers */        cc = *(++ptr);                           /* ++ in initializers */
# Line 1061  Returns:         type value from ucp_typ Line 1196  Returns:         type value from ucp_typ
1196  */  */
1197    
1198  static int  static int
1199  get_ucp(const uschar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)  get_ucp(const pcre_uchar **ptrptr, BOOL *negptr, int *dptr, int *errorcodeptr)
1200  {  {
1201  int c, i, bot, top;  int c, i, bot, top;
1202  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
1203  char name[32];  pcre_uchar name[32];
1204    
1205  c = *(++ptr);  c = *(++ptr);
1206  if (c == 0) goto ERROR_RETURN;  if (c == 0) goto ERROR_RETURN;
# Line 1082  if (c == CHAR_LEFT_CURLY_BRACKET) Line 1217  if (c == CHAR_LEFT_CURLY_BRACKET)
1217      *negptr = TRUE;      *negptr = TRUE;
1218      ptr++;      ptr++;
1219      }      }
1220    for (i = 0; i < (int)sizeof(name) - 1; i++)    for (i = 0; i < (int)(sizeof(name) / sizeof(pcre_uchar)) - 1; i++)
1221      {      {
1222      c = *(++ptr);      c = *(++ptr);
1223      if (c == 0) goto ERROR_RETURN;      if (c == 0) goto ERROR_RETURN;
# Line 1106  else Line 1241  else
1241  /* Search for a recognized property name using binary chop */  /* Search for a recognized property name using binary chop */
1242    
1243  bot = 0;  bot = 0;
1244  top = _pcre_utt_size;  top = PRIV(utt_size);
1245    
1246  while (bot < top)  while (bot < top)
1247    {    {
1248    i = (bot + top) >> 1;    i = (bot + top) >> 1;
1249    c = strcmp(name, _pcre_utt_names + _pcre_utt[i].name_offset);    c = STRCMP_UC_C8(name, PRIV(utt_names) + PRIV(utt)[i].name_offset);
1250    if (c == 0)    if (c == 0)
1251      {      {
1252      *dptr = _pcre_utt[i].value;      *dptr = PRIV(utt)[i].value;
1253      return _pcre_utt[i].type;      return PRIV(utt)[i].type;
1254      }      }
1255    if (c > 0) bot = i + 1; else top = i;    if (c > 0) bot = i + 1; else top = i;
1256    }    }
# Line 1153  Returns:         pointer to '}' on succe Line 1288  Returns:         pointer to '}' on succe
1288                   current ptr on error, with errorcodeptr set non-zero                   current ptr on error, with errorcodeptr set non-zero
1289  */  */
1290    
1291  static const uschar *  static const pcre_uchar *
1292  read_repeat_counts(const uschar *p, int *minp, int *maxp, int *errorcodeptr)  read_repeat_counts(const pcre_uchar *p, int *minp, int *maxp, int *errorcodeptr)
1293  {  {
1294  int min = 0;  int min = 0;
1295  int max = -1;  int max = -1;
# Line 1162  int max = -1; Line 1297  int max = -1;
1297  /* Read the minimum value and do a paranoid check: a negative value indicates  /* Read the minimum value and do a paranoid check: a negative value indicates
1298  an integer overflow. */  an integer overflow. */
1299    
1300  while ((digitab[*p] & ctype_digit) != 0) min = min * 10 + *p++ - CHAR_0;  while (IS_DIGIT(*p)) min = min * 10 + *p++ - CHAR_0;
1301  if (min < 0 || min > 65535)  if (min < 0 || min > 65535)
1302    {    {
1303    *errorcodeptr = ERR5;    *errorcodeptr = ERR5;
# Line 1177  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1312  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1312    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1313      {      {
1314      max = 0;      max = 0;
1315      while((digitab[*p] & ctype_digit) != 0) max = max * 10 + *p++ - CHAR_0;      while(IS_DIGIT(*p)) max = max * 10 + *p++ - CHAR_0;
1316      if (max < 0 || max > 65535)      if (max < 0 || max > 65535)
1317        {        {
1318        *errorcodeptr = ERR5;        *errorcodeptr = ERR5;
# Line 1232  Arguments: Line 1367  Arguments:
1367    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1368    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1369    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1370    utf8         TRUE if we are in UTF-8 mode    utf          TRUE if we are in UTF-8 / UTF-16 mode
1371    count        pointer to the current capturing subpattern number (updated)    count        pointer to the current capturing subpattern number (updated)
1372    
1373  Returns:       the number of the named subpattern, or -1 if not found  Returns:       the number of the named subpattern, or -1 if not found
1374  */  */
1375    
1376  static int  static int
1377  find_parens_sub(uschar **ptrptr, compile_data *cd, const uschar *name, int lorn,  find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,
1378    BOOL xmode, BOOL utf8, int *count)    BOOL xmode, BOOL utf, int *count)
1379  {  {
1380  uschar *ptr = *ptrptr;  pcre_uchar *ptr = *ptrptr;
1381  int start_count = *count;  int start_count = *count;
1382  int hwm_count = start_count;  int hwm_count = start_count;
1383  BOOL dup_parens = FALSE;  BOOL dup_parens = FALSE;
# Line 1309  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1444  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1444          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)          ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)
1445        {        {
1446        int term;        int term;
1447        const uschar *thisname;        const pcre_uchar *thisname;
1448        *count += 1;        *count += 1;
1449        if (name == NULL && *count == lorn) return *count;        if (name == NULL && *count == lorn) return *count;
1450        term = *ptr++;        term = *ptr++;
# Line 1317  if (ptr[0] == CHAR_LEFT_PARENTHESIS) Line 1452  if (ptr[0] == CHAR_LEFT_PARENTHESIS)
1452        thisname = ptr;        thisname = ptr;
1453        while (*ptr != term) ptr++;        while (*ptr != term) ptr++;
1454        if (name != NULL && lorn == ptr - thisname &&        if (name != NULL && lorn == ptr - thisname &&
1455            strncmp((const char *)name, (const char *)thisname, lorn) == 0)            STRNCMP_UC_UC(name, thisname, lorn) == 0)
1456          return *count;          return *count;
1457        term++;        term++;
1458        }        }
# Line 1360  for (; ptr < cd->end_pattern; ptr++) Line 1495  for (; ptr < cd->end_pattern; ptr++)
1495          {          {
1496          if (ptr[2] == CHAR_E)          if (ptr[2] == CHAR_E)
1497            ptr+= 2;            ptr+= 2;
1498          else if (strncmp((const char *)ptr+2,          else if (STRNCMP_UC_C8(ptr + 2,
1499                   STR_Q STR_BACKSLASH STR_E, 3) == 0)                   STR_Q STR_BACKSLASH STR_E, 3) == 0)
1500            ptr += 4;            ptr += 4;
1501          else          else
# Line 1408  for (; ptr < cd->end_pattern; ptr++) Line 1543  for (; ptr < cd->end_pattern; ptr++)
1543        {        {
1544        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }        if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
1545        ptr++;        ptr++;
1546  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1547        if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;        if (utf) FORWARDCHAR(ptr);
1548  #endif  #endif
1549        }        }
1550      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
# Line 1420  for (; ptr < cd->end_pattern; ptr++) Line 1555  for (; ptr < cd->end_pattern; ptr++)
1555    
1556    if (*ptr == CHAR_LEFT_PARENTHESIS)    if (*ptr == CHAR_LEFT_PARENTHESIS)
1557      {      {
1558      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, count);      int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);
1559      if (rc > 0) return rc;      if (rc > 0) return rc;
1560      if (*ptr == 0) goto FAIL_EXIT;      if (*ptr == 0) goto FAIL_EXIT;
1561      }      }
# Line 1466  Arguments: Line 1601  Arguments:
1601    name         name to seek, or NULL if seeking a numbered subpattern    name         name to seek, or NULL if seeking a numbered subpattern
1602    lorn         name length, or subpattern number if name is NULL    lorn         name length, or subpattern number if name is NULL
1603    xmode        TRUE if we are in /x mode    xmode        TRUE if we are in /x mode
1604    utf8         TRUE if we are in UTF-8 mode    utf          TRUE if we are in UTF-8 / UTF-16 mode
1605    
1606  Returns:       the number of the found subpattern, or -1 if not found  Returns:       the number of the found subpattern, or -1 if not found
1607  */  */
1608    
1609  static int  static int
1610  find_parens(compile_data *cd, const uschar *name, int lorn, BOOL xmode,  find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,
1611    BOOL utf8)    BOOL utf)
1612  {  {
1613  uschar *ptr = (uschar *)cd->start_pattern;  pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;
1614  int count = 0;  int count = 0;
1615  int rc;  int rc;
1616    
# Line 1486  matching closing parens. That is why we Line 1621  matching closing parens. That is why we
1621    
1622  for (;;)  for (;;)
1623    {    {
1624    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf8, &count);    rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);
1625    if (rc > 0 || *ptr++ == 0) break;    if (rc > 0 || *ptr++ == 0) break;
1626    }    }
1627    
# Line 1513  Arguments: Line 1648  Arguments:
1648  Returns:       pointer to the first significant opcode  Returns:       pointer to the first significant opcode
1649  */  */
1650    
1651  static const uschar*  static const pcre_uchar*
1652  first_significant_code(const uschar *code, BOOL skipassert)  first_significant_code(const pcre_uchar *code, BOOL skipassert)
1653  {  {
1654  for (;;)  for (;;)
1655    {    {
# Line 1525  for (;;) Line 1660  for (;;)
1660      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1661      if (!skipassert) return code;      if (!skipassert) return code;
1662      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
1663      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1664      break;      break;
1665    
1666      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
# Line 1539  for (;;) Line 1674  for (;;)
1674      case OP_RREF:      case OP_RREF:
1675      case OP_NRREF:      case OP_NRREF:
1676      case OP_DEF:      case OP_DEF:
1677      code += _pcre_OP_lengths[*code];      code += PRIV(OP_lengths)[*code];
1678      break;      break;
1679    
1680      default:      default:
# Line 1569  and doing the check at the end; a flag s Line 1704  and doing the check at the end; a flag s
1704    
1705  Arguments:  Arguments:
1706    code     points to the start of the pattern (the bracket)    code     points to the start of the pattern (the bracket)
1707    utf8     TRUE in UTF-8 mode    utf      TRUE in UTF-8 / UTF-16 mode
1708    atend    TRUE if called when the pattern is complete    atend    TRUE if called when the pattern is complete
1709    cd       the "compile data" structure    cd       the "compile data" structure
1710    
# Line 1581  Returns:   the fixed length, Line 1716  Returns:   the fixed length,
1716  */  */
1717    
1718  static int  static int
1719  find_fixedlength(uschar *code, BOOL utf8, BOOL atend, compile_data *cd)  find_fixedlength(pcre_uchar *code, BOOL utf, BOOL atend, compile_data *cd)
1720  {  {
1721  int length = -1;  int length = -1;
1722    
1723  register int branchlength = 0;  register int branchlength = 0;
1724  register uschar *cc = code + 1 + LINK_SIZE;  register pcre_uchar *cc = code + 1 + LINK_SIZE;
1725    
1726  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
1727  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 1594  branch, check the length against that of Line 1729  branch, check the length against that of
1729  for (;;)  for (;;)
1730    {    {
1731    int d;    int d;
1732    uschar *ce, *cs;    pcre_uchar *ce, *cs;
1733    register int op = *cc;    register int op = *cc;
1734    
1735    switch (op)    switch (op)
1736      {      {
1737      /* We only need to continue for OP_CBRA (normal capturing bracket) and      /* We only need to continue for OP_CBRA (normal capturing bracket) and
# Line 1608  for (;;) Line 1744  for (;;)
1744      case OP_ONCE:      case OP_ONCE:
1745      case OP_ONCE_NC:      case OP_ONCE_NC:
1746      case OP_COND:      case OP_COND:
1747      d = find_fixedlength(cc + ((op == OP_CBRA)? 2:0), utf8, atend, cd);      d = find_fixedlength(cc + ((op == OP_CBRA)? IMM2_SIZE : 0), utf, atend, cd);
1748      if (d < 0) return d;      if (d < 0) return d;
1749      branchlength += d;      branchlength += d;
1750      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 1639  for (;;) Line 1775  for (;;)
1775    
1776      case OP_RECURSE:      case OP_RECURSE:
1777      if (!atend) return -3;      if (!atend) return -3;
1778      cs = ce = (uschar *)cd->start_code + GET(cc, 1);  /* Start subpattern */      cs = ce = (pcre_uchar *)cd->start_code + GET(cc, 1);  /* Start subpattern */
1779      do ce += GET(ce, 1); while (*ce == OP_ALT);       /* End subpattern */      do ce += GET(ce, 1); while (*ce == OP_ALT);           /* End subpattern */
1780      if (cc > cs && cc < ce) return -1;                /* Recursion */      if (cc > cs && cc < ce) return -1;                    /* Recursion */
1781      d = find_fixedlength(cs + 2, utf8, atend, cd);      d = find_fixedlength(cs + IMM2_SIZE, utf, atend, cd);
1782      if (d < 0) return d;      if (d < 0) return d;
1783      branchlength += d;      branchlength += d;
1784      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
# Line 1655  for (;;) Line 1791  for (;;)
1791      case OP_ASSERTBACK:      case OP_ASSERTBACK:
1792      case OP_ASSERTBACK_NOT:      case OP_ASSERTBACK_NOT:
1793      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
1794      /* Fall through */      cc += PRIV(OP_lengths)[*cc];
1795        break;
1796    
1797      /* Skip over things that don't match chars */      /* Skip over things that don't match chars */
1798    
# Line 1663  for (;;) Line 1800  for (;;)
1800      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
1801      case OP_SKIP_ARG:      case OP_SKIP_ARG:
1802      case OP_THEN_ARG:      case OP_THEN_ARG:
1803      cc += cc[1] + _pcre_OP_lengths[*cc];      cc += cc[1] + PRIV(OP_lengths)[*cc];
1804      break;      break;
1805    
1806      case OP_CALLOUT:      case OP_CALLOUT:
# Line 1690  for (;;) Line 1827  for (;;)
1827      case OP_SOM:      case OP_SOM:
1828      case OP_THEN:      case OP_THEN:
1829      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
1830      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
1831      break;      break;
1832    
1833      /* Handle literal characters */      /* Handle literal characters */
# Line 1701  for (;;) Line 1838  for (;;)
1838      case OP_NOTI:      case OP_NOTI:
1839      branchlength++;      branchlength++;
1840      cc += 2;      cc += 2;
1841  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1842      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1843  #endif  #endif
1844      break;      break;
1845    
# Line 1714  for (;;) Line 1851  for (;;)
1851      case OP_NOTEXACT:      case OP_NOTEXACT:
1852      case OP_NOTEXACTI:      case OP_NOTEXACTI:
1853      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1854      cc += 4;      cc += 2 + IMM2_SIZE;
1855  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
1856      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
1857  #endif  #endif
1858      break;      break;
1859    
1860      case OP_TYPEEXACT:      case OP_TYPEEXACT:
1861      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
1862      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP || cc[1 + IMM2_SIZE] == OP_NOTPROP)
1863      cc += 4;        cc += 2;
1864        cc += 1 + IMM2_SIZE + 1;
1865      break;      break;
1866    
1867      /* Handle single-char matchers */      /* Handle single-char matchers */
# Line 1757  for (;;) Line 1895  for (;;)
1895    
1896      /* Check a class for variable quantification */      /* Check a class for variable quantification */
1897    
1898  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || defined COMPILE_PCRE16
1899      case OP_XCLASS:      case OP_XCLASS:
1900      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
1901      /* Fall through */      /* Fall through */
1902  #endif  #endif
1903    
1904      case OP_CLASS:      case OP_CLASS:
1905      case OP_NCLASS:      case OP_NCLASS:
1906      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
1907    
1908      switch (*cc)      switch (*cc)
1909        {        {
# Line 1779  for (;;) Line 1917  for (;;)
1917    
1918        case OP_CRRANGE:        case OP_CRRANGE:
1919        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1920        if (GET2(cc,1) != GET2(cc,3)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1921        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
1922        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
1923        break;        break;
1924    
1925        default:        default:
# Line 1896  length. Line 2034  length.
2034    
2035  Arguments:  Arguments:
2036    code        points to start of expression    code        points to start of expression
2037    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2038    number      the required bracket number or negative to find a lookbehind    number      the required bracket number or negative to find a lookbehind
2039    
2040  Returns:      pointer to the opcode for the bracket, or NULL if not found  Returns:      pointer to the opcode for the bracket, or NULL if not found
2041  */  */
2042    
2043  const uschar *  const pcre_uchar *
2044  _pcre_find_bracket(const uschar *code, BOOL utf8, int number)  PRIV(find_bracket)(const pcre_uchar *code, BOOL utf, int number)
2045  {  {
2046  for (;;)  for (;;)
2047    {    {
# Line 1921  for (;;) Line 2059  for (;;)
2059    
2060    else if (c == OP_REVERSE)    else if (c == OP_REVERSE)
2061      {      {
2062      if (number < 0) return (uschar *)code;      if (number < 0) return (pcre_uchar *)code;
2063      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2064      }      }
2065    
2066    /* Handle capturing bracket */    /* Handle capturing bracket */
# Line 1931  for (;;) Line 2069  for (;;)
2069             c == OP_CBRAPOS || c == OP_SCBRAPOS)             c == OP_CBRAPOS || c == OP_SCBRAPOS)
2070      {      {
2071      int n = GET2(code, 1+LINK_SIZE);      int n = GET2(code, 1+LINK_SIZE);
2072      if (n == number) return (uschar *)code;      if (n == number) return (pcre_uchar *)code;
2073      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2074      }      }
2075    
2076    /* Otherwise, we can get the item's length from the table, except that for    /* Otherwise, we can get the item's length from the table, except that for
# Line 1960  for (;;) Line 2098  for (;;)
2098        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2099        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2100        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
2101        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2102            code += 2;
2103        break;        break;
2104    
2105        case OP_MARK:        case OP_MARK:
# Line 1976  for (;;) Line 2115  for (;;)
2115    
2116      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2117    
2118      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2119    
2120    /* In UTF-8 mode, opcodes that are followed by a character may be followed by    /* In UTF-8 mode, opcodes that are followed by a character may be followed by
2121    a multi-byte character. The length in the table is a minimum, so we have to    a multi-byte character. The length in the table is a minimum, so we have to
2122    arrange to skip the extra bytes. */    arrange to skip the extra bytes. */
2123    
2124  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2125      if (utf8) switch(c)      if (utf) switch(c)
2126        {        {
2127        case OP_CHAR:        case OP_CHAR:
2128        case OP_CHARI:        case OP_CHARI:
# Line 2013  for (;;) Line 2152  for (;;)
2152        case OP_MINQUERYI:        case OP_MINQUERYI:
2153        case OP_POSQUERY:        case OP_POSQUERY:
2154        case OP_POSQUERYI:        case OP_POSQUERYI:
2155        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2156        break;        break;
2157        }        }
2158  #else  #else
2159      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2160  #endif  #endif
2161      }      }
2162    }    }
# Line 2034  instance of OP_RECURSE. Line 2173  instance of OP_RECURSE.
2173    
2174  Arguments:  Arguments:
2175    code        points to start of expression    code        points to start of expression
2176    utf8        TRUE in UTF-8 mode    utf         TRUE in UTF-8 / UTF-16 mode
2177    
2178  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found  Returns:      pointer to the opcode for OP_RECURSE, or NULL if not found
2179  */  */
2180    
2181  static const uschar *  static const pcre_uchar *
2182  find_recurse(const uschar *code, BOOL utf8)  find_recurse(const pcre_uchar *code, BOOL utf)
2183  {  {
2184  for (;;)  for (;;)
2185    {    {
# Line 2079  for (;;) Line 2218  for (;;)
2218        case OP_TYPEUPTO:        case OP_TYPEUPTO:
2219        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
2220        case OP_TYPEEXACT:        case OP_TYPEEXACT:
2221        if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;        if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2222            code += 2;
2223        break;        break;
2224    
2225        case OP_MARK:        case OP_MARK:
# Line 2095  for (;;) Line 2235  for (;;)
2235    
2236      /* Add in the fixed length from the table */      /* Add in the fixed length from the table */
2237    
2238      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2239    
2240      /* In UTF-8 mode, opcodes that are followed by a character may be followed      /* In UTF-8 mode, opcodes that are followed by a character may be followed
2241      by a multi-byte character. The length in the table is a minimum, so we have      by a multi-byte character. The length in the table is a minimum, so we have
2242      to arrange to skip the extra bytes. */      to arrange to skip the extra bytes. */
2243    
2244  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2245      if (utf8) switch(c)      if (utf) switch(c)
2246        {        {
2247        case OP_CHAR:        case OP_CHAR:
2248        case OP_CHARI:        case OP_CHARI:
2249          case OP_NOT:
2250          case OP_NOTI:
2251        case OP_EXACT:        case OP_EXACT:
2252        case OP_EXACTI:        case OP_EXACTI:
2253          case OP_NOTEXACT:
2254          case OP_NOTEXACTI:
2255        case OP_UPTO:        case OP_UPTO:
2256        case OP_UPTOI:        case OP_UPTOI:
2257          case OP_NOTUPTO:
2258          case OP_NOTUPTOI:
2259        case OP_MINUPTO:        case OP_MINUPTO:
2260        case OP_MINUPTOI:        case OP_MINUPTOI:
2261          case OP_NOTMINUPTO:
2262          case OP_NOTMINUPTOI:
2263        case OP_POSUPTO:        case OP_POSUPTO:
2264        case OP_POSUPTOI:        case OP_POSUPTOI:
2265          case OP_NOTPOSUPTO:
2266          case OP_NOTPOSUPTOI:
2267        case OP_STAR:        case OP_STAR:
2268        case OP_STARI:        case OP_STARI:
2269          case OP_NOTSTAR:
2270          case OP_NOTSTARI:
2271        case OP_MINSTAR:        case OP_MINSTAR:
2272        case OP_MINSTARI:        case OP_MINSTARI:
2273          case OP_NOTMINSTAR:
2274          case OP_NOTMINSTARI:
2275        case OP_POSSTAR:        case OP_POSSTAR:
2276        case OP_POSSTARI:        case OP_POSSTARI:
2277          case OP_NOTPOSSTAR:
2278          case OP_NOTPOSSTARI:
2279        case OP_PLUS:        case OP_PLUS:
2280        case OP_PLUSI:        case OP_PLUSI:
2281          case OP_NOTPLUS:
2282          case OP_NOTPLUSI:
2283        case OP_MINPLUS:        case OP_MINPLUS:
2284        case OP_MINPLUSI:        case OP_MINPLUSI:
2285          case OP_NOTMINPLUS:
2286          case OP_NOTMINPLUSI:
2287        case OP_POSPLUS:        case OP_POSPLUS:
2288        case OP_POSPLUSI:        case OP_POSPLUSI:
2289          case OP_NOTPOSPLUS:
2290          case OP_NOTPOSPLUSI:
2291        case OP_QUERY:        case OP_QUERY:
2292        case OP_QUERYI:        case OP_QUERYI:
2293          case OP_NOTQUERY:
2294          case OP_NOTQUERYI:
2295        case OP_MINQUERY:        case OP_MINQUERY:
2296        case OP_MINQUERYI:        case OP_MINQUERYI:
2297          case OP_NOTMINQUERY:
2298          case OP_NOTMINQUERYI:
2299        case OP_POSQUERY:        case OP_POSQUERY:
2300        case OP_POSQUERYI:        case OP_POSQUERYI:
2301        if (code[-1] >= 0xc0) code += _pcre_utf8_table4[code[-1] & 0x3f];        case OP_NOTPOSQUERY:
2302          case OP_NOTPOSQUERYI:
2303          if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2304        break;        break;
2305        }        }
2306  #else  #else
2307      (void)(utf8);  /* Keep compiler happy by referencing function argument */      (void)(utf);  /* Keep compiler happy by referencing function argument */
2308  #endif  #endif
2309      }      }
2310    }    }
# Line 2159  bracket whose current branch will alread Line 2327  bracket whose current branch will alread
2327  Arguments:  Arguments:
2328    code        points to start of search    code        points to start of search
2329    endcode     points to where to stop    endcode     points to where to stop
2330    utf8        TRUE if in UTF8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2331    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2332    
2333  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2334  */  */
2335    
2336  static BOOL  static BOOL
2337  could_be_empty_branch(const uschar *code, const uschar *endcode, BOOL utf8,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2338    compile_data *cd)    BOOL utf, compile_data *cd)
2339  {  {
2340  register int c;  register int c;
2341  for (code = first_significant_code(code + _pcre_OP_lengths[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2342       code < endcode;       code < endcode;
2343       code = first_significant_code(code + _pcre_OP_lengths[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
2344    {    {
2345    const uschar *ccode;    const pcre_uchar *ccode;
2346    
2347    c = *code;    c = *code;
2348    
# Line 2197  for (code = first_significant_code(code Line 2365  for (code = first_significant_code(code
2365    
2366    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2367      {      {
2368      const uschar *scode;      const pcre_uchar *scode;
2369      BOOL empty_branch;      BOOL empty_branch;
2370    
2371      /* Test for forward reference */      /* Test for forward reference */
# Line 2215  for (code = first_significant_code(code Line 2383  for (code = first_significant_code(code
2383    
2384      do      do
2385        {        {
2386        if (could_be_empty_branch(scode, endcode, utf8, cd))        if (could_be_empty_branch(scode, endcode, utf, cd))
2387          {          {
2388          empty_branch = TRUE;          empty_branch = TRUE;
2389          break;          break;
# Line 2233  for (code = first_significant_code(code Line 2401  for (code = first_significant_code(code
2401    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||    if (c == OP_BRAZERO || c == OP_BRAMINZERO || c == OP_SKIPZERO ||
2402        c == OP_BRAPOSZERO)        c == OP_BRAPOSZERO)
2403      {      {
2404      code += _pcre_OP_lengths[c];      code += PRIV(OP_lengths)[c];
2405      do code += GET(code, 1); while (*code == OP_ALT);      do code += GET(code, 1); while (*code == OP_ALT);
2406      c = *code;      c = *code;
2407      continue;      continue;
# Line 2271  for (code = first_significant_code(code Line 2439  for (code = first_significant_code(code
2439        empty_branch = FALSE;        empty_branch = FALSE;
2440        do        do
2441          {          {
2442          if (!empty_branch && could_be_empty_branch(code, endcode, utf8, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))
2443            empty_branch = TRUE;            empty_branch = TRUE;
2444          code += GET(code, 1);          code += GET(code, 1);
2445          }          }
# Line 2289  for (code = first_significant_code(code Line 2457  for (code = first_significant_code(code
2457      {      {
2458      /* Check for quantifiers after a class. XCLASS is used for classes that      /* Check for quantifiers after a class. XCLASS is used for classes that
2459      cannot be represented just by a bit map. This includes negated single      cannot be represented just by a bit map. This includes negated single
2460      high-valued characters. The length in _pcre_OP_lengths[] is zero; the      high-valued characters. The length in PRIV(OP_lengths)[] is zero; the
2461      actual length is stored in the compiled code, so we must update "code"      actual length is stored in the compiled code, so we must update "code"
2462      here. */      here. */
2463    
2464  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2465      case OP_XCLASS:      case OP_XCLASS:
2466      ccode = code += GET(code, 1);      ccode = code += GET(code, 1);
2467      goto CHECK_CLASS_REPEAT;      goto CHECK_CLASS_REPEAT;
# Line 2301  for (code = first_significant_code(code Line 2469  for (code = first_significant_code(code
2469    
2470      case OP_CLASS:      case OP_CLASS:
2471      case OP_NCLASS:      case OP_NCLASS:
2472      ccode = code + 33;      ccode = code + PRIV(OP_lengths)[OP_CLASS];
2473    
2474  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
2475      CHECK_CLASS_REPEAT:      CHECK_CLASS_REPEAT:
2476  #endif  #endif
2477    
# Line 2376  for (code = first_significant_code(code Line 2544  for (code = first_significant_code(code
2544      case OP_TYPEUPTO:      case OP_TYPEUPTO:
2545      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
2546      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
2547      if (code[3] == OP_PROP || code[3] == OP_NOTPROP) code += 2;      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
2548          code += 2;
2549      break;      break;
2550    
2551      /* End of branch */      /* End of branch */
# Line 2391  for (code = first_significant_code(code Line 2560  for (code = first_significant_code(code
2560      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2561      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO may be followed by a multibyte character */
2562    
2563  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
2564      case OP_STAR:      case OP_STAR:
2565      case OP_STARI:      case OP_STARI:
2566      case OP_MINSTAR:      case OP_MINSTAR:
# Line 2404  for (code = first_significant_code(code Line 2573  for (code = first_significant_code(code
2573      case OP_MINQUERYI:      case OP_MINQUERYI:
2574      case OP_POSQUERY:      case OP_POSQUERY:
2575      case OP_POSQUERYI:      case OP_POSQUERYI:
2576      if (utf8 && code[1] >= 0xc0) code += _pcre_utf8_table4[code[1] & 0x3f];      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2577      break;      break;
2578    
2579      case OP_UPTO:      case OP_UPTO:
# Line 2413  for (code = first_significant_code(code Line 2582  for (code = first_significant_code(code
2582      case OP_MINUPTOI:      case OP_MINUPTOI:
2583      case OP_POSUPTO:      case OP_POSUPTO:
2584      case OP_POSUPTOI:      case OP_POSUPTOI:
2585      if (utf8 && code[3] >= 0xc0) code += _pcre_utf8_table4[code[3] & 0x3f];      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2586      break;      break;
2587  #endif  #endif
2588    
# Line 2457  Arguments: Line 2626  Arguments:
2626    code        points to start of the recursion    code        points to start of the recursion
2627    endcode     points to where to stop (current RECURSE item)    endcode     points to where to stop (current RECURSE item)
2628    bcptr       points to the chain of current (unclosed) branch starts    bcptr       points to the chain of current (unclosed) branch starts
2629    utf8        TRUE if in UTF-8 mode    utf         TRUE if in UTF-8 / UTF-16 mode
2630    cd          pointers to tables etc    cd          pointers to tables etc
2631    
2632  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2633  */  */
2634    
2635  static BOOL  static BOOL
2636  could_be_empty(const uschar *code, const uschar *endcode, branch_chain *bcptr,  could_be_empty(const pcre_uchar *code, const pcre_uchar *endcode,
2637    BOOL utf8, compile_data *cd)    branch_chain *bcptr, BOOL utf, compile_data *cd)
2638  {  {
2639  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2640    {    {
2641    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf8, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))
2642      return FALSE;      return FALSE;
2643    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2644    }    }
# Line 2521  Returns:   TRUE or FALSE Line 2690  Returns:   TRUE or FALSE
2690  */  */
2691    
2692  static BOOL  static BOOL
2693  check_posix_syntax(const uschar *ptr, const uschar **endptr)  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
2694  {  {
2695  int terminator;          /* Don't combine these lines; the Solaris cc */  int terminator;          /* Don't combine these lines; the Solaris cc */
2696  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
# Line 2565  Returns:     a value representing the na Line 2734  Returns:     a value representing the na
2734  */  */
2735    
2736  static int  static int
2737  check_posix_name(const uschar *ptr, int len)  check_posix_name(const pcre_uchar *ptr, int len)
2738  {  {
2739  const char *pn = posix_names;  const char *pn = posix_names;
2740  register int yield = 0;  register int yield = 0;
2741  while (posix_name_lengths[yield] != 0)  while (posix_name_lengths[yield] != 0)
2742    {    {
2743    if (len == posix_name_lengths[yield] &&    if (len == posix_name_lengths[yield] &&
2744      strncmp((const char *)ptr, pn, len) == 0) return yield;      STRNCMP_UC_C8(ptr, pn, len) == 0) return yield;
2745    pn += posix_name_lengths[yield] + 1;    pn += posix_name_lengths[yield] + 1;
2746    yield++;    yield++;
2747    }    }
# Line 2604  value in the reference (which is a group Line 2773  value in the reference (which is a group
2773  Arguments:  Arguments:
2774    group      points to the start of the group    group      points to the start of the group
2775    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
2776    utf8       TRUE in UTF-8 mode    utf        TRUE in UTF-8 / UTF-16 mode
2777    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
2778    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm   the hwm forward reference pointer at the start of the group
2779    
# Line 2612  Returns:     nothing Line 2781  Returns:     nothing
2781  */  */
2782    
2783  static void  static void
2784  adjust_recurse(uschar *group, int adjust, BOOL utf8, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
2785    uschar *save_hwm)    pcre_uchar *save_hwm)
2786  {  {
2787  uschar *ptr = group;  pcre_uchar *ptr = group;
2788    
2789  while ((ptr = (uschar *)find_recurse(ptr, utf8)) != NULL)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
2790    {    {
2791    int offset;    int offset;
2792    uschar *hc;    pcre_uchar *hc;
2793    
2794    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
2795    reference. */    reference. */
# Line 2665  Arguments: Line 2834  Arguments:
2834  Returns:         new code pointer  Returns:         new code pointer
2835  */  */
2836    
2837  static uschar *  static pcre_uchar *
2838  auto_callout(uschar *code, const uschar *ptr, compile_data *cd)  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
2839  {  {
2840  *code++ = OP_CALLOUT;  *code++ = OP_CALLOUT;
2841  *code++ = 255;  *code++ = 255;
2842  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
2843  PUT(code, LINK_SIZE, 0);                       /* Default length */  PUT(code, LINK_SIZE, 0);                       /* Default length */
2844  return code + 2*LINK_SIZE;  return code + 2 * LINK_SIZE;
2845  }  }
2846    
2847    
# Line 2694  Returns:             nothing Line 2863  Returns:             nothing
2863  */  */
2864    
2865  static void  static void
2866  complete_callout(uschar *previous_callout, const uschar *ptr, compile_data *cd)  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
2867  {  {
2868  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
2869  PUT(previous_callout, 2 + LINK_SIZE, length);  PUT(previous_callout, 2 + LINK_SIZE, length);
# Line 2708  PUT(previous_callout, 2 + LINK_SIZE, len Line 2877  PUT(previous_callout, 2 + LINK_SIZE, len
2877  *************************************************/  *************************************************/
2878    
2879  /* This function is passed the start and end of a class range, in UTF-8 mode  /* This function is passed the start and end of a class range, in UTF-8 mode
2880  with UCP support. It searches up the characters, looking for internal ranges of  with UCP support. It searches up the characters, looking for ranges of
2881  characters in the "other" case. Each call returns the next one, updating the  characters in the "other" case. Each call returns the next one, updating the
2882  start address.  start address. A character with multiple other cases is returned on its own
2883    with a special return value.
2884    
2885  Arguments:  Arguments:
2886    cptr        points to starting character value; updated    cptr        points to starting character value; updated
# Line 2718  Arguments: Line 2888  Arguments:
2888    ocptr       where to put start of othercase range    ocptr       where to put start of othercase range
2889    odptr       where to put end of othercase range    odptr       where to put end of othercase range
2890    
2891  Yield:        TRUE when range returned; FALSE when no more  Yield:        -1 when no more
2892                   0 when a range is returned
2893                  >0 the CASESET offset for char with multiple other cases
2894                    in this case, ocptr contains the original
2895  */  */
2896    
2897  static BOOL  static int
2898  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,  get_othercase_range(unsigned int *cptr, unsigned int d, unsigned int *ocptr,
2899    unsigned int *odptr)    unsigned int *odptr)
2900  {  {
2901  unsigned int c, othercase, next;  unsigned int c, othercase, next;
2902    int co;
2903    
2904    /* Find the first character that has an other case. If it has multiple other
2905    cases, return its case offset value. */
2906    
2907  for (c = *cptr; c <= d; c++)  for (c = *cptr; c <= d; c++)
2908    { if ((othercase = UCD_OTHERCASE(c)) != c) break; }    {
2909      if ((co = UCD_CASESET(c)) != 0)
2910        {
2911        *ocptr = c++;   /* Character that has the set */
2912        *cptr = c;      /* Rest of input range */
2913        return co;
2914        }
2915      if ((othercase = UCD_OTHERCASE(c)) != c) break;
2916      }
2917    
2918  if (c > d) return FALSE;  if (c > d) return -1;  /* Reached end of range */
2919    
2920  *ocptr = othercase;  *ocptr = othercase;
2921  next = othercase + 1;  next = othercase + 1;
# Line 2741  for (++c; c <= d; c++) Line 2926  for (++c; c <= d; c++)
2926    next++;    next++;
2927    }    }
2928    
2929  *odptr = next - 1;  *odptr = next - 1;     /* End of othercase range */
2930  *cptr = c;  *cptr = c;             /* Rest of input range */
2931    return 0;
 return TRUE;  
2932  }  }
2933    
2934    
# Line 2768  Returns:       TRUE if auto-possessifyin Line 2952  Returns:       TRUE if auto-possessifyin
2952  static BOOL  static BOOL
2953  check_char_prop(int c, int ptype, int pdata, BOOL negated)  check_char_prop(int c, int ptype, int pdata, BOOL negated)
2954  {  {
2955    #ifdef SUPPORT_UCP
2956    const pcre_uint32 *p;
2957    #endif
2958    
2959  const ucd_record *prop = GET_UCD(c);  const ucd_record *prop = GET_UCD(c);
2960    
2961  switch(ptype)  switch(ptype)
2962    {    {
2963    case PT_LAMP:    case PT_LAMP:
# Line 2777  switch(ptype) Line 2966  switch(ptype)
2966            prop->chartype == ucp_Lt) == negated;            prop->chartype == ucp_Lt) == negated;
2967    
2968    case PT_GC:    case PT_GC:
2969    return (pdata == _pcre_ucp_gentype[prop->chartype]) == negated;    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2970    
2971    case PT_PC:    case PT_PC:
2972    return (pdata == prop->chartype) == negated;    return (pdata == prop->chartype) == negated;
# Line 2788  switch(ptype) Line 2977  switch(ptype)
2977    /* These are specials */    /* These are specials */
2978    
2979    case PT_ALNUM:    case PT_ALNUM:
2980    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2981            _pcre_ucp_gentype[prop->chartype] == ucp_N) == negated;            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2982    
2983    case PT_SPACE:    /* Perl space */    case PT_SPACE:    /* Perl space */
2984    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2985            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)            c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)
2986            == negated;            == negated;
2987    
2988    case PT_PXSPACE:  /* POSIX space */    case PT_PXSPACE:  /* POSIX space */
2989    return (_pcre_ucp_gentype[prop->chartype] == ucp_Z ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||
2990            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||            c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||
2991            c == CHAR_FF || c == CHAR_CR)            c == CHAR_FF || c == CHAR_CR)
2992            == negated;            == negated;
2993    
2994    case PT_WORD:    case PT_WORD:
2995    return (_pcre_ucp_gentype[prop->chartype] == ucp_L ||    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2996            _pcre_ucp_gentype[prop->chartype] == ucp_N ||            PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2997            c == CHAR_UNDERSCORE) == negated;            c == CHAR_UNDERSCORE) == negated;
2998    
2999    #ifdef SUPPORT_UCP
3000      case PT_CLIST:
3001      p = PRIV(ucd_caseless_sets) + prop->caseset;
3002      for (;;)
3003        {
3004        if ((unsigned int)c < *p) return !negated;
3005        if ((unsigned int)c == *p++) return negated;
3006        }
3007      break;  /* Control never reaches here */
3008    #endif
3009    }    }
3010    
3011  return FALSE;  return FALSE;
3012  }  }
3013  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
# Line 2823  sense to automatically possessify the re Line 3024  sense to automatically possessify the re
3024    
3025  Arguments:  Arguments:
3026    previous      pointer to the repeated opcode    previous      pointer to the repeated opcode
3027    utf8          TRUE in UTF-8 mode    utf           TRUE in UTF-8 / UTF-16 mode
3028    ptr           next character in pattern    ptr           next character in pattern
3029    options       options bits    options       options bits
3030    cd            contains pointers to tables etc.    cd            contains pointers to tables etc.
# Line 2832  Returns:        TRUE if possessifying is Line 3033  Returns:        TRUE if possessifying is
3033  */  */
3034    
3035  static BOOL  static BOOL
3036  check_auto_possessive(const uschar *previous, BOOL utf8, const uschar *ptr,  check_auto_possessive(const pcre_uchar *previous, BOOL utf,
3037    int options, compile_data *cd)    const pcre_uchar *ptr, int options, compile_data *cd)
3038  {  {
3039  int c, next;  pcre_int32 c, next;
3040  int op_code = *previous++;  int op_code = *previous++;
3041    
3042  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2844  if ((options & PCRE_EXTENDED) != 0) Line 3045  if ((options & PCRE_EXTENDED) != 0)
3045    {    {
3046    for (;;)    for (;;)
3047      {      {
3048      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3049      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3050        {        {
3051        ptr++;        ptr++;
# Line 2852  if ((options & PCRE_EXTENDED) != 0) Line 3053  if ((options & PCRE_EXTENDED) != 0)
3053          {          {
3054          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3055          ptr++;          ptr++;
3056  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3057          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3058  #endif  #endif
3059          }          }
3060        }        }
# Line 2871  if (*ptr == CHAR_BACKSLASH) Line 3072  if (*ptr == CHAR_BACKSLASH)
3072    if (temperrorcode != 0) return FALSE;    if (temperrorcode != 0) return FALSE;
3073    ptr++;    /* Point after the escape sequence */    ptr++;    /* Point after the escape sequence */
3074    }    }
3075    else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)
 else if ((cd->ctypes[*ptr] & ctype_meta) == 0)  
3076    {    {
3077  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3078    if (utf8) { GETCHARINC(next, ptr); } else    if (utf) { GETCHARINC(next, ptr); } else
3079  #endif  #endif
3080    next = *ptr++;    next = *ptr++;
3081    }    }
   
3082  else return FALSE;  else return FALSE;
3083    
3084  /* Skip whitespace and comments in extended mode */  /* Skip whitespace and comments in extended mode */
# Line 2888  if ((options & PCRE_EXTENDED) != 0) Line 3087  if ((options & PCRE_EXTENDED) != 0)
3087    {    {
3088    for (;;)    for (;;)
3089      {      {
3090      while ((cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;
3091      if (*ptr == CHAR_NUMBER_SIGN)      if (*ptr == CHAR_NUMBER_SIGN)
3092        {        {
3093        ptr++;        ptr++;
# Line 2896  if ((options & PCRE_EXTENDED) != 0) Line 3095  if ((options & PCRE_EXTENDED) != 0)
3095          {          {
3096          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }
3097          ptr++;          ptr++;
3098  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3099          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3100  #endif  #endif
3101          }          }
3102        }        }
# Line 2908  if ((options & PCRE_EXTENDED) != 0) Line 3107  if ((options & PCRE_EXTENDED) != 0)
3107  /* If the next thing is itself optional, we have to give up. */  /* If the next thing is itself optional, we have to give up. */
3108    
3109  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3110    strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3111      return FALSE;      return FALSE;
3112    
3113    /* If the previous item is a character, get its value. */
3114    
3115  /* Now compare the next item with the previous opcode. First, handle cases when  if (op_code == OP_CHAR || op_code == OP_CHARI ||
3116  the next item is a character. */      op_code == OP_NOT || op_code == OP_NOTI)
3117      {
3118  if (next >= 0) switch(op_code)  #ifdef SUPPORT_UTF
   {  
   case OP_CHAR:  
 #ifdef SUPPORT_UTF8  
3119    GETCHARTEST(c, previous);    GETCHARTEST(c, previous);
3120  #else  #else
3121    c = *previous;    c = *previous;
3122  #endif  #endif
3123    return c != next;    }
3124    
3125    /* For CHARI (caseless character) we must check the other case. If we have  /* Now compare the next item with the previous opcode. First, handle cases when
3126    Unicode property support, we can use it to test the other case of  the next item is a character. For a caseless UTF match, the next character may
3127    high-valued characters. */  have more than one other case; convert this to a special property. */
3128    
3129    case OP_CHARI:  if (next >= 0)
3130  #ifdef SUPPORT_UTF8    {
3131    GETCHARTEST(c, previous);  #ifdef SUPPORT_UCP
3132  #else    if (utf && (options & PCRE_CASELESS) != 0)
3133    c = *previous;      {
3134        int ocs = UCD_CASESET(next);
3135        if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, FALSE);
3136        }
3137  #endif  #endif
3138    if (c == next) return FALSE;  
3139  #ifdef SUPPORT_UTF8    switch(op_code)
   if (utf8)  
3140      {      {
3141      unsigned int othercase;      case OP_CHAR:
3142      if (next < 128) othercase = cd->fcc[next]; else      return c != next;
3143    
3144        /* For CHARI (caseless character) we must check the other case. If we have
3145        Unicode property support, we can use it to test the other case of
3146        high-valued characters. We know that next can have only one other case,
3147        because multi-other-case characters are dealt with above. */
3148    
3149        case OP_CHARI:
3150        if (c == next) return FALSE;
3151    #ifdef SUPPORT_UTF
3152        if (utf)
3153          {
3154          unsigned int othercase;
3155          if (next < 128) othercase = cd->fcc[next]; else
3156  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3157      othercase = UCD_OTHERCASE((unsigned int)next);        othercase = UCD_OTHERCASE((unsigned int)next);
3158  #else  #else
3159      othercase = NOTACHAR;        othercase = NOTACHAR;
3160  #endif  #endif
3161      return (unsigned int)c != othercase;        return (unsigned int)c != othercase;
3162      }        }
3163    else      else
3164  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3165    return (c != cd->fcc[next]);  /* Non-UTF-8 mode */      return (c != TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */
3166    
3167    /* For OP_NOT and OP_NOTI, the data is always a single-byte character. These      case OP_NOT:
3168    opcodes are not used for multi-byte characters, because they are coded using      return c == next;
3169    an XCLASS instead. */  
3170        case OP_NOTI:
3171    case OP_NOT:      if (c == next) return TRUE;
3172    return (c = *previous) == next;  #ifdef SUPPORT_UTF
3173        if (utf)
3174    case OP_NOTI:        {
3175    if ((c = *previous) == next) return TRUE;        unsigned int othercase;
3176  #ifdef SUPPORT_UTF8        if (next < 128) othercase = cd->fcc[next]; else
   if (utf8)  
     {  
     unsigned int othercase;  
     if (next < 128) othercase = cd->fcc[next]; else  
3177  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3178      othercase = UCD_OTHERCASE(next);        othercase = UCD_OTHERCASE((unsigned int)next);
3179  #else  #else
3180      othercase = NOTACHAR;        othercase = NOTACHAR;
3181  #endif  #endif
3182      return (unsigned int)c == othercase;        return (unsigned int)c == othercase;
3183      }        }
3184    else      else
3185  #endif  /* SUPPORT_UTF8 */  #endif  /* SUPPORT_UTF */
3186    return (c == cd->fcc[next]);  /* Non-UTF-8 mode */      return (c == TABLE_GET((unsigned int)next, cd->fcc, next));  /* Not UTF */
3187    
3188    /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.      /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.
3189    When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */      When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3190    
3191    case OP_DIGIT:      case OP_DIGIT:
3192    return next > 127 || (cd->ctypes[next] & ctype_digit) == 0;      return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;
3193    
3194    case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
3195    return next <= 127 && (cd->ctypes[next] & ctype_digit) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;
3196    
3197    case OP_WHITESPACE:      case OP_WHITESPACE:
3198    return next > 127 || (cd->ctypes[next] & ctype_space) == 0;      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;
3199    
3200    case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
3201    return next <= 127 && (cd->ctypes[next] & ctype_space) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;
3202    
3203    case OP_WORDCHAR:      case OP_WORDCHAR:
3204    return next > 127 || (cd->ctypes[next] & ctype_word) == 0;      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;
3205    
3206    case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
3207    return next <= 127 && (cd->ctypes[next] & ctype_word) != 0;      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;
3208    
3209    case OP_HSPACE:      case OP_HSPACE:
3210    case OP_NOT_HSPACE:      case OP_NOT_HSPACE:
3211    switch(next)      switch(next)
3212      {        {
3213      case 0x09:        HSPACE_CASES:
3214      case 0x20:        return op_code == OP_NOT_HSPACE;
3215      case 0xa0:  
3216      case 0x1680:        default:
3217      case 0x180e:        return op_code != OP_NOT_HSPACE;
3218      case 0x2000:        }
3219      case 0x2001:  
3220      case 0x2002:      case OP_ANYNL:
3221      case 0x2003:      case OP_VSPACE:
3222      case 0x2004:      case OP_NOT_VSPACE:
3223      case 0x2005:      switch(next)
3224      case 0x2006:        {
3225      case 0x2007:        VSPACE_CASES:
3226      case 0x2008:        return op_code == OP_NOT_VSPACE;
3227      case 0x2009:  
3228      case 0x200A:        default:
3229      case 0x202f:        return op_code != OP_NOT_VSPACE;
3230      case 0x205f:        }
3231      case 0x3000:  
     return op_code == OP_NOT_HSPACE;  
     default:  
     return op_code != OP_NOT_HSPACE;  
     }  
   
   case OP_ANYNL:  
   case OP_VSPACE:  
   case OP_NOT_VSPACE:  
   switch(next)  
     {  
     case 0x0a:  
     case 0x0b:  
     case 0x0c:  
     case 0x0d:  
     case 0x85:  
     case 0x2028:  
     case 0x2029:  
     return op_code == OP_NOT_VSPACE;  
     default:  
     return op_code != OP_NOT_VSPACE;  
     }  
   
3232  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3233    case OP_PROP:      case OP_PROP:
3234    return check_char_prop(next, previous[0], previous[1], FALSE);      return check_char_prop(next, previous[0], previous[1], FALSE);
3235    
3236    case OP_NOTPROP:      case OP_NOTPROP:
3237    return check_char_prop(next, previous[0], previous[1], TRUE);      return check_char_prop(next, previous[0], previous[1], TRUE);
3238  #endif  #endif
3239    
3240    default:      default:
3241    return FALSE;      return FALSE;
3242    }      }
3243      }
3244    
3245  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP
3246  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are
# Line 3065  switch(op_code) Line 3252  switch(op_code)
3252    {    {
3253    case OP_CHAR:    case OP_CHAR:
3254    case OP_CHARI:    case OP_CHARI:
 #ifdef SUPPORT_UTF8  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
3255    switch(-next)    switch(-next)
3256      {      {
3257      case ESC_d:      case ESC_d:
3258      return c > 127 || (cd->ctypes[c] & ctype_digit) == 0;      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;
3259    
3260      case ESC_D:      case ESC_D:
3261      return c <= 127 && (cd->ctypes[c] & ctype_digit) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;
3262    
3263      case ESC_s:      case ESC_s:
3264      return c > 127 || (cd->ctypes[c] & ctype_space) == 0;      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;
3265    
3266      case ESC_S:      case ESC_S:
3267      return c <= 127 && (cd->ctypes[c] & ctype_space) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;
3268    
3269      case ESC_w:      case ESC_w:
3270      return c > 127 || (cd->ctypes[c] & ctype_word) == 0;      return c > 255 || (cd->ctypes[c] & ctype_word) == 0;
3271    
3272      case ESC_W:      case ESC_W:
3273      return c <= 127 && (cd->ctypes[c] & ctype_word) != 0;      return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;
3274    
3275      case ESC_h:      case ESC_h:
3276      case ESC_H:      case ESC_H:
3277      switch(c)      switch(c)
3278        {        {
3279        case 0x09:        HSPACE_CASES:
       case 0x20:  
       case 0xa0:  
       case 0x1680:  
       case 0x180e:  
       case 0x2000:  
       case 0x2001:  
       case 0x2002:  
       case 0x2003:  
       case 0x2004:  
       case 0x2005:  
       case 0x2006:  
       case 0x2007:  
       case 0x2008:  
       case 0x2009:  
       case 0x200A:  
       case 0x202f:  
       case 0x205f:  
       case 0x3000:  
3280        return -next != ESC_h;        return -next != ESC_h;
3281    
3282        default:        default:
3283        return -next == ESC_h;        return -next == ESC_h;
3284        }        }
# Line 3122  switch(op_code) Line 3287  switch(op_code)
3287      case ESC_V:      case ESC_V:
3288      switch(c)      switch(c)
3289        {        {
3290        case 0x0a:        VSPACE_CASES:
       case 0x0b:  
       case 0x0c:  
       case 0x0d:  
       case 0x85:  
       case 0x2028:  
       case 0x2029:  
3291        return -next != ESC_v;        return -next != ESC_v;
3292    
3293        default:        default:
3294        return -next == ESC_v;        return -next == ESC_v;
3295        }        }
# Line 3170  switch(op_code) Line 3330  switch(op_code)
3330        to the original \d etc. At this point, ptr will point to a zero byte. */        to the original \d etc. At this point, ptr will point to a zero byte. */
3331    
3332        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||
3333          strncmp((char *)ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)
3334            return FALSE;            return FALSE;
3335    
3336        /* Do the property check. */        /* Do the property check. */
# Line 3197  switch(op_code) Line 3357  switch(op_code)
3357    return next == -ESC_d;    return next == -ESC_d;
3358    
3359    case OP_WHITESPACE:    case OP_WHITESPACE:
3360    return next == -ESC_S || next == -ESC_d || next == -ESC_w || next == -ESC_R;    return next == -ESC_S || next == -ESC_d || next == -ESC_w;
3361    
3362    case OP_NOT_WHITESPACE:    case OP_NOT_WHITESPACE:
3363    return next == -ESC_s || next == -ESC_h || next == -ESC_v;    return next == -ESC_s || next == -ESC_h || next == -ESC_v || next == -ESC_R;
3364    
3365    case OP_HSPACE:    case OP_HSPACE:
3366    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||    return next == -ESC_S || next == -ESC_H || next == -ESC_d ||
# Line 3234  switch(op_code) Line 3394  switch(op_code)
3394    
3395    
3396  /*************************************************  /*************************************************
3397    *        Add a character or range to a class     *
3398    *************************************************/
3399    
3400    /* This function packages up the logic of adding a character or range of
3401    characters to a class. The character values in the arguments will be within the
3402    valid values for the current mode (8-bit, 16-bit, UTF, etc). This function is
3403    mutually recursive with the function immediately below.
3404    
3405    Arguments:
3406      classbits     the bit map for characters < 256
3407      uchardptr     points to the pointer for extra data
3408      options       the options word
3409      cd            contains pointers to tables etc.
3410      start         start of range character
3411      end           end of range character
3412    
3413    Returns:        the number of < 256 characters added
3414                    the pointer to extra data is updated
3415    */
3416    
3417    static int
3418    add_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3419      compile_data *cd, unsigned int start, unsigned int end)
3420    {
3421    unsigned int c;
3422    int n8 = 0;
3423    
3424    /* If caseless matching is required, scan the range and process alternate
3425    cases. In Unicode, there are 8-bit characters that have alternate cases that
3426    are greater than 255 and vice-versa. Sometimes we can just extend the original
3427    range. */
3428    
3429    if ((options & PCRE_CASELESS) != 0)
3430      {
3431    #ifdef SUPPORT_UCP
3432      if ((options & PCRE_UTF8) != 0)
3433        {
3434        int rc;
3435        unsigned int oc, od;
3436    
3437        options &= ~PCRE_CASELESS;   /* Remove for recursive calls */
3438        c = start;
3439    
3440        while ((rc = get_othercase_range(&c, end, &oc, &od)) >= 0)
3441          {
3442          /* Handle a single character that has more than one other case. */
3443    
3444          if (rc > 0) n8 += add_list_to_class(classbits, uchardptr, options, cd,
3445            PRIV(ucd_caseless_sets) + rc, oc);
3446    
3447          /* Do nothing if the other case range is within the original range. */
3448    
3449          else if (oc >= start && od <= end) continue;
3450    
3451          /* Extend the original range if there is overlap, noting that if oc < c, we
3452          can't have od > end because a subrange is always shorter than the basic
3453          range. Otherwise, use a recursive call to add the additional range. */
3454    
3455          else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
3456          else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */
3457          else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
3458          }
3459        }
3460      else
3461    #endif  /* SUPPORT_UCP */
3462    
3463      /* Not UTF-mode, or no UCP */
3464    
3465      for (c = start; c <= end && c < 256; c++)
3466        {
3467        SETBIT(classbits, cd->fcc[c]);
3468        n8++;
3469        }
3470      }
3471    
3472    /* Now handle the original range. Adjust the final value according to the bit
3473    length - this means that the same lists of (e.g.) horizontal spaces can be used
3474    in all cases. */
3475    
3476    #ifdef COMPILE_PCRE8
3477    #ifdef SUPPORT_UTF
3478      if ((options & PCRE_UTF8) == 0)
3479    #endif
3480      if (end > 0xff) end = 0xff;
3481    #endif
3482    
3483    #ifdef COMPILE_PCRE16
3484    #ifdef SUPPORT_UTF
3485      if ((options & PCRE_UTF16) == 0)
3486    #endif
3487      if (end > 0xffff) end = 0xffff;
3488    #endif
3489    
3490    /* If all characters are less than 256, use the bit map. Otherwise use extra
3491    data. */
3492    
3493    if (end < 0x100)
3494      {
3495      for (c = start; c <= end; c++)
3496        {
3497        n8++;
3498        SETBIT(classbits, c);
3499        }
3500      }
3501    
3502    else
3503      {
3504      pcre_uchar *uchardata = *uchardptr;
3505    
3506    #ifdef SUPPORT_UTF
3507      if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
3508        {
3509        if (start < end)
3510          {
3511          *uchardata++ = XCL_RANGE;
3512          uchardata += PRIV(ord2utf)(start, uchardata);
3513          uchardata += PRIV(ord2utf)(end, uchardata);
3514          }
3515        else if (start == end)
3516          {
3517          *uchardata++ = XCL_SINGLE;
3518          uchardata += PRIV(ord2utf)(start, uchardata);
3519          }
3520        }
3521      else
3522    #endif  /* SUPPORT_UTF */
3523    
3524      /* Without UTF support, character values are constrained by the bit length,
3525      and can only be > 256 for 16-bit and 32-bit libraries. */
3526    
3527    #ifdef COMPILE_PCRE8
3528        {}
3529    #else
3530      if (start < end)
3531        {
3532        *uchardata++ = XCL_RANGE;
3533        *uchardata++ = start;
3534        *uchardata++ = end;
3535        }
3536      else if (start == end)
3537        {
3538        *uchardata++ = XCL_SINGLE;
3539        *uchardata++ = start;
3540        }
3541    #endif
3542    
3543      *uchardptr = uchardata;   /* Updata extra data pointer */
3544      }
3545    
3546    return n8;    /* Number of 8-bit characters */
3547    }
3548    
3549    
3550    
3551    
3552    /*************************************************
3553    *        Add a list of characters to a class     *
3554    *************************************************/
3555    
3556    /* This function is used for adding a list of case-equivalent characters to a
3557    class, and also for adding a list of horizontal or vertical whitespace. If the
3558    list is in order (which it should be), ranges of characters are detected and
3559    handled appropriately. This function is mutually recursive with the function
3560    above.
3561    
3562    Arguments:
3563      classbits     the bit map for characters < 256
3564      uchardptr     points to the pointer for extra data
3565      options       the options word
3566      cd            contains pointers to tables etc.
3567      p             points to row of 32-bit values, terminated by NOTACHAR
3568      except        character to omit; this is used when adding lists of
3569                      case-equivalent characters to avoid including the one we
3570                      already know about
3571    
3572    Returns:        the number of < 256 characters added
3573                    the pointer to extra data is updated
3574    */
3575    
3576    static int
3577    add_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr, int options,
3578      compile_data *cd, const pcre_uint32 *p, unsigned int except)
3579    {
3580    int n8 = 0;
3581    while (p[0] < NOTACHAR)
3582      {
3583      int n = 0;
3584      if (p[0] != except)
3585        {
3586        while(p[n+1] == p[0] + n + 1) n++;
3587        n8 += add_to_class(classbits, uchardptr, options, cd, p[0], p[n]);
3588        }
3589      p += n + 1;
3590      }
3591    return n8;
3592    }
3593    
3594    
3595    
3596    /*************************************************
3597    *    Add characters not in a list to a class     *
3598    *************************************************/
3599    
3600    /* This function is used for adding the complement of a list of horizontal or
3601    vertical whitespace to a class. The list must be in order.
3602    
3603    Arguments:
3604      classbits     the bit map for characters < 256
3605      uchardptr     points to the pointer for extra data
3606      options       the options word
3607      cd            contains pointers to tables etc.
3608      p             points to row of 32-bit values, terminated by NOTACHAR
3609    
3610    Returns:        the number of < 256 characters added
3611                    the pointer to extra data is updated
3612    */
3613    
3614    static int
3615    add_not_list_to_class(pcre_uint8 *classbits, pcre_uchar **uchardptr,
3616      int options, compile_data *cd, const pcre_uint32 *p)
3617    {
3618    int n8 = 0;
3619    if (p[0] > 0)
3620      n8 += add_to_class(classbits, uchardptr, options, cd, 0, p[0] - 1);
3621    while (p[0] < NOTACHAR)
3622      {
3623      while (p[1] == p[0] + 1) p++;
3624      n8 += add_to_class(classbits, uchardptr, options, cd, p[0] + 1,
3625        (p[1] == NOTACHAR)? 0x10ffff : p[1] - 1);
3626      p++;
3627      }
3628    return n8;
3629    }
3630    
3631    
3632    
3633    /*************************************************
3634  *           Compile one branch                   *  *           Compile one branch                   *
3635  *************************************************/  *************************************************/
3636    
# Line 3248  Arguments: Line 3645  Arguments:
3645    codeptr        points to the pointer to the current code point    codeptr        points to the pointer to the current code point
3646    ptrptr         points to the current pattern pointer    ptrptr         points to the current pattern pointer
3647    errorcodeptr   points to error code variable    errorcodeptr   points to error code variable
3648    firstbyteptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)    firstcharptr   set to initial literal character, or < 0 (REQ_UNSET, REQ_NONE)
3649    reqbyteptr     set to the last literal character required, else < 0    reqcharptr     set to the last literal character required, else < 0
3650    bcptr          points to current branch chain    bcptr          points to current branch chain
3651    cond_depth     conditional nesting depth    cond_depth     conditional nesting depth
3652    cd             contains pointers to tables etc.    cd             contains pointers to tables etc.
# Line 3261  Returns:         TRUE on success Line 3658  Returns:         TRUE on success
3658  */  */
3659    
3660  static BOOL  static BOOL
3661  compile_branch(int *optionsptr, uschar **codeptr, const uschar **ptrptr,  compile_branch(int *optionsptr, pcre_uchar **codeptr,
3662    int *errorcodeptr, int *firstbyteptr, int *reqbyteptr, branch_chain *bcptr,    const pcre_uchar **ptrptr, int *errorcodeptr, pcre_int32 *firstcharptr,
3663    int cond_depth, compile_data *cd, int *lengthptr)    pcre_int32 *reqcharptr, branch_chain *bcptr, int cond_depth,
3664      compile_data *cd, int *lengthptr)
3665  {  {
3666  int repeat_type, op_type;  int repeat_type, op_type;
3667  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */  int repeat_min = 0, repeat_max = 0;      /* To please picky compilers */
3668  int bravalue = 0;  int bravalue = 0;
3669  int greedy_default, greedy_non_default;  int greedy_default, greedy_non_default;
3670  int firstbyte, reqbyte;  pcre_int32 firstchar, reqchar;
3671  int zeroreqbyte, zerofirstbyte;  pcre_int32 zeroreqchar, zerofirstchar;
3672  int req_caseopt, reqvary, tempreqvary;  pcre_int32 req_caseopt, reqvary, tempreqvary;
3673  int options = *optionsptr;               /* May change dynamically */  int options = *optionsptr;               /* May change dynamically */
3674  int after_manual_callout = 0;  int after_manual_callout = 0;
3675  int length_prevgroup = 0;  int length_prevgroup = 0;
3676  register int c;  register int c;
3677  register uschar *code = *codeptr;  register pcre_uchar *code = *codeptr;
3678  uschar *last_code = code;  pcre_uchar *last_code = code;
3679  uschar *orig_code = code;  pcre_uchar *orig_code = code;
3680  uschar *tempcode;  pcre_uchar *tempcode;
3681  BOOL inescq = FALSE;  BOOL inescq = FALSE;
3682  BOOL groupsetfirstbyte = FALSE;  BOOL groupsetfirstchar = FALSE;
3683  const uschar *ptr = *ptrptr;  const pcre_uchar *ptr = *ptrptr;
3684  const uschar *tempptr;  const pcre_uchar *tempptr;
3685  const uschar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
3686  uschar *previous = NULL;  pcre_uchar *previous = NULL;
3687  uschar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
3688  uschar *save_hwm = NULL;  pcre_uchar *save_hwm = NULL;
3689  uschar classbits[32];  pcre_uint8 classbits[32];
3690    
3691  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
3692  must not do this for other options (e.g. PCRE_EXTENDED) because they may change  must not do this for other options (e.g. PCRE_EXTENDED) because they may change
3693  dynamically as we process the pattern. */  dynamically as we process the pattern. */
3694    
3695  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3696  BOOL class_utf8;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
3697  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf = (options & PCRE_UTF8) != 0;
3698  uschar *class_utf8data;  pcre_uchar utf_chars[6];
 uschar *class_utf8data_base;  
 uschar utf8_char[6];  
3699  #else  #else
3700  BOOL utf8 = FALSE;  BOOL utf = FALSE;
3701    #endif
3702    
3703    /* Helper variables for OP_XCLASS opcode (for characters > 255). We define
3704    class_uchardata always so that it can be passed to add_to_class() always,
3705    though it will not be used in non-UTF 8-bit cases. This avoids having to supply
3706    alternative calls for the different cases. */
3707    
3708    pcre_uchar *class_uchardata;
3709    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3710    BOOL xclass;
3711    pcre_uchar *class_uchardata_base;
3712  #endif  #endif
3713    
3714  #ifdef PCRE_DEBUG  #ifdef PCRE_DEBUG
# Line 3315  greedy_non_default = greedy_default ^ 1; Line 3722  greedy_non_default = greedy_default ^ 1;
3722    
3723  /* Initialize no first byte, no required byte. REQ_UNSET means "no char  /* Initialize no first byte, no required byte. REQ_UNSET means "no char
3724  matching encountered yet". It gets changed to REQ_NONE if we hit something that  matching encountered yet". It gets changed to REQ_NONE if we hit something that
3725  matches a non-fixed char first char; reqbyte just remains unset if we never  matches a non-fixed char first char; reqchar just remains unset if we never
3726  find one.  find one.
3727    
3728  When we hit a repeat whose minimum is zero, we may have to adjust these values  When we hit a repeat whose minimum is zero, we may have to adjust these values
3729  to take the zero repeat into account. This is implemented by setting them to  to take the zero repeat into account. This is implemented by setting them to
3730  zerofirstbyte and zeroreqbyte when such a repeat is encountered. The individual  zerofirstbyte and zeroreqchar when such a repeat is encountered. The individual
3731  item types that can be repeated set these backoff variables appropriately. */  item types that can be repeated set these backoff variables appropriately. */
3732    
3733  firstbyte = reqbyte = zerofirstbyte = zeroreqbyte = REQ_UNSET;  firstchar = reqchar = zerofirstchar = zeroreqchar = REQ_UNSET;
3734    
3735  /* The variable req_caseopt contains either the REQ_CASELESS value or zero,  /* The variable req_caseopt contains either the REQ_CASELESS value
3736  according to the current setting of the caseless flag. REQ_CASELESS is a bit  or zero, according to the current setting of the caseless flag. The
3737  value > 255. It is added into the firstbyte or reqbyte variables to record the  REQ_CASELESS leaves the lower 28 bit empty. It is added into the
3738  case status of the value. This is used only for ASCII characters. */  firstchar or reqchar variables to record the case status of the
3739    value. This is used only for ASCII characters. */
3740    
3741  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS : 0;  req_caseopt = ((options & PCRE_CASELESS) != 0)? REQ_CASELESS:0;
3742    
3743  /* Switch on next character until the end of the branch */  /* Switch on next character until the end of the branch */
3744    
# Line 3342  for (;; ptr++) Line 3750  for (;; ptr++)
3750    BOOL is_quantifier;    BOOL is_quantifier;
3751    BOOL is_recurse;    BOOL is_recurse;
3752    BOOL reset_bracount;    BOOL reset_bracount;
3753    int class_charcount;    int class_has_8bitchar;
3754    int class_lastchar;    int class_one_char;
3755    int newoptions;    int newoptions;
3756    int recno;    int recno;
3757    int refsign;    int refsign;
3758    int skipbytes;    int skipbytes;
3759    int subreqbyte;    int subreqchar;
3760    int subfirstbyte;    int subfirstchar;
3761    int terminator;    int terminator;
3762    int mclength;    int mclength;
3763    int tempbracount;    int tempbracount;
3764    uschar mcbuffer[8];    pcre_uchar mcbuffer[8];
3765    
3766    /* Get next byte in the pattern */    /* Get next character in the pattern */
3767    
3768    c = *ptr;    c = *ptr;
3769    
# Line 3401  for (;; ptr++) Line 3809  for (;; ptr++)
3809        }        }
3810    
3811      *lengthptr += (int)(code - last_code);      *lengthptr += (int)(code - last_code);
3812      DPRINTF(("length=%d added %d c=%c\n", *lengthptr, (int)(code - last_code),      DPRINTF(("length=%d added %d c=%c (0x%x)\n", *lengthptr,
3813        c));        (int)(code - last_code), c, c));
3814    
3815      /* If "previous" is set and it is not at the start of the work space, move      /* If "previous" is set and it is not at the start of the work space, move
3816      it back to there, in order to avoid filling up the work space. Otherwise,      it back to there, in order to avoid filling up the work space. Otherwise,
# Line 3412  for (;; ptr++) Line 3820  for (;; ptr++)
3820        {        {
3821        if (previous > orig_code)        if (previous > orig_code)
3822          {          {
3823          memmove(orig_code, previous, code - previous);          memmove(orig_code, previous, IN_UCHARS(code - previous));
3824          code -= previous - orig_code;          code -= previous - orig_code;
3825          previous = orig_code;          previous = orig_code;
3826          }          }
# Line 3481  for (;; ptr++) Line 3889  for (;; ptr++)
3889    
3890    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
3891      {      {
3892      if ((cd->ctypes[c] & ctype_space) != 0) continue;      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;
3893      if (c == CHAR_NUMBER_SIGN)      if (c == CHAR_NUMBER_SIGN)
3894        {        {
3895        ptr++;        ptr++;
# Line 3489  for (;; ptr++) Line 3897  for (;; ptr++)
3897          {          {
3898          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }
3899          ptr++;          ptr++;
3900  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
3901          if (utf8) while ((*ptr & 0xc0) == 0x80) ptr++;          if (utf) FORWARDCHAR(ptr);
3902  #endif  #endif
3903          }          }
3904        if (*ptr != 0) continue;        if (*ptr != 0) continue;
# Line 3514  for (;; ptr++) Line 3922  for (;; ptr++)
3922      case 0:                        /* The branch terminates at string end */      case 0:                        /* The branch terminates at string end */
3923      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
3924      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
3925      *firstbyteptr = firstbyte;      *firstcharptr = firstchar;
3926      *reqbyteptr = reqbyte;      *reqcharptr = reqchar;
3927      *codeptr = code;      *codeptr = code;
3928      *ptrptr = ptr;      *ptrptr = ptr;
3929      if (lengthptr != NULL)      if (lengthptr != NULL)
# Line 3539  for (;; ptr++) Line 3947  for (;; ptr++)
3947      previous = NULL;      previous = NULL;
3948      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
3949        {        {
3950        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3951        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
3952        }        }
3953      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 3551  for (;; ptr++) Line 3959  for (;; ptr++)
3959      break;      break;
3960    
3961      /* There can never be a first char if '.' is first, whatever happens about      /* There can never be a first char if '.' is first, whatever happens about
3962      repeats. The value of reqbyte doesn't change either. */      repeats. The value of reqchar doesn't change either. */
3963    
3964      case CHAR_DOT:      case CHAR_DOT:
3965      if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;      if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
3966      zerofirstbyte = firstbyte;      zerofirstchar = firstchar;
3967      zeroreqbyte = reqbyte;      zeroreqchar = reqchar;
3968      previous = code;      previous = code;
3969      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;      *code++ = ((options & PCRE_DOTALL) != 0)? OP_ALLANY: OP_ANY;
3970      break;      break;
# Line 3611  for (;; ptr++) Line 4019  for (;; ptr++)
4019          {          {
4020          if (ptr[1] == CHAR_E)          if (ptr[1] == CHAR_E)
4021            ptr++;            ptr++;
4022          else if (strncmp((const char *)ptr+1,          else if (STRNCMP_UC_C8(ptr + 1, STR_Q STR_BACKSLASH STR_E, 3) == 0)
                           STR_Q STR_BACKSLASH STR_E, 3) == 0)  
4023            ptr += 3;            ptr += 3;
4024          else          else
4025            break;            break;
# Line 3631  for (;; ptr++) Line 4038  for (;; ptr++)
4038          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)          (cd->external_options & PCRE_JAVASCRIPT_COMPAT) != 0)
4039        {        {
4040        *code++ = negate_class? OP_ALLANY : OP_FAIL;        *code++ = negate_class? OP_ALLANY : OP_FAIL;
4041        if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4042        zerofirstbyte = firstbyte;        zerofirstchar = firstchar;
4043        break;        break;
4044        }        }
4045    
# Line 3642  for (;; ptr++) Line 4049  for (;; ptr++)
4049    
4050      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4051    
4052      /* Keep a count of chars with values < 256 so that we can optimize the case      /* For optimization purposes, we track some properties of the class:
4053      of just a single character (as long as it's < 256). However, For higher      class_has_8bitchar will be non-zero if the class contains at least one <
4054      valued UTF-8 characters, we don't yet do any optimization. */      256 character; class_one_char will be 1 if the class contains just one
4055        character. */
4056    
4057      class_charcount = 0;      class_has_8bitchar = 0;
4058      class_lastchar = -1;      class_one_char = 0;
4059    
4060      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
4061      temporary bit of memory, in case the class contains only 1 character (less      temporary bit of memory, in case the class contains fewer than two
4062      than 256), because in that case the compiled code doesn't use the bit map.      8-bit characters because in that case the compiled code doesn't use the bit
4063      */      map. */
4064    
4065      memset(classbits, 0, 32 * sizeof(uschar));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4066    
4067  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4068      class_utf8 = FALSE;                       /* No chars >= 256 */      xclass = FALSE;
4069      class_utf8data = code + LINK_SIZE + 2;    /* For UTF-8 items */      class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4070      class_utf8data_base = class_utf8data;     /* For resetting in pass 1 */      class_uchardata_base = class_uchardata;   /* Save the start */
4071  #endif  #endif
4072    
4073      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
# Line 3668  for (;; ptr++) Line 4076  for (;; ptr++)
4076    
4077      if (c != 0) do      if (c != 0) do
4078        {        {
4079        const uschar *oldptr;        const pcre_uchar *oldptr;
4080    
4081  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4082        if (utf8 && c > 127)        if (utf && HAS_EXTRALEN(c))
4083          {                           /* Braces are required because the */          {                           /* Braces are required because the */
4084          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */          GETCHARLEN(c, ptr, ptr);    /* macro generates multiple statements */
4085          }          }
4086    #endif
4087    
4088        /* In the pre-compile phase, accumulate the length of any UTF-8 extra  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4089          /* In the pre-compile phase, accumulate the length of any extra
4090        data and reset the pointer. This is so that very large classes that        data and reset the pointer. This is so that very large classes that
4091        contain a zillion UTF-8 characters no longer overwrite the work space        contain a zillion > 255 characters no longer overwrite the work space
4092        (which is on the stack). */        (which is on the stack). We have to remember that there was XCLASS data,
4093          however. */
4094        if (lengthptr != NULL)  
4095          {        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4096          *lengthptr += (int)(class_utf8data - class_utf8data_base);          {
4097          class_utf8data = class_utf8data_base;          xclass = TRUE;
4098            *lengthptr += class_uchardata - class_uchardata_base;
4099            class_uchardata = class_uchardata_base;
4100          }          }
   
4101  #endif  #endif
4102    
4103        /* Inside \Q...\E everything is literal except \E */        /* Inside \Q...\E everything is literal except \E */
# Line 3714  for (;; ptr++) Line 4125  for (;; ptr++)
4125          {          {
4126          BOOL local_negate = FALSE;          BOOL local_negate = FALSE;
4127          int posix_class, taboffset, tabopt;          int posix_class, taboffset, tabopt;
4128          register const uschar *cbits = cd->cbits;          register const pcre_uint8 *cbits = cd->cbits;
4129          uschar pbits[32];          pcre_uint8 pbits[32];
4130    
4131          if (ptr[1] != CHAR_COLON)          if (ptr[1] != CHAR_COLON)
4132            {            {
# Line 3742  for (;; ptr++) Line 4153  for (;; ptr++)
4153          alpha. This relies on the fact that the class table starts with          alpha. This relies on the fact that the class table starts with
4154          alpha, lower, upper as the first 3 entries. */          alpha, lower, upper as the first 3 entries. */
4155    
4156          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)          if ((options & PCRE_CASELESS) != 0 && posix_class <= 2)
4157            posix_class = 0;            posix_class = 0;
4158    
4159          /* When PCRE_UCP is set, some of the POSIX classes are converted to          /* When PCRE_UCP is set, some of the POSIX classes are converted to
# Line 3770  for (;; ptr++) Line 4181  for (;; ptr++)
4181          /* Copy in the first table (always present) */          /* Copy in the first table (always present) */
4182    
4183          memcpy(pbits, cbits + posix_class_maps[posix_class],          memcpy(pbits, cbits + posix_class_maps[posix_class],
4184            32 * sizeof(uschar));            32 * sizeof(pcre_uint8));
4185    
4186          /* If there is a second table, add or remove it as required. */          /* If there is a second table, add or remove it as required. */
4187    
# Line 3785  for (;; ptr++) Line 4196  for (;; ptr++)
4196              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];              for (c = 0; c < 32; c++) pbits[c] &= ~cbits[c + taboffset];
4197            }            }
4198    
4199          /* Not see if we need to remove any special characters. An option          /* Now see if we need to remove any special characters. An option
4200          value of 1 removes vertical space and 2 removes underscore. */          value of 1 removes vertical space and 2 removes underscore. */
4201    
4202          if (tabopt < 0) tabopt = -tabopt;          if (tabopt < 0) tabopt = -tabopt;
# Line 3801  for (;; ptr++) Line 4212  for (;; ptr++)
4212            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];            for (c = 0; c < 32; c++) classbits[c] |= pbits[c];
4213    
4214          ptr = tempptr + 1;          ptr = tempptr + 1;
4215          class_charcount = 10;  /* Set > 1; assumes more than 1 per class */          /* Every class contains at least one < 256 character. */
4216            class_has_8bitchar = 1;
4217            /* Every class contains at least two characters. */
4218            class_one_char = 2;
4219          continue;    /* End of POSIX syntax handling */          continue;    /* End of POSIX syntax handling */
4220          }          }
4221    
4222        /* Backslash may introduce a single character, or it may introduce one        /* Backslash may introduce a single character, or it may introduce one
4223        of the specials, which just set a flag. The sequence \b is a special        of the specials, which just set a flag. The sequence \b is a special
4224        case. Inside a class (and only there) it is treated as backspace. We        case. Inside a class (and only there) it is treated as backspace. We
4225        assume that other escapes have more than one character in them, so set        assume that other escapes have more than one character in them, so
4226        class_charcount bigger than one. Unrecognized escapes fall through and        speculatively set both class_has_8bitchar and class_one_char bigger
4227        are either treated as literal characters (by default), or are faulted if        than one. Unrecognized escapes fall through and are either treated
4228          as literal characters (by default), or are faulted if
4229        PCRE_EXTRA is set. */        PCRE_EXTRA is set. */
4230    
4231        if (c == CHAR_BACKSLASH)        if (c == CHAR_BACKSLASH)
# Line 3837  for (;; ptr++) Line 4252  for (;; ptr++)
4252    
4253          if (c < 0)          if (c < 0)
4254            {            {
4255            register const uschar *cbits = cd->cbits;            register const pcre_uint8 *cbits = cd->cbits;
4256            class_charcount += 2;     /* Greater than 1 is what matters */            /* Every class contains at least two < 256 characters. */
4257              class_has_8bitchar++;
4258              /* Every class contains at least two characters. */
4259              class_one_char += 2;
4260    
4261            switch (-c)            switch (-c)
4262              {              {
# Line 3851  for (;; ptr++) Line 4269  for (;; ptr++)
4269              case ESC_SU:              case ESC_SU:
4270              nestptr = ptr;              nestptr = ptr;
4271              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */              ptr = substitutes[-c - ESC_DU] - 1;  /* Just before substitute */
4272              class_charcount -= 2;                /* Undo! */              class_has_8bitchar--;                /* Undo! */
4273              continue;              continue;
4274  #endif  #endif
4275              case ESC_d:              case ESC_d:
# Line 3874  for (;; ptr++) Line 4292  for (;; ptr++)
4292    
4293              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omits VT from \s, but we must preserve it
4294              if it was previously set by something earlier in the character              if it was previously set by something earlier in the character
4295              class. */              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and
4296                EBCDIC, so we lazily just adjust the appropriate bit. */
4297    
4298              case ESC_s:              case ESC_s:
4299              classbits[0] |= cbits[cbit_space];              classbits[0] |= cbits[cbit_space];
# Line 3887  for (;; ptr++) Line 4306  for (;; ptr++)
4306              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
4307              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */              classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */
4308              continue;              continue;
4309    
4310                /* The rest apply in both UCP and non-UCP cases. */
4311    
4312              case ESC_h:              case ESC_h:
4313              SETBIT(classbits, 0x09); /* VT */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4314              SETBIT(classbits, 0x20); /* SPACE */                PRIV(hspace_list), NOTACHAR);
             SETBIT(classbits, 0xa0); /* NSBP */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x1680, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x180e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2000, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x200A, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x202f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x205f, class_utf8data);  
               *class_utf8data++ = XCL_SINGLE;  
               class_utf8data += _pcre_ord2utf8(0x3000, class_utf8data);  
               }  
 #endif  
4315              continue;              continue;
4316    
4317              case ESC_H:              case ESC_H:
4318              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4319                {                cd, PRIV(hspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x09/8: x ^= 1 << (0x09%8); break;  
                 case 0x20/8: x ^= 1 << (0x20%8); break;  
                 case 0xa0/8: x ^= 1 << (0xa0%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x167f, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x1681, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x180d, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x180f, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x1fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x200B, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x202e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2030, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x205e, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2060, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2fff, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x3001, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4320              continue;              continue;
4321    
4322              case ESC_v:              case ESC_v:
4323              SETBIT(classbits, 0x0a); /* LF */              (void)add_list_to_class(classbits, &class_uchardata, options, cd,
4324              SETBIT(classbits, 0x0b); /* VT */                PRIV(vspace_list), NOTACHAR);
             SETBIT(classbits, 0x0c); /* FF */  
             SETBIT(classbits, 0x0d); /* CR */  
             SETBIT(classbits, 0x85); /* NEL */  
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2028, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               }  
 #endif  
4325              continue;              continue;
4326    
4327              case ESC_V:              case ESC_V:
4328              for (c = 0; c < 32; c++)              (void)add_not_list_to_class(classbits, &class_uchardata, options,
4329                {                cd, PRIV(vspace_list));
               int x = 0xff;  
               switch (c)  
                 {  
                 case 0x0a/8: x ^= 1 << (0x0a%8);  
                              x ^= 1 << (0x0b%8);  
                              x ^= 1 << (0x0c%8);  
                              x ^= 1 << (0x0d%8);  
                              break;  
                 case 0x85/8: x ^= 1 << (0x85%8); break;  
                 default: break;  
                 }  
               classbits[c] |= x;  
               }  
   
 #ifdef SUPPORT_UTF8  
             if (utf8)  
               {  
               class_utf8 = TRUE;  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x0100, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x2027, class_utf8data);  
               *class_utf8data++ = XCL_RANGE;  
               class_utf8data += _pcre_ord2utf8(0x2029, class_utf8data);  
               class_utf8data += _pcre_ord2utf8(0x7fffffff, class_utf8data);  
               }  
 #endif  
4330              continue;              continue;
4331    
4332  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
# Line 4012  for (;; ptr++) Line 4337  for (;; ptr++)
4337                int pdata;                int pdata;
4338                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);                int ptype = get_ucp(&ptr, &negated, &pdata, errorcodeptr);
4339                if (ptype < 0) goto FAILED;                if (ptype < 0) goto FAILED;
4340                class_utf8 = TRUE;                *class_uchardata++ = ((-c == ESC_p) != negated)?
               *class_utf8data++ = ((-c == ESC_p) != negated)?  
4341                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
4342                *class_utf8data++ = ptype;                *class_uchardata++ = ptype;
4343                *class_utf8data++ = pdata;                *class_uchardata++ = pdata;
4344                class_charcount -= 2;   /* Not a < 256 character */                class_has_8bitchar--;                /* Undo! */
4345                continue;                continue;
4346                }                }
4347  #endif  #endif
# Line 4031  for (;; ptr++) Line 4355  for (;; ptr++)
4355                *errorcodeptr = ERR7;                *errorcodeptr = ERR7;
4356                goto FAILED;                goto FAILED;
4357                }                }
4358              class_charcount -= 2;  /* Undo the default count from above */              class_has_8bitchar--;    /* Undo the speculative increase. */
4359              c = *ptr;              /* Get the final character and fall through */              class_one_char -= 2;     /* Undo the speculative increase. */
4360                c = *ptr;                /* Get the final character and fall through */
4361              break;              break;
4362              }              }
4363            }            }
4364    
4365          /* Fall through if we have a single character (c >= 0). This may be          /* Fall through if the escape just defined a single character (c >= 0).
4366          greater than 256 in UTF-8 mode. */          This may be greater than 256. */
4367    
4368          }   /* End of backslash handling */          }   /* End of backslash handling */
4369    
4370        /* A single character may be followed by '-' to form a range. However,        /* A character may be followed by '-' to form a range. However, Perl does
4371        Perl does not permit ']' to be the end of the range. A '-' character        not permit ']' to be the end of the range. A '-' character at the end is
4372        at the end is treated as a literal. Perl ignores orphaned \E sequences        treated as a literal. Perl ignores orphaned \E sequences entirely. The
4373        entirely. The code for handling \Q and \E is messy. */        code for handling \Q and \E is messy. */
4374    
4375        CHECK_RANGE:        CHECK_RANGE:
4376        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)        while (ptr[1] == CHAR_BACKSLASH && ptr[2] == CHAR_E)
# Line 4053  for (;; ptr++) Line 4378  for (;; ptr++)
4378          inescq = FALSE;          inescq = FALSE;
4379          ptr += 2;          ptr += 2;
4380          }          }
   
4381        oldptr = ptr;        oldptr = ptr;
4382    
4383        /* Remember \r or \n */        /* Remember if \r or \n were explicitly used */
4384    
4385        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;        if (c == CHAR_CR || c == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4386    
# Line 4079  for (;; ptr++) Line 4403  for (;; ptr++)
4403            inescq = TRUE;            inescq = TRUE;
4404            break;            break;
4405            }            }
4406    
4407            /* Minus (hyphen) at the end of a class is treated as a literal, so put
4408            back the pointer and jump to handle the character that preceded it. */
4409    
4410          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))          if (*ptr == 0 || (!inescq && *ptr == CHAR_RIGHT_SQUARE_BRACKET))
4411            {            {
4412            ptr = oldptr;            ptr = oldptr;
4413            goto LONE_SINGLE_CHARACTER;            goto CLASS_SINGLE_CHARACTER;
4414            }            }
4415    
4416            /* Otherwise, we have a potential range; pick up the next character */
4417    
4418  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4419          if (utf8)          if (utf)
4420            {                           /* Braces are required because the */            {                           /* Braces are required because the */
4421            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */            GETCHARLEN(d, ptr, ptr);    /* macro generates multiple statements */
4422            }            }
# Line 4104  for (;; ptr++) Line 4433  for (;; ptr++)
4433            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);            d = check_escape(&ptr, errorcodeptr, cd->bracount, options, TRUE);
4434            if (*errorcodeptr != 0) goto FAILED;            if (*errorcodeptr != 0) goto FAILED;
4435    
4436            /* \b is backspace; any other special means the '-' was literal */            /* \b is backspace; any other special means the '-' was literal. */
4437    
4438            if (d < 0)            if (d < 0)
4439              {              {
4440              if (d == -ESC_b) d = CHAR_BS; else              if (d == -ESC_b) d = CHAR_BS; else
4441                {                {
4442                ptr = oldptr;                ptr = oldptr;
4443                goto LONE_SINGLE_CHARACTER;  /* A few lines below */                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4444                }                }
4445              }              }
4446            }            }
4447    
4448          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
4449          one-character ranges */          one-character ranges. */
4450    
4451          if (d < c)          if (d < c)
4452            {            {
4453            *errorcodeptr = ERR8;            *errorcodeptr = ERR8;
4454            goto FAILED;            goto FAILED;
4455            }            }
4456            if (d == c) goto CLASS_SINGLE_CHARACTER;  /* A few lines below */
4457    
4458          if (d == c) goto LONE_SINGLE_CHARACTER;  /* A few lines below */          /* We have found a character range, so single character optimizations
4459            cannot be done anymore. Any value greater than 1 indicates that there
4460            is more than one character. */
4461    
4462            class_one_char = 2;
4463    
4464          /* Remember \r or \n */          /* Remember an explicit \r or \n, and add the range to the class. */
4465    
4466          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;          if (d == CHAR_CR || d == CHAR_NL) cd->external_flags |= PCRE_HASCRORLF;
4467    
4468          /* In UTF-8 mode, if the upper limit is > 255, or > 127 for caseless          class_has_8bitchar +=
4469          matching, we have to use an XCLASS with extra data items. Caseless            add_to_class(classbits, &class_uchardata, options, cd, c, d);
4470          matching for characters > 127 is available only if UCP support is  
         available. */  
   
 #ifdef SUPPORT_UTF8  
         if (utf8 && (d > 255 || ((options & PCRE_CASELESS) != 0 && d > 127)))  
           {  
           class_utf8 = TRUE;  
   
           /* With UCP support, we can find the other case equivalents of  
           the relevant characters. There may be several ranges. Optimize how  
           they fit with the basic range. */  
   
 #ifdef SUPPORT_UCP  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             unsigned int occ, ocd;  
             unsigned int cc = c;  
             unsigned int origd = d;  
             while (get_othercase_range(&cc, origd, &occ, &ocd))  
               {  
               if (occ >= (unsigned int)c &&  
                   ocd <= (unsigned int)d)  
                 continue;                          /* Skip embedded ranges */  
   
               if (occ < (unsigned int)c  &&  
                   ocd >= (unsigned int)c - 1)      /* Extend the basic range */  
                 {                                  /* if there is overlap,   */  
                 c = occ;                           /* noting that if occ < c */  
                 continue;                          /* we can't have ocd > d  */  
                 }                                  /* because a subrange is  */  
               if (ocd > (unsigned int)d &&  
                   occ <= (unsigned int)d + 1)      /* always shorter than    */  
                 {                                  /* the basic range.       */  
                 d = ocd;  
                 continue;  
                 }  
   
               if (occ == ocd)  
                 {  
                 *class_utf8data++ = XCL_SINGLE;  
                 }  
               else  
                 {  
                 *class_utf8data++ = XCL_RANGE;  
                 class_utf8data += _pcre_ord2utf8(occ, class_utf8data);  
                 }  
               class_utf8data += _pcre_ord2utf8(ocd, class_utf8data);  
               }  
             }  
 #endif  /* SUPPORT_UCP */  
   
           /* Now record the original range, possibly modified for UCP caseless  
           overlapping ranges. */  
   
           *class_utf8data++ = XCL_RANGE;  
           class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
           class_utf8data += _pcre_ord2utf8(d, class_utf8data);  
   
           /* With UCP support, we are done. Without UCP support, there is no  
           caseless matching for UTF-8 characters > 127; we can use the bit map  
           for the smaller ones. */  
   
 #ifdef SUPPORT_UCP  
           continue;    /* With next character in the class */  
 #else  
           if ((options & PCRE_CASELESS) == 0 || c > 127) continue;  
   
           /* Adjust upper limit and fall through to set up the map */  
   
           d = 127;  
   
 #endif  /* SUPPORT_UCP */  
           }  
 #endif  /* SUPPORT_UTF8 */  
   
         /* We use the bit map for all cases when not in UTF-8 mode; else  
         ranges that lie entirely within 0-127 when there is UCP support; else  
         for partial ranges without UCP support. */  
   
         class_charcount += d - c + 1;  
         class_lastchar = d;  
   
         /* We can save a bit of time by skipping this in the pre-compile. */  
   
         if (lengthptr == NULL) for (; c <= d; c++)  
           {  
           classbits[c/8] |= (1 << (c&7));  
           if ((options & PCRE_CASELESS) != 0)  
             {  
             int uc = cd->fcc[c];           /* flip case */  
             classbits[uc/8] |= (1 << (uc&7));  
             }  
           }  
   
4471          continue;   /* Go get the next char in the class */          continue;   /* Go get the next char in the class */
4472          }          }
4473    
4474        /* Handle a lone single character - we can get here for a normal        /* Handle a single character - we can get here for a normal non-escape
4475        non-escape char, or after \ that introduces a single character or for an        char, or after \ that introduces a single character or for an apparent
4476        apparent range that isn't. */        range that isn't. Only the value 1 matters for class_one_char, so don't
4477          increase it if it is already 2 or more ... just in case there's a class
4478          with a zillion characters in it. */
4479    
4480          CLASS_SINGLE_CHARACTER:
4481          if (class_one_char < 2) class_one_char++;
4482    
4483          /* If class_one_char is 1, we have the first single character in the
4484          class, and there have been no prior ranges, or XCLASS items generated by
4485          escapes. If this is the final character in the class, we can optimize by
4486          turning the item into a 1-character OP_CHAR[I] if it's positive, or
4487          OP_NOT[I] if it's negative. In the positive case, it can cause firstchar
4488          to be set. Otherwise, there can be no first char if this item is first,
4489          whatever repeat count may follow. In the case of reqchar, save the
4490          previous value for reinstating. */
4491    
4492        LONE_SINGLE_CHARACTER:        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
   
       /* Handle a character that cannot go in the bit map */  
   
 #ifdef SUPPORT_UTF8  
       if (utf8 && (c > 255 || ((options & PCRE_CASELESS) != 0 && c > 127)))  
4493          {          {
4494          class_utf8 = TRUE;          ptr++;
4495          *class_utf8data++ = XCL_SINGLE;          zeroreqchar = reqchar;
         class_utf8data += _pcre_ord2utf8(c, class_utf8data);  
4496    
4497  #ifdef SUPPORT_UCP          if (negate_class)
         if ((options & PCRE_CASELESS) != 0)  
4498            {            {
4499            unsigned int othercase;  #ifdef SUPPORT_UCP
4500            if ((othercase = UCD_OTHERCASE(c)) != c)            int d;
4501    #endif
4502              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4503              zerofirstchar = firstchar;
4504    
4505              /* For caseless UTF-8 mode when UCP support is available, check
4506              whether this character has more than one other case. If so, generate
4507              a special OP_NOTPROP item instead of OP_NOTI. */
4508    
4509    #ifdef SUPPORT_UCP
4510              if (utf && (options & PCRE_CASELESS) != 0 &&
4511                  (d = UCD_CASESET(c)) != 0)
4512                {
4513                *code++ = OP_NOTPROP;
4514                *code++ = PT_CLIST;
4515                *code++ = d;
4516                }
4517              else
4518    #endif
4519              /* Char has only one other case, or UCP not available */
4520    
4521              {              {
4522              *class_utf8data++ = XCL_SINGLE;              *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;
4523              class_utf8data += _pcre_ord2utf8(othercase, class_utf8data);  #ifdef SUPPORT_UTF
4524                if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4525                  code += PRIV(ord2utf)(c, code);
4526                else
4527    #endif
4528                  *code++ = c;
4529              }              }
4530    
4531              /* We are finished with this character class */
4532    
4533              goto END_CLASS;
4534            }            }
 #endif  /* SUPPORT_UCP */  
4535    
4536          }          /* For a single, positive character, get the value into mcbuffer, and
4537        else          then we can handle this with the normal one-character code. */
 #endif  /* SUPPORT_UTF8 */  
4538    
4539        /* Handle a single-byte character */  #ifdef SUPPORT_UTF
4540          {          if (utf && c > MAX_VALUE_FOR_SINGLE_CHAR)
4541          classbits[c/8] |= (1 << (c&7));            mclength = PRIV(ord2utf)(c, mcbuffer);
4542          if ((options & PCRE_CASELESS) != 0)          else
4543    #endif
4544            {            {
4545            c = cd->fcc[c];   /* flip case */            mcbuffer[0] = c;
4546            classbits[c/8] |= (1 << (c&7));            mclength = 1;
4547            }            }
4548          class_charcount++;          goto ONE_CHAR;
4549          class_lastchar = c;          }       /* End of 1-char optimization */
4550          }  
4551          /* There is more than one character in the class, or an XCLASS item
4552          has been generated. Add this character to the class. */
4553    
4554          class_has_8bitchar +=
4555            add_to_class(classbits, &class_uchardata, options, cd, c, c);
4556        }        }
4557    
4558      /* Loop until ']' reached. This "while" is the end of the "do" far above.      /* Loop until ']' reached. This "while" is the end of the "do" far above.
# Line 4291  for (;; ptr++) Line 4572  for (;; ptr++)
4572        goto FAILED;        goto FAILED;
4573        }        }
4574    
4575      /* If class_charcount is 1, we saw precisely one character whose value is      /* We will need an XCLASS if data has been placed in class_uchardata. In
4576      less than 256. As long as there were no characters >= 128 and there was no      the second phase this is a sufficient test. However, in the pre-compile
4577      use of \p or \P, in other words, no use of any XCLASS features, we can      phase, class_uchardata gets emptied to prevent workspace overflow, so it
4578      optimize.      only if the very last character in the class needs XCLASS will it contain
4579        anything at this point. For this reason, xclass gets set TRUE above when
4580      In UTF-8 mode, we can optimize the negative case only if there were no      uchar_classdata is emptied, and that's why this code is the way it is here
4581      characters >= 128 because OP_NOT and the related opcodes like OP_NOTSTAR      instead of just doing a test on class_uchardata below. */
4582      operate on single-bytes characters only. This is an historical hangover.  
4583      Maybe one day we can tidy these opcodes to handle multi-byte characters.  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4584        if (class_uchardata > class_uchardata_base) xclass = TRUE;
4585      The optimization throws away the bit map. We turn the item into a  #endif
4586      1-character OP_CHAR[I] if it's positive, or OP_NOT[I] if it's negative.  
4587      Note that OP_NOT[I] does not support multibyte characters. In the positive      /* If this is the first thing in the branch, there can be no first char
4588      case, it can cause firstbyte to be set. Otherwise, there can be no first      setting, whatever the repeat count. Any reqchar setting must remain
4589      char if this item is first, whatever repeat count may follow. In the case      unchanged after any kind of repeat. */
4590      of reqbyte, save the previous value for reinstating. */  
4591        if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
4592  #ifdef SUPPORT_UTF8      zerofirstchar = firstchar;
4593      if (class_charcount == 1 && !class_utf8 &&      zeroreqchar = reqchar;
       (!utf8 || !negate_class || class_lastchar < 128))  
 #else  
     if (class_charcount == 1)  
 #endif  
       {  
       zeroreqbyte = reqbyte;  
   
       /* The OP_NOT[I] opcodes work on one-byte characters only. */  
   
       if (negate_class)  
         {  
         if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
         zerofirstbyte = firstbyte;  
         *code++ = ((options & PCRE_CASELESS) != 0)? OP_NOTI: OP_NOT;  
         *code++ = class_lastchar;  
         break;  
         }  
   
       /* For a single, positive character, get the value into mcbuffer, and  
       then we can handle this with the normal one-character code. */  
   
 #ifdef SUPPORT_UTF8  
       if (utf8 && class_lastchar > 127)  
         mclength = _pcre_ord2utf8(class_lastchar, mcbuffer);  
       else  
 #endif  
         {  
         mcbuffer[0] = class_lastchar;  
         mclength = 1;  
         }  
       goto ONE_CHAR;  
       }       /* End of 1-char optimization */  
   
     /* The general case - not the one-char optimization. If this is the first  
     thing in the branch, there can be no first char setting, whatever the  
     repeat count. Any reqbyte setting must remain unchanged after any kind of  
     repeat. */  
   
     if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;  
     zerofirstbyte = firstbyte;  
     zeroreqbyte = reqbyte;  
4594    
4595      /* If there are characters with values > 255, we have to compile an      /* If there are characters with values > 255, we have to compile an
4596      extended class, with its own opcode, unless there was a negated special      extended class, with its own opcode, unless there was a negated special
# Line 4360  for (;; ptr++) Line 4600  for (;; ptr++)
4600      be listed) there are no characters < 256, we can omit the bitmap in the      be listed) there are no characters < 256, we can omit the bitmap in the
4601      actual compiled code. */      actual compiled code. */
4602    
4603  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4604      if (class_utf8 && (!should_flip_negation || (options & PCRE_UCP) != 0))      if (xclass && (!should_flip_negation || (options & PCRE_UCP) != 0))
4605    #elif !defined COMPILE_PCRE8
4606        if (xclass && !should_flip_negation)
4607    #endif
4608    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4609        {        {
4610        *class_utf8data++ = XCL_END;    /* Marks the end of extra data */        *class_uchardata++ = XCL_END;    /* Marks the end of extra data */
4611        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
4612        code += LINK_SIZE;        code += LINK_SIZE;
4613        *code = negate_class? XCL_NOT : 0;        *code = negate_class? XCL_NOT:0;
4614    
4615        /* If the map is required, move up the extra data to make room for it;        /* If the map is required, move up the extra data to make room for it;
4616        otherwise just move the code pointer to the end of the extra data. */        otherwise just move the code pointer to the end of the extra data. */
4617    
4618        if (class_charcount > 0)        if (class_has_8bitchar > 0)
4619          {          {
4620          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
4621          memmove(code + 32, code, class_utf8data - code);          memmove(code + (32 / sizeof(pcre_uchar)), code,
4622              IN_UCHARS(class_uchardata - code));
4623          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
4624          code = class_utf8data + 32;          code = class_uchardata + (32 / sizeof(pcre_uchar));
4625          }          }
4626        else code = class_utf8data;        else code = class_uchardata;
4627    
4628        /* Now fill in the complete length of the item */        /* Now fill in the complete length of the item */
4629    
# Line 4394  for (;; ptr++) Line 4639  for (;; ptr++)
4639      negating it if necessary. */      negating it if necessary. */
4640    
4641      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;      *code++ = (negate_class == should_flip_negation) ? OP_CLASS : OP_NCLASS;
4642      if (negate_class)      if (lengthptr == NULL)    /* Save time in the pre-compile phase */
       {  
       if (lengthptr == NULL)    /* Save time in the pre-compile phase */  
         for (c = 0; c < 32; c++) code[c] = ~classbits[c];  
       }  
     else  
4643        {        {
4644          if (negate_class)
4645            for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
4646        memcpy(code, classbits, 32);        memcpy(code, classbits, 32);
4647        }        }
4648      code += 32;      code += 32 / sizeof(pcre_uchar);
4649    
4650        END_CLASS:
4651      break;      break;
4652    
4653    
# Line 4440  for (;; ptr++) Line 4684  for (;; ptr++)
4684    
4685      if (repeat_min == 0)      if (repeat_min == 0)
4686        {        {
4687        firstbyte = zerofirstbyte;    /* Adjust for zero repeat */        firstchar = zerofirstchar;    /* Adjust for zero repeat */
4688        reqbyte = zeroreqbyte;        /* Ditto */        reqchar = zeroreqchar;        /* Ditto */
4689        }        }
4690    
4691      /* Remember whether this is a variable length repeat */      /* Remember whether this is a variable length repeat */
# Line 4483  for (;; ptr++) Line 4727  for (;; ptr++)
4727    
4728      if (*previous == OP_RECURSE)      if (*previous == OP_RECURSE)
4729        {        {
4730        memmove(previous + 1 + LINK_SIZE, previous, 1 + LINK_SIZE);        memmove(previous + 1 + LINK_SIZE, previous, IN_UCHARS(1 + LINK_SIZE));
4731        *previous = OP_ONCE;        *previous = OP_ONCE;
4732        PUT(previous, 1, 2 + 2*LINK_SIZE);        PUT(previous, 1, 2 + 2*LINK_SIZE);
4733        previous[2 + 2*LINK_SIZE] = OP_KET;        previous[2 + 2*LINK_SIZE] = OP_KET;
# Line 4504  for (;; ptr++) Line 4748  for (;; ptr++)
4748    
4749      /* Now handle repetition for the different types of item. */      /* Now handle repetition for the different types of item. */
4750    
4751      /* If previous was a character match, abolish the item and generate a      /* If previous was a character or negated character match, abolish the item
4752      repeat item instead. If a char item has a minumum of more than one, ensure      and generate a repeat item instead. If a char item has a minimum of more
4753      that it is set in reqbyte - it might not be if a sequence such as x{3} is      than one, ensure that it is set in reqchar - it might not be if a sequence
4754      the first thing in a branch because the x will have gone into firstbyte      such as x{3} is the first thing in a branch because the x will have gone
4755      instead.  */      into firstchar instead.  */
4756    
4757      if (*previous == OP_CHAR || *previous == OP_CHARI)      if (*previous == OP_CHAR || *previous == OP_CHARI
4758            || *previous == OP_NOT || *previous == OP_NOTI)
4759        {        {
4760        op_type = (*previous == OP_CHAR)? 0 : OP_STARI - OP_STAR;        switch (*previous)
4761            {
4762            default: /* Make compiler happy. */
4763            case OP_CHAR:  op_type = OP_STAR - OP_STAR; break;
4764            case OP_CHARI: op_type = OP_STARI - OP_STAR; break;
4765            case OP_NOT:   op_type = OP_NOTSTAR - OP_STAR; break;
4766            case OP_NOTI:  op_type = OP_NOTSTARI - OP_STAR; break;
4767            }
4768    
4769        /* Deal with UTF-8 characters that take up more than one byte. It's        /* Deal with UTF characters that take up more than one character. It's
4770        easier to write this out separately than try to macrify it. Use c to        easier to write this out separately than try to macrify it. Use c to
4771        hold the length of the character in bytes, plus 0x80 to flag that it's a        hold the length of the character in bytes, plus UTF_LENGTH to flag that
4772        length rather than a small character. */        it's a length rather than a small character. */
4773    
4774  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4775        if (utf8 && (code[-1] & 0x80) != 0)        if (utf && NOT_FIRSTCHAR(code[-1]))
4776          {          {
4777          uschar *lastchar = code - 1;          pcre_uchar *lastchar = code - 1;
4778          while((*lastchar & 0xc0) == 0x80) lastchar--;          BACKCHAR(lastchar);
4779          c = (int)(code - lastchar);     /* Length of UTF-8 character */          c = (int)(code - lastchar);     /* Length of UTF-8 character */
4780          memcpy(utf8_char, lastchar, c); /* Save the char */          memcpy(utf_chars, lastchar, IN_UCHARS(c)); /* Save the char */
4781          c |= 0x80;                      /* Flag c as a length */          c |= UTF_LENGTH;                /* Flag c as a length */
4782          }          }
4783        else        else
4784  #endif  #endif /* SUPPORT_UTF */
   
       /* Handle the case of a single byte - either with no UTF8 support, or  
       with UTF-8 disabled, or for a UTF-8 character < 128. */  
4785    
4786          /* Handle the case of a single charater - either with no UTF support, or
4787          with UTF disabled, or for a single character UTF character. */
4788          {          {
4789          c = code[-1];          c = code[-1];
4790          if (repeat_min > 1) reqbyte = c | req_caseopt | cd->req_varyopt;          if (*previous <= OP_CHARI && repeat_min > 1)
4791              reqchar = c | req_caseopt | cd->req_varyopt;
4792          }          }
4793    
4794        /* If the repetition is unlimited, it pays to see if the next thing on        /* If the repetition is unlimited, it pays to see if the next thing on
# Line 4546  for (;; ptr++) Line 4798  for (;; ptr++)
4798    
4799        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4800            repeat_max < 0 &&            repeat_max < 0 &&
4801            check_auto_possessive(previous, utf8, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4802          {          {
4803          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4804          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 4555  for (;; ptr++) Line 4807  for (;; ptr++)
4807        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
4808        }        }
4809    
     /* If previous was a single negated character ([^a] or similar), we use  
     one of the special opcodes, replacing it. The code is shared with single-  
     character repeats by setting opt_type to add a suitable offset into  
     repeat_type. We can also test for auto-possessification. OP_NOT and OP_NOTI  
     are currently used only for single-byte chars. */  
   
     else if (*previous == OP_NOT || *previous == OP_NOTI)  
       {  
       op_type = ((*previous == OP_NOT)? OP_NOTSTAR : OP_NOTSTARI) - OP_STAR;  
       c = previous[1];  
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf8, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
       goto OUTPUT_SINGLE_REPEAT;  
       }  
   
4810      /* If previous was a character type match (\d or similar), abolish it and      /* If previous was a character type match (\d or similar), abolish it and
4811      create a suitable repeat item. The code is shared with single-character      create a suitable repeat item. The code is shared with single-character
4812      repeats by setting op_type to add a suitable offset into repeat_type. Note      repeats by setting op_type to add a suitable offset into repeat_type. Note
# Line 4584  for (;; ptr++) Line 4816  for (;; ptr++)
4816    
4817      else if (*previous < OP_EODN)      else if (*previous < OP_EODN)
4818        {        {
4819        uschar *oldcode;        pcre_uchar *oldcode;
4820        int prop_type, prop_value;        int prop_type, prop_value;
4821        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
4822        c = *previous;        c = *previous;
4823    
4824        if (!possessive_quantifier &&        if (!possessive_quantifier &&
4825            repeat_max < 0 &&            repeat_max < 0 &&
4826            check_auto_possessive(previous, utf8, ptr + 1, options, cd))            check_auto_possessive(previous, utf, ptr + 1, options, cd))
4827          {          {
4828          repeat_type = 0;    /* Force greedy */          repeat_type = 0;    /* Force greedy */
4829          possessive_quantifier = TRUE;          possessive_quantifier = TRUE;
# Line 4671  for (;; ptr++) Line 4903  for (;; ptr++)
4903          we have to insert the character for the previous code. For a repeated          we have to insert the character for the previous code. For a repeated
4904          Unicode property match, there are two extra bytes that define the          Unicode property match, there are two extra bytes that define the
4905          required property. In UTF-8 mode, long characters have their length in          required property. In UTF-8 mode, long characters have their length in
4906          c, with the 0x80 bit as a flag. */          c, with the UTF_LENGTH bit as a flag. */
4907    
4908          if (repeat_max < 0)          if (repeat_max < 0)
4909            {            {
4910  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4911            if (utf8 && c >= 128)            if (utf && (c & UTF_LENGTH) != 0)
4912              {              {
4913              memcpy(code, utf8_char, c & 7);              memcpy(code, utf_chars, IN_UCHARS(c & 7));
4914              code += c & 7;              code += c & 7;
4915              }              }
4916            else            else
# Line 4700  for (;; ptr++) Line 4932  for (;; ptr++)
4932    
4933          else if (repeat_max != repeat_min)          else if (repeat_max != repeat_min)
4934            {            {
4935  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4936            if (utf8 && c >= 128)            if (utf && (c & UTF_LENGTH) != 0)
4937              {              {
4938              memcpy(code, utf8_char, c & 7);              memcpy(code, utf_chars, IN_UCHARS(c & 7));
4939              code += c & 7;              code += c & 7;
4940              }              }
4941            else            else
# Line 4730  for (;; ptr++) Line 4962  for (;; ptr++)
4962    
4963        /* The character or character type itself comes last in all cases. */        /* The character or character type itself comes last in all cases. */
4964    
4965  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
4966        if (utf8 && c >= 128)        if (utf && (c & UTF_LENGTH) != 0)
4967          {          {
4968          memcpy(code, utf8_char, c & 7);          memcpy(code, utf_chars, IN_UCHARS(c & 7));
4969          code += c & 7;          code += c & 7;
4970          }          }
4971        else        else
# Line 4757  for (;; ptr++) Line 4989  for (;; ptr++)
4989    
4990      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS ||
4991               *previous == OP_NCLASS ||               *previous == OP_NCLASS ||
4992  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4993               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
4994  #endif  #endif
4995               *previous == OP_REF ||               *previous == OP_REF ||
# Line 4806  for (;; ptr++) Line 5038  for (;; ptr++)
5038        {        {
5039        register int i;        register int i;
5040        int len = (int)(code - previous);        int len = (int)(code - previous);
5041        uschar *bralink = NULL;        pcre_uchar *bralink = NULL;
5042        uschar *brazeroptr = NULL;        pcre_uchar *brazeroptr = NULL;
5043    
5044        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so        /* Repeating a DEFINE group is pointless, but Perl allows the syntax, so
5045        we just ignore the repeat. */        we just ignore the repeat. */
# Line 4860  for (;; ptr++) Line 5092  for (;; ptr++)
5092          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
5093            {            {
5094            *code = OP_END;            *code = OP_END;
5095            adjust_recurse(previous, 1, utf8, cd, save_hwm);            adjust_recurse(previous, 1, utf, cd, save_hwm);
5096            memmove(previous+1, previous, len);            memmove(previous + 1, previous, IN_UCHARS(len));
5097            code++;            code++;
5098            if (repeat_max == 0)            if (repeat_max == 0)
5099              {              {
# Line 4884  for (;; ptr++) Line 5116  for (;; ptr++)
5116            {            {
5117            int offset;            int offset;
5118            *code = OP_END;            *code = OP_END;
5119            adjust_recurse(previous, 2 + LINK_SIZE, utf8, cd, save_hwm);            adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);
5120            memmove(previous + 2 + LINK_SIZE, previous, len);            memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
5121            code += 2 + LINK_SIZE;            code += 2 + LINK_SIZE;
5122            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
5123            *previous++ = OP_BRA;            *previous++ = OP_BRA;
# Line 4938  for (;; ptr++) Line 5170  for (;; ptr++)
5170    
5171            else            else
5172              {              {
5173              if (groupsetfirstbyte && reqbyte < 0) reqbyte = firstbyte;              if (groupsetfirstchar && reqchar < 0) reqchar = firstchar;
5174    
5175              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
5176                {                {
5177                uschar *hc;                pcre_uchar *hc;
5178                uschar *this_hwm = cd->hwm;                pcre_uchar *this_hwm = cd->hwm;
5179                memcpy(code, previous, len);                memcpy(code, previous, IN_UCHARS(len));
5180    
5181                while (cd->hwm > cd->start_workspace + cd->workspace_size -                while (cd->hwm > cd->start_workspace + cd->workspace_size -
5182                       WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))                       WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))
# Line 4953  for (;; ptr++) Line 5185  for (;; ptr++)
5185                  int this_offset = this_hwm - cd->start_workspace;                  int this_offset = this_hwm - cd->start_workspace;
5186                  *errorcodeptr = expand_workspace(cd);                  *errorcodeptr = expand_workspace(cd);
5187                  if (*errorcodeptr != 0) goto FAILED;                  if (*errorcodeptr != 0) goto FAILED;
5188                  save_hwm = (uschar *)cd->start_workspace + save_offset;                  save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5189                  this_hwm = (uschar *)cd->start_workspace + this_offset;                  this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5190                  }                  }
5191    
5192                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
# Line 5006  for (;; ptr++) Line 5238  for (;; ptr++)
5238    
5239          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
5240            {            {
5241            uschar *hc;            pcre_uchar *hc;
5242            uschar *this_hwm = cd->hwm;            pcre_uchar *this_hwm = cd->hwm;
5243    
5244            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
5245    
# Line 5023  for (;; ptr++) Line 5255  for (;; ptr++)
5255              PUTINC(code, 0, offset);              PUTINC(code, 0, offset);
5256              }              }
5257    
5258            memcpy(code, previous, len);            memcpy(code, previous, IN_UCHARS(len));
5259    
5260            /* Ensure there is enough workspace for forward references before            /* Ensure there is enough workspace for forward references before
5261            copying them. */            copying them. */
# Line 5035  for (;; ptr++) Line 5267  for (;; ptr++)
5267              int this_offset = this_hwm - cd->start_workspace;              int this_offset = this_hwm - cd->start_workspace;
5268              *errorcodeptr = expand_workspace(cd);              *errorcodeptr = expand_workspace(cd);
5269              if (*errorcodeptr != 0) goto FAILED;              if (*errorcodeptr != 0) goto FAILED;
5270              save_hwm = (uschar *)cd->start_workspace + save_offset;              save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;
5271              this_hwm = (uschar *)cd->start_workspace + this_offset;              this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;
5272              }              }
5273    
5274            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)
# Line 5055  for (;; ptr++) Line 5287  for (;; ptr++)
5287            {            {
5288            int oldlinkoffset;            int oldlinkoffset;
5289            int offset = (int)(code - bralink + 1);            int offset = (int)(code - bralink + 1);
5290            uschar *bra = code - offset;            pcre_uchar *bra = code - offset;
5291            oldlinkoffset = GET(bra, 1);            oldlinkoffset = GET(bra, 1);
5292            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;            bralink = (oldlinkoffset == 0)? NULL : bralink - oldlinkoffset;
5293            *code++ = OP_KET;            *code++ = OP_KET;
# Line 5091  for (;; ptr++) Line 5323  for (;; ptr++)
5323    
5324        else        else
5325          {          {
5326          uschar *ketcode = code - 1 - LINK_SIZE;          pcre_uchar *ketcode = code - 1 - LINK_SIZE;
5327          uschar *bracode = ketcode - GET(ketcode, 1);          pcre_uchar *bracode = ketcode - GET(ketcode, 1);
5328    
5329          /* Convert possessive ONCE brackets to non-capturing */          /* Convert possessive ONCE brackets to non-capturing */
5330    
# Line 5114  for (;; ptr++) Line 5346  for (;; ptr++)
5346    
5347            if (lengthptr == NULL)            if (lengthptr == NULL)
5348              {              {
5349              uschar *scode = bracode;              pcre_uchar *scode = bracode;
5350              do              do
5351                {                {
5352                if (could_be_empty_branch(scode, ketcode, utf8, cd))                if (could_be_empty_branch(scode, ketcode, utf, cd))
5353                  {                  {
5354                  *bracode += OP_SBRA - OP_BRA;                  *bracode += OP_SBRA - OP_BRA;
5355                  break;                  break;
# Line 5140  for (;; ptr++) Line 5372  for (;; ptr++)
5372                {                {
5373                int nlen = (int)(code - bracode);                int nlen = (int)(code - bracode);
5374                *code = OP_END;                *code = OP_END;
5375                adjust_recurse(bracode, 1 + LINK_SIZE, utf8, cd, save_hwm);                adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);
5376                memmove(bracode + 1+LINK_SIZE, bracode, nlen);                memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
5377                code += 1 + LINK_SIZE;                code += 1 + LINK_SIZE;
5378                nlen += 1 + LINK_SIZE;                nlen += 1 + LINK_SIZE;
5379                *bracode = OP_BRAPOS;                *bracode = OP_BRAPOS;
# Line 5210  for (;; ptr++) Line 5442  for (;; ptr++)
5442        int len;        int len;
5443    
5444        if (*tempcode == OP_TYPEEXACT)        if (*tempcode == OP_TYPEEXACT)
5445          tempcode += _pcre_OP_lengths[*tempcode] +          tempcode += PRIV(OP_lengths)[*tempcode] +
5446            ((tempcode[3] == OP_PROP || tempcode[3] == OP_NOTPROP)? 2 : 0);            ((tempcode[1 + IMM2_SIZE] == OP_PROP
5447              || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
5448    
5449        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)
5450          {          {
5451          tempcode += _pcre_OP_lengths[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
5452  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
5453          if (utf8 && tempcode[-1] >= 0xc0)          if (utf && HAS_EXTRALEN(tempcode[-1]))
5454            tempcode += _pcre_utf8_table4[tempcode[-1] & 0x3f];            tempcode += GET_EXTRALEN(tempcode[-1]);
5455  #endif  #endif
5456          }          }
5457    
# Line 5255  for (;; ptr++) Line 5488  for (;; ptr++)
5488    
5489          default:          default:
5490          *code = OP_END;          *code = OP_END;
5491          adjust_recurse(tempcode, 1 + LINK_SIZE, utf8, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);
5492          memmove(tempcode + 1+LINK_SIZE, tempcode, len);          memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
5493          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
5494          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
5495          tempcode[0] = OP_ONCE;          tempcode[0] = OP_ONCE;
# Line 5268  for (;; ptr++) Line 5501  for (;; ptr++)
5501        }        }
5502    
5503      /* In all case we no longer have a previous item. We also set the      /* In all case we no longer have a previous item. We also set the
5504      "follows varying string" flag for subsequently encountered reqbytes if      "follows varying string" flag for subsequently encountered reqchars if
5505      it isn't already set and we have just passed a varying length item. */      it isn't already set and we have just passed a varying length item. */
5506    
5507      END_REPEAT:      END_REPEAT:
# Line 5291  for (;; ptr++) Line 5524  for (;; ptr++)
5524    
5525      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with various "verbs" that can be introduced by '*'. */
5526    
5527      if (*(++ptr) == CHAR_ASTERISK &&      ptr++;
5528           ((cd->ctypes[ptr[1]] & ctype_letter) != 0 || ptr[1] == ':'))      if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
5529             || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
5530        {        {
5531        int i, namelen;        int i, namelen;
5532        int arglen = 0;        int arglen = 0;
5533        const char *vn = verbnames;        const char *vn = verbnames;
5534        const uschar *name = ptr + 1;        const pcre_uchar *name = ptr + 1;
5535        const uschar *arg = NULL;        const pcre_uchar *arg = NULL;
5536        previous = NULL;        previous = NULL;
5537        while ((cd->ctypes[*++ptr] & ctype_letter) != 0) {};        ptr++;
5538          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_letter) != 0) ptr++;
5539        namelen = (int)(ptr - name);        namelen = (int)(ptr - name);
5540    
5541        /* It appears that Perl allows any characters whatsoever, other than        /* It appears that Perl allows any characters whatsoever, other than
# Line 5312  for (;; ptr++) Line 5547  for (;; ptr++)
5547          arg = ++ptr;          arg = ++ptr;
5548          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;          while (*ptr != 0 && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
5549          arglen = (int)(ptr - arg);          arglen = (int)(ptr - arg);
5550            if (arglen > (int)MAX_MARK)
5551              {
5552              *errorcodeptr = ERR75;
5553              goto FAILED;
5554              }
5555          }          }
5556    
5557        if (*ptr != CHAR_RIGHT_PARENTHESIS)        if (*ptr != CHAR_RIGHT_PARENTHESIS)
# Line 5325  for (;; ptr++) Line 5565  for (;; ptr++)
5565        for (i = 0; i < verbcount; i++)        for (i = 0; i < verbcount; i++)
5566          {          {
5567          if (namelen == verbs[i].len &&          if (namelen == verbs[i].len &&
5568              strncmp((char *)name, vn, namelen) == 0)              STRNCMP_UC_C8(name, vn, namelen) == 0)
5569            {            {
5570              int setverb;
5571    
5572            /* Check for open captures before ACCEPT and convert it to            /* Check for open captures before ACCEPT and convert it to
5573            ASSERT_ACCEPT if in an assertion. */            ASSERT_ACCEPT if in an assertion. */
5574    
# Line 5344  for (;; ptr++) Line 5586  for (;; ptr++)
5586                *code++ = OP_CLOSE;                *code++ = OP_CLOSE;
5587                PUT2INC(code, 0, oc->number);                PUT2INC(code, 0, oc->number);
5588                }                }
5589              *code++ = (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;              setverb = *code++ =
5590                  (cd->assert_depth > 0)? OP_ASSERT_ACCEPT : OP_ACCEPT;
5591    
5592              /* Do not set firstbyte after *ACCEPT */              /* Do not set firstchar after *ACCEPT */
5593              if (firstbyte == REQ_UNSET) firstbyte = REQ_NONE;              if (firstchar == REQ_UNSET) firstchar = REQ_NONE;
5594              }              }
5595    
5596            /* Handle other cases with/without an argument */            /* Handle other cases with/without an argument */
# Line 5359  for (;; ptr++) Line 5602  for (;; ptr++)
5602                *errorcodeptr = ERR66;                *errorcodeptr = ERR66;
5603                goto FAILED;                goto FAILED;
5604                }                }
5605              *code = verbs[i].op;              setverb = *code++ = verbs[i].op;
             if (*code++ == OP_THEN) cd->external_flags |= PCRE_HASTHEN;  
5606              }              }
5607    
5608            else            else
# Line 5370  for (;; ptr++) Line 5612  for (;; ptr++)
5612                *errorcodeptr = ERR59;                *errorcodeptr = ERR59;
5613                goto FAILED;                goto FAILED;
5614                }                }
5615              *code = verbs[i].op_arg;              setverb = *code++ = verbs[i].op_arg;
             if (*code++ == OP_THEN_ARG) cd->external_flags |= PCRE_HASTHEN;  
5616              *code++ = arglen;              *code++ = arglen;
5617              memcpy(code, arg, arglen);              memcpy(code, arg, IN_UCHARS(arglen));
5618              code += arglen;              code += arglen;
5619              *code++ = 0;              *code++ = 0;
5620              }              }
5621    
5622              switch (setverb)
5623                {
5624                case OP_THEN:
5625                case OP_THEN_ARG:
5626                cd->external_flags |= PCRE_HASTHEN;
5627                break;
5628    
5629                case OP_PRUNE:
5630                case OP_PRUNE_ARG:
5631                case OP_SKIP:
5632                case OP_SKIP_ARG:
5633                cd->had_pruneorskip = TRUE;
5634                break;
5635                }
5636    
5637            break;  /* Found verb, exit loop */            break;  /* Found verb, exit loop */
5638            }            }
5639    
# Line 5396  for (;; ptr++) Line 5652  for (;; ptr++)
5652        {        {
5653        int i, set, unset, namelen;        int i, set, unset, namelen;
5654        int *optset;        int *optset;
5655        const uschar *name;        const pcre_uchar *name;
5656        uschar *slot;        pcre_uchar *slot;
5657    
5658        switch (*(++ptr))        switch (*(++ptr))
5659          {          {
# Line 5450  for (;; ptr++) Line 5706  for (;; ptr++)
5706            break;            break;
5707    
5708          /* Most other conditions use OP_CREF (a couple change to OP_RREF          /* Most other conditions use OP_CREF (a couple change to OP_RREF
5709          below), and all need to skip 3 bytes at the start of the group. */          below), and all need to skip 1+IMM2_SIZE bytes at the start of the group. */
5710    
5711          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
5712          skipbytes = 3;          skipbytes = 1+IMM2_SIZE;
5713          refsign = -1;          refsign = -1;
5714    
5715          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
# Line 5486  for (;; ptr++) Line 5742  for (;; ptr++)
5742    
5743          /* We now expect to read a name; any thing else is an error */          /* We now expect to read a name; any thing else is an error */
5744    
5745          if ((cd->ctypes[ptr[1]] & ctype_word) == 0)          if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)
5746            {            {
5747            ptr += 1;  /* To get the right offset */            ptr += 1;  /* To get the right offset */
5748            *errorcodeptr = ERR28;            *errorcodeptr = ERR28;
# Line 5497  for (;; ptr++) Line 5753  for (;; ptr++)
5753    
5754          recno = 0;          recno = 0;
5755          name = ++ptr;          name = ++ptr;
5756          while ((cd->ctypes[*ptr] & ctype_word) != 0)          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
5757            {            {
5758            if (recno >= 0)            if (recno >= 0)
5759              recno = ((digitab[*ptr] & ctype_digit) != 0)?              recno = (IS_DIGIT(*ptr))? recno * 10 + *ptr - CHAR_0 : -1;
               recno * 10 + *ptr - CHAR_0 : -1;  
5760            ptr++;            ptr++;
5761            }            }
5762          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
# Line 5549  for (;; ptr++) Line 5804  for (;; ptr++)
5804          slot = cd->name_table;          slot = cd->name_table;
5805          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
5806            {            {
5807            if (strncmp((char *)name, (char *)slot+2, namelen) == 0) break;            if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0) break;
5808            slot += cd->name_entry_size;            slot += cd->name_entry_size;
5809            }            }
5810    
# Line 5565  for (;; ptr++) Line 5820  for (;; ptr++)
5820          /* Search the pattern for a forward reference */          /* Search the pattern for a forward reference */
5821    
5822          else if ((i = find_parens(cd, name, namelen,          else if ((i = find_parens(cd, name, namelen,
5823                          (options & PCRE_EXTENDED) != 0, utf8)) > 0)                          (options & PCRE_EXTENDED) != 0, utf)) > 0)
5824            {            {
5825            PUT2(code, 2+LINK_SIZE, i);            PUT2(code, 2+LINK_SIZE, i);
5826            code[1+LINK_SIZE]++;            code[1+LINK_SIZE]++;
# Line 5591  for (;; ptr++) Line 5846  for (;; ptr++)
5846            recno = 0;            recno = 0;
5847            for (i = 1; i < namelen; i++)            for (i = 1; i < namelen; i++)
5848              {              {
5849              if ((digitab[name[i]] & ctype_digit) == 0)              if (!IS_DIGIT(name[i]))
5850                {                {
5851                *errorcodeptr = ERR15;                *errorcodeptr = ERR15;
5852                goto FAILED;                goto FAILED;
# Line 5606  for (;; ptr++) Line 5861  for (;; ptr++)
5861          /* Similarly, check for the (?(DEFINE) "condition", which is always          /* Similarly, check for the (?(DEFINE) "condition", which is always
5862          false. */          false. */
5863    
5864          else if (namelen == 6 && strncmp((char *)name, STRING_DEFINE, 6) == 0)          else if (namelen == 6 && STRNCMP_UC_C8(name, STRING_DEFINE, 6) == 0)
5865            {            {
5866            code[1+LINK_SIZE] = OP_DEF;            code[1+LINK_SIZE] = OP_DEF;
5867            skipbytes = 1;            skipbytes = 1;
# Line 5669  for (;; ptr++) Line 5924  for (;; ptr++)
5924            break;            break;
5925    
5926            default:                /* Could be name define, else bad */            default:                /* Could be name define, else bad */
5927            if ((cd->ctypes[ptr[1]] & ctype_word) != 0) goto DEFINE_NAME;            if (MAX_255(ptr[1]) && (cd->ctypes[ptr[1]] & ctype_word) != 0)
5928                goto DEFINE_NAME;
5929            ptr++;                  /* Correct offset for error */            ptr++;                  /* Correct offset for error */
5930            *errorcodeptr = ERR24;            *errorcodeptr = ERR24;
5931            goto FAILED;            goto FAILED;
# Line 5691  for (;; ptr++) Line 5947  for (;; ptr++)
5947          *code++ = OP_CALLOUT;          *code++ = OP_CALLOUT;
5948            {            {
5949            int n = 0;            int n = 0;
5950            while ((digitab[*(++ptr)] & ctype_digit) != 0)            ptr++;
5951              n = n * 10 + *ptr - CHAR_0;            while(IS_DIGIT(*ptr))
5952                n = n * 10 + *ptr++ - CHAR_0;
5953            if (*ptr != CHAR_RIGHT_PARENTHESIS)            if (*ptr != CHAR_RIGHT_PARENTHESIS)
5954              {              {
5955              *errorcodeptr = ERR39;              *errorcodeptr = ERR39;
# Line 5737  for (;; ptr++) Line 5994  for (;; ptr++)
5994              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;              CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
5995            name = ++ptr;            name = ++ptr;
5996    
5997            while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;            while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
5998            namelen = (int)(ptr - name);            namelen = (int)(ptr - name);
5999    
6000            /* In the pre-compile phase, just do a syntax check. */            /* In the pre-compile phase, just do a syntax check. */
# Line 5754  for (;; ptr++) Line 6011  for (;; ptr++)
6011                *errorcodeptr = ERR49;                *errorcodeptr = ERR49;
6012                goto FAILED;                goto FAILED;
6013                }                }
6014              if (namelen + 3 > cd->name_entry_size)              if (namelen + IMM2_SIZE + 1 > cd->name_entry_size)
6015                {                {
6016                cd->name_entry_size = namelen + 3;                cd->name_entry_size = namelen + IMM2_SIZE + 1;
6017                if (namelen > MAX_NAME_SIZE)                if (namelen > MAX_NAME_SIZE)
6018                  {                  {
6019                  *errorcodeptr = ERR48;                  *errorcodeptr = ERR48;
# Line 5785  for (;; ptr++) Line 6042  for (;; ptr++)
6042    
6043              for (i = 0; i < cd->names_found; i++)              for (i = 0; i < cd->names_found; i++)
6044                {                {
6045                int crc = memcmp(name, slot+2, namelen);                int crc = memcmp(name, slot+IMM2_SIZE, IN_UCHARS(namelen));
6046                if (crc == 0)                if (crc == 0)
6047                  {                  {
6048                  if (slot[2+namelen] == 0)                  if (slot[IMM2_SIZE+namelen] == 0)
6049                    {                    {
6050                    if (GET2(slot, 0) != cd->bracount + 1 &&                    if (GET2(slot, 0) != cd->bracount + 1 &&
6051                        (options & PCRE_DUPNAMES) == 0)                        (options & PCRE_DUPNAMES) == 0)
# Line 5809  for (;; ptr++) Line 6066  for (;; ptr++)
6066                if (crc < 0)                if (crc < 0)
6067                  {                  {
6068                  memmove(slot + cd->name_entry_size, slot,                  memmove(slot + cd->name_entry_size, slot,
6069                    (cd->names_found - i) * cd->name_entry_size);                    IN_UCHARS((cd->names_found - i) * cd->name_entry_size));
6070                  break;                  break;
6071                  }                  }
6072    
# Line 5823  for (;; ptr++) Line 6080  for (;; ptr++)
6080    
6081              if (!dupname)              if (!dupname)
6082                {                {
6083                uschar *cslot = cd->name_table;                pcre_uchar *cslot = cd->name_table;
6084                for (i = 0; i < cd->names_found; i++)                for (i = 0; i < cd->names_found; i++)
6085                  {                  {
6086                  if (cslot != slot)                  if (cslot != slot)
# Line 5840  for (;; ptr++) Line 6097  for (;; ptr++)
6097                }                }
6098    
6099              PUT2(slot, 0, cd->bracount + 1);              PUT2(slot, 0, cd->bracount + 1);
6100              memcpy(slot + 2, name, namelen);              memcpy(slot + IMM2_SIZE, name, IN_UCHARS(namelen));
6101              slot[2+namelen] = 0;              slot[IMM2_SIZE + namelen] = 0;
6102              }              }
6103            }            }
6104    
# Line 5867  for (;; ptr++) Line 6124  for (;; ptr++)
6124    
6125          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
6126          name = ++ptr;          name = ++ptr;
6127          while ((cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
6128          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
6129    
6130          /* In the pre-compile phase, do a syntax check. We used to just set          /* In the pre-compile phase, do a syntax check. We used to just set
# Line 5879  for (;; ptr++) Line 6136  for (;; ptr++)
6136    
6137          if (lengthptr != NULL)          if (lengthptr != NULL)
6138            {            {
6139            const uschar *temp;            const pcre_uchar *temp;
6140    
6141            if (namelen == 0)            if (namelen == 0)
6142              {              {
# Line 5909  for (;; ptr++) Line 6166  for (;; ptr++)
6166            temp = cd->end_pattern;            temp = cd->end_pattern;
6167            cd->end_pattern = ptr;            cd->end_pattern = ptr;
6168            recno = find_parens(cd, name, namelen,            recno = find_parens(cd, name, namelen,
6169              (options & PCRE_EXTENDED) != 0, utf8);              (options & PCRE_EXTENDED) != 0, utf);
6170            cd->end_pattern = temp;            cd->end_pattern = temp;
6171            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */            if (recno < 0) recno = 0;    /* Forward ref; set dummy number */
6172            }            }
# Line 5924  for (;; ptr++) Line 6181  for (;; ptr++)
6181            slot = cd->name_table;            slot = cd->name_table;
6182            for (i = 0; i < cd->names_found; i++)            for (i = 0; i < cd->names_found; i++)
6183              {              {
6184              if (strncmp((char *)name, (char *)slot+2, namelen) == 0 &&              if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) == 0 &&
6185                  slot[2+namelen] == 0)                  slot[IMM2_SIZE+namelen] == 0)
6186                break;                break;
6187              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6188              }              }
# Line 5936  for (;; ptr++) Line 6193  for (;; ptr++)
6193              }              }
6194            else if ((recno =                /* Forward back reference */            else if ((recno =                /* Forward back reference */
6195                      find_parens(cd, name, namelen,                      find_parens(cd, name, namelen,