/[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 1313 by ph10, Wed Apr 24 12:07:09 2013 UTC revision 1538 by ph10, Sun Mar 29 11:22:24 2015 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-2013 University of Cambridge             Copyright (c) 1997-2014 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 47  supporting internal functions that are n Line 47  supporting internal functions that are n
47  #endif  #endif
48    
49  #define NLBLOCK cd             /* Block containing newline information */  #define NLBLOCK cd             /* Block containing newline information */
50  #define PSSTART start_pattern  /* Field containing processed string start */  #define PSSTART start_pattern  /* Field containing pattern start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing pattern end */
52    
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
# Line 115  kicks in at the same number of forward r Line 115  kicks in at the same number of forward r
115  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)  #define COMPILE_WORK_SIZE (2048*LINK_SIZE)
116  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)  #define COMPILE_WORK_SIZE_MAX (100*COMPILE_WORK_SIZE)
117    
118    /* This value determines the size of the initial vector that is used for
119    remembering named groups during the pre-compile. It is allocated on the stack,
120    but if it is too small, it is expanded using malloc(), in a similar way to the
121    workspace. The value is the number of slots in the list. */
122    
123    #define NAMED_GROUP_LIST_SIZE  20
124    
125  /* The overrun tests check for a slightly smaller size so that they detect the  /* The overrun tests check for a slightly smaller size so that they detect the
126  overrun before it actually does run off the end of the data block. */  overrun before it actually does run off the end of the data block. */
127    
# Line 253  static const verbitem verbs[] = { Line 260  static const verbitem verbs[] = {
260  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
261    
262    
263    /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
264    another regex library. */
265    
266    static const pcre_uchar sub_start_of_word[] = {
267      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
268      CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' };
269    
270    static const pcre_uchar sub_end_of_word[] = {
271      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
272      CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w,
273      CHAR_RIGHT_PARENTHESIS, '\0' };
274    
275    
276  /* Tables of names of POSIX character classes and their lengths. The names are  /* Tables of names of POSIX character classes and their lengths. The names are
277  now all in a single string, to reduce the number of relocations when a shared  now all in a single string, to reduce the number of relocations when a shared
278  library is dynamically loaded. The list of lengths is terminated by a zero  library is dynamically loaded. The list of lengths is terminated by a zero
279  length entry. The first three must be alpha, lower, upper, as this is assumed  length entry. The first three must be alpha, lower, upper, as this is assumed
280  for handling case independence. */  for handling case independence. The indices for graph, print, and punct are
281    needed, so identify them. */
282    
283  static const char posix_names[] =  static const char posix_names[] =
284    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
# Line 268  static const char posix_names[] = Line 289  static const char posix_names[] =
289  static const pcre_uint8 posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
290    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 };
291    
292    #define PC_GRAPH  8
293    #define PC_PRINT  9
294    #define PC_PUNCT 10
295    
296    
297  /* 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
298  base map, with an optional addition or removal of another map. Then, for some  base map, with an optional addition or removal of another map. Then, for some
299  classes, there is some additional tweaking: for [:blank:] the vertical space  classes, there is some additional tweaking: for [:blank:] the vertical space
# Line 295  static const int posix_class_maps[] = { Line 321  static const int posix_class_maps[] = {
321    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
322  };  };
323    
324  /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class  /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
325  substitutes must be in the order of the names, defined above, and there are  Unicode property escapes. */
 both positive and negative cases. NULL means no substitute. */  
326    
327  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
328  static const pcre_uchar string_PNd[]  = {  static const pcre_uchar string_PNd[]  = {
# Line 322  static const pcre_uchar string_pXwd[] = Line 347  static const pcre_uchar string_pXwd[] =
347  static const pcre_uchar *substitutes[] = {  static const pcre_uchar *substitutes[] = {
348    string_PNd,           /* \D */    string_PNd,           /* \D */
349    string_pNd,           /* \d */    string_pNd,           /* \d */
350    string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */    string_PXsp,          /* \S */   /* Xsp is Perl space, but from 8.34, Perl */
351    string_pXsp,          /* \s */    string_pXsp,          /* \s */   /* space and POSIX space are the same. */
352    string_PXwd,          /* \W */    string_PXwd,          /* \W */
353    string_pXwd           /* \w */    string_pXwd           /* \w */
354  };  };
355    
356    /* The POSIX class substitutes must be in the order of the POSIX class names,
357    defined above, and there are both positive and negative cases. NULL means no
358    general substitute of a Unicode property escape (\p or \P). However, for some
359    POSIX classes (e.g. graph, print, punct) a special property code is compiled
360    directly. */
361    
362  static const pcre_uchar string_pL[] =   {  static const pcre_uchar string_pL[] =   {
363    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
364    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
# Line 375  static const pcre_uchar *posix_substitut Line 406  static const pcre_uchar *posix_substitut
406    NULL,                 /* graph */    NULL,                 /* graph */
407    NULL,                 /* print */    NULL,                 /* print */
408    NULL,                 /* punct */    NULL,                 /* punct */
409    string_pXps,          /* space */    /* NOTE: Xps is POSIX space */    string_pXps,          /* space */   /* Xps is POSIX space, but from 8.34 */
410    string_pXwd,          /* word */    string_pXwd,          /* word  */   /* Perl and POSIX space are the same */
411    NULL,                 /* xdigit */    NULL,                 /* xdigit */
412    /* Negated cases */    /* Negated cases */
413    string_PL,            /* ^alpha */    string_PL,            /* ^alpha */
# Line 390  static const pcre_uchar *posix_substitut Line 421  static const pcre_uchar *posix_substitut
421    NULL,                 /* ^graph */    NULL,                 /* ^graph */
422    NULL,                 /* ^print */    NULL,                 /* ^print */
423    NULL,                 /* ^punct */    NULL,                 /* ^punct */
424    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */  /* Xps is POSIX space, but from 8.34 */
425    string_PXwd,          /* ^word */    string_PXwd,          /* ^word */   /* Perl and POSIX space are the same */
426    NULL                  /* ^xdigit */    NULL                  /* ^xdigit */
427  };  };
428  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
# Line 455  static const char error_texts[] = Line 486  static const char error_texts[] =
486    "POSIX collating elements are not supported\0"    "POSIX collating elements are not supported\0"
487    "this version of PCRE is compiled without UTF support\0"    "this version of PCRE is compiled without UTF support\0"
488    "spare error\0"  /** DEAD **/    "spare error\0"  /** DEAD **/
489    "character value in \\x{...} sequence is too large\0"    "character value in \\x{} or \\o{} is too large\0"
490    /* 35 */    /* 35 */
491    "invalid condition (?(0)\0"    "invalid condition (?(0)\0"
492    "\\C not allowed in lookbehind assertion\0"    "\\C not allowed in lookbehind assertion\0"
# Line 509  static const char error_texts[] = Line 540  static const char error_texts[] =
540    "character value in \\u.... sequence is too large\0"    "character value in \\u.... sequence is too large\0"
541    "invalid UTF-32 string\0"    "invalid UTF-32 string\0"
542    "setting UTF is disabled by the application\0"    "setting UTF is disabled by the application\0"
543      "non-hex character in \\x{} (closing brace missing?)\0"
544      /* 80 */
545      "non-octal character in \\o{} (closing brace missing?)\0"
546      "missing opening brace after \\o\0"
547      "parentheses are too deeply nested\0"
548      "invalid range in character class\0"
549      "group name must start with a non-digit\0"
550      /* 85 */
551      "parentheses are too deeply nested (stack check)\0"
552      "digits missing in \\x{} or \\o{}\0"
553    ;    ;
554    
555  /* 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 648  static const pcre_uint8 ebcdic_chartab[] Line 689  static const pcre_uint8 ebcdic_chartab[]
689  #endif  #endif
690    
691    
692    /* This table is used to check whether auto-possessification is possible
693    between adjacent character-type opcodes. The left-hand (repeated) opcode is
694    used to select the row, and the right-hand opcode is use to select the column.
695    A value of 1 means that auto-possessification is OK. For example, the second
696    value in the first row means that \D+\d can be turned into \D++\d.
697    
698    The Unicode property types (\P and \p) have to be present to fill out the table
699    because of what their opcode values are, but the table values should always be
700    zero because property types are handled separately in the code. The last four
701    columns apply to items that cannot be repeated, so there is no need to have
702    rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
703    *not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
704    
705    #define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
706    #define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
707    
708    static const pcre_uint8 autoposstab[APTROWS][APTCOLS] = {
709    /* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
710      { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
711      { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
712      { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
713      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
714      { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
715      { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
716      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
717      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
718      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
719      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
720      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
721      { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
722      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
723      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
724      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
725      { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
726      { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
727    };
728    
729    
730    /* This table is used to check whether auto-possessification is possible
731    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
732    left-hand (repeated) opcode is used to select the row, and the right-hand
733    opcode is used to select the column. The values are as follows:
734    
735      0   Always return FALSE (never auto-possessify)
736      1   Character groups are distinct (possessify if both are OP_PROP)
737      2   Check character categories in the same group (general or particular)
738      3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
739    
740      4   Check left general category vs right particular category
741      5   Check right general category vs left particular category
742    
743      6   Left alphanum vs right general category
744      7   Left space vs right general category
745      8   Left word vs right general category
746    
747      9   Right alphanum vs left general category
748     10   Right space vs left general category
749     11   Right word vs left general category
750    
751     12   Left alphanum vs right particular category
752     13   Left space vs right particular category
753     14   Left word vs right particular category
754    
755     15   Right alphanum vs left particular category
756     16   Right space vs left particular category
757     17   Right word vs left particular category
758    */
759    
760    static const pcre_uint8 propposstab[PT_TABSIZE][PT_TABSIZE] = {
761    /* ANY LAMP GC  PC  SC ALNUM SPACE PXSPACE WORD CLIST UCNC */
762      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_ANY */
763      { 0,  3,  0,  0,  0,    3,    1,      1,   0,    0,   0 },  /* PT_LAMP */
764      { 0,  0,  2,  4,  0,    9,   10,     10,  11,    0,   0 },  /* PT_GC */
765      { 0,  0,  5,  2,  0,   15,   16,     16,  17,    0,   0 },  /* PT_PC */
766      { 0,  0,  0,  0,  2,    0,    0,      0,   0,    0,   0 },  /* PT_SC */
767      { 0,  3,  6, 12,  0,    3,    1,      1,   0,    0,   0 },  /* PT_ALNUM */
768      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_SPACE */
769      { 0,  1,  7, 13,  0,    1,    3,      3,   1,    0,   0 },  /* PT_PXSPACE */
770      { 0,  0,  8, 14,  0,    0,    1,      1,   3,    0,   0 },  /* PT_WORD */
771      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   0 },  /* PT_CLIST */
772      { 0,  0,  0,  0,  0,    0,    0,      0,   0,    0,   3 }   /* PT_UCNC */
773    };
774    
775    /* This table is used to check whether auto-possessification is possible
776    between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
777    specifies a general category and the other specifies a particular category. The
778    row is selected by the general category and the column by the particular
779    category. The value is 1 if the particular category is not part of the general
780    category. */
781    
782    static const pcre_uint8 catposstab[7][30] = {
783    /* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
784      { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
785      { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
786      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
787      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
788      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
789      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
790      { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
791    };
792    
793    /* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
794    a general or particular category. The properties in each row are those
795    that apply to the character set in question. Duplication means that a little
796    unnecessary work is done when checking, but this keeps things much simpler
797    because they can all use the same code. For more details see the comment where
798    this table is used.
799    
800    Note: SPACE and PXSPACE used to be different because Perl excluded VT from
801    "space", but from Perl 5.18 it's included, so both categories are treated the
802    same here. */
803    
804    static const pcre_uint8 posspropstab[3][4] = {
805      { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
806      { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
807      { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
808    };
809    
810    /* This table is used when converting repeating opcodes into possessified
811    versions as a result of an explicit possessive quantifier such as ++. A zero
812    value means there is no possessified version - in those cases the item in
813    question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
814    because all relevant opcodes are less than that. */
815    
816    static const pcre_uint8 opcode_possessify[] = {
817      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0 - 15  */
818      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 16 - 31 */
819    
820      0,                       /* NOTI */
821      OP_POSSTAR, 0,           /* STAR, MINSTAR */
822      OP_POSPLUS, 0,           /* PLUS, MINPLUS */
823      OP_POSQUERY, 0,          /* QUERY, MINQUERY */
824      OP_POSUPTO, 0,           /* UPTO, MINUPTO */
825      0,                       /* EXACT */
826      0, 0, 0, 0,              /* POS{STAR,PLUS,QUERY,UPTO} */
827    
828      OP_POSSTARI, 0,          /* STARI, MINSTARI */
829      OP_POSPLUSI, 0,          /* PLUSI, MINPLUSI */
830      OP_POSQUERYI, 0,         /* QUERYI, MINQUERYI */
831      OP_POSUPTOI, 0,          /* UPTOI, MINUPTOI */
832      0,                       /* EXACTI */
833      0, 0, 0, 0,              /* POS{STARI,PLUSI,QUERYI,UPTOI} */
834    
835      OP_NOTPOSSTAR, 0,        /* NOTSTAR, NOTMINSTAR */
836      OP_NOTPOSPLUS, 0,        /* NOTPLUS, NOTMINPLUS */
837      OP_NOTPOSQUERY, 0,       /* NOTQUERY, NOTMINQUERY */
838      OP_NOTPOSUPTO, 0,        /* NOTUPTO, NOTMINUPTO */
839      0,                       /* NOTEXACT */
840      0, 0, 0, 0,              /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
841    
842      OP_NOTPOSSTARI, 0,       /* NOTSTARI, NOTMINSTARI */
843      OP_NOTPOSPLUSI, 0,       /* NOTPLUSI, NOTMINPLUSI */
844      OP_NOTPOSQUERYI, 0,      /* NOTQUERYI, NOTMINQUERYI */
845      OP_NOTPOSUPTOI, 0,       /* NOTUPTOI, NOTMINUPTOI */
846      0,                       /* NOTEXACTI */
847      0, 0, 0, 0,              /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
848    
849      OP_TYPEPOSSTAR, 0,       /* TYPESTAR, TYPEMINSTAR */
850      OP_TYPEPOSPLUS, 0,       /* TYPEPLUS, TYPEMINPLUS */
851      OP_TYPEPOSQUERY, 0,      /* TYPEQUERY, TYPEMINQUERY */
852      OP_TYPEPOSUPTO, 0,       /* TYPEUPTO, TYPEMINUPTO */
853      0,                       /* TYPEEXACT */
854      0, 0, 0, 0,              /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
855    
856      OP_CRPOSSTAR, 0,         /* CRSTAR, CRMINSTAR */
857      OP_CRPOSPLUS, 0,         /* CRPLUS, CRMINPLUS */
858      OP_CRPOSQUERY, 0,        /* CRQUERY, CRMINQUERY */
859      OP_CRPOSRANGE, 0,        /* CRRANGE, CRMINRANGE */
860      0, 0, 0, 0,              /* CRPOS{STAR,PLUS,QUERY,RANGE} */
861    
862      0, 0, 0,                 /* CLASS, NCLASS, XCLASS */
863      0, 0,                    /* REF, REFI */
864      0, 0,                    /* DNREF, DNREFI */
865      0, 0                     /* RECURSE, CALLOUT */
866    };
867    
868    
869    
870  /*************************************************  /*************************************************
871  *            Find an error text                  *  *            Find an error text                  *
# Line 675  return s; Line 893  return s;
893  }  }
894    
895    
896    
897  /*************************************************  /*************************************************
898  *           Expand the workspace                 *  *           Expand the workspace                 *
899  *************************************************/  *************************************************/
# Line 752  return (*p == CHAR_RIGHT_CURLY_BRACKET); Line 971  return (*p == CHAR_RIGHT_CURLY_BRACKET);
971  *************************************************/  *************************************************/
972    
973  /* This function is called when a \ has been encountered. It either returns a  /* This function is called when a \ has been encountered. It either returns a
974  positive value for a simple escape such as \n, or 0 for a data character  positive value for a simple escape such as \n, or 0 for a data character which
975  which will be placed in chptr. A backreference to group n is returned as  will be placed in chptr. A backreference to group n is returned as negative n.
976  negative n. When UTF-8 is enabled, a positive value greater than 255 may  When UTF-8 is enabled, a positive value greater than 255 may be returned in
977  be returned in chptr.  chptr. On entry, ptr is pointing at the \. On exit, it is on the final
978  On entry,ptr is pointing at the \. On exit, it is on the final character of the  character of the escape sequence.
 escape sequence.  
979    
980  Arguments:  Arguments:
981    ptrptr         points to the pattern position pointer    ptrptr         points to the pattern position pointer
982    chptr          points to the data character    chptr          points to a returned data character
983    errorcodeptr   points to the errorcode variable    errorcodeptr   points to the errorcode variable
984    bracount       number of previous extracting brackets    bracount       number of previous extracting brackets
985    options        the options bits    options        the options bits
# Line 965  else Line 1183  else
1183      break;      break;
1184    
1185      /* The handling of escape sequences consisting of a string of digits      /* The handling of escape sequences consisting of a string of digits
1186      starting with one that is not zero is not straightforward. By experiment,      starting with one that is not zero is not straightforward. Perl has changed
1187      the way Perl works seems to be as follows:      over the years. Nowadays \g{} for backreferences and \o{} for octal are
1188        recommended to avoid the ambiguities in the old syntax.
1189    
1190      Outside a character class, the digits are read as a decimal number. If the      Outside a character class, the digits are read as a decimal number. If the
1191      number is less than 10, or if there are that many previous extracting      number is less than 8 (used to be 10), or if there are that many previous
1192      left brackets, then it is a back reference. Otherwise, up to three octal      extracting left brackets, then it is a back reference. Otherwise, up to
1193      digits are read to form an escaped byte. Thus \123 is likely to be octal      three octal digits are read to form an escaped byte. Thus \123 is likely to
1194      123 (cf \0123, which is octal 012 followed by the literal 3). If the octal      be octal 123 (cf \0123, which is octal 012 followed by the literal 3). If
1195      value is greater than 377, the least significant 8 bits are taken. Inside a      the octal value is greater than 377, the least significant 8 bits are
1196      character class, \ followed by a digit is always an octal number. */      taken. \8 and \9 are treated as the literal characters 8 and 9.
1197    
1198        Inside a character class, \ followed by a digit is always either a literal
1199        8 or 9 or an octal number. */
1200    
1201      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:      case CHAR_1: case CHAR_2: case CHAR_3: case CHAR_4: case CHAR_5:
1202      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:      case CHAR_6: case CHAR_7: case CHAR_8: case CHAR_9:
# Line 1001  else Line 1223  else
1223          *errorcodeptr = ERR61;          *errorcodeptr = ERR61;
1224          break;          break;
1225          }          }
1226        if (s < 10 || s <= bracount)        if (s < 8 || s <= bracount)  /* Check for back reference */
1227          {          {
1228          escape = -s;          escape = -s;
1229          break;          break;
# Line 1009  else Line 1231  else
1231        ptr = oldptr;      /* Put the pointer back and fall through */        ptr = oldptr;      /* Put the pointer back and fall through */
1232        }        }
1233    
1234      /* Handle an octal number following \. If the first digit is 8 or 9, Perl      /* Handle a digit following \ when the number is not a back reference. If
1235      generates a binary zero byte and treats the digit as a following literal.      the first digit is 8 or 9, Perl used to generate a binary zero byte and
1236      Thus we have to pull back the pointer by one. */      then treat the digit as a following literal. At least by Perl 5.18 this
1237        changed so as not to insert the binary zero. */
1238    
1239      if ((c = *ptr) >= CHAR_8)      if ((c = *ptr) >= CHAR_8) break;
1240        {  
1241        ptr--;      /* Fall through with a digit less than 8 */
       c = 0;  
       break;  
       }  
1242    
1243      /* \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
1244      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
# Line 1035  else Line 1255  else
1255  #endif  #endif
1256      break;      break;
1257    
1258      /* \x is complicated. \x{ddd} is a character number which can be greater      /* \o is a relatively new Perl feature, supporting a more general way of
1259      than 0xff in utf or non-8bit mode, but only if the ddd are hex digits.      specifying character codes in octal. The only supported form is \o{ddd}. */
1260      If not, { is treated as a data character. */  
1261        case CHAR_o:
1262        if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1263        if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else
1264          {
1265          ptr += 2;
1266          c = 0;
1267          overflow = FALSE;
1268          while (*ptr >= CHAR_0 && *ptr <= CHAR_7)
1269            {
1270            register pcre_uint32 cc = *ptr++;
1271            if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1272    #ifdef COMPILE_PCRE32
1273            if (c >= 0x20000000l) { overflow = TRUE; break; }
1274    #endif
1275            c = (c << 3) + cc - CHAR_0 ;
1276    #if defined COMPILE_PCRE8
1277            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1278    #elif defined COMPILE_PCRE16
1279            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1280    #elif defined COMPILE_PCRE32
1281            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1282    #endif
1283            }
1284          if (overflow)
1285            {
1286            while (*ptr >= CHAR_0 && *ptr <= CHAR_7) ptr++;
1287            *errorcodeptr = ERR34;
1288            }
1289          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1290            {
1291            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1292            }
1293          else *errorcodeptr = ERR80;
1294          }
1295        break;
1296    
1297        /* \x is complicated. In JavaScript, \x must be followed by two hexadecimal
1298        numbers. Otherwise it is a lowercase x letter. */
1299    
1300      case CHAR_x:      case CHAR_x:
1301      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) != 0)
1302        {        {
       /* In JavaScript, \x must be followed by two hexadecimal numbers.  
       Otherwise it is a lowercase x letter. */  
1303        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0        if (MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0
1304          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)          && MAX_255(ptr[2]) && (digitab[ptr[2]] & ctype_xdigit) != 0)
1305          {          {
# Line 1060  else Line 1316  else
1316  #endif  #endif
1317            }            }
1318          }          }
1319        break;        }    /* End JavaScript handling */
       }  
