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

Diff of /code/branches/pcre16/pcre_study.c

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

revision 512 by ph10, Tue Mar 30 11:11:52 2010 UTC revision 612 by ph10, Sat Jul 2 15:20:59 2011 UTC
# Line 48  supporting functions. */ Line 48  supporting functions. */
48    
49  #include "pcre_internal.h"  #include "pcre_internal.h"
50    
51    #define SET_BIT(c) start_bits[c/8] |= (1 << (c&7))
52    
53  /* Returns from set_start_bits() */  /* Returns from set_start_bits() */
54    
55  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };  enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN };
56    
57    
58    
# Line 65  string of that length that matches. In U Line 66  string of that length that matches. In U
66  rather than bytes.  rather than bytes.
67    
68  Arguments:  Arguments:
69    code       pointer to start of group (the bracket)    code        pointer to start of group (the bracket)
70    startcode  pointer to start of the whole pattern    startcode   pointer to start of the whole pattern
71    options    the compiling options    options     the compiling options
72      had_accept  pointer to flag for (*ACCEPT) encountered
73    
74  Returns:   the minimum length  Returns:   the minimum length
75             -1 if \C was encountered             -1 if \C was encountered
76             -2 internal error (missing capturing bracket)             -2 internal error (missing capturing bracket)
77               -3 internal error (opcode not listed)
78  */  */
79    
80  static int  static int
81  find_minlength(const uschar *code, const uschar *startcode, int options)  find_minlength(const uschar *code, const uschar *startcode, int options,
82      BOOL *had_accept_ptr)
83  {  {
84  int length = -1;  int length = -1;
85  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
# Line 83  BOOL had_recurse = FALSE; Line 87  BOOL had_recurse = FALSE;
87  register int branchlength = 0;  register int branchlength = 0;
88  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
89    
90  if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;  if (*code == OP_CBRA || *code == OP_SCBRA ||
91        *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += 2;
92    
93  /* Scan along the opcodes for this branch. If we get to the end of the  /* Scan along the opcodes for this branch. If we get to the end of the
94  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 117  for (;;) Line 122  for (;;)
122      case OP_SCBRA:      case OP_SCBRA:
123      case OP_BRA:      case OP_BRA:
124      case OP_SBRA:      case OP_SBRA:
125        case OP_CBRAPOS:
126        case OP_SCBRAPOS:
127        case OP_BRAPOS:
128        case OP_SBRAPOS:
129      case OP_ONCE:      case OP_ONCE:
130      d = find_minlength(cc, startcode, options);      d = find_minlength(cc, startcode, options, had_accept_ptr);
131      if (d < 0) return d;      if (d < 0) return d;
132      branchlength += d;      branchlength += d;
133        if (*had_accept_ptr) return branchlength;
134      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
135      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
136      break;      break;
137    
138      /* Reached end of a branch; if it's a ket it is the end of a nested      /* Reached end of a branch; if it's a ket it is the end of a nested
139      call. If it's ALT it is an alternation in a nested call. If it is      call. If it's ALT it is an alternation in a nested call. If it is END it's
140      END it's the end of the outer call. All can be handled by the same code. */      the end of the outer call. All can be handled by the same code. If it is
141        ACCEPT, it is essentially the same as END, but we set a flag so that
142        counting stops. */
143    
144        case OP_ACCEPT:
145        *had_accept_ptr = TRUE;
146        /* Fall through */
147      case OP_ALT:      case OP_ALT:
148      case OP_KET:      case OP_KET:
149      case OP_KETRMAX:      case OP_KETRMAX:
150      case OP_KETRMIN:      case OP_KETRMIN:
151        case OP_KETRPOS:
152      case OP_END:      case OP_END:
153      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
154        length = branchlength;        length = branchlength;
155      if (*cc != OP_ALT) return length;      if (op != OP_ALT) return length;
156      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
157      branchlength = 0;      branchlength = 0;
158      had_recurse = FALSE;      had_recurse = FALSE;
# Line 159  for (;;) Line 175  for (;;)
175      case OP_RREF:      case OP_RREF:
176      case OP_NRREF:      case OP_NRREF:
177      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
178      case OP_CALLOUT:      case OP_CALLOUT:
179      case OP_SOD:      case OP_SOD:
180      case OP_SOM:      case OP_SOM:
181      case OP_EOD:      case OP_EOD:
182      case OP_EODN:      case OP_EODN:
183      case OP_CIRC:      case OP_CIRC:
184        case OP_CIRCM:
185      case OP_DOLL:      case OP_DOLL:
186        case OP_DOLLM:
187      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
188      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
189      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
# Line 176  for (;;) Line 193  for (;;)
193    
194      case OP_BRAZERO:      case OP_BRAZERO:
195      case OP_BRAMINZERO:      case OP_BRAMINZERO:
196        case OP_BRAPOSZERO:
197      case OP_SKIPZERO:      case OP_SKIPZERO:
198      cc += _pcre_OP_lengths[*cc];      cc += _pcre_OP_lengths[*cc];
199      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
# Line 185  for (;;) Line 203  for (;;)
203      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
204    
205      case OP_CHAR:      case OP_CHAR:
206      case OP_CHARNC:      case OP_CHARI:
207      case OP_NOT:      case OP_NOT:
208        case OP_NOTI:
209      case OP_PLUS:      case OP_PLUS:
210        case OP_PLUSI:
211      case OP_MINPLUS:      case OP_MINPLUS:
212        case OP_MINPLUSI:
213      case OP_POSPLUS:      case OP_POSPLUS:
214        case OP_POSPLUSI:
215      case OP_NOTPLUS:      case OP_NOTPLUS:
216        case OP_NOTPLUSI:
217      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
218        case OP_NOTMINPLUSI:
219      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
220        case OP_NOTPOSPLUSI:
221      branchlength++;      branchlength++;
222      cc += 2;      cc += 2;
223  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 211  for (;;) Line 236  for (;;)
236      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
237    
238      case OP_EXACT:      case OP_EXACT:
239        case OP_EXACTI:
240      case OP_NOTEXACT:      case OP_NOTEXACT:
241        case OP_NOTEXACTI:
242      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
243      cc += 4;      cc += 4;
244  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 248  for (;;) Line 275  for (;;)
275      cc++;      cc++;
276      break;      break;
277    
278      /* "Any newline" might match two characters */      /* "Any newline" might match two characters, but it also might match just
279        one. */
280    
281      case OP_ANYNL:      case OP_ANYNL:
282      branchlength += 2;      branchlength += 1;
283      cc++;      cc++;
284      break;      break;
285    
# Line 336  for (;;) Line 364  for (;;)
364      that case we must set the minimum length to zero. */      that case we must set the minimum length to zero. */
365    
366      case OP_REF:      case OP_REF:
367        case OP_REFI:
368      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
369        {        {
370        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
# Line 346  for (;;) Line 375  for (;;)
375          d = 0;          d = 0;
376          had_recurse = TRUE;          had_recurse = TRUE;
377          }          }
378        else d = find_minlength(cs, startcode, options);        else
379            {
380            d = find_minlength(cs, startcode, options, had_accept_ptr);
381            *had_accept_ptr = FALSE;
382            }
383        }        }
384      else d = 0;      else d = 0;
385      cc += 3;      cc += 3;
# Line 363  for (;;) Line 396  for (;;)
396        cc++;        cc++;
397        break;        break;
398    
399          case OP_CRPLUS:
400          case OP_CRMINPLUS:
401          min = 1;
402          cc++;
403          break;
404    
405        case OP_CRRANGE:        case OP_CRRANGE:
406        case OP_CRMINRANGE:        case OP_CRMINRANGE:
407        min = GET2(cc, 1);        min = GET2(cc, 1);
# Line 384  for (;;) Line 423  for (;;)
423      if (cc > cs && cc < ce)      if (cc > cs && cc < ce)
424        had_recurse = TRUE;        had_recurse = TRUE;
425      else      else
426        branchlength += find_minlength(cs, startcode, options);        {
427          branchlength += find_minlength(cs, startcode, options, had_accept_ptr);
428          *had_accept_ptr = FALSE;
429          }
430      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
431      break;      break;
432    
433      /* Anything else does not or need not match a character. We can get the      /* Anything else does not or need not match a character. We can get the
434      item's length from the table, but for those that can match zero occurrences      item's length from the table, but for those that can match zero occurrences
435      of a character, we must take special action for UTF-8 characters. */      of a character, we must take special action for UTF-8 characters. As it
436        happens, the "NOT" versions of these opcodes are used at present only for
437        ASCII characters, so they could be omitted from this list. However, in
438        future that may change, so we include them here so as not to leave a
439        gotcha for a future maintainer. */
440    
441      case OP_UPTO:      case OP_UPTO:
442        case OP_UPTOI:
443      case OP_NOTUPTO:      case OP_NOTUPTO:
444        case OP_NOTUPTOI:
445      case OP_MINUPTO:      case OP_MINUPTO:
446        case OP_MINUPTOI:
447      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
448        case OP_NOTMINUPTOI:
449      case OP_POSUPTO:      case OP_POSUPTO:
450        case OP_POSUPTOI:
451        case OP_NOTPOSUPTO:
452        case OP_NOTPOSUPTOI:
453    
454      case OP_STAR:      case OP_STAR:
455        case OP_STARI:
456        case OP_NOTSTAR:
457        case OP_NOTSTARI:
458      case OP_MINSTAR:      case OP_MINSTAR:
459        case OP_MINSTARI:
460      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
461        case OP_NOTMINSTARI:
462      case OP_POSSTAR:      case OP_POSSTAR:
463        case OP_POSSTARI:
464      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
465        case OP_NOTPOSSTARI:
466    
467      case OP_QUERY:      case OP_QUERY:
468        case OP_QUERYI:
469        case OP_NOTQUERY:
470        case OP_NOTQUERYI:
471      case OP_MINQUERY:      case OP_MINQUERY:
472        case OP_MINQUERYI:
473      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
474        case OP_NOTMINQUERYI:
475      case OP_POSQUERY:      case OP_POSQUERY:
476        case OP_POSQUERYI:
477      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
478        case OP_NOTPOSQUERYI:
479    
480      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
481  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
482      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
# Line 418  for (;;) Line 488  for (;;)
488      case OP_MARK:      case OP_MARK:
489      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
490      case OP_SKIP_ARG:      case OP_SKIP_ARG:
     case OP_THEN_ARG:  
491      cc += _pcre_OP_lengths[op] + cc[1];      cc += _pcre_OP_lengths[op] + cc[1];
492      break;      break;
493    
494      /* For the record, these are the opcodes that are matched by "default":      case OP_THEN_ARG:
495      OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];
496      OP_THEN. */      break;
497    
498        /* The remaining opcodes are just skipped over. */
499    
500      default:      case OP_CLOSE:
501        case OP_COMMIT:
502        case OP_FAIL:
503        case OP_PRUNE:
504        case OP_SET_SOM:
505        case OP_SKIP:
506        case OP_THEN:
507      cc += _pcre_OP_lengths[op];      cc += _pcre_OP_lengths[op];
508      break;      break;
509    
510        /* This should not occur: we list all opcodes explicitly so that when
511        new ones get added they are properly considered. */
512    
513        default:
514        return -3;
515      }      }
516    }    }
517  /* Control never gets here */  /* Control never gets here */
# Line 440  for (;;) Line 523  for (;;)
523  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
524  *************************************************/  *************************************************/
525    
526  /* Given a character, set its bit in the table, and also the bit for the other  /* Given a character, set its first byte's bit in the table, and also the
527  version of a letter if we are caseless.  corresponding bit for the other version of a letter if we are caseless. In
528    UTF-8 mode, for characters greater than 127, we can only do the caseless thing
529    when Unicode property support is available.
530    
531  Arguments:  Arguments:
532    start_bits    points to the bit map    start_bits    points to the bit map
533    c             is the character    p             points to the character
534    caseless      the caseless flag    caseless      the caseless flag
535    cd            the block with char table pointers    cd            the block with char table pointers
536      utf8          TRUE for UTF-8 mode
537    
538    Returns:        pointer after the character
539    */
540    
541    static const uschar *
542    set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,
543      compile_data *cd, BOOL utf8)
544    {
545    unsigned int c = *p;
546    
547    SET_BIT(c);
548    
549    #ifdef SUPPORT_UTF8
550    if (utf8 && c > 127)
551      {
552      GETCHARINC(c, p);
553    #ifdef SUPPORT_UCP
554      if (caseless)
555        {
556        uschar buff[8];
557        c = UCD_OTHERCASE(c);
558        (void)_pcre_ord2utf8(c, buff);
559        SET_BIT(buff[0]);
560        }
561    #endif
562      return p;
563      }
564    #endif
565    
566  Returns:        nothing  /* Not UTF-8 mode, or character is less than 127. */
567    
568    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
569    return p + 1;
570    }
571    
572    
573    
574    /*************************************************
575    *     Set bits for a positive character type     *
576    *************************************************/
577    
578    /* This function sets starting bits for a character type. In UTF-8 mode, we can
579    only do a direct setting for bytes less than 128, as otherwise there can be
580    confusion with bytes in the middle of UTF-8 characters. In a "traditional"
581    environment, the tables will only recognize ASCII characters anyway, but in at
582    least one Windows environment, some higher bytes bits were set in the tables.
583    So we deal with that case by considering the UTF-8 encoding.
584    
585    Arguments:
586      start_bits     the starting bitmap
587      cbit type      the type of character wanted
588      table_limit    32 for non-UTF-8; 16 for UTF-8
589      cd             the block with char table pointers
590    
591    Returns:         nothing
592  */  */
593    
594  static void  static void
595  set_table_bit(uschar *start_bits, unsigned int c, BOOL caseless,  set_type_bits(uschar *start_bits, int cbit_type, int table_limit,
596    compile_data *cd)    compile_data *cd)
597  {  {
598  start_bits[c/8] |= (1 << (c&7));  register int c;
599  if (caseless && (cd->ctypes[c] & ctype_letter) != 0)  for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
600    start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));  if (table_limit == 32) return;
601    for (c = 128; c < 256; c++)
602      {
603      if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
604        {
605        uschar buff[8];
606        (void)_pcre_ord2utf8(c, buff);
607        SET_BIT(buff[0]);
608        }
609      }
610    }
611    
612    
613    /*************************************************
614    *     Set bits for a negative character type     *
615    *************************************************/
616    
617    /* This function sets starting bits for a negative character type such as \D.
618    In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
619    otherwise there can be confusion with bytes in the middle of UTF-8 characters.
620    Unlike in the positive case, where we can set appropriate starting bits for
621    specific high-valued UTF-8 characters, in this case we have to set the bits for
622    all high-valued characters. The lowest is 0xc2, but we overkill by starting at
623    0xc0 (192) for simplicity.
624    
625    Arguments:
626      start_bits     the starting bitmap
627      cbit type      the type of character wanted
628      table_limit    32 for non-UTF-8; 16 for UTF-8
629      cd             the block with char table pointers
630    
631    Returns:         nothing
632    */
633    
634    static void
635    set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,
636      compile_data *cd)
637    {
638    register int c;
639    for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
640    if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
641  }  }
642    
643    
# Line 478  function fails unless the result is SSB_ Line 657  function fails unless the result is SSB_
657  Arguments:  Arguments:
658    code         points to an expression    code         points to an expression
659    start_bits   points to a 32-byte table, initialized to 0    start_bits   points to a 32-byte table, initialized to 0
   caseless     the current state of the caseless flag  