1320    
1321      if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)      /* Handle \x in Perl's style. \x{ddd} is a character number which can be
1322        {      greater than 0xff in utf or non-8bit mode, but only if the ddd are hex
1323        const pcre_uchar *pt = ptr + 2;      digits. If not, { used to be treated as a data character. However, Perl
1324        seems to read hex digits up to the first non-such, and ignore the rest, so
1325        that, for example \x{zz} matches a binary zero. This seems crazy, so PCRE
1326        now gives an error. */
1327    
1328        c = 0;      else
1329        overflow = FALSE;        {
1330        while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0)        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1331          {          {
1332          register pcre_uint32 cc = *pt++;          ptr += 2;
1333          if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */          if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1334              {
1335              *errorcodeptr = ERR86;
1336              break;
1337              }
1338            c = 0;
1339            overflow = FALSE;
1340            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
1341              {
1342              register pcre_uint32 cc = *ptr++;
1343              if (c == 0 && cc == CHAR_0) continue;     /* Leading zeroes */
1344    
1345  #ifdef COMPILE_PCRE32  #ifdef COMPILE_PCRE32
1346          if (c >= 0x10000000l) { overflow = TRUE; break; }            if (c >= 0x10000000l) { overflow = TRUE; break; }
1347  #endif  #endif
1348    
1349  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1350          if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;               /* Convert to upper case */
1351          c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1352  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1353          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 */
1354          c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = (c << 4) + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1355  #endif  #endif
1356    
1357  #if defined COMPILE_PCRE8  #if defined COMPILE_PCRE8
1358          if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffU)) { overflow = TRUE; break; }
1359  #elif defined COMPILE_PCRE16  #elif defined COMPILE_PCRE16
1360          if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }            if (c > (utf ? 0x10ffffU : 0xffffU)) { overflow = TRUE; break; }
1361  #elif defined COMPILE_PCRE32  #elif defined COMPILE_PCRE32
1362          if (utf && c > 0x10ffffU) { overflow = TRUE; break; }            if (utf && c > 0x10ffffU) { overflow = TRUE; break; }
1363  #endif  #endif
1364          }            }
1365    
1366        if (overflow)          if (overflow)
1367          {            {
1368          while (MAX_255(*pt) && (digitab[*pt] & ctype_xdigit) != 0) pt++;            while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0) ptr++;
1369          *errorcodeptr = ERR34;            *errorcodeptr = ERR34;
1370          }            }
1371    
1372        if (*pt == CHAR_RIGHT_CURLY_BRACKET)          else if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1373          {            {
1374          if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;            if (utf && c >= 0xd800 && c <= 0xdfff) *errorcodeptr = ERR73;
1375          ptr = pt;            }
         break;  
         }  
1376    
1377        /* If the sequence of hex digits does not end with '}', then we don't          /* If the sequence of hex digits does not end with '}', give an error.
1378        recognize this construct; fall through to the normal \x handling. */          We used just to recognize this construct and fall through to the normal
1379        }          \x handling, but nowadays Perl gives an error, which seems much more
1380            sensible, so we do too. */
1381    
1382      /* Read just a single-byte hex-defined char */          else *errorcodeptr = ERR79;
1383            }   /* End of \x{} processing */
1384    
1385      c = 0;        /* Read a single-byte hex-defined char (up to two hex digits after \x) */
1386      while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)  
1387        {        else
1388        pcre_uint32 cc;                          /* Some compilers don't like */          {
1389        cc = *(++ptr);                           /* ++ in initializers */          c = 0;
1390            while (i++ < 2 && MAX_255(ptr[1]) && (digitab[ptr[1]] & ctype_xdigit) != 0)
1391              {
1392              pcre_uint32 cc;                          /* Some compilers don't like */
1393              cc = *(++ptr);                           /* ++ in initializers */
1394  #ifndef EBCDIC  /* ASCII/UTF-8 coding */  #ifndef EBCDIC  /* ASCII/UTF-8 coding */
1395        if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */            if (cc >= CHAR_a) cc -= 32;              /* Convert to upper case */
1396        c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc < CHAR_A)? CHAR_0 : (CHAR_A - 10));
1397  #else           /* EBCDIC coding */  #else           /* EBCDIC coding */
1398        if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */            if (cc <= CHAR_z) cc += 64;              /* Convert to upper case */
1399        c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));            c = c * 16 + cc - ((cc >= CHAR_0)? CHAR_0 : (CHAR_A - 10));
1400  #endif  #endif
1401        }            }
1402            }     /* End of \xdd handling */
1403          }       /* End of Perl-style \x handling */
1404      break;      break;
1405    
1406      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.      /* For \c, a following letter is upper-cased; then the 0x40 bit is flipped.
# Line 1192  if ((options & PCRE_UCP) != 0 && escape Line 1466  if ((options & PCRE_UCP) != 0 && escape
1466  return escape;  return escape;
1467  }  }
1468    
1469    
1470    
1471  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
1472  /*************************************************  /*************************************************
1473  *               Handle \P and \p                 *  *               Handle \P and \p                 *
# Line 1289  return FALSE; Line 1565  return FALSE;
1565    
1566    
1567    
   
1568  /*************************************************  /*************************************************
1569  *         Read repeat counts                     *  *         Read repeat counts                     *
1570  *************************************************/  *************************************************/
# Line 1315  read_repeat_counts(const pcre_uchar *p, Line 1590  read_repeat_counts(const pcre_uchar *p,
1590  int min = 0;  int min = 0;
1591  int max = -1;  int max = -1;
1592    
1593  /* Read the minimum value and do a paranoid check: a negative value indicates  while (IS_DIGIT(*p))
 an integer overflow. */  
   
 while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);  
 if (min < 0 || min > 65535)  
1594    {    {
1595    *errorcodeptr = ERR5;    min = min * 10 + (int)(*p++ - CHAR_0);
1596    return p;    if (min > 65535)
1597        {
1598        *errorcodeptr = ERR5;
1599        return p;
1600        }
1601    }    }
1602    
 /* Read the maximum value if there is one, and again do a paranoid on its size.  
 Also, max must not be less than min. */  
   
1603  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1604    {    {
1605    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1606      {      {
1607      max = 0;      max = 0;
1608      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);      while(IS_DIGIT(*p))
     if (max < 0 || max > 65535)  
1609        {        {
1610        *errorcodeptr = ERR5;        max = max * 10 + (int)(*p++ - CHAR_0);
1611        return p;        if (max > 65535)
1612            {
1613            *errorcodeptr = ERR5;
1614            return p;
1615            }
1616        }        }
1617      if (max < min)      if (max < min)
1618        {        {
# Line 1347  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1622  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1622      }      }
1623    }    }
1624    
 /* Fill in the required variables, and pass back the pointer to the terminating  
 '}'. */  
   
1625  *minp = min;  *minp = min;
1626  *maxp = max;  *maxp = max;
1627  return p;  return p;
# Line 1358  return p; Line 1630  return p;
1630    
1631    
1632  /*************************************************  /*************************************************
 *  Subroutine for finding forward reference      *  
 *************************************************/  
   
 /* This recursive function is called only from find_parens() below. The  
 top-level call starts at the beginning of the pattern. All other calls must  
 start at a parenthesis. It scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. Recursion is used to keep  
 track of subpatterns that reset the capturing group numbers - the (?| feature.  
   
 This function was originally called only from the second pass, in which we know  
 that if (?< or (?' or (?P< is encountered, the name will be correctly  
 terminated because that is checked in the first pass. There is now one call to  
 this function in the first pass, to check for a recursive back reference by  
 name (so that we can make the whole group atomic). In this case, we need check  
 only up to the current position in the pattern, and that is still OK because  
 and previous occurrences will have been checked. To make this work, the test  
 for "end of pattern" is a check against cd->end_pattern in the main loop,  
 instead of looking for a binary zero. This means that the special first-pass  
 call can adjust cd->end_pattern temporarily. (Checks for binary zero while  
 processing items within the loop are OK, because afterwards the main loop will  
 terminate.)  
   
 Arguments:  
   ptrptr       address of the current character pointer (updated)  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf          TRUE if we are in UTF-8 / UTF-16 / UTF-32 mode  
   count        pointer to the current capturing subpattern number (updated)  
   
 Returns:       the number of the named subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens_sub(pcre_uchar **ptrptr, compile_data *cd, const pcre_uchar *name, int lorn,  
   BOOL xmode, BOOL utf, int *count)  
 {  
 pcre_uchar *ptr = *ptrptr;  
 int start_count = *count;  
 int hwm_count = start_count;  
 BOOL dup_parens = FALSE;  
   
 /* If the first character is a parenthesis, check on the type of group we are  
 dealing with. The very first call may not start with a parenthesis. */  
   
 if (ptr[0] == CHAR_LEFT_PARENTHESIS)  
   {  
   /* Handle specials such as (*SKIP) or (*UTF8) etc. */  
   
   if (ptr[1] == CHAR_ASTERISK)  
     {  
     ptr += 2;  
     while (ptr < cd->end_pattern && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
     }  
   
   /* Handle a normal, unnamed capturing parenthesis. */  
   
   else if (ptr[1] != CHAR_QUESTION_MARK)  
     {  
     *count += 1;  
     if (name == NULL && *count == lorn) return *count;  
     ptr++;  
     }  
   
   /* All cases now have (? at the start. Remember when we are in a group  
   where the parenthesis numbers are duplicated. */  
   
   else if (ptr[2] == CHAR_VERTICAL_LINE)  
     {  
     ptr += 3;  
     dup_parens = TRUE;  
     }  
   
   /* Handle comments; all characters are allowed until a ket is reached. */  
   
   else if (ptr[2] == CHAR_NUMBER_SIGN)  
     {  
     for (ptr += 3; *ptr != CHAR_NULL; ptr++)  
       if (*ptr == CHAR_RIGHT_PARENTHESIS) break;  
     goto FAIL_EXIT;  
     }  
   
   /* Handle a condition. If it is an assertion, just carry on so that it  
   is processed as normal. If not, skip to the closing parenthesis of the  
   condition (there can't be any nested parens). */  
   
   else if (ptr[2] == CHAR_LEFT_PARENTHESIS)  
     {  
     ptr += 2;  
     if (ptr[1] != CHAR_QUESTION_MARK)  
       {  
       while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
       if (*ptr != CHAR_NULL) ptr++;  
       }  
     }  
   
   /* Start with (? but not a condition. */  
   
   else  
     {  
     ptr += 2;  
     if (*ptr == CHAR_P) ptr++;                      /* Allow optional P */  
   
     /* We have to disambiguate (?<! and (?<= from (?<name> for named groups */  
   
     if ((*ptr == CHAR_LESS_THAN_SIGN && ptr[1] != CHAR_EXCLAMATION_MARK &&  
         ptr[1] != CHAR_EQUALS_SIGN) || *ptr == CHAR_APOSTROPHE)  
       {  
       pcre_uchar term;  
       const pcre_uchar *thisname;  
       *count += 1;  
       if (name == NULL && *count == lorn) return *count;  
       term = *ptr++;  
       if (term == CHAR_LESS_THAN_SIGN) term = CHAR_GREATER_THAN_SIGN;  
       thisname = ptr;  
       while (*ptr != term) ptr++;  
       if (name != NULL && lorn == (int)(ptr - thisname) &&  
           STRNCMP_UC_UC(name, thisname, (unsigned int)lorn) == 0)  
         return *count;  
       term++;  
       }  
     }  
   }  
   
 /* Past any initial parenthesis handling, scan for parentheses or vertical  
 bars. Stop if we get to cd->end_pattern. Note that this is important for the  
 first-pass call when this value is temporarily adjusted to stop at the current  
 position. So DO NOT change this to a test for binary zero. */  
   
 for (; ptr < cd->end_pattern; ptr++)  
   {  
   /* Skip over backslashed characters and also entire \Q...\E */  
   
   if (*ptr == CHAR_BACKSLASH)  
     {  
     if (*(++ptr) == CHAR_NULL) goto FAIL_EXIT;  
     if (*ptr == CHAR_Q) for (;;)  
       {  
       while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};  
       if (*ptr == CHAR_NULL) goto FAIL_EXIT;  
       if (*(++ptr) == CHAR_E) break;  
       }  
     continue;  
     }  
   
   /* Skip over character classes; this logic must be similar to the way they  
   are handled for real. If the first character is '^', skip it. Also, if the  
   first few characters (either before or after ^) are \Q\E or \E we skip them  
   too. This makes for compatibility with Perl. Note the use of STR macros to  
   encode "Q\\E" so that it works in UTF-8 on EBCDIC platforms. */  
   
   if (*ptr == CHAR_LEFT_SQUARE_BRACKET)  
     {  
     BOOL negate_class = FALSE;  
     for (;;)  
       {  
       if (ptr[1] == CHAR_BACKSLASH)  
         {  
         if (ptr[2] == CHAR_E)  
           ptr+= 2;  
         else if (STRNCMP_UC_C8(ptr + 2,  
                  STR_Q STR_BACKSLASH STR_E, 3) == 0)  
           ptr += 4;  
         else  
           break;  
         }  
       else if (!negate_class && ptr[1] == CHAR_CIRCUMFLEX_ACCENT)  
         {  
         negate_class = TRUE;  
         ptr++;  
         }  
       else break;  
       }  
   
     /* If the next character is ']', it is a data character that must be  
     skipped, except in JavaScript compatibility mode. */  
   
     if (ptr[1] == CHAR_RIGHT_SQUARE_BRACKET &&  
         (cd->external_options & PCRE_JAVASCRIPT_COMPAT) == 0)  
       ptr++;  
   
     while (*(++ptr) != CHAR_RIGHT_SQUARE_BRACKET)  
       {  
       if (*ptr == CHAR_NULL) return -1;  
       if (*ptr == CHAR_BACKSLASH)  
         {  
         if (*(++ptr) == CHAR_NULL) goto FAIL_EXIT;  
         if (*ptr == CHAR_Q) for (;;)  
           {  
           while (*(++ptr) != CHAR_NULL && *ptr != CHAR_BACKSLASH) {};  
           if (*ptr == CHAR_NULL) goto FAIL_EXIT;  
           if (*(++ptr) == CHAR_E) break;  
           }  
         continue;  
         }  
       }  
     continue;  
     }  
   
   /* Skip comments in /x mode */  
   
   if (xmode && *ptr == CHAR_NUMBER_SIGN)  
     {  
     ptr++;  
     while (*ptr != CHAR_NULL)  
       {  
       if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }  
       ptr++;  
 #ifdef SUPPORT_UTF  
       if (utf) FORWARDCHAR(ptr);  
 #endif  
       }  
     if (*ptr == CHAR_NULL) goto FAIL_EXIT;  
     continue;  
     }  
   
   /* Check for the special metacharacters */  
   
   if (*ptr == CHAR_LEFT_PARENTHESIS)  
     {  
     int rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, count);  
     if (rc > 0) return rc;  
     if (*ptr == CHAR_NULL) goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_RIGHT_PARENTHESIS)  
     {  
     if (dup_parens && *count < hwm_count) *count = hwm_count;  
     goto FAIL_EXIT;  
     }  
   
   else if (*ptr == CHAR_VERTICAL_LINE && dup_parens)  
     {  
     if (*count > hwm_count) hwm_count = *count;  
     *count = start_count;  
     }  
   }  
   
 FAIL_EXIT:  
 *ptrptr = ptr;  
 return -1;  
 }  
   
   
   
   
 /*************************************************  
 *       Find forward referenced subpattern       *  
 *************************************************/  
   
 /* This function scans along a pattern's text looking for capturing  
 subpatterns, and counting them. If it finds a named pattern that matches the  
 name it is given, it returns its number. Alternatively, if the name is NULL, it  
 returns when it reaches a given numbered subpattern. This is used for forward  
 references to subpatterns. We used to be able to start this scan from the  
 current compiling point, using the current count value from cd->bracount, and  
 do it all in a single loop, but the addition of the possibility of duplicate  
 subpattern numbers means that we have to scan from the very start, in order to  
 take account of such duplicates, and to use a recursive function to keep track  
 of the different types of group.  
   
 Arguments:  
   cd           compile background data  
   name         name to seek, or NULL if seeking a numbered subpattern  
   lorn         name length, or subpattern number if name is NULL  
   xmode        TRUE if we are in /x mode  
   utf          TRUE if we are in UTF-8 / UTF-16 / UTF-32 mode  
   
 Returns:       the number of the found subpattern, or -1 if not found  
 */  
   
 static int  
 find_parens(compile_data *cd, const pcre_uchar *name, int lorn, BOOL xmode,  
   BOOL utf)  
 {  
 pcre_uchar *ptr = (pcre_uchar *)cd->start_pattern;  
 int count = 0;  
 int rc;  
   
 /* If the pattern does not start with an opening parenthesis, the first call  
 to find_parens_sub() will scan right to the end (if necessary). However, if it  
 does start with a parenthesis, find_parens_sub() will return when it hits the  
 matching closing parens. That is why we have to have a loop. */  
   
 for (;;)  
   {  
   rc = find_parens_sub(&ptr, cd, name, lorn, xmode, utf, &count);  
   if (rc > 0 || *ptr++ == CHAR_NULL) break;  
   }  
   
 return rc;  
 }  
   
   
   
   
 /*************************************************  
1633  *      Find first significant op code            *  *      Find first significant op code            *
1634  *************************************************/  *************************************************/
1635    
# Line 1696  for (;;) Line 1668  for (;;)
1668    
1669      case OP_CALLOUT:      case OP_CALLOUT:
1670      case OP_CREF:      case OP_CREF:
1671      case OP_NCREF:      case OP_DNCREF:
1672      case OP_RREF:      case OP_RREF:
1673      case OP_NRREF:      case OP_DNRREF:
1674      case OP_DEF:      case OP_DEF:
1675      code += PRIV(OP_lengths)[*code];      code += PRIV(OP_lengths)[*code];
1676      break;      break;
# Line 1712  for (;;) Line 1684  for (;;)
1684    
1685    
1686    
   
1687  /*************************************************  /*************************************************
1688  *        Find the fixed length of a branch       *  *        Find the fixed length of a branch       *
1689  *************************************************/  *************************************************/
# Line 1836  for (;;) Line 1807  for (;;)
1807      case OP_COMMIT:      case OP_COMMIT:
1808      case OP_CREF:      case OP_CREF:
1809      case OP_DEF:      case OP_DEF:
1810        case OP_DNCREF:
1811        case OP_DNRREF:
1812      case OP_DOLL:      case OP_DOLL:
1813      case OP_DOLLM:      case OP_DOLLM:
1814      case OP_EOD:      case OP_EOD:
1815      case OP_EODN:      case OP_EODN:
1816      case OP_FAIL:      case OP_FAIL:
     case OP_NCREF:  
     case OP_NRREF:  
1817      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
1818      case OP_PRUNE:      case OP_PRUNE:
1819      case OP_REVERSE:      case OP_REVERSE:
# Line 1937  for (;;) Line 1908  for (;;)
1908    
1909      switch (*cc)      switch (*cc)
1910        {        {
       case OP_CRPLUS:  
       case OP_CRMINPLUS:  
1911        case OP_CRSTAR:        case OP_CRSTAR:
1912        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1913          case OP_CRPLUS:
1914          case OP_CRMINPLUS:
1915        case OP_CRQUERY:        case OP_CRQUERY:
1916        case OP_CRMINQUERY:        case OP_CRMINQUERY:
1917          case OP_CRPOSSTAR:
1918          case OP_CRPOSPLUS:
1919          case OP_CRPOSQUERY:
1920        return -1;        return -1;
1921    
1922        case OP_CRRANGE:        case OP_CRRANGE:
1923        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1924          case OP_CRPOSRANGE:
1925        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1926        branchlength += (int)GET2(cc,1);        branchlength += (int)GET2(cc,1);
1927        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
# Line 2015  for (;;) Line 1990  for (;;)
1990      case OP_QUERYI:      case OP_QUERYI:
1991      case OP_REF:      case OP_REF:
1992      case OP_REFI:      case OP_REFI:
1993        case OP_DNREF:
1994        case OP_DNREFI:
1995      case OP_SBRA:      case OP_SBRA:
1996      case OP_SBRAPOS:      case OP_SBRAPOS:
1997      case OP_SCBRA:      case OP_SCBRA:
# Line 2051  for (;;) Line 2028  for (;;)
2028    
2029    
2030    
   
2031  /*************************************************  /*************************************************
2032  *    Scan compiled regex for specific bracket    *  *    Scan compiled regex for specific bracket    *
2033  *************************************************/  *************************************************/
# Line 2153  for (;;) Line 2129  for (;;)
2129        {        {
2130        case OP_CHAR:        case OP_CHAR:
2131        case OP_CHARI:        case OP_CHARI:
2132          case OP_NOT:
2133          case OP_NOTI:
2134        case OP_EXACT:        case OP_EXACT:
2135        case OP_EXACTI:        case OP_EXACTI:
2136          case OP_NOTEXACT:
2137          case OP_NOTEXACTI:
2138        case OP_UPTO:        case OP_UPTO:
2139        case OP_UPTOI:        case OP_UPTOI:
2140        case OP_MINUPTO:        case OP_NOTUPTO:
2141          case OP_NOTUPTOI:
2142          case OP_MINUPTO:
2143        case OP_MINUPTOI:        case OP_MINUPTOI:
2144          case OP_NOTMINUPTO:
2145          case OP_NOTMINUPTOI:
2146        case OP_POSUPTO:        case OP_POSUPTO:
2147        case OP_POSUPTOI:        case OP_POSUPTOI:
2148          case OP_NOTPOSUPTO:
2149          case OP_NOTPOSUPTOI:
2150        case OP_STAR:        case OP_STAR:
2151        case OP_STARI:        case OP_STARI:
2152          case OP_NOTSTAR:
2153          case OP_NOTSTARI:
2154        case OP_MINSTAR:        case OP_MINSTAR:
2155        case OP_MINSTARI:        case OP_MINSTARI:
2156          case OP_NOTMINSTAR:
2157          case OP_NOTMINSTARI:
2158        case OP_POSSTAR:        case OP_POSSTAR:
2159        case OP_POSSTARI:        case OP_POSSTARI:
2160          case OP_NOTPOSSTAR:
2161          case OP_NOTPOSSTARI:
2162        case OP_PLUS:        case OP_PLUS:
2163        case OP_PLUSI:        case OP_PLUSI:
2164          case OP_NOTPLUS:
2165          case OP_NOTPLUSI:
2166        case OP_MINPLUS:        case OP_MINPLUS:
2167        case OP_MINPLUSI:        case OP_MINPLUSI:
2168          case OP_NOTMINPLUS:
2169          case OP_NOTMINPLUSI:
2170        case OP_POSPLUS:        case OP_POSPLUS:
2171        case OP_POSPLUSI:        case OP_POSPLUSI:
2172          case OP_NOTPOSPLUS:
2173          case OP_NOTPOSPLUSI:
2174        case OP_QUERY:        case OP_QUERY:
2175        case OP_QUERYI:        case OP_QUERYI:
2176          case OP_NOTQUERY:
2177          case OP_NOTQUERYI:
2178        case OP_MINQUERY:        case OP_MINQUERY:
2179        case OP_MINQUERYI:        case OP_MINQUERYI:
2180          case OP_NOTMINQUERY:
2181          case OP_NOTMINQUERYI:
2182        case OP_POSQUERY:        case OP_POSQUERY:
2183        case OP_POSQUERYI:        case OP_POSQUERYI:
2184          case OP_NOTPOSQUERY:
2185          case OP_NOTPOSQUERYI:
2186        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2187        break;        break;
2188        }        }
# Line 2353  Arguments: Line 2357  Arguments:
2357    endcode     points to where to stop    endcode     points to where to stop
2358    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2359    cd          contains pointers to tables etc.    cd          contains pointers to tables etc.
2360      recurses    chain of recurse_check to catch mutual recursion
2361    
2362  Returns:      TRUE if what is matched could be empty  Returns:      TRUE if what is matched could be empty
2363  */  */
2364    
2365    typedef struct recurse_check {
2366      struct recurse_check *prev;
2367      const pcre_uchar *group;
2368    } recurse_check;
2369    
2370  static BOOL  static BOOL
2371  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,  could_be_empty_branch(const pcre_uchar *code, const pcre_uchar *endcode,
2372    BOOL utf, compile_data *cd)    BOOL utf, compile_data *cd, recurse_check *recurses)
2373  {  {
2374  register pcre_uchar c;  register pcre_uchar c;
2375    recurse_check this_recurse;
2376    
2377  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);  for (code = first_significant_code(code + PRIV(OP_lengths)[*code], TRUE);
2378       code < endcode;       code < endcode;
2379       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))       code = first_significant_code(code + PRIV(OP_lengths)[c], TRUE))
# Line 2389  for (code = first_significant_code(code Line 2401  for (code = first_significant_code(code
2401    
2402    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2403      {      {
2404      const pcre_uchar *scode;      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2405        const pcre_uchar *endgroup = scode;
2406      BOOL empty_branch;      BOOL empty_branch;
2407    
2408      /* Test for forward reference */      /* Test for forward reference or uncompleted reference. This is disabled
2409        when called to scan a completed pattern by setting cd->start_workspace to
2410        NULL. */
2411    
2412      for (scode = cd->start_workspace; scode < cd->hwm; scode += LINK_SIZE)      if (cd->start_workspace != NULL)
2413        if ((int)GET(scode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;        {
2414          const pcre_uchar *tcode;
2415          for (tcode = cd->start_workspace; tcode < cd->hwm; tcode += LINK_SIZE)
2416            if ((int)GET(tcode, 0) == (int)(code + 1 - cd->start_code)) return TRUE;
2417          if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2418          }
2419    
2420      /* Not a forward reference, test for completed backward reference */      /* If the reference is to a completed group, we need to detect whether this
2421        is a recursive call, as otherwise there will be an infinite loop. If it is
2422        a recursion, just skip over it. Simple recursions are easily detected. For
2423        mutual recursions we keep a chain on the stack. */
2424    
2425      empty_branch = FALSE;      do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2426      scode = cd->start_code + GET(code, 1);      if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2427      if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */      else
2428          {
2429          recurse_check *r = recurses;
2430          for (r = recurses; r != NULL; r = r->prev)
2431            if (r->group == scode) break;
2432          if (r != NULL) continue;   /* Mutual recursion */
2433          }
2434    
2435        /* Completed reference; scan the referenced group, remembering it on the
2436        stack chain to detect mutual recursions. */
2437    
2438      /* Completed backwards reference */      empty_branch = FALSE;
2439        this_recurse.prev = recurses;
2440        this_recurse.group = scode;
2441    
2442      do      do
2443        {        {
2444        if (could_be_empty_branch(scode, endcode, utf, cd))        if (could_be_empty_branch(scode, endcode, utf, cd, &this_recurse))
2445          {          {
2446          empty_branch = TRUE;          empty_branch = TRUE;
2447          break;          break;
# Line 2463  for (code = first_significant_code(code Line 2497  for (code = first_significant_code(code
2497        empty_branch = FALSE;        empty_branch = FALSE;
2498        do        do
2499          {          {
2500          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd,
2501            empty_branch = TRUE;            recurses)) empty_branch = TRUE;
2502          code += GET(code, 1);          code += GET(code, 1);
2503          }          }
2504        while (*code == OP_ALT);        while (*code == OP_ALT);
# Line 2505  for (code = first_significant_code(code Line 2539  for (code = first_significant_code(code
2539        case OP_CRMINSTAR:        case OP_CRMINSTAR:
2540        case OP_CRQUERY:        case OP_CRQUERY:
2541        case OP_CRMINQUERY:        case OP_CRMINQUERY:
2542          case OP_CRPOSSTAR:
2543          case OP_CRPOSQUERY:
2544        break;        break;
2545    
2546        default:                   /* Non-repeat => class must match */        default:                   /* Non-repeat => class must match */
2547        case OP_CRPLUS:            /* These repeats aren't empty */        case OP_CRPLUS:            /* These repeats aren't empty */
2548        case OP_CRMINPLUS:        case OP_CRMINPLUS:
2549          case OP_CRPOSPLUS:
2550        return FALSE;        return FALSE;
2551    
2552        case OP_CRRANGE:        case OP_CRRANGE:
2553        case OP_CRMINRANGE:        case OP_CRMINRANGE:
2554          case OP_CRPOSRANGE:
2555        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
2556        break;        break;
2557        }        }
# Line 2521  for (code = first_significant_code(code Line 2559  for (code = first_significant_code(code
2559    
2560      /* Opcodes that must match a character */      /* Opcodes that must match a character */
2561    
2562        case OP_ANY:
2563        case OP_ALLANY:
2564        case OP_ANYBYTE:
2565    
2566      case OP_PROP:      case OP_PROP:
2567      case OP_NOTPROP:      case OP_NOTPROP:
2568        case OP_ANYNL:
2569    
2570        case OP_NOT_HSPACE:
2571        case OP_HSPACE:
2572        case OP_NOT_VSPACE:
2573        case OP_VSPACE:
2574      case OP_EXTUNI:      case OP_EXTUNI:
2575    
2576      case OP_NOT_DIGIT:      case OP_NOT_DIGIT:
2577      case OP_DIGIT:      case OP_DIGIT:
2578      case OP_NOT_WHITESPACE:      case OP_NOT_WHITESPACE:
2579      case OP_WHITESPACE:      case OP_WHITESPACE:
2580      case OP_NOT_WORDCHAR:      case OP_NOT_WORDCHAR:
2581      case OP_WORDCHAR:      case OP_WORDCHAR:
2582      case OP_ANY:  
     case OP_ALLANY:  
     case OP_ANYBYTE:  
2583      case OP_CHAR:      case OP_CHAR:
2584      case OP_CHARI:      case OP_CHARI:
2585      case OP_NOT:      case OP_NOT:
2586      case OP_NOTI:      case OP_NOTI:
2587    
2588      case OP_PLUS:      case OP_PLUS:
2589        case OP_PLUSI:
2590      case OP_MINPLUS:      case OP_MINPLUS:
2591      case OP_POSPLUS:      case OP_MINPLUSI:
2592      case OP_EXACT:  
2593      case OP_NOTPLUS:      case OP_NOTPLUS:
2594        case OP_NOTPLUSI:
2595      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
2596        case OP_NOTMINPLUSI:
2597    
2598        case OP_POSPLUS:
2599        case OP_POSPLUSI:
2600      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
2601        case OP_NOTPOSPLUSI:
2602    
2603        case OP_EXACT:
2604        case OP_EXACTI:
2605      case OP_NOTEXACT:      case OP_NOTEXACT:
2606        case OP_NOTEXACTI:
2607    
2608      case OP_TYPEPLUS:      case OP_TYPEPLUS:
2609      case OP_TYPEMINPLUS:      case OP_TYPEMINPLUS:
2610      case OP_TYPEPOSPLUS:      case OP_TYPEPOSPLUS:
2611      case OP_TYPEEXACT:      case OP_TYPEEXACT:
2612    
2613      return FALSE;      return FALSE;
2614    
2615      /* These are going to continue, as they may be empty, but we have to      /* These are going to continue, as they may be empty, but we have to
# Line 2582  for (code = first_significant_code(code Line 2643  for (code = first_significant_code(code
2643      return TRUE;      return TRUE;
2644    
2645      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,      /* In UTF-8 mode, STAR, MINSTAR, POSSTAR, QUERY, MINQUERY, POSQUERY, UPTO,
2646      MINUPTO, and POSUPTO may be followed by a multibyte character */      MINUPTO, and POSUPTO and their caseless and negative versions may be
2647        followed by a multibyte character. */
2648    
2649  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32  #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
2650      case OP_STAR:      case OP_STAR:
2651      case OP_STARI:      case OP_STARI:
2652        case OP_NOTSTAR:
2653        case OP_NOTSTARI:
2654    
2655      case OP_MINSTAR:      case OP_MINSTAR:
2656      case OP_MINSTARI:      case OP_MINSTARI:
2657        case OP_NOTMINSTAR:
2658        case OP_NOTMINSTARI:
2659    
2660      case OP_POSSTAR:      case OP_POSSTAR:
2661      case OP_POSSTARI:      case OP_POSSTARI:
2662        case OP_NOTPOSSTAR:
2663        case OP_NOTPOSSTARI:
2664    
2665      case OP_QUERY:      case OP_QUERY:
2666      case OP_QUERYI:      case OP_QUERYI:
2667        case OP_NOTQUERY:
2668        case OP_NOTQUERYI:
2669    
2670      case OP_MINQUERY:      case OP_MINQUERY:
2671      case OP_MINQUERYI:      case OP_MINQUERYI:
2672        case OP_NOTMINQUERY:
2673        case OP_NOTMINQUERYI:
2674    
2675      case OP_POSQUERY:      case OP_POSQUERY:
2676      case OP_POSQUERYI:      case OP_POSQUERYI:
2677        case OP_NOTPOSQUERY:
2678        case OP_NOTPOSQUERYI:
2679    
2680      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);      if (utf && HAS_EXTRALEN(code[1])) code += GET_EXTRALEN(code[1]);
2681      break;      break;
2682    
2683      case OP_UPTO:      case OP_UPTO:
2684      case OP_UPTOI:      case OP_UPTOI:
2685        case OP_NOTUPTO:
2686        case OP_NOTUPTOI:
2687    
2688      case OP_MINUPTO:      case OP_MINUPTO:
2689      case OP_MINUPTOI:      case OP_MINUPTOI:
2690        case OP_NOTMINUPTO:
2691        case OP_NOTMINUPTOI:
2692    
2693      case OP_POSUPTO:      case OP_POSUPTO:
2694      case OP_POSUPTOI:      case OP_POSUPTOI:
2695        case OP_NOTPOSUPTO:
2696        case OP_NOTPOSUPTOI:
2697    
2698      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);      if (utf && HAS_EXTRALEN(code[1 + IMM2_SIZE])) code += GET_EXTRALEN(code[1 + IMM2_SIZE]);
2699      break;      break;
2700  #endif  #endif
# Line 2659  could_be_empty(const pcre_uchar *code, c Line 2748  could_be_empty(const pcre_uchar *code, c
2748  {  {
2749  while (bcptr != NULL && bcptr->current_branch >= code)  while (bcptr != NULL && bcptr->current_branch >= code)
2750    {    {
2751    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd))    if (!could_be_empty_branch(bcptr->current_branch, endcode, utf, cd, NULL))
2752      return FALSE;      return FALSE;
2753    bcptr = bcptr->outer;    bcptr = bcptr->outer;
2754    }    }
# Line 2669  return TRUE; Line 2758  return TRUE;
2758    
2759    
2760  /*************************************************  /*************************************************
2761  *           Check for POSIX class syntax         *  *        Base opcode of repeated opcodes         *
2762  *************************************************/  *************************************************/
2763    
2764  /* This function is called when the sequence "[:" or "[." or "[=" is  /* Returns the base opcode for repeated single character type opcodes. If the
2765  encountered in a character class. It checks whether this is followed by a  opcode is not a repeated character type, it returns with the original value.
 sequence of characters terminated by a matching ":]" or ".]" or "=]". If we  
 reach an unescaped ']' without the special preceding character, return FALSE.  
2766    
2767  Originally, this function only recognized a sequence of letters between the  Arguments:  c opcode
2768  terminators, but it seems that Perl recognizes any sequence of characters,  Returns:    base opcode for the type
2769  though of course unknown POSIX names are subsequently rejected. Perl gives an  */
 "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE  
 didn't consider this to be a POSIX class. Likewise for [:1234:].  
2770    
2771  The problem in trying to be exactly like Perl is in the handling of escapes. We  static pcre_uchar
2772  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX  get_repeat_base(pcre_uchar c)
2773  class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code  {
2774  below handles the special case of \], but does not try to do any other escape  return (c > OP_TYPEPOSUPTO)? c :
2775  processing. This makes it different from Perl for cases such as [:l\ower:]         (c >= OP_TYPESTAR)?   OP_TYPESTAR :
2776  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize         (c >= OP_NOTSTARI)?   OP_NOTSTARI :
2777  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,         (c >= OP_NOTSTAR)?    OP_NOTSTAR :
2778  I think.         (c >= OP_STARI)?      OP_STARI :
2779                                 OP_STAR;
2780    }
2781    
 A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  
 It seems that the appearance of a nested POSIX class supersedes an apparent  
 external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or  
 a digit.  
2782    
2783  In Perl, unescaped square brackets may also appear as part of class names. For  
2784  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for  #ifdef SUPPORT_UCP
2785  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not  /*************************************************
2786  seem right at all. PCRE does not allow closing square brackets in POSIX class  *        Check a character and a property        *
2787  names.  *************************************************/
2788    
2789    /* This function is called by check_auto_possessive() when a property item
2790    is adjacent to a fixed character.
2791    
2792  Arguments:  Arguments:
2793    ptr      pointer to the initial [    c            the character
2794    endptr   where to return the end pointer    ptype        the property type
2795      pdata        the data for the type
2796      negated      TRUE if it's a negated property (\P or \p{^)
2797    
2798  Returns:   TRUE or FALSE  Returns:       TRUE if auto-possessifying is OK
2799  */  */
2800    
2801  static BOOL  static BOOL
2802  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata,
2803      BOOL negated)
2804  {  {
2805  pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */  const pcre_uint32 *p;
2806  terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */  const ucd_record *prop = GET_UCD(c);
2807  for (++ptr; *ptr != CHAR_NULL; ptr++)  
2808    switch(ptype)
2809    {    {
2810    if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)    case PT_LAMP:
2811      ptr++;    return (prop->chartype == ucp_Lu ||
2812    else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;            prop->chartype == ucp_Ll ||
2813    else            prop->chartype == ucp_Lt) == negated;
2814    
2815      case PT_GC:
2816      return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
2817    
2818      case PT_PC:
2819      return (pdata == prop->chartype) == negated;
2820    
2821      case PT_SC:
2822      return (pdata == prop->script) == negated;
2823    
2824      /* These are specials */
2825    
2826      case PT_ALNUM:
2827      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2828              PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
2829    
2830      /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
2831      means that Perl space and POSIX space are now identical. PCRE was changed
2832      at release 8.34. */
2833    
2834      case PT_SPACE:    /* Perl space */
2835      case PT_PXSPACE:  /* POSIX space */
2836      switch(c)
2837      {      {
2838      if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)      HSPACE_CASES:
2839        {      VSPACE_CASES:
2840        *endptr = ptr;      return negated;
2841        return TRUE;  
2842        }      default:
2843      if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2844           (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||      }
2845            ptr[1] == CHAR_EQUALS_SIGN) &&    break;  /* Control never reaches here */
2846          check_posix_syntax(ptr, endptr))  
2847        return FALSE;    case PT_WORD:
2848      return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
2849              PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
2850              c == CHAR_UNDERSCORE) == negated;
2851    
2852      case PT_CLIST:
2853      p = PRIV(ucd_caseless_sets) + prop->caseset;
2854      for (;;)
2855        {
2856        if (c < *p) return !negated;
2857        if (c == *p++) return negated;
2858      }      }
2859      break;  /* Control never reaches here */
2860    }    }
2861    
2862  return FALSE;  return FALSE;
2863  }  }
2864    #endif  /* SUPPORT_UCP */
2865    
2866    
2867    
2868  /*************************************************  /*************************************************
2869  *          Check POSIX class name                *  *        Fill the character property list        *
2870  *************************************************/  *************************************************/
2871    
2872  /* This function is called to check the name given in a POSIX-style class entry  /* Checks whether the code points to an opcode that can take part in auto-
2873  such as [:alnum:].  possessification, and if so, fills a list with its properties.
2874    
2875  Arguments:  Arguments:
2876    ptr        points to the first letter    code        points to start of expression
2877    len        the length of the name    utf         TRUE if in UTF-8 / UTF-16 / UTF-32 mode
2878      fcc         points to case-flipping table
2879      list        points to output list
2880                  list[0] will be filled with the opcode
2881                  list[1] will be non-zero if this opcode
2882                    can match an empty character string
2883                  list[2..7] depends on the opcode
2884    
2885  Returns:     a value representing the name, or -1 if unknown  Returns:      points to the start of the next opcode if *code is accepted
2886                  NULL if *code is not accepted
2887  */  */
2888    
2889  static int  static const pcre_uchar *
2890  check_posix_name(const pcre_uchar *ptr, int len)  get_chr_property_list(const pcre_uchar *code, BOOL utf,
2891      const pcre_uint8 *fcc, pcre_uint32 *list)
2892  {  {
2893  const char *pn = posix_names;  pcre_uchar c = *code;
2894  register int yield = 0;  pcre_uchar base;
2895  while (posix_name_lengths[yield] != 0)  const pcre_uchar *end;
2896    {  pcre_uint32 chr;
   if (len == posix_name_lengths[yield] &&  
     STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;  
   pn += posix_name_lengths[yield] + 1;  
   yield++;  
   }  
 return -1;  
 }  
2897    
2898    #ifdef SUPPORT_UCP
2899    pcre_uint32 *clist_dest;
2900    const pcre_uint32 *clist_src;
2901    #else
2902    utf = utf;  /* Suppress "unused parameter" compiler warning */
2903    #endif
2904    
2905  /*************************************************  list[0] = c;
2906  *    Adjust OP_RECURSE items in repeated group   *  list[1] = FALSE;
2907  *************************************************/  code++;
2908    
2909  /* OP_RECURSE items contain an offset from the start of the regex to the group  if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
2910  that is referenced. This means that groups can be replicated for fixed    {
2911  repetition simply by copying (because the recursion is allowed to refer to    base = get_repeat_base(c);
2912  earlier groups that are outside the current group). However, when a group is    c -= (base - OP_STAR);
 optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is  
 inserted before it, after it has been compiled. This means that any OP_RECURSE  
 items within it that refer to the group itself or any contained groups have to  
 have their offsets adjusted. That one of the jobs of this function. Before it  
 is called, the partially compiled regex must be temporarily terminated with  
 OP_END.  
2913    
2914  This function has been extended with the possibility of forward references for    if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
2915  recursions and subroutine calls. It must also check the list of such references      code += IMM2_SIZE;
 for the group we are dealing with. If it finds that one of the recursions in  
 the current group is on this list, it adjusts the offset in the list, not the  
 value in the reference (which is a group number).  
2916    
2917  Arguments:    list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT && c != OP_POSPLUS);
   group      points to the start of the group  
   adjust     the amount by which the group is to be moved  
   utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode  
   cd         contains pointers to tables etc.  
   save_hwm   the hwm forward reference pointer at the start of the group  
2918    
2919  Returns:     nothing    switch(base)
2920  */      {
2921        case OP_STAR:
2922        list[0] = OP_CHAR;
2923        break;
2924    
2925  static void      case OP_STARI:
2926  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,      list[0] = OP_CHARI;
2927    pcre_uchar *save_hwm)      break;
 {  
 pcre_uchar *ptr = group;  
2928    
2929  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)      case OP_NOTSTAR:
2930    {      list[0] = OP_NOT;
2931    int offset;      break;
   pcre_uchar *hc;  
2932    
2933    /* See if this recursion is on the forward reference list. If so, adjust the      case OP_NOTSTARI:
2934    reference. */      list[0] = OP_NOTI;
2935        break;
2936    
2937    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)      case OP_TYPESTAR:
2938      {      list[0] = *code;
2939      offset = (int)GET(hc, 0);      code++;
2940      if (cd->start_code + offset == ptr + 1)      break;
       {  
       PUT(hc, 0, offset + adjust);  
       break;  
       }  
2941      }      }
2942      c = list[0];
2943      }
2944    
2945    /* Otherwise, adjust the recursion offset if it's after the start of this  switch(c)
2946    group. */    {
2947      case OP_NOT_DIGIT:
2948    if (hc >= cd->hwm)    case OP_DIGIT:
2949      {    case OP_NOT_WHITESPACE:
2950      offset = (int)GET(ptr, 1);    case OP_WHITESPACE:
2951      if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);    case OP_NOT_WORDCHAR:
2952      }    case OP_WORDCHAR:
2953      case OP_ANY:
2954      case OP_ALLANY:
2955      case OP_ANYNL:
2956      case OP_NOT_HSPACE:
2957      case OP_HSPACE:
2958      case OP_NOT_VSPACE:
2959      case OP_VSPACE:
2960      case OP_EXTUNI:
2961      case OP_EODN:
2962      case OP_EOD:
2963      case OP_DOLL:
2964      case OP_DOLLM:
2965      return code;
2966    
2967    ptr += 1 + LINK_SIZE;    case OP_CHAR:
2968    }    case OP_NOT:
2969  }    GETCHARINCTEST(chr, code);
2970      list[2] = chr;
2971      list[3] = NOTACHAR;
2972      return code;
2973    
2974      case OP_CHARI:
2975      case OP_NOTI:
2976      list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
2977      GETCHARINCTEST(chr, code);
2978      list[2] = chr;
2979    
2980    #ifdef SUPPORT_UCP
2981      if (chr < 128 || (chr < 256 && !utf))
2982        list[3] = fcc[chr];
2983      else
2984        list[3] = UCD_OTHERCASE(chr);
2985    #elif defined SUPPORT_UTF || !defined COMPILE_PCRE8
2986      list[3] = (chr < 256) ? fcc[chr] : chr;
2987    #else
2988      list[3] = fcc[chr];
2989    #endif
2990    
2991  /*************************************************    /* The othercase might be the same value. */
 *        Insert an automatic callout point       *  
 *************************************************/  