660    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
661    cd           the block with char table pointers    cd           the block with char table pointers
662    
663  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
664                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
665                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
666                   SSB_UNKNOWN  => Hit an unrecognized opcode
667  */  */
668    
669  static int  static int
670  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const uschar *code, uschar *start_bits, BOOL utf8,
671    BOOL utf8, compile_data *cd)    compile_data *cd)
672  {  {
673  register int c;  register int c;
674  int yield = SSB_DONE;  int yield = SSB_DONE;
675    int table_limit = utf8? 16:32;
676    
677  #if 0  #if 0
678  /* ========================================================================= */  /* ========================================================================= */
# Line 513  volatile int dummy; Line 693  volatile int dummy;
693    
694  do  do
695    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
696    BOOL try_next = TRUE;    BOOL try_next = TRUE;
697      const uschar *tcode = code + 1 + LINK_SIZE;
698    
699      if (*code == OP_CBRA || *code == OP_SCBRA ||
700          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += 2;
701    
702    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
703      {      {
704      int rc;      int rc;
705    
706      switch(*tcode)      switch(*tcode)
707        {        {
708        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
709          been created that hasn't been added to this code. Hopefully this problem
710          will be discovered during testing. */
711    
712        default:        default:
713          return SSB_UNKNOWN;
714    
715          /* Fail for a valid opcode that implies no starting bits. */
716    
717          case OP_ACCEPT:
718          case OP_ALLANY:
719          case OP_ANY:
720          case OP_ANYBYTE:
721          case OP_CIRC:
722          case OP_CIRCM:
723          case OP_CLOSE:
724          case OP_COMMIT:
725          case OP_COND:
726          case OP_CREF:
727          case OP_DEF:
728          case OP_DOLL:
729          case OP_DOLLM:
730          case OP_END:
731          case OP_EOD:
732          case OP_EODN:
733          case OP_EXTUNI:
734          case OP_FAIL:
735          case OP_MARK:
736          case OP_NCREF:
737          case OP_NOT:
738          case OP_NOTEXACT:
739          case OP_NOTEXACTI:
740          case OP_NOTI:
741          case OP_NOTMINPLUS:
742          case OP_NOTMINPLUSI:
743          case OP_NOTMINQUERY:
744          case OP_NOTMINQUERYI:
745          case OP_NOTMINSTAR:
746          case OP_NOTMINSTARI:
747          case OP_NOTMINUPTO:
748          case OP_NOTMINUPTOI:
749          case OP_NOTPLUS:
750          case OP_NOTPLUSI:
751          case OP_NOTPOSPLUS:
752          case OP_NOTPOSPLUSI:
753          case OP_NOTPOSQUERY:
754          case OP_NOTPOSQUERYI:
755          case OP_NOTPOSSTAR:
756          case OP_NOTPOSSTARI:
757          case OP_NOTPOSUPTO:
758          case OP_NOTPOSUPTOI:
759          case OP_NOTPROP:
760          case OP_NOTQUERY:
761          case OP_NOTQUERYI:
762          case OP_NOTSTAR:
763          case OP_NOTSTARI:
764          case OP_NOTUPTO:
765          case OP_NOTUPTOI:
766          case OP_NOT_HSPACE:
767          case OP_NOT_VSPACE:
768          case OP_NOT_WORD_BOUNDARY:
769          case OP_NRREF:
770          case OP_PROP:
771          case OP_PRUNE:
772          case OP_PRUNE_ARG:
773          case OP_RECURSE:
774          case OP_REF:
775          case OP_REFI:
776          case OP_REVERSE:
777          case OP_RREF:
778          case OP_SCOND:
779          case OP_SET_SOM:
780          case OP_SKIP:
781          case OP_SKIP_ARG:
782          case OP_SOD:
783          case OP_SOM:
784          case OP_THEN:
785          case OP_THEN_ARG:
786          case OP_WORD_BOUNDARY:
787          case OP_XCLASS:
788        return SSB_FAIL;        return SSB_FAIL;
789    
790        /* If we hit a bracket or a positive lookahead assertion, recurse to set        /* If we hit a bracket or a positive lookahead assertion, recurse to set
# Line 535  do Line 796  do
796        case OP_SBRA:        case OP_SBRA:
797        case OP_CBRA:        case OP_CBRA:
798        case OP_SCBRA:        case OP_SCBRA:
799          case OP_BRAPOS:
800          case OP_SBRAPOS:
801          case OP_CBRAPOS:
802          case OP_SCBRAPOS:
803        case OP_ONCE:        case OP_ONCE:
804        case OP_ASSERT:        case OP_ASSERT:
805        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
806        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
807        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
808          {          {
809          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 561  do Line 826  do
826        case OP_KET:        case OP_KET:
827        case OP_KETRMAX:        case OP_KETRMAX:
828        case OP_KETRMIN:        case OP_KETRMIN:
829          case OP_KETRPOS:
830        return SSB_CONTINUE;        return SSB_CONTINUE;
831    
832        /* Skip over callout */        /* Skip over callout */
# Line 578  do Line 844  do
844        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
845        break;        break;
846    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
847        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
848    
849        case OP_BRAZERO:        case OP_BRAZERO:
850        case OP_BRAMINZERO:        case OP_BRAMINZERO:
851        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
852          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf8, cd);
853          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
854  /* =========================================================================  /* =========================================================================
855        See the comment at the head of this function concerning the next line,        See the comment at the head of this function concerning the next line,
856        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 616  do Line 876  do
876        case OP_QUERY:        case OP_QUERY:
877        case OP_MINQUERY:        case OP_MINQUERY:
878        case OP_POSQUERY:        case OP_POSQUERY:
879        set_table_bit(start_bits, tcode[1], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
880        tcode += 2;        break;
881  #ifdef SUPPORT_UTF8  
882        if (utf8 && tcode[-1] >= 0xc0)        case OP_STARI:
883          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINSTARI:
884  #endif        case OP_POSSTARI:
885          case OP_QUERYI:
886          case OP_MINQUERYI:
887          case OP_POSQUERYI:
888          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
889        break;        break;
890    
891        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 629  do Line 893  do
893        case OP_UPTO:        case OP_UPTO:
894        case OP_MINUPTO:        case OP_MINUPTO:
895        case OP_POSUPTO:        case OP_POSUPTO:
896        set_table_bit(start_bits, tcode[3], caseless, cd);        tcode = set_table_bit(start_bits, tcode + 3, FALSE, cd, utf8);
897        tcode += 4;        break;
898  #ifdef SUPPORT_UTF8  
899        if (utf8 && tcode[-1] >= 0xc0)        case OP_UPTOI:
900          tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];        case OP_MINUPTOI:
901  #endif        case OP_POSUPTOI:
902          tcode = set_table_bit(start_bits, tcode + 3, TRUE, cd, utf8);
903        break;        break;
904    
905        /* At least one single char sets the bit and stops */        /* At least one single char sets the bit and stops */
906    
907        case OP_EXACT:       /* Fall through */        case OP_EXACT:
908        tcode += 2;        tcode += 2;
909          /* Fall through */
910        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
911        case OP_PLUS:        case OP_PLUS:
912        case OP_MINPLUS:        case OP_MINPLUS:
913        case OP_POSPLUS:        case OP_POSPLUS:
914        set_table_bit(start_bits, tcode[1], caseless, cd);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
915          try_next = FALSE;
916          break;
917    
918          case OP_EXACTI:
919          tcode += 2;
920          /* Fall through */
921          case OP_CHARI:
922          case OP_PLUSI:
923          case OP_MINPLUSI:
924          case OP_POSPLUSI:
925          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
926          try_next = FALSE;
927          break;
928    
929          /* Special spacing and line-terminating items. These recognize specific
930          lists of characters. The difference between VSPACE and ANYNL is that the
931          latter can match the two-character CRLF sequence, but that is not
932          relevant for finding the first character, so their code here is
933          identical. */
934    
935          case OP_HSPACE:
936          SET_BIT(0x09);
937          SET_BIT(0x20);
938          if (utf8)
939            {
940            SET_BIT(0xC2);  /* For U+00A0 */
941            SET_BIT(0xE1);  /* For U+1680, U+180E */
942            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
943            SET_BIT(0xE3);  /* For U+3000 */
944            }
945          else SET_BIT(0xA0);
946          try_next = FALSE;
947          break;
948    
949          case OP_ANYNL:
950          case OP_VSPACE:
951          SET_BIT(0x0A);
952          SET_BIT(0x0B);
953          SET_BIT(0x0C);
954          SET_BIT(0x0D);
955          if (utf8)
956            {
957            SET_BIT(0xC2);  /* For U+0085 */
958            SET_BIT(0xE2);  /* For U+2028, U+2029 */
959            }
960          else SET_BIT(0x85);
961        try_next = FALSE;        try_next = FALSE;
962        break;        break;
963    
964        /* Single character type sets the bits and stops */        /* Single character types set the bits and stop. Note that if PCRE_UCP
965          is set, we do not see these op codes because \d etc are converted to
966          properties. Therefore, these apply in the case when only characters less
967          than 256 are recognized to match the types. */
968    
969        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
970        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_digit];  
971        try_next = FALSE;        try_next = FALSE;
972        break;        break;
973    
974        case OP_DIGIT:        case OP_DIGIT:
975        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_digit];  
976        try_next = FALSE;        try_next = FALSE;
977        break;        break;
978    
979        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
980        discard it. */        ensure it is set as not whitespace. */
981    
982        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
983        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
984          {        start_bits[1] |= 0x08;
         int d = cd->cbits[c+cbit_space];  
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= ~d;  
         }  
985        try_next = FALSE;        try_next = FALSE;
986        break;        break;
987    
988        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
989        discard it. */        not set it from the table. */
990    
991        case OP_WHITESPACE:        case OP_WHITESPACE:
992        for (c = 0; c < 32; c++)        c = start_bits[1];    /* Save in case it was already set */
993          {        set_type_bits(start_bits, cbit_space, table_limit, cd);
994          int d = cd->cbits[c+cbit_space];        start_bits[1] = (start_bits[1] & ~0x08) | c;
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= d;  
         }  
995        try_next = FALSE;        try_next = FALSE;
996        break;        break;
997    
998        case OP_NOT_WORDCHAR:        case OP_NOT_WORDCHAR:
999        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_word];  
1000        try_next = FALSE;        try_next = FALSE;
1001        break;        break;
1002    
1003        case OP_WORDCHAR:        case OP_WORDCHAR:
1004        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_word];  
1005        try_next = FALSE;        try_next = FALSE;
1006        break;        break;
1007    
# Line 708  do Line 1010  do
1010    
1011        case OP_TYPEPLUS:        case OP_TYPEPLUS:
1012        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
1013          case OP_TYPEPOSPLUS:
1014        tcode++;        tcode++;
1015        break;        break;
1016    
# Line 731  do Line 1034  do
1034        case OP_TYPEPOSQUERY:        case OP_TYPEPOSQUERY:
1035        switch(tcode[1])        switch(tcode[1])
1036          {          {
1037            default:
1038          case OP_ANY:          case OP_ANY:
1039          case OP_ALLANY:          case OP_ALLANY:
1040          return SSB_FAIL;          return SSB_FAIL;
1041    
1042            case OP_HSPACE:
1043            SET_BIT(0x09);
1044            SET_BIT(0x20);
1045            if (utf8)
1046              {
1047              SET_BIT(0xC2);  /* For U+00A0 */
1048              SET_BIT(0xE1);  /* For U+1680, U+180E */
1049              SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1050              SET_BIT(0xE3);  /* For U+3000 */
1051              }
1052            else SET_BIT(0xA0);
1053            break;
1054    
1055            case OP_ANYNL:
1056            case OP_VSPACE:
1057            SET_BIT(0x0A);
1058            SET_BIT(0x0B);
1059            SET_BIT(0x0C);
1060            SET_BIT(0x0D);
1061            if (utf8)
1062              {
1063              SET_BIT(0xC2);  /* For U+0085 */
1064              SET_BIT(0xE2);  /* For U+2028, U+2029 */
1065              }
1066            else SET_BIT(0x85);
1067            break;
1068    
1069          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
1070          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_digit];  
1071          break;          break;
1072    
1073          case OP_DIGIT:          case OP_DIGIT:
1074          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_digit];  
1075          break;          break;
1076    
1077          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1078          discard it. */          ensure it gets set as not whitespace. */
1079    
1080          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
1081          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1082            {          start_bits[1] |= 0x08;
           int d = cd->cbits[c+cbit_space];  
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= ~d;  
           }  
1083          break;          break;
1084    
1085          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1086          discard it. */          avoid setting it. */
1087    
1088          case OP_WHITESPACE:          case OP_WHITESPACE:
1089          for (c = 0; c < 32; c++)          c = start_bits[1];    /* Save in case it was already set */
1090            {          set_type_bits(start_bits, cbit_space, table_limit, cd);
1091            int d = cd->cbits[c+cbit_space];          start_bits[1] = (start_bits[1] & ~0x08) | c;
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= d;  
           }  
1092          break;          break;
1093    
1094          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
1095          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_word];  
1096          break;          break;
1097    
1098          case OP_WORDCHAR:          case OP_WORDCHAR:
1099          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_word];  
1100          break;          break;
1101          }          }
1102    
# Line 832  do Line 1152  do
1152            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
1153            }            }
1154    
1155          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1156            minimum repeat, continue; otherwise stop processing. */
1157    
1158          tcode += 32;          tcode += 32;
1159          switch (*tcode)          switch (*tcode)
# Line 849  do Line 1170  do
1170            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
1171              else try_next = FALSE;              else try_next = FALSE;
1172            break;            break;
1173    
1174            default:            default:
1175            try_next = FALSE;            try_next = FALSE;
1176            break;            break;
# Line 868  return yield; Line 1189  return yield;
1189    
1190    
1191    
1192    
1193    
1194  /*************************************************  /*************************************************
1195  *          Study a compiled expression           *  *          Study a compiled expression           *
1196  *************************************************/  *************************************************/
# Line 892  pcre_study(const pcre *external_re, int Line 1215  pcre_study(const pcre *external_re, int
1215  {  {
1216  int min;  int min;
1217  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1218    BOOL had_accept = FALSE;
1219  uschar start_bits[32];  uschar start_bits[32];
1220  pcre_extra *extra;  pcre_extra *extra;
1221  pcre_study_data *study;  pcre_study_data *study;
# Line 924  seeking a list of starting bytes. */ Line 1248  seeking a list of starting bytes. */
1248  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1249      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1250    {    {
1251      int rc;
1252    
1253    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1254    
1255    tables = re->tables;    tables = re->tables;
# Line 939  if ((re->options & PCRE_ANCHORED) == 0 & Line 1265  if ((re->options & PCRE_ANCHORED) == 0 &
1265    /* See if we can find a fixed set of initial characters for the pattern. */    /* See if we can find a fixed set of initial characters for the pattern. */
1266    
1267    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(uschar));
1268    bits_set = set_start_bits(code, start_bits,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1269      (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,      &compile_block);
1270      &compile_block) == SSB_DONE;    bits_set = rc == SSB_DONE;
1271      if (rc == SSB_UNKNOWN) *errorptr = "internal error: opcode not recognized";
1272    }    }
1273    
1274  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1275    
1276  min = find_minlength(code, code, re->options);  switch(min = find_minlength(code, code, re->options, &had_accept))
1277      {
1278      case -2: *errorptr = "internal error: missing capturing bracket"; break;
1279      case -3: *errorptr = "internal error: opcode not recognized"; break;
1280      default: break;
1281      }
1282    
1283  /* Return NULL if no optimization is possible. */  /* Return NULL if there's been an error or if no optimization is possible. */
1284    
1285  if (!bits_set && min < 0) return NULL;  if (*errorptr != NULL || (!bits_set && min < 0)) return NULL;
1286    
1287  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
1288  the latter, which is pointed to by the former, which may also get additional  the latter, which is pointed to by the former, which may also get additional

Legend:
Removed from v.512  
changed lines
  Added in v.612

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12