2992    
2993  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert    if (chr == list[3])
2994  callout points before each pattern item.      list[3] = NOTACHAR;
2995      else
2996        list[4] = NOTACHAR;
2997      return code;
2998    
2999  Arguments:  #ifdef SUPPORT_UCP
3000    code           current code pointer    case OP_PROP:
3001    ptr            current pattern pointer    case OP_NOTPROP:
3002    cd             pointers to tables etc    if (code[0] != PT_CLIST)
3003        {
3004        list[2] = code[0];
3005        list[3] = code[1];
3006        return code + 2;
3007        }
3008    
3009  Returns:         new code pointer    /* Convert only if we have enough space. */
 */  
3010    
3011  static pcre_uchar *    clist_src = PRIV(ucd_caseless_sets) + code[1];
3012  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)    clist_dest = list + 2;
3013  {    code += 2;
 *code++ = OP_CALLOUT;  
 *code++ = 255;  
 PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */  
 PUT(code, LINK_SIZE, 0);                       /* Default length */  
 return code + 2 * LINK_SIZE;  
 }  
3014    
3015      do {
3016         if (clist_dest >= list + 8)
3017           {
3018           /* Early return if there is not enough space. This should never
3019           happen, since all clists are shorter than 5 character now. */
3020           list[2] = code[0];
3021           list[3] = code[1];
3022           return code;
3023           }
3024         *clist_dest++ = *clist_src;
3025         }
3026      while(*clist_src++ != NOTACHAR);
3027    
3028      /* All characters are stored. The terminating NOTACHAR
3029      is copied form the clist itself. */
3030    
3031  /*************************************************    list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
3032  *         Complete a callout item                *    return code;
3033  *************************************************/  #endif
3034    
3035  /* A callout item contains the length of the next item in the pattern, which    case OP_NCLASS:
3036  we can't fill in till after we have reached the relevant point. This is used    case OP_CLASS:
3037  for both automatic and manual callouts.  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3038      case OP_XCLASS:
3039      if (c == OP_XCLASS)
3040        end = code + GET(code, 0) - 1;
3041      else
3042    #endif
3043        end = code + 32 / sizeof(pcre_uchar);
3044    
3045  Arguments:    switch(*end)
3046    previous_callout   points to previous callout item      {
3047    ptr                current pattern pointer      case OP_CRSTAR:
3048    cd                 pointers to tables etc      case OP_CRMINSTAR:
3049        case OP_CRQUERY:
3050        case OP_CRMINQUERY:
3051        case OP_CRPOSSTAR:
3052        case OP_CRPOSQUERY:
3053        list[1] = TRUE;
3054        end++;
3055        break;
3056    
3057  Returns:             nothing      case OP_CRPLUS:
3058  */      case OP_CRMINPLUS:
3059        case OP_CRPOSPLUS:
3060        end++;
3061        break;
3062    
3063  static void      case OP_CRRANGE:
3064  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)      case OP_CRMINRANGE:
3065  {      case OP_CRPOSRANGE:
3066  int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));      list[1] = (GET2(end, 1) == 0);
3067  PUT(previous_callout, 2 + LINK_SIZE, length);      end += 1 + 2 * IMM2_SIZE;
3068        break;
3069        }
3070      list[2] = (pcre_uint32)(end - code);
3071      return end;
3072      }
3073    return NULL;    /* Opcode not accepted */
3074  }  }
3075    
3076    
3077    
 #ifdef SUPPORT_UCP  
3078  /*************************************************  /*************************************************
3079  *           Get othercase range                  *  *    Scan further character sets for match       *
3080  *************************************************/  *************************************************/
3081    
3082  /* This function is passed the start and end of a class range, in UTF-8 mode  /* Checks whether the base and the current opcode have a common character, in
3083  with UCP support. It searches up the characters, looking for ranges of  which case the base cannot be possessified.
 characters in the "other" case. Each call returns the next one, updating the  
 start address. A character with multiple other cases is returned on its own  
 with a special return value.  
3084    
3085  Arguments:  Arguments:
3086    cptr        points to starting character value; updated    code        points to the byte code
3087    d           end value    utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3088    ocptr       where to put start of othercase range    cd          static compile data
3089    odptr       where to put end of othercase range    base_list   the data list of the base opcode
3090    
3091  Yield:        -1 when no more  Returns:      TRUE if the auto-possessification is possible
                0 when a range is returned  
               >0 the CASESET offset for char with multiple other cases  
                 in this case, ocptr contains the original  
3092  */  */
3093    
3094  static int  static BOOL
3095  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
3096    pcre_uint32 *odptr)    const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit)
3097  {  {
3098  pcre_uint32 c, othercase, next;  pcre_uchar c;
3099  unsigned int co;  pcre_uint32 list[8];
3100    const pcre_uint32 *chr_ptr;
3101  /* Find the first character that has an other case. If it has multiple other  const pcre_uint32 *ochr_ptr;
3102  cases, return its case offset value. */  const pcre_uint32 *list_ptr;
3103    const pcre_uchar *next_code;
3104    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3105    const pcre_uchar *xclass_flags;
3106    #endif
3107    const pcre_uint8 *class_bitset;
3108    const pcre_uint8 *set1, *set2, *set_end;
3109    pcre_uint32 chr;
3110    BOOL accepted, invert_bits;
3111    BOOL entered_a_group = FALSE;
3112    
3113    if (*rec_limit == 0) return FALSE;
3114    --(*rec_limit);
3115    
3116    /* Note: the base_list[1] contains whether the current opcode has greedy
3117    (represented by a non-zero value) quantifier. This is a different from
3118    other character type lists, which stores here that the character iterator
3119    matches to an empty string (also represented by a non-zero value). */
3120    
3121  for (c = *cptr; c <= d; c++)  for(;;)
3122    {    {
3123    if ((co = UCD_CASESET(c)) != 0)    /* All operations move the code pointer forward.
3124      {    Therefore infinite recursions are not possible. */
     *ocptr = c++;   /* Character that has the set */  
     *cptr = c;      /* Rest of input range */  
     return (int)co;  
     }  
   if ((othercase = UCD_OTHERCASE(c)) != c) break;  
   }  
   
 if (c > d) return -1;  /* Reached end of range */  
3125    
3126  *ocptr = othercase;    c = *code;
 next = othercase + 1;  
   
 for (++c; c <= d; c++)  
   {  
   if (UCD_OTHERCASE(c) != next) break;  
   next++;  
   }  
3127    
3128  *odptr = next - 1;     /* End of othercase range */    /* Skip over callouts */
 *cptr = c;             /* Rest of input range */  
 return 0;  
 }  
3129    
3130      if (c == OP_CALLOUT)
3131        {
3132        code += PRIV(OP_lengths)[c];
3133        continue;
3134        }
3135    
3136      if (c == OP_ALT)
3137        {
3138        do code += GET(code, 1); while (*code == OP_ALT);
3139        c = *code;
3140        }
3141    
3142  /*************************************************    switch(c)
3143  *        Check a character and a property        *      {
3144  *************************************************/      case OP_END:
3145        case OP_KETRPOS:
3146        /* TRUE only in greedy case. The non-greedy case could be replaced by
3147        an OP_EXACT, but it is probably not worth it. (And note that OP_EXACT
3148        uses more memory, which we cannot get at this stage.) */
3149    
3150  /* This function is called by check_auto_possessive() when a property item      return base_list[1] != 0;
 is adjacent to a fixed character.  
3151    
3152  Arguments:      case OP_KET:
3153    c            the character      /* If the bracket is capturing, and referenced by an OP_RECURSE, or
3154    ptype        the property type      it is an atomic sub-pattern (assert, once, etc.) the non-greedy case
3155    pdata        the data for the type      cannot be converted to a possessive form. */
3156    negated      TRUE if it's a negated property (\P or \p{^)  
3157        if (base_list[1] == 0) return FALSE;
3158    
3159        switch(*(code - GET(code, 1)))
3160          {
3161          case OP_ASSERT:
3162          case OP_ASSERT_NOT:
3163          case OP_ASSERTBACK:
3164          case OP_ASSERTBACK_NOT:
3165          case OP_ONCE:
3166          case OP_ONCE_NC:
3167          /* Atomic sub-patterns and assertions can always auto-possessify their
3168          last iterator. However, if the group was entered as a result of checking
3169          a previous iterator, this is not possible. */
3170    
3171  Returns:       TRUE if auto-possessifying is OK        return !entered_a_group;
3172  */        }
3173    
3174  static BOOL      code += PRIV(OP_lengths)[c];
3175  check_char_prop(pcre_uint32 c, unsigned int ptype, unsigned int pdata, BOOL negated)      continue;
 {  
 #ifdef SUPPORT_UCP  
 const pcre_uint32 *p;  
 #endif  
3176    
3177  const ucd_record *prop = GET_UCD(c);      case OP_ONCE:
3178        case OP_ONCE_NC:
3179        case OP_BRA:
3180        case OP_CBRA:
3181        next_code = code + GET(code, 1);
3182        code += PRIV(OP_lengths)[c];
3183    
3184  switch(ptype)      while (*next_code == OP_ALT)
3185    {        {
3186    case PT_LAMP:        if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit))
3187    return (prop->chartype == ucp_Lu ||          return FALSE;
3188            prop->chartype == ucp_Ll ||        code = next_code + 1 + LINK_SIZE;
3189            prop->chartype == ucp_Lt) == negated;        next_code += GET(next_code, 1);
3190          }
3191    
3192    case PT_GC:      entered_a_group = TRUE;
3193    return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;      continue;
3194    
3195    case PT_PC:      case OP_BRAZERO:
3196    return (pdata == prop->chartype) == negated;      case OP_BRAMINZERO:
3197    
3198    case PT_SC:      next_code = code + 1;
3199    return (pdata == prop->script) == negated;      if (*next_code != OP_BRA && *next_code != OP_CBRA
3200            && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE;
3201    
3202        do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3203    
3204        /* The bracket content will be checked by the
3205        OP_BRA/OP_CBRA case above. */
3206        next_code += 1 + LINK_SIZE;
3207        if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit))
3208          return FALSE;
3209    
3210    /* These are specials */      code += PRIV(OP_lengths)[c];
3211        continue;
3212    
3213    case PT_ALNUM:      default:
3214    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||      break;
3215            PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;      }
3216    
3217    case PT_SPACE:    /* Perl space */    /* Check for a supported opcode, and load its properties. */
   return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||  
           c == CHAR_HT || c == CHAR_NL || c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3218    
3219    case PT_PXSPACE:  /* POSIX space */    code = get_chr_property_list(code, utf, cd->fcc, list);
3220    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z ||    if (code == NULL) return FALSE;    /* Unsupported */
           c == CHAR_HT || c == CHAR_NL || c == CHAR_VT ||  
           c == CHAR_FF || c == CHAR_CR)  
           == negated;  
3221    
3222    case PT_WORD:    /* If either opcode is a small character list, set pointers for comparing
3223    return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||    characters from that list with another list, or with a property. */
           PRIV(ucp_gentype)[prop->chartype] == ucp_N ||  
           c == CHAR_UNDERSCORE) == negated;  
3224    
3225  #ifdef SUPPORT_UCP    if (base_list[0] == OP_CHAR)
   case PT_CLIST:  
   p = PRIV(ucd_caseless_sets) + prop->caseset;  
   for (;;)  
3226      {      {
3227      if (c < *p) return !negated;      chr_ptr = base_list + 2;
3228      if (c == *p++) return negated;      list_ptr = list;
3229        }
3230      else if (list[0] == OP_CHAR)
3231        {
3232        chr_ptr = list + 2;
3233        list_ptr = base_list;
3234      }      }
   break;  /* Control never reaches here */  
 #endif  
   }  
   
 return FALSE;  
 }  
 #endif  /* SUPPORT_UCP */  
   
   
3235    
3236  /*************************************************    /* Character bitsets can also be compared to certain opcodes. */
 *     Check if auto-possessifying is possible    *  
 *************************************************/  
3237    
3238  /* This function is called for unlimited repeats of certain items, to see    else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
3239  whether the next thing could possibly match the repeated item. If not, it makes  #ifdef COMPILE_PCRE8
3240  sense to automatically possessify the repeated item.        /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
3241          || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
3242    #endif
3243          )
3244        {
3245    #ifdef COMPILE_PCRE8
3246        if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
3247    #else
3248        if (base_list[0] == OP_CLASS)
3249    #endif
3250          {
3251          set1 = (pcre_uint8 *)(base_end - base_list[2]);
3252          list_ptr = list;
3253          }
3254        else
3255          {
3256          set1 = (pcre_uint8 *)(code - list[2]);
3257          list_ptr = base_list;
3258          }
3259    
3260  Arguments:      invert_bits = FALSE;
3261    previous      pointer to the repeated opcode      switch(list_ptr[0])
3262    utf           TRUE in UTF-8 / UTF-16 / UTF-32 mode        {
3263    ptr           next character in pattern        case OP_CLASS:
3264    options       options bits        case OP_NCLASS:
3265    cd            contains pointers to tables etc.        set2 = (pcre_uint8 *)
3266            ((list_ptr == list ? code : base_end) - list_ptr[2]);
3267          break;
3268    
3269  Returns:        TRUE if possessifying is wanted  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3270  */        case OP_XCLASS:
3271          xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
3272          if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
3273          if ((*xclass_flags & XCL_MAP) == 0)
3274            {
3275            /* No bits are set for characters < 256. */
3276            if (list[1] == 0) return TRUE;
3277            /* Might be an empty repeat. */
3278            continue;
3279            }
3280          set2 = (pcre_uint8 *)(xclass_flags + 1);
3281          break;
3282    #endif
3283    
3284  static BOOL        case OP_NOT_DIGIT:
3285  check_auto_possessive(const pcre_uchar *previous, BOOL utf,        invert_bits = TRUE;
3286    const pcre_uchar *ptr, int options, compile_data *cd)        /* Fall through */
3287  {        case OP_DIGIT:
3288  pcre_uint32 c = NOTACHAR;        set2 = (pcre_uint8 *)(cd->cbits + cbit_digit);
3289  pcre_uint32 next;        break;
 int escape;  
 pcre_uchar op_code = *previous++;  
3290    
3291  /* Skip whitespace and comments in extended mode */        case OP_NOT_WHITESPACE:
3292          invert_bits = TRUE;
3293          /* Fall through */
3294          case OP_WHITESPACE:
3295          set2 = (pcre_uint8 *)(cd->cbits + cbit_space);
3296          break;
3297    
3298  if ((options & PCRE_EXTENDED) != 0)        case OP_NOT_WORDCHAR:
3299    {        invert_bits = TRUE;
3300    for (;;)        /* Fall through */
3301      {        case OP_WORDCHAR:
3302      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;        set2 = (pcre_uint8 *)(cd->cbits + cbit_word);
3303      if (*ptr == CHAR_NUMBER_SIGN)        break;
3304    
3305          default:
3306          return FALSE;
3307          }
3308    
3309        /* Because the sets are unaligned, we need
3310        to perform byte comparison here. */
3311        set_end = set1 + 32;
3312        if (invert_bits)
3313        {        {
3314        ptr++;        do
       while (*ptr != CHAR_NULL)  
3315          {          {
3316          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if ((*set1++ & ~(*set2++)) != 0) return FALSE;
3317          ptr++;          }
3318  #ifdef SUPPORT_UTF        while (set1 < set_end);
3319          if (utf) FORWARDCHAR(ptr);        }
3320  #endif      else
3321          {
3322          do
3323            {
3324            if ((*set1++ & *set2++) != 0) return FALSE;
3325          }          }
3326          while (set1 < set_end);
3327        }        }
3328      else break;  
3329        if (list[1] == 0) return TRUE;
3330        /* Might be an empty repeat. */
3331        continue;
3332      }      }
   }  
3333    
3334  /* If the next item is one that we can handle, get its value. A non-negative    /* Some property combinations also acceptable. Unicode property opcodes are
3335  value is a character, a negative value is an escape value. */    processed specially; the rest can be handled with a lookup table. */
3336    
3337  if (*ptr == CHAR_BACKSLASH)    else
3338    {      {
3339    int temperrorcode = 0;      pcre_uint32 leftop, rightop;
   escape = check_escape(&ptr, &next, &temperrorcode, cd->bracount, options,  
     FALSE);  
   if (temperrorcode != 0) return FALSE;  
   ptr++;    /* Point after the escape sequence */  
   }  
 else if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_meta) == 0)  
   {  
   escape = 0;  
 #ifdef SUPPORT_UTF  
   if (utf) { GETCHARINC(next, ptr); } else  
 #endif  
   next = *ptr++;  
   }  
 else return FALSE;  
3340    
3341  /* Skip whitespace and comments in extended mode */      leftop = base_list[0];
3342        rightop = list[0];
3343    
3344  if ((options & PCRE_EXTENDED) != 0)  #ifdef SUPPORT_UCP
3345    {      accepted = FALSE; /* Always set in non-unicode case. */
3346    for (;;)      if (leftop == OP_PROP || leftop == OP_NOTPROP)
3347          {
3348          if (rightop == OP_EOD)
3349            accepted = TRUE;
3350          else if (rightop == OP_PROP || rightop == OP_NOTPROP)
3351            {
3352            int n;
3353            const pcre_uint8 *p;
3354            BOOL same = leftop == rightop;
3355            BOOL lisprop = leftop == OP_PROP;
3356            BOOL risprop = rightop == OP_PROP;
3357            BOOL bothprop = lisprop && risprop;
3358    
3359            /* There's a table that specifies how each combination is to be
3360            processed:
3361              0   Always return FALSE (never auto-possessify)
3362              1   Character groups are distinct (possessify if both are OP_PROP)
3363              2   Check character categories in the same group (general or particular)
3364              3   Return TRUE if the two opcodes are not the same
3365              ... see comments below
3366            */
3367    
3368            n = propposstab[base_list[2]][list[2]];
3369            switch(n)
3370              {
3371              case 0: break;
3372              case 1: accepted = bothprop; break;
3373              case 2: accepted = (base_list[3] == list[3]) != same; break;
3374              case 3: accepted = !same; break;
3375    
3376              case 4:  /* Left general category, right particular category */
3377              accepted = risprop && catposstab[base_list[3]][list[3]] == same;
3378              break;
3379    
3380              case 5:  /* Right general category, left particular category */
3381              accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
3382              break;
3383    
3384              /* This code is logically tricky. Think hard before fiddling with it.
3385              The posspropstab table has four entries per row. Each row relates to
3386              one of PCRE's special properties such as ALNUM or SPACE or WORD.
3387              Only WORD actually needs all four entries, but using repeats for the
3388              others means they can all use the same code below.
3389    
3390              The first two entries in each row are Unicode general categories, and
3391              apply always, because all the characters they include are part of the
3392              PCRE character set. The third and fourth entries are a general and a
3393              particular category, respectively, that include one or more relevant
3394              characters. One or the other is used, depending on whether the check
3395              is for a general or a particular category. However, in both cases the
3396              category contains more characters than the specials that are defined
3397              for the property being tested against. Therefore, it cannot be used
3398              in a NOTPROP case.
3399    
3400              Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
3401              Underscore is covered by ucp_P or ucp_Po. */
3402    
3403              case 6:  /* Left alphanum vs right general category */
3404              case 7:  /* Left space vs right general category */
3405              case 8:  /* Left word vs right general category */
3406              p = posspropstab[n-6];
3407              accepted = risprop && lisprop ==
3408                (list[3] != p[0] &&
3409                 list[3] != p[1] &&
3410                (list[3] != p[2] || !lisprop));
3411              break;
3412    
3413              case 9:   /* Right alphanum vs left general category */
3414              case 10:  /* Right space vs left general category */
3415              case 11:  /* Right word vs left general category */
3416              p = posspropstab[n-9];
3417              accepted = lisprop && risprop ==
3418                (base_list[3] != p[0] &&
3419                 base_list[3] != p[1] &&
3420                (base_list[3] != p[2] || !risprop));
3421              break;
3422    
3423              case 12:  /* Left alphanum vs right particular category */
3424              case 13:  /* Left space vs right particular category */
3425              case 14:  /* Left word vs right particular category */
3426              p = posspropstab[n-12];
3427              accepted = risprop && lisprop ==
3428                (catposstab[p[0]][list[3]] &&
3429                 catposstab[p[1]][list[3]] &&
3430                (list[3] != p[3] || !lisprop));
3431              break;
3432    
3433              case 15:  /* Right alphanum vs left particular category */
3434              case 16:  /* Right space vs left particular category */
3435              case 17:  /* Right word vs left particular category */
3436              p = posspropstab[n-15];
3437              accepted = lisprop && risprop ==
3438                (catposstab[p[0]][base_list[3]] &&
3439                 catposstab[p[1]][base_list[3]] &&
3440                (base_list[3] != p[3] || !risprop));
3441              break;
3442              }
3443            }
3444          }
3445    
3446        else
3447    #endif  /* SUPPORT_UCP */
3448    
3449        accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3450               rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3451               autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3452    
3453        if (!accepted) return FALSE;
3454    
3455        if (list[1] == 0) return TRUE;
3456        /* Might be an empty repeat. */
3457        continue;
3458        }
3459    
3460      /* Control reaches here only if one of the items is a small character list.
3461      All characters are checked against the other side. */
3462    
3463      do
3464      {      {
3465      while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_space) != 0) ptr++;      chr = *chr_ptr;
3466      if (*ptr == CHAR_NUMBER_SIGN)  
3467        switch(list_ptr[0])
3468        {        {
3469        ptr++;        case OP_CHAR:
3470        while (*ptr != CHAR_NULL)        ochr_ptr = list_ptr + 2;
3471          do
3472          {          {
3473          if (IS_NEWLINE(ptr)) { ptr += cd->nllen; break; }          if (chr == *ochr_ptr) return FALSE;
3474          ptr++;          ochr_ptr++;
 #ifdef SUPPORT_UTF  
         if (utf) FORWARDCHAR(ptr);  
 #endif  
3475          }          }
3476          while(*ochr_ptr != NOTACHAR);
3477          break;
3478    
3479          case OP_NOT:
3480          ochr_ptr = list_ptr + 2;
3481          do
3482            {
3483            if (chr == *ochr_ptr)
3484              break;
3485            ochr_ptr++;
3486            }
3487          while(*ochr_ptr != NOTACHAR);
3488          if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
3489          break;
3490    
3491          /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not*
3492          set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
3493    
3494          case OP_DIGIT:
3495          if (chr < 256 && (cd->ctypes[chr] & ctype_digit) != 0) return FALSE;
3496          break;
3497    
3498          case OP_NOT_DIGIT:
3499          if (chr > 255 || (cd->ctypes[chr] & ctype_digit) == 0) return FALSE;
3500          break;
3501    
3502          case OP_WHITESPACE:
3503          if (chr < 256 && (cd->ctypes[chr] & ctype_space) != 0) return FALSE;
3504          break;
3505    
3506          case OP_NOT_WHITESPACE:
3507          if (chr > 255 || (cd->ctypes[chr] & ctype_space) == 0) return FALSE;
3508          break;
3509    
3510          case OP_WORDCHAR:
3511          if (chr < 255 && (cd->ctypes[chr] & ctype_word) != 0) return FALSE;
3512          break;
3513    
3514          case OP_NOT_WORDCHAR:
3515          if (chr > 255 || (cd->ctypes[chr] & ctype_word) == 0) return FALSE;
3516          break;
3517    
3518          case OP_HSPACE:
3519          switch(chr)
3520            {
3521            HSPACE_CASES: return FALSE;
3522            default: break;
3523            }
3524          break;
3525    
3526          case OP_NOT_HSPACE:
3527          switch(chr)
3528            {
3529            HSPACE_CASES: break;
3530            default: return FALSE;
3531            }
3532          break;
3533    
3534          case OP_ANYNL:
3535          case OP_VSPACE:
3536          switch(chr)
3537            {
3538            VSPACE_CASES: return FALSE;
3539            default: break;
3540            }
3541          break;
3542    
3543          case OP_NOT_VSPACE:
3544          switch(chr)
3545            {
3546            VSPACE_CASES: break;
3547            default: return FALSE;
3548            }
3549          break;
3550    
3551          case OP_DOLL:
3552          case OP_EODN:
3553          switch (chr)
3554            {
3555            case CHAR_CR:
3556            case CHAR_LF:
3557            case CHAR_VT:
3558            case CHAR_FF:
3559            case CHAR_NEL:
3560    #ifndef EBCDIC
3561            case 0x2028:
3562            case 0x2029:
3563    #endif  /* Not EBCDIC */
3564            return FALSE;
3565            }
3566          break;
3567    
3568          case OP_EOD:    /* Can always possessify before \z */
3569          break;
3570    
3571    #ifdef SUPPORT_UCP
3572          case OP_PROP:
3573          case OP_NOTPROP:
3574          if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3575                list_ptr[0] == OP_NOTPROP))
3576            return FALSE;
3577          break;
3578    #endif
3579    
3580          case OP_NCLASS:
3581          if (chr > 255) return FALSE;
3582          /* Fall through */
3583    
3584          case OP_CLASS:
3585          if (chr > 255) break;
3586          class_bitset = (pcre_uint8 *)
3587            ((list_ptr == list ? code : base_end) - list_ptr[2]);
3588          if ((class_bitset[chr >> 3] & (1 << (chr & 7))) != 0) return FALSE;
3589          break;
3590    
3591    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3592          case OP_XCLASS:
3593          if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
3594              list_ptr[2] + LINK_SIZE, utf)) return FALSE;
3595          break;
3596    #endif
3597    
3598          default:
3599          return FALSE;
3600        }        }
3601      else break;  
3602        chr_ptr++;
3603      }      }
3604      while(*chr_ptr != NOTACHAR);
3605    
3606      /* At least one character must be matched from this opcode. */
3607    
3608      if (list[1] == 0) return TRUE;
3609    }    }
3610    
3611  /* If the next thing is itself optional, we have to give up. */  /* Control never reaches here. There used to be a fail-save return FALSE; here,
3612    but some compilers complain about an unreachable statement. */
3613    
3614  if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  }
   STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)  
     return FALSE;  
3615    
 /* If the previous item is a character, get its value. */  
3616    
 if (op_code == OP_CHAR || op_code == OP_CHARI ||  
     op_code == OP_NOT || op_code == OP_NOTI)  
   {  
 #ifdef SUPPORT_UTF  
   GETCHARTEST(c, previous);  
 #else  
   c = *previous;  
 #endif  
   }  
3617    
3618  /* Now compare the next item with the previous opcode. First, handle cases when  /*************************************************
3619  the next item is a character. */  *    Scan compiled regex for auto-possession     *
3620    *************************************************/
3621    
3622    /* Replaces single character iterations with their possessive alternatives
3623    if appropriate. This function modifies the compiled opcode!
3624    
3625    Arguments:
3626      code        points to start of the byte code
3627      utf         TRUE in UTF-8 / UTF-16 / UTF-32 mode
3628      cd          static compile data
3629    
3630    Returns:      nothing
3631    */
3632    
3633  if (escape == 0)  static void
3634    auto_possessify(pcre_uchar *code, BOOL utf, const compile_data *cd)
3635    {
3636    register pcre_uchar c;
3637    const pcre_uchar *end;
3638    pcre_uchar *repeat_opcode;
3639    pcre_uint32 list[8];
3640    int rec_limit;
3641    
3642    for (;;)
3643    {    {
3644    /* For a caseless UTF match, the next character may have more than one other    c = *code;
   case, which maps to the special PT_CLIST property. Check this first. */  
3645    
3646  #ifdef SUPPORT_UCP    /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK,
3647    if (utf && c != NOTACHAR && (options & PCRE_CASELESS) != 0)    it may compile without complaining, but may get into a loop here if the code
3648      pointer points to a bad value. This is, of course a documentated possibility,
3649      when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and
3650      just give up on this optimization. */
3651    
3652      if (c >= OP_TABLE_LENGTH) return;
3653    
3654      if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3655      {      {
3656      unsigned int ocs = UCD_CASESET(next);      c -= get_repeat_base(c) - OP_STAR;
3657      if (ocs > 0) return check_char_prop(c, PT_CLIST, ocs, op_code >= OP_NOT);      end = (c <= OP_MINUPTO) ?
3658          get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3659        list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3660    
3661        rec_limit = 10000;
3662        if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit))
3663          {
3664          switch(c)
3665            {
3666            case OP_STAR:
3667            *code += OP_POSSTAR - OP_STAR;
3668            break;
3669    
3670            case OP_MINSTAR:
3671            *code += OP_POSSTAR - OP_MINSTAR;
3672            break;
3673    
3674            case OP_PLUS:
3675            *code += OP_POSPLUS - OP_PLUS;
3676            break;
3677    
3678            case OP_MINPLUS:
3679            *code += OP_POSPLUS - OP_MINPLUS;
3680            break;
3681    
3682            case OP_QUERY:
3683            *code += OP_POSQUERY - OP_QUERY;
3684            break;
3685    
3686            case OP_MINQUERY:
3687            *code += OP_POSQUERY - OP_MINQUERY;
3688            break;
3689    
3690            case OP_UPTO:
3691            *code += OP_POSUPTO - OP_UPTO;
3692            break;
3693    
3694            case OP_MINUPTO:
3695            *code += OP_POSUPTO - OP_MINUPTO;
3696            break;
3697            }
3698          }
3699        c = *code;
3700      }      }
3701      else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
3702        {
3703    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3704        if (c == OP_XCLASS)
3705          repeat_opcode = code + GET(code, 1);
3706        else
3707  #endif  #endif
3708          repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar));
3709    
3710    switch(op_code)      c = *repeat_opcode;
3711        if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
3712          {
3713          /* end must not be NULL. */
3714          end = get_chr_property_list(code, utf, cd->fcc, list);
3715    
3716          list[1] = (c & 1) == 0;
3717    
3718          rec_limit = 10000;
3719          if (compare_opcodes(end, utf, cd, list, end, &rec_limit))
3720            {
3721            switch (c)
3722              {
3723              case OP_CRSTAR:
3724              case OP_CRMINSTAR:
3725              *repeat_opcode = OP_CRPOSSTAR;
3726              break;
3727    
3728              case OP_CRPLUS:
3729              case OP_CRMINPLUS:
3730              *repeat_opcode = OP_CRPOSPLUS;
3731              break;
3732    
3733              case OP_CRQUERY:
3734              case OP_CRMINQUERY:
3735              *repeat_opcode = OP_CRPOSQUERY;
3736              break;
3737    
3738              case OP_CRRANGE:
3739              case OP_CRMINRANGE:
3740              *repeat_opcode = OP_CRPOSRANGE;
3741              break;
3742              }
3743            }
3744          }
3745        c = *code;
3746        }
3747    
3748      switch(c)
3749      {      {
3750      case OP_CHAR:      case OP_END:
3751      return c != next;      return;
3752    
3753      /* For CHARI (caseless character) we must check the other case. If we have      case OP_TYPESTAR:
3754      Unicode property support, we can use it to test the other case of      case OP_TYPEMINSTAR:
3755      high-valued characters. We know that next can have only one other case,      case OP_TYPEPLUS:
3756      because multi-other-case characters are dealt with above. */      case OP_TYPEMINPLUS:
3757        case OP_TYPEQUERY:
3758        case OP_TYPEMINQUERY:
3759        case OP_TYPEPOSSTAR:
3760        case OP_TYPEPOSPLUS:
3761        case OP_TYPEPOSQUERY:
3762        if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
3763        break;
3764    
3765      case OP_CHARI:      case OP_TYPEUPTO:
3766      if (c == next) return FALSE;      case OP_TYPEMINUPTO:
3767  #ifdef SUPPORT_UTF      case OP_TYPEEXACT:
3768      if (utf)      case OP_TYPEPOSUPTO:
3769        {      if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
3770        pcre_uint32 othercase;        code += 2;
3771        if (next < 128) othercase = cd->fcc[next]; else      break;
3772  #ifdef SUPPORT_UCP  
3773        othercase = UCD_OTHERCASE(next);  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3774  #else      case OP_XCLASS:
3775        othercase = NOTACHAR;      code += GET(code, 1);
3776        break;
3777  #endif  #endif
       return c != othercase;  
       }  
     else  
 #endif  /* SUPPORT_UTF */  
     return (c != TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
3778    
3779      case OP_NOT:      case OP_MARK:
3780      return c == next;      case OP_PRUNE_ARG:
3781        case OP_SKIP_ARG:
3782        case OP_THEN_ARG:
3783        code += code[1];
3784        break;
3785        }
3786    
3787      /* Add in the fixed length from the table */
3788    
3789      code += PRIV(OP_lengths)[c];
3790    
3791      /* In UTF-8 mode, opcodes that are followed by a character may be followed by
3792      a multi-byte character. The length in the table is a minimum, so we have to
3793      arrange to skip the extra bytes. */
3794    
3795    #if defined SUPPORT_UTF && !defined COMPILE_PCRE32
3796      if (utf) switch(c)
3797        {
3798        case OP_CHAR:
3799        case OP_CHARI:
3800        case OP_NOT:
3801      case OP_NOTI:      case OP_NOTI:
3802      if (c == next) return TRUE;      case OP_STAR:
3803  #ifdef SUPPORT_UTF      case OP_MINSTAR:
3804      if (utf)      case OP_PLUS:
3805        {      case OP_MINPLUS:
3806        pcre_uint32 othercase;      case OP_QUERY:
3807        if (next < 128) othercase = cd->fcc[next]; else      case OP_MINQUERY:
3808  #ifdef SUPPORT_UCP      case OP_UPTO:
3809        othercase = UCD_OTHERCASE(next);      case OP_MINUPTO:
3810        case OP_EXACT:
3811        case OP_POSSTAR:
3812        case OP_POSPLUS:
3813        case OP_POSQUERY:
3814        case OP_POSUPTO:
3815        case OP_STARI:
3816        case OP_MINSTARI:
3817        case OP_PLUSI:
3818        case OP_MINPLUSI:
3819        case OP_QUERYI:
3820        case OP_MINQUERYI:
3821        case OP_UPTOI:
3822        case OP_MINUPTOI:
3823        case OP_EXACTI:
3824        case OP_POSSTARI:
3825        case OP_POSPLUSI:
3826        case OP_POSQUERYI:
3827        case OP_POSUPTOI:
3828        case OP_NOTSTAR:
3829        case OP_NOTMINSTAR:
3830        case OP_NOTPLUS:
3831        case OP_NOTMINPLUS:
3832        case OP_NOTQUERY:
3833        case OP_NOTMINQUERY:
3834        case OP_NOTUPTO:
3835        case OP_NOTMINUPTO:
3836        case OP_NOTEXACT:
3837        case OP_NOTPOSSTAR:
3838        case OP_NOTPOSPLUS:
3839        case OP_NOTPOSQUERY:
3840        case OP_NOTPOSUPTO:
3841        case OP_NOTSTARI:
3842        case OP_NOTMINSTARI:
3843        case OP_NOTPLUSI:
3844        case OP_NOTMINPLUSI:
3845        case OP_NOTQUERYI:
3846        case OP_NOTMINQUERYI:
3847        case OP_NOTUPTOI:
3848        case OP_NOTMINUPTOI:
3849        case OP_NOTEXACTI:
3850        case OP_NOTPOSSTARI:
3851        case OP_NOTPOSPLUSI:
3852        case OP_NOTPOSQUERYI:
3853        case OP_NOTPOSUPTOI:
3854        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
3855        break;
3856        }
3857  #else  #else
3858        othercase = NOTACHAR;    (void)(utf);  /* Keep compiler happy by referencing function argument */
3859  #endif  #endif
3860        return c == othercase;    }
3861        }  }
     else  
 #endif  /* SUPPORT_UTF */  
     return (c == TABLE_GET(next, cd->fcc, next));  /* Not UTF */  
3862    
     /* Note that OP_DIGIT etc. are generated only when PCRE_UCP is *not* set.  
     When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */  
3863    
     case OP_DIGIT:  
     return next > 255 || (cd->ctypes[next] & ctype_digit) == 0;  
3864    
3865      case OP_NOT_DIGIT:  /*************************************************
3866      return next <= 255 && (cd->ctypes[next] & ctype_digit) != 0;  *           Check for POSIX class syntax         *
3867    *************************************************/
3868    
3869      case OP_WHITESPACE:  /* This function is called when the sequence "[:" or "[." or "[=" is
3870      return next > 255 || (cd->ctypes[next] & ctype_space) == 0;  encountered in a character class. It checks whether this is followed by a
3871    sequence of characters terminated by a matching ":]" or ".]" or "=]". If we
3872    reach an unescaped ']' without the special preceding character, return FALSE.
3873    
3874      case OP_NOT_WHITESPACE:  Originally, this function only recognized a sequence of letters between the
3875      return next <= 255 && (cd->ctypes[next] & ctype_space) != 0;  terminators, but it seems that Perl recognizes any sequence of characters,
3876    though of course unknown POSIX names are subsequently rejected. Perl gives an
3877    "Unknown POSIX class" error for [:f\oo:] for example, where previously PCRE
3878    didn't consider this to be a POSIX class. Likewise for [:1234:].
3879    
3880      case OP_WORDCHAR:  The problem in trying to be exactly like Perl is in the handling of escapes. We
3881      return next > 255 || (cd->ctypes[next] & ctype_word) == 0;  have to be sure that [abc[:x\]pqr] is *not* treated as containing a POSIX
3882    class, but [abc[:x\]pqr:]] is (so that an error can be generated). The code
3883    below handles the special case of \], but does not try to do any other escape
3884    processing. This makes it different from Perl for cases such as [:l\ower:]
3885    where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3886    "l\ower". This is a lesser evil than not diagnosing bad classes when Perl does,
3887    I think.
3888    
3889      case OP_NOT_WORDCHAR:  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
3890      return next <= 255 && (cd->ctypes[next] & ctype_word) != 0;  It seems that the appearance of a nested POSIX class supersedes an apparent
3891    external class. For example, [:a[:digit:]b:] matches "a", "b", ":", or
3892    a digit.
3893    
3894      case OP_HSPACE:  In Perl, unescaped square brackets may also appear as part of class names. For
3895      case OP_NOT_HSPACE:  example, [:a[:abc]b:] gives unknown POSIX class "[:abc]b:]". However, for
3896      switch(next)  [:a[:abc]b][b:] it gives unknown POSIX class "[:abc]b][b:]", which does not
3897        {  seem right at all. PCRE does not allow closing square brackets in POSIX class
3898        HSPACE_CASES:  names.
       return op_code == OP_NOT_HSPACE;  
3899    
3900        default:  Arguments:
3901        return op_code != OP_NOT_HSPACE;    ptr      pointer to the initial [
3902        }    endptr   where to return the end pointer
3903    
3904      case OP_ANYNL:  Returns:   TRUE or FALSE
3905      case OP_VSPACE:  */
     case OP_NOT_VSPACE:  
     switch(next)  
       {  
       VSPACE_CASES:  
       return op_code == OP_NOT_VSPACE;  
3906    
3907        default:  static BOOL
3908        return op_code != OP_NOT_VSPACE;  check_posix_syntax(const pcre_uchar *ptr, const pcre_uchar **endptr)
3909    {
3910    pcre_uchar terminator;          /* Don't combine these lines; the Solaris cc */
3911    terminator = *(++ptr);   /* compiler warns about "non-constant" initializer. */
3912    for (++ptr; *ptr != CHAR_NULL; ptr++)
3913      {
3914      if (*ptr == CHAR_BACKSLASH && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3915        ptr++;
3916      else if (*ptr == CHAR_RIGHT_SQUARE_BRACKET) return FALSE;
3917      else
3918        {
3919        if (*ptr == terminator && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
3920          {
3921          *endptr = ptr;
3922          return TRUE;
3923        }        }
3924        if (*ptr == CHAR_LEFT_SQUARE_BRACKET &&
3925             (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
3926              ptr[1] == CHAR_EQUALS_SIGN) &&
3927            check_posix_syntax(ptr, endptr))
3928          return FALSE;
3929        }
3930      }
3931    return FALSE;
3932    }
3933    
 #ifdef SUPPORT_UCP  
     case OP_PROP:  
     return check_char_prop(next, previous[0], previous[1], FALSE);  
3934    
     case OP_NOTPROP:  
     return check_char_prop(next, previous[0], previous[1], TRUE);  
 #endif  
3935    
3936      default:  
3937      return FALSE;  /*************************************************
3938      }  *          Check POSIX class name                *
3939    *************************************************/
3940    
3941    /* This function is called to check the name given in a POSIX-style class entry
3942    such as [:alnum:].
3943    
3944    Arguments:
3945      ptr        points to the first letter
3946      len        the length of the name
3947    
3948    Returns:     a value representing the name, or -1 if unknown
3949    */
3950    
3951    static int
3952    check_posix_name(const pcre_uchar *ptr, int len)
3953    {
3954    const char *pn = posix_names;
3955    register int yield = 0;
3956    while (posix_name_lengths[yield] != 0)
3957      {
3958      if (len == posix_name_lengths[yield] &&
3959        STRNCMP_UC_C8(ptr, pn, (unsigned int)len) == 0) return yield;
3960      pn += posix_name_lengths[yield] + 1;
3961      yield++;
3962    }    }
3963    return -1;
3964    }
3965    
3966    
3967    /*************************************************
3968    *    Adjust OP_RECURSE items in repeated group   *
3969    *************************************************/
3970    
3971    /* OP_RECURSE items contain an offset from the start of the regex to the group
3972    that is referenced. This means that groups can be replicated for fixed
3973    repetition simply by copying (because the recursion is allowed to refer to
3974    earlier groups that are outside the current group). However, when a group is
3975    optional (i.e. the minimum quantifier is zero), OP_BRAZERO or OP_SKIPZERO is
3976    inserted before it, after it has been compiled. This means that any OP_RECURSE
3977    items within it that refer to the group itself or any contained groups have to
3978    have their offsets adjusted. That one of the jobs of this function. Before it
3979    is called, the partially compiled regex must be temporarily terminated with
3980    OP_END.
3981    
3982    This function has been extended with the possibility of forward references for
3983    recursions and subroutine calls. It must also check the list of such references
3984    for the group we are dealing with. If it finds that one of the recursions in
3985    the current group is on this list, it adjusts the offset in the list, not the
3986    value in the reference (which is a group number).
3987    
3988    Arguments:
3989      group      points to the start of the group
3990      adjust     the amount by which the group is to be moved
3991      utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3992      cd         contains pointers to tables etc.
3993      save_hwm_offset   the hwm forward reference offset at the start of the group
3994    
3995    Returns:     nothing
3996    */
3997    
3998  /* Handle the case when the next item is \d, \s, etc. Note that when PCRE_UCP  static void
3999  is set, \d turns into ESC_du rather than ESC_d, etc., so ESC_d etc. are  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
4000  generated only when PCRE_UCP is *not* set, that is, when only ASCII    size_t save_hwm_offset)
4001  characteristics are recognized. Similarly, the opcodes OP_DIGIT etc. are  {
4002  replaced by OP_PROP codes when PCRE_UCP is set. */  pcre_uchar *ptr = group;
4003    
4004  switch(op_code)  while ((ptr = (pcre_uchar *)find_recurse(ptr, utf)) != NULL)
4005    {    {
4006    case OP_CHAR:    int offset;
4007    case OP_CHARI:    pcre_uchar *hc;
4008    switch(escape)  
4009      /* See if this recursion is on the forward reference list. If so, adjust the
4010      reference. */
4011    
4012      for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
4013           hc += LINK_SIZE)
4014      {      {
4015      case ESC_d:      offset = (int)GET(hc, 0);
4016      return c > 255 || (cd->ctypes[c] & ctype_digit) == 0;      if (cd->start_code + offset == ptr + 1)
4017          {
4018          PUT(hc, 0, offset + adjust);
4019          break;
4020          }
4021        }
4022    
4023      case ESC_D:    /* Otherwise, adjust the recursion offset if it's after the start of this
4024      return c <= 255 && (cd->ctypes[c] & ctype_digit) != 0;    group. */
4025    
4026      case ESC_s:    if (hc >= cd->hwm)
4027      return c > 255 || (cd->ctypes[c] & ctype_space) == 0;      {
4028        offset = (int)GET(ptr, 1);
4029        if (cd->start_code + offset >= group) PUT(ptr, 1, offset + adjust);
4030        }
4031    
4032      case ESC_S:    ptr += 1 + LINK_SIZE;
4033      return c <= 255 && (cd->ctypes[c] & ctype_space) != 0;    }
4034    }
4035    
     case ESC_w:  
     return c > 255 || (cd->ctypes[c] & ctype_word) == 0;  
4036    
     case ESC_W:  
     return c <= 255 && (cd->ctypes[c] & ctype_word) != 0;  
4037    
4038      case ESC_h:  /*************************************************
4039      case ESC_H:  *        Insert an automatic callout point       *
4040      switch(c)  *************************************************/
       {  
       HSPACE_CASES:  
       return escape != ESC_h;  
4041    
4042        default:  /* This function is called when the PCRE_AUTO_CALLOUT option is set, to insert
4043        return escape == ESC_h;  callout points before each pattern item.
       }  
4044    
4045      case ESC_v:  Arguments:
4046      case ESC_V:    code           current code pointer
4047      switch(c)    ptr            current pattern pointer
4048        {    cd             pointers to tables etc
       VSPACE_CASES:  
       return escape != ESC_v;  
4049    
4050        default:  Returns:         new code pointer
4051        return escape == ESC_v;  */
       }  
4052    
4053      /* When PCRE_UCP is set, these values get generated for \d etc. Find  static pcre_uchar *
4054      their substitutions and process them. The result will always be either  auto_callout(pcre_uchar *code, const pcre_uchar *ptr, compile_data *cd)
4055      ESC_p or ESC_P. Then fall through to process those values. */  {
4056    *code++ = OP_CALLOUT;
4057    *code++ = 255;
4058    PUT(code, 0, (int)(ptr - cd->start_pattern));  /* Pattern offset */
4059    PUT(code, LINK_SIZE, 0);                       /* Default length */
4060    return code + 2 * LINK_SIZE;
4061    }
4062    
 #ifdef SUPPORT_UCP  
     case ESC_du:  
     case ESC_DU:  
     case ESC_wu:  
     case ESC_WU:  
     case ESC_su:  
     case ESC_SU:  
       {  
       int temperrorcode = 0;  
       ptr = substitutes[escape - ESC_DU];  
       escape = check_escape(&ptr, &next, &temperrorcode, 0, options, FALSE);  
       if (temperrorcode != 0) return FALSE;  
       ptr++;    /* For compatibility */  
       }  
     /* Fall through */  
4063    
     case ESC_p:  
     case ESC_P:  
       {  
       unsigned int ptype = 0, pdata = 0;  
       int errorcodeptr;  
       BOOL negated;  
4064    
4065        ptr--;      /* Make ptr point at the p or P */  /*************************************************
4066        if (!get_ucp(&ptr, &negated, &ptype, &pdata, &errorcodeptr))  *         Complete a callout item                *
4067          return FALSE;  *************************************************/
       ptr++;      /* Point past the final curly ket */  
4068    
4069        /* If the property item is optional, we have to give up. (When generated  /* A callout item contains the length of the next item in the pattern, which
4070        from \d etc by PCRE_UCP, this test will have been applied much earlier,  we can't fill in till after we have reached the relevant point. This is used
4071        to the original \d etc. At this point, ptr will point to a zero byte. */  for both automatic and manual callouts.
4072    
4073        if (*ptr == CHAR_ASTERISK || *ptr == CHAR_QUESTION_MARK ||  Arguments:
4074          STRNCMP_UC_C8(ptr, STR_LEFT_CURLY_BRACKET STR_0 STR_COMMA, 3) == 0)    previous_callout   points to previous callout item
4075            return FALSE;    ptr                current pattern pointer
4076      cd                 pointers to tables etc
4077    
4078        /* Do the property check. */  Returns:             nothing
4079    */
4080    
4081        return check_char_prop(c, ptype, pdata, (escape == ESC_P) != negated);  static void
4082        }  complete_callout(pcre_uchar *previous_callout, const pcre_uchar *ptr, compile_data *cd)
4083  #endif  {
4084    int length = (int)(ptr - cd->start_pattern - GET(previous_callout, 2));
4085    PUT(previous_callout, 2 + LINK_SIZE, length);
4086    }
4087    
     default:  
     return FALSE;  
     }  
4088    
   /* In principle, support for Unicode properties should be integrated here as  
   well. It means re-organizing the above code so as to get hold of the property  
   values before switching on the op-code. However, I wonder how many patterns  
   combine ASCII \d etc with Unicode properties? (Note that if PCRE_UCP is set,  
   these op-codes are never generated.) */  
4089    
4090    case OP_DIGIT:  #ifdef SUPPORT_UCP
4091    return escape == ESC_D || escape == ESC_s || escape == ESC_W ||  /*************************************************
4092           escape == ESC_h || escape == ESC_v || escape == ESC_R;  *           Get othercase range                  *
4093    *************************************************/
4094    
4095    case OP_NOT_DIGIT:  /* This function is passed the start and end of a class range, in UTF-8 mode
4096    return escape == ESC_d;  with UCP support. It searches up the characters, looking for ranges of
4097    characters in the "other" case. Each call returns the next one, updating the
4098    start address. A character with multiple other cases is returned on its own
4099    with a special return value.
4100    
4101    case OP_WHITESPACE:  Arguments:
4102    return escape == ESC_S || escape == ESC_d || escape == ESC_w;    cptr        points to starting character value; updated
4103      d           end value
4104      ocptr       where to put start of othercase range
4105      odptr       where to put end of othercase range
4106    
4107    case OP_NOT_WHITESPACE:  Yield:        -1 when no more
4108    return escape == ESC_s || escape == ESC_h || escape == ESC_v || escape == ESC_R;                 0 when a range is returned
4109                  >0 the CASESET offset for char with multiple other cases
4110                    in this case, ocptr contains the original
4111    */
4112    
4113    case OP_HSPACE:  static int
4114    return escape == ESC_S || escape == ESC_H || escape == ESC_d ||  get_othercase_range(pcre_uint32 *cptr, pcre_uint32 d, pcre_uint32 *ocptr,
4115           escape == ESC_w || escape == ESC_v || escape == ESC_R;    pcre_uint32 *odptr)
4116    {
4117    pcre_uint32 c, othercase, next;
4118    unsigned int co;
4119    
4120    case OP_NOT_HSPACE:  /* Find the first character that has an other case. If it has multiple other
4121    return escape == ESC_h;  cases, return its case offset value. */
4122    
4123    /* Can't have \S in here because VT matches \S (Perl anomaly) */  for (c = *cptr; c <= d; c++)
4124    case OP_ANYNL:    {
4125    case OP_VSPACE:    if ((co = UCD_CASESET(c)) != 0)
4126    return escape == ESC_V || escape == ESC_d || escape == ESC_w;      {
4127        *ocptr = c++;   /* Character that has the set */
4128        *cptr = c;      /* Rest of input range */
4129        return (int)co;
4130        }
4131      if ((othercase = UCD_OTHERCASE(c)) != c) break;
4132      }
4133    
4134    case OP_NOT_VSPACE:  if (c > d) return -1;  /* Reached end of range */
   return escape == ESC_v || escape == ESC_R;  
4135    
4136    case OP_WORDCHAR:  /* Found a character that has a single other case. Search for the end of the
4137    return escape == ESC_W || escape == ESC_s || escape == ESC_h ||  range, which is either the end of the input range, or a character that has zero
4138           escape == ESC_v || escape == ESC_R;  or more than one other cases. */
4139    
4140    case OP_NOT_WORDCHAR:  *ocptr = othercase;
4141    return escape == ESC_w || escape == ESC_d;  next = othercase + 1;
4142    
4143    default:  for (++c; c <= d; c++)
4144    return FALSE;    {
4145      if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break;
4146      next++;
4147    }    }
4148    
4149  /* Control does not reach here */  *odptr = next - 1;     /* End of othercase range */
4150    *cptr = c;             /* Rest of input range */
4151    return 0;
4152  }  }
4153    #endif  /* SUPPORT_UCP */
4154    
4155    
4156    
# Line 3447  add_to_class(pcre_uint8 *classbits, pcre Line 4180  add_to_class(pcre_uint8 *classbits, pcre
4180    compile_data *cd, pcre_uint32 start, pcre_uint32 end)    compile_data *cd, pcre_uint32 start, pcre_uint32 end)
4181  {  {
4182  pcre_uint32 c;  pcre_uint32 c;
4183    pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff);
4184  int n8 = 0;  int n8 = 0;
4185    
4186  /* If caseless matching is required, scan the range and process alternate  /* If caseless matching is required, scan the range and process alternate
# Line 3481  if ((options & PCRE_CASELESS) != 0) Line 4215  if ((options & PCRE_CASELESS) != 0)
4215        range. Otherwise, use a recursive call to add the additional range. */        range. Otherwise, use a recursive call to add the additional range. */
4216    
4217        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
4218        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */        else if (od > end && oc <= end + 1)
4219            {
4220            end = od;       /* Extend upwards */
4221            if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff);
4222            }
4223        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
4224        }        }
4225      }      }
# Line 3490  if ((options & PCRE_CASELESS) != 0) Line 4228  if ((options & PCRE_CASELESS) != 0)
4228    
4229    /* Not UTF-mode, or no UCP */    /* Not UTF-mode, or no UCP */
4230    
4231    for (c = start; c <= end && c < 256; c++)    for (c = start; c <= classbits_end; c++)
4232      {      {
4233      SETBIT(classbits, cd->fcc[c]);      SETBIT(classbits, cd->fcc[c]);
4234      n8++;      n8++;
# Line 3515  in all cases. */ Line 4253  in all cases. */
4253    
4254  #endif /* COMPILE_PCRE[8|16] */  #endif /* COMPILE_PCRE[8|16] */
4255    
4256  /* If all characters are less than 256, use the bit map. Otherwise use extra  /* Use the bitmap for characters < 256. Otherwise use extra data.*/
 data. */  
4257    
4258  if (end < 0x100)  for (c = start; c <= classbits_end; c++)
4259    {    {
4260    for (c = start; c <= end; c++)    /* Regardless of start, c will always be <= 255. */
4261      {    SETBIT(classbits, c);
4262      n8++;    n8++;
     SETBIT(classbits, c);  
     }  
4263    }    }
4264    
4265  else  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4266    if (start <= 0xff) start = 0xff + 1;
4267    
4268    if (end >= start)
4269    {    {
4270    pcre_uchar *uchardata = *uchardptr;    pcre_uchar *uchardata = *uchardptr;
   
4271  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4272    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
4273      {      {
# Line 3570  else Line 4307  else
4307    
4308    *uchardptr = uchardata;   /* Updata extra data pointer */    *uchardptr = uchardata;   /* Updata extra data pointer */
4309    }    }
4310    #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
4311    
4312  return n8;    /* Number of 8-bit characters */  return n8;    /* Number of 8-bit characters */
4313  }  }
# Line 3670  to find out the amount of memory needed, Line 4408  to find out the amount of memory needed,
4408  phase. The value of lengthptr distinguishes the two phases.  phase. The value of lengthptr distinguishes the two phases.
4409    
4410  Arguments:  Arguments:
4411    optionsptr     pointer to the option bits    optionsptr        pointer to the option bits
4412    codeptr        points to the pointer to the current code point    codeptr           points to the pointer to the current code point
4413    ptrptr         points to the current pattern pointer    ptrptr            points to the current pattern pointer
4414    errorcodeptr   points to error code variable    errorcodeptr      points to error code variable
4415    firstcharptr    place to put the first required character    firstcharptr      place to put the first required character
4416    firstcharflagsptr place to put the first character flags, or a negative number    firstcharflagsptr place to put the first character flags, or a negative number
4417    reqcharptr     place to put the last required character    reqcharptr        place to put the last required character
4418    reqcharflagsptr place to put the last required character flags, or a negative number    reqcharflagsptr   place to put the last required character flags, or a negative number
4419    bcptr          points to current branch chain    bcptr             points to current branch chain
4420    cond_depth     conditional nesting depth    cond_depth        conditional nesting depth
4421    cd             contains pointers to tables etc.    cd                contains pointers to tables etc.
4422    lengthptr      NULL during the real compile phase    lengthptr         NULL during the real compile phase
4423                   points to length accumulator during pre-compile phase                      points to length accumulator during pre-compile phase
4424    
4425  Returns:         TRUE on success  Returns:            TRUE on success
4426                   FALSE, with *errorcodeptr set non-zero on error                      FALSE, with *errorcodeptr set non-zero on error
4427  */  */
4428    
4429  static BOOL  static BOOL
# Line 3721  const pcre_uchar *tempptr; Line 4459  const pcre_uchar *tempptr;
4459  const pcre_uchar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
4460  pcre_uchar *previous = NULL;  pcre_uchar *previous = NULL;
4461  pcre_uchar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
4462  pcre_uchar *save_hwm = NULL;  size_t save_hwm_offset = 0;
4463  pcre_uint8 classbits[32];  pcre_uint8 classbits[32];
4464    
4465  /* 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
# Line 3791  for (;; ptr++) Line 4529  for (;; ptr++)
4529    BOOL reset_bracount;    BOOL reset_bracount;
4530    int class_has_8bitchar;    int class_has_8bitchar;
4531    int class_one_char;    int class_one_char;
4532    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4533      BOOL xclass_has_prop;
4534    #endif
4535    int newoptions;    int newoptions;
4536    int recno;    int recno;
4537    int refsign;    int refsign;
# Line 3908  for (;; ptr++) Line 4649  for (;; ptr++)
4649          }          }
4650        goto NORMAL_CHAR;        goto NORMAL_CHAR;
4651        }        }
4652        /* Control does not reach here. */
4653      }      }
4654    
4655    /* Fill in length of a previous callout, except when the next thing is    /* In extended mode, skip white space and comments. We need a loop in order
4656    a quantifier. */    to check for more white space and more comments after a comment. */
   
   is_quantifier =  
     c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||  
     (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));  
   
   if (!is_quantifier && previous_callout != NULL &&  
        after_manual_callout-- <= 0)  
     {  
     if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */  
       complete_callout(previous_callout, ptr, cd);  
     previous_callout = NULL;  
     }  
   
   /* In extended mode, skip white space and comments. */  
4657    
4658    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
4659      {      {
4660      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;      for (;;)
     if (c == CHAR_NUMBER_SIGN)  
4661        {        {
4662          while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr);
4663          if (c != CHAR_NUMBER_SIGN) break;
4664        ptr++;        ptr++;
4665        while (*ptr != CHAR_NULL)        while (*ptr != CHAR_NULL)
4666          {          {
4667          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr))         /* For non-fixed-length newline cases, */
4668              {                          /* IS_NEWLINE sets cd->nllen. */
4669              ptr += cd->nllen;
4670              break;
4671              }
4672          ptr++;          ptr++;
4673  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4674          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
4675  #endif  #endif
4676          }          }
4677        if (*ptr != CHAR_NULL) continue;        c = *ptr;     /* Either NULL or the char after a newline */
   
       /* Else fall through to handle end of string */  
       c = 0;  
4678        }        }
4679      }      }
4680    
4681    /* No auto callout for quantifiers. */    /* See if the next thing is a quantifier. */
4682    
4683    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier)    is_quantifier =
4684        c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4685        (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4686    
4687      /* Fill in length of a previous callout, except when the next thing is a
4688      quantifier or when processing a property substitution string in UCP mode. */
4689    
4690      if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4691           after_manual_callout-- <= 0)
4692        {
4693        if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
4694          complete_callout(previous_callout, ptr, cd);
4695        previous_callout = NULL;
4696        }
4697    
4698      /* Create auto callout, except for quantifiers, or while processing property
4699      strings that are substituted for \w etc in UCP mode. */
4700    
4701      if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4702      {      {
4703      previous_callout = code;      previous_callout = code;
4704      code = auto_callout(code, ptr, cd);      code = auto_callout(code, ptr, cd);
4705      }      }
4706    
4707      /* Process the next pattern item. */
4708    
4709    switch(c)    switch(c)
4710      {      {
4711      /* ===================================================================*/      /* ===================================================================*/
4712      case 0:                        /* The branch terminates at string end */      case CHAR_NULL:                /* The branch terminates at string end */
4713      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
4714      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
4715      *firstcharptr = firstchar;      *firstcharptr = firstchar;
# Line 3989  for (;; ptr++) Line 4739  for (;; ptr++)
4739      previous = NULL;      previous = NULL;
4740      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
4741        {        {
4742        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;        if (firstcharflags == REQ_UNSET)
4743            zerofirstcharflags = firstcharflags = REQ_NONE;
4744        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
4745        }        }
4746      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 4037  for (;; ptr++) Line 4788  for (;; ptr++)
4788        }        }
4789      goto NORMAL_CHAR;      goto NORMAL_CHAR;
4790    
4791        /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
4792        used for "start of word" and "end of word". As these are otherwise illegal
4793        sequences, we don't break anything by recognizing them. They are replaced
4794        by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
4795        erroneous and are handled by the normal code below. */
4796    
4797      case CHAR_LEFT_SQUARE_BRACKET:      case CHAR_LEFT_SQUARE_BRACKET:
4798        if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0)
4799          {
4800          nestptr = ptr + 7;
4801          ptr = sub_start_of_word - 1;
4802          continue;
4803          }
4804    
4805        if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0)
4806          {
4807          nestptr = ptr + 7;
4808          ptr = sub_end_of_word - 1;
4809          continue;
4810          }
4811    
4812        /* Handle a real character class. */
4813    
4814      previous = code;      previous = code;
4815    
4816      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
# Line 4094  for (;; ptr++) Line 4867  for (;; ptr++)
4867    
4868      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4869    
4870        /* Extended class (xclass) will be used when characters > 255
4871        might match. */
4872    
4873    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4874        xclass = FALSE;
4875        class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4876        class_uchardata_base = class_uchardata;   /* Save the start */
4877    #endif
4878    
4879      /* For optimization purposes, we track some properties of the class:      /* For optimization purposes, we track some properties of the class:
4880      class_has_8bitchar will be non-zero if the class contains at least one <      class_has_8bitchar will be non-zero if the class contains at least one <
4881      256 character; class_one_char will be 1 if the class contains just one      256 character; class_one_char will be 1 if the class contains just one
4882      character. */      character; xclass_has_prop will be TRUE if unicode property checks
4883        are present in the class. */
4884    
4885      class_has_8bitchar = 0;      class_has_8bitchar = 0;
4886      class_one_char = 0;      class_one_char = 0;
4887    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4888        xclass_has_prop = FALSE;
4889    #endif
4890    
4891      /* 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
4892      temporary bit of memory, in case the class contains fewer than two      temporary bit of memory, in case the class contains fewer than two
# Line 4109  for (;; ptr++) Line 4895  for (;; ptr++)
4895    
4896      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4897    
 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  
     xclass = FALSE;  
     class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */  
     class_uchardata_base = class_uchardata;   /* Save the start */  
 #endif  
   
4898      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
4899      means that an initial ] is taken as a data character. At the start of the      means that an initial ] is taken as a data character. At the start of the
4900      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
# Line 4140  for (;; ptr++) Line 4920  for (;; ptr++)
4920        if (lengthptr != NULL && class_uchardata > class_uchardata_base)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4921          {          {
4922          xclass = TRUE;          xclass = TRUE;
4923          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += (int)(class_uchardata - class_uchardata_base);
4924          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
4925          }          }
4926  #endif  #endif
# Line 4202  for (;; ptr++) Line 4982  for (;; ptr++)
4982            posix_class = 0;            posix_class = 0;
4983    
4984          /* 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
4985          different escape sequences that use Unicode properties. */          different escape sequences that use Unicode properties \p or \P. Others
4986            that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP
4987            directly. */
4988    
4989  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4990          if ((options & PCRE_UCP) != 0)          if ((options & PCRE_UCP) != 0)
4991            {            {
4992              unsigned int ptype = 0;
4993            int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);            int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
4994    
4995              /* The posix_substitutes table specifies which POSIX classes can be
4996              converted to \p or \P items. */
4997    
4998            if (posix_substitutes[pc] != NULL)            if (posix_substitutes[pc] != NULL)
4999              {              {
5000              nestptr = tempptr + 1;              nestptr = tempptr + 1;
5001              ptr = posix_substitutes[pc] - 1;              ptr = posix_substitutes[pc] - 1;
5002              continue;              continue;
5003              }              }
5004    
5005              /* There are three other classes that generate special property calls
5006              that are recognized only in an XCLASS. */
5007    
5008              else switch(posix_class)
5009                {
5010                case PC_GRAPH:
5011                ptype = PT_PXGRAPH;
5012                /* Fall through */
5013                case PC_PRINT:
5014                if (ptype == 0) ptype = PT_PXPRINT;
5015                /* Fall through */
5016                case PC_PUNCT:
5017                if (ptype == 0) ptype = PT_PXPUNCT;
5018                *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP;
5019                *class_uchardata++ = ptype;
5020                *class_uchardata++ = 0;
5021                xclass_has_prop = TRUE;
5022                ptr = tempptr + 1;
5023                continue;
5024    
5025                /* For all other POSIX classes, no special action is taken in UCP
5026                mode. Fall through to the non_UCP case. */
5027    
5028                default:
5029                break;
5030                }
5031            }            }
5032  #endif  #endif
5033          /* In the non-UCP case, we build the bit map for the POSIX class in a          /* In the non-UCP case, or when UCP makes no difference, we build the
5034          chunk of local store because we may be adding and subtracting from it,          bit map for the POSIX class in a chunk of local store because we may be
5035          and we don't want to subtract bits that may be in the main map already.          adding and subtracting from it, and we don't want to subtract bits that
5036          At the end we or the result into the bit map that is being built. */          may be in the main map already. At the end we or the result into the
5037            bit map that is being built. */
5038    
5039          posix_class *= 3;          posix_class *= 3;
5040    
# Line 4336  for (;; ptr++) Line 5151  for (;; ptr++)
5151              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_word];
5152              continue;              continue;
5153    
5154              /* Perl 5.004 onwards omits VT from \s, but we must preserve it              /* Perl 5.004 onwards omitted VT from \s, but restored it at Perl
5155              if it was previously set by something earlier in the character              5.18. Before PCRE 8.34, we had to preserve the VT bit if it was
5156              class. Luckily, the value of CHAR_VT is 0x0b in both ASCII and              previously set by something earlier in the character class.
5157              EBCDIC, so we lazily just adjust the appropriate bit. */              Luckily, the value of CHAR_VT is 0x0b in both ASCII and EBCDIC, so
5158                we could just adjust the appropriate bit. From PCRE 8.34 we no
5159                longer treat \s and \S specially. */
5160    
5161              case ESC_s:              case ESC_s:
5162              classbits[0] |= cbits[cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= cbits[c+cbit_space];
             classbits[1] |= cbits[cbit_space+1] & ~0x08;  
             for (c = 2; c < 32; c++) classbits[c] |= cbits[c+cbit_space];  
5163              continue;              continue;
5164    
5165              case ESC_S:              case ESC_S:
5166              should_flip_negation = TRUE;              should_flip_negation = TRUE;
5167              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];              for (c = 0; c < 32; c++) classbits[c] |= ~cbits[c+cbit_space];
             classbits[1] |= 0x08;    /* Perl 5.004 onwards omits VT from \s */  
5168              continue;              continue;
5169    
5170              /* The rest apply in both UCP and non-UCP cases. */              /* The rest apply in both UCP and non-UCP cases. */
# Line 4387  for (;; ptr++) Line 5201  for (;; ptr++)
5201                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
5202                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
5203                *class_uchardata++ = pdata;                *class_uchardata++ = pdata;
5204                  xclass_has_prop = TRUE;
5205                class_has_8bitchar--;                /* Undo! */                class_has_8bitchar--;                /* Undo! */
5206                continue;                continue;
5207                }                }
# Line 4472  for (;; ptr++) Line 5287  for (;; ptr++)
5287  #endif  #endif
5288          d = *ptr;  /* Not UTF-8 mode */          d = *ptr;  /* Not UTF-8 mode */
5289    
5290          /* The second part of a range can be a single-character escape, but          /* The second part of a range can be a single-character escape
5291          not any of the other escapes. Perl 5.6 treats a hyphen as a literal          sequence, but not any of the other escapes. Perl treats a hyphen as a
5292          in such circumstances. */          literal in such circumstances. However, in Perl's warning mode, a
5293            warning is given, so PCRE now faults it as it is almost certainly a
5294            mistake on the user's part. */
5295    
5296          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq)
5297            {            {
5298            int descape;            if (d == CHAR_BACKSLASH)
5299            descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);              {
5300            if (*errorcodeptr != 0) goto FAILED;              int descape;
5301                descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
5302                if (*errorcodeptr != 0) goto FAILED;
5303    
5304            /* \b is backspace; any other special means the '-' was literal. */              /* 0 means a character was put into d; \b is backspace; any other
5305                special causes an error. */
5306    
5307            if (descape != 0)              if (descape != 0)
             {  
             if (descape == ESC_b) d = CHAR_BS; else  
5308                {                {
5309                ptr = oldptr;                if (descape == ESC_b) d = CHAR_BS; else
5310                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */                  {
5311                    *errorcodeptr = ERR83;
5312                    goto FAILED;
5313                    }
5314                }                }
5315              }              }
5316    
5317              /* A hyphen followed by a POSIX class is treated in the same way. */
5318    
5319              else if (d == CHAR_LEFT_SQUARE_BRACKET &&
5320                       (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
5321                        ptr[1] == CHAR_EQUALS_SIGN) &&
5322                       check_posix_syntax(ptr, &tempptr))
5323                {
5324                *errorcodeptr = ERR83;
5325                goto FAILED;
5326                }
5327            }            }
5328    
5329          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
# Line 4538  for (;; ptr++) Line 5370  for (;; ptr++)
5370        whatever repeat count may follow. In the case of reqchar, save the        whatever repeat count may follow. In the case of reqchar, save the
5371        previous value for reinstating. */        previous value for reinstating. */
5372    
5373        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)        if (!inescq && class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
5374          {          {
5375          ptr++;          ptr++;
5376          zeroreqchar = reqchar;          zeroreqchar = reqchar;
# Line 4664  for (;; ptr++) Line 5496  for (;; ptr++)
5496        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
5497        code += LINK_SIZE;        code += LINK_SIZE;
5498        *code = negate_class? XCL_NOT:0;        *code = negate_class? XCL_NOT:0;
5499          if (xclass_has_prop) *code |= XCL_HASPROP;
5500    
5501        /* 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;
5502        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. */
# Line 4673  for (;; ptr++) Line 5506  for (;; ptr++)
5506          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
5507          memmove(code + (32 / sizeof(pcre_uchar)), code,          memmove(code + (32 / sizeof(pcre_uchar)), code,
5508            IN_UCHARS(class_uchardata - code));            IN_UCHARS(class_uchardata - code));
5509            if (negate_class && !xclass_has_prop)
5510              for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
5511          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
5512          code = class_uchardata + (32 / sizeof(pcre_uchar));          code = class_uchardata + (32 / sizeof(pcre_uchar));
5513          }          }
# Line 4685  for (;; ptr++) Line 5520  for (;; ptr++)
5520        }        }
5521  #endif  #endif
5522    
5523        /* Even though any XCLASS list is now discarded, we must allow for
5524        its memory. */
5525    
5526        if (lengthptr != NULL)
5527          *lengthptr += (int)(class_uchardata - class_uchardata_base);
5528    
5529      /* If there are no characters > 255, or they are all to be included or      /* If there are no characters > 255, or they are all to be included or
5530      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
5531      whole class was negated and whether there were negative specials such as \S      whole class was negated and whether there were negative specials such as \S
# Line 4755  for (;; ptr++) Line 5596  for (;; ptr++)
5596    
5597      tempcode = previous;      tempcode = previous;
5598    
5599        /* Before checking for a possessive quantifier, we must skip over
5600        whitespace and comments in extended mode because Perl allows white space at
5601        this point. */
5602    
5603        if ((options & PCRE_EXTENDED) != 0)
5604          {
5605          const pcre_uchar *p = ptr + 1;
5606          for (;;)
5607            {
5608            while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++;
5609            if (*p != CHAR_NUMBER_SIGN) break;
5610            p++;
5611            while (*p != CHAR_NULL)
5612              {
5613              if (IS_NEWLINE(p))         /* For non-fixed-length newline cases, */
5614                {                        /* IS_NEWLINE sets cd->nllen. */
5615                p += cd->nllen;
5616                break;
5617                }
5618              p++;
5619    #ifdef SUPPORT_UTF
5620              if (utf) FORWARDCHAR(p);
5621    #endif
5622              }           /* Loop for comment characters */
5623            }             /* Loop for multiple comments */
5624          ptr = p - 1;    /* Character before the next significant one. */
5625          }
5626    
5627      /* If the next character is '+', we have a possessive quantifier. This      /* If the next character is '+', we have a possessive quantifier. This
5628      implies greediness, whatever the setting of the PCRE_UNGREEDY option.      implies greediness, whatever the setting of the PCRE_UNGREEDY option.
5629      If the next character is '?' this is a minimizing repeat, by default,      If the next character is '?' this is a minimizing repeat, by default,
# Line 4849  for (;; ptr++) Line 5718  for (;; ptr++)
5718            }            }
5719          }          }
5720    
       /* If the repetition is unlimited, it pays to see if the next thing on  
       the line is something that cannot possibly match this character. If so,  
       automatically possessifying this item gains some performance in the case  
       where the match fails. */  
   
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5721        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */        goto OUTPUT_SINGLE_REPEAT;   /* Code shared with single character types */
5722        }        }
5723    
# Line 4879  for (;; ptr++) Line 5735  for (;; ptr++)
5735        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */        op_type = OP_TYPESTAR - OP_STAR;  /* Use type opcodes */
5736        c = *previous;        c = *previous;
5737    
       if (!possessive_quantifier &&  
           repeat_max < 0 &&  
           check_auto_possessive(previous, utf, ptr + 1, options, cd))  
         {  
         repeat_type = 0;    /* Force greedy */  
         possessive_quantifier = TRUE;  
         }  
   
5738        OUTPUT_SINGLE_REPEAT:        OUTPUT_SINGLE_REPEAT:
5739        if (*previous == OP_PROP || *previous == OP_NOTPROP)        if (*previous == OP_PROP || *previous == OP_NOTPROP)
5740          {          {
# Line 5035  for (;; ptr++) Line 5883  for (;; ptr++)
5883      /* If previous was a character class or a back reference, we put the repeat      /* If previous was a character class or a back reference, we put the repeat
5884      stuff after it, but just skip the item if the repeat was {0,0}. */      stuff after it, but just skip the item if the repeat was {0,0}. */
5885    
5886      else if (*previous == OP_CLASS ||      else if (*previous == OP_CLASS || *previous == OP_NCLASS ||
              *previous == OP_NCLASS ||  
5887  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
5888               *previous == OP_XCLASS ||               *previous == OP_XCLASS ||
5889  #endif  #endif
5890               *previous == OP_REF ||               *previous == OP_REF   || *previous == OP_REFI ||
5891               *previous == OP_REFI)               *previous == OP_DNREF || *previous == OP_DNREFI)
5892        {        {
5893        if (repeat_max == 0)        if (repeat_max == 0)
5894          {          {
# Line 5069  for (;; ptr++) Line 5916  for (;; ptr++)
5916      opcodes such as BRA and CBRA, as this is the place where they get converted      opcodes such as BRA and CBRA, as this is the place where they get converted
5917      into the more special varieties such as BRAPOS and SBRA. A test for >=      into the more special varieties such as BRAPOS and SBRA. A test for >=
5918      OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,      OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
5919      ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow      ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND.
5920      repetition of assertions, but now it does, for Perl compatibility. */      Originally, PCRE did not allow repetition of assertions, but now it does,
5921        for Perl compatibility. */
5922    
5923      else if (*previous >= OP_ASSERT && *previous <= OP_COND)      else if (*previous >= OP_ASSERT && *previous <= OP_COND)
5924        {        {
# Line 5088  for (;; ptr++) Line 5936  for (;; ptr++)
5936        /* There is no sense in actually repeating assertions. The only potential        /* There is no sense in actually repeating assertions. The only potential
5937        use of repetition is in cases when the assertion is optional. Therefore,        use of repetition is in cases when the assertion is optional. Therefore,
5938        if the minimum is greater than zero, just ignore the repeat. If the        if the minimum is greater than zero, just ignore the repeat. If the
5939        maximum is not not zero or one, set it to 1. */        maximum is not zero or one, set it to 1. */
5940    
5941        if (*previous < OP_ONCE)    /* Assertion */        if (*previous < OP_ONCE)    /* Assertion */
5942          {          {
# Line 5130  for (;; ptr++) Line 5978  for (;; ptr++)
5978          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
5979            {            {
5980            *code = OP_END;            *code = OP_END;
5981            adjust_recurse(previous, 1, utf, cd, save_hwm);            adjust_recurse(previous, 1, utf, cd, save_hwm_offset);
5982            memmove(previous + 1, previous, IN_UCHARS(len));            memmove(previous + 1, previous, IN_UCHARS(len));
5983            code++;            code++;
5984            if (repeat_max == 0)            if (repeat_max == 0)
# Line 5154  for (;; ptr++) Line 6002  for (;; ptr++)
6002            {            {
6003            int offset;            int offset;
6004            *code = OP_END;            *code = OP_END;