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

Diff of /code/trunk/pcre_study.c

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

revision 550 by ph10, Sun Oct 10 16:24:11 2010 UTC revision 1364 by ph10, Sat Oct 5 15:45:11 2013 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-2010 University of Cambridge             Copyright (c) 1997-2012 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 52  supporting functions. */ Line 52  supporting functions. */
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 66  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)    re              compiled pattern block
70    startcode  pointer to start of the whole pattern    code            pointer to start of group (the bracket)
71    options    the compiling options    startcode       pointer to start of the whole pattern's code
72      options         the compiling options
73      int             RECURSE depth
74    
75  Returns:   the minimum length  Returns:   the minimum length
76             -1 if \C was encountered             -1 if \C in UTF-8 mode or (*ACCEPT) was encountered
77             -2 internal error (missing capturing bracket)             -2 internal error (missing capturing bracket)
78               -3 internal error (opcode not listed)
79  */  */
80    
81  static int  static int
82  find_minlength(const uschar *code, const uschar *startcode, int options)  find_minlength(const REAL_PCRE *re, const pcre_uchar *code,
83      const pcre_uchar *startcode, int options, int recurse_depth)
84  {  {
85  int length = -1;  int length = -1;
86  BOOL utf8 = (options & PCRE_UTF8) != 0;  /* PCRE_UTF16 has the same value as PCRE_UTF8. */
87    BOOL utf = (options & PCRE_UTF8) != 0;
88  BOOL had_recurse = FALSE;  BOOL had_recurse = FALSE;
89  register int branchlength = 0;  register int branchlength = 0;
90  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;  register pcre_uchar *cc = (pcre_uchar *)code + 1 + LINK_SIZE;
91    
92  if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;  if (*code == OP_CBRA || *code == OP_SCBRA ||
93        *code == OP_CBRAPOS || *code == OP_SCBRAPOS) cc += IMM2_SIZE;
94    
95  /* 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
96  branch, check the length against that of the other branches. */  branch, check the length against that of the other branches. */
# Line 92  branch, check the length against that of Line 98  branch, check the length against that of
98  for (;;)  for (;;)
99    {    {
100    int d, min;    int d, min;
101    uschar *cs, *ce;    pcre_uchar *cs, *ce;
102    register int op = *cc;    register pcre_uchar op = *cc;
103    
104    switch (op)    switch (op)
105      {      {
# Line 118  for (;;) Line 124  for (;;)
124      case OP_SCBRA:      case OP_SCBRA:
125      case OP_BRA:      case OP_BRA:
126      case OP_SBRA:      case OP_SBRA:
127        case OP_CBRAPOS:
128        case OP_SCBRAPOS:
129        case OP_BRAPOS:
130        case OP_SBRAPOS:
131      case OP_ONCE:      case OP_ONCE:
132      d = find_minlength(cc, startcode, options);      case OP_ONCE_NC:
133        d = find_minlength(re, cc, startcode, options, recurse_depth);
134      if (d < 0) return d;      if (d < 0) return d;
135      branchlength += d;      branchlength += d;
136      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
137      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
138      break;      break;
139    
140        /* ACCEPT makes things far too complicated; we have to give up. */
141    
142        case OP_ACCEPT:
143        case OP_ASSERT_ACCEPT:
144        return -1;
145    
146      /* 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
147      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
148      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 an
149        ACCEPT was previously encountered, use the length that was in force at that
150        time, and pass back the shortest ACCEPT length. */
151    
152      case OP_ALT:      case OP_ALT:
153      case OP_KET:      case OP_KET:
154      case OP_KETRMAX:      case OP_KETRMAX:
155      case OP_KETRMIN:      case OP_KETRMIN:
156        case OP_KETRPOS:
157      case OP_END:      case OP_END:
158      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
159        length = branchlength;        length = branchlength;
160      if (*cc != OP_ALT) return length;      if (op != OP_ALT) return length;
161      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
162      branchlength = 0;      branchlength = 0;
163      had_recurse = FALSE;      had_recurse = FALSE;
# Line 160  for (;;) Line 180  for (;;)
180      case OP_RREF:      case OP_RREF:
181      case OP_NRREF:      case OP_NRREF:
182      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
183      case OP_CALLOUT:      case OP_CALLOUT:
184      case OP_SOD:      case OP_SOD:
185      case OP_SOM:      case OP_SOM:
186      case OP_EOD:      case OP_EOD:
187      case OP_EODN:      case OP_EODN:
188      case OP_CIRC:      case OP_CIRC:
189        case OP_CIRCM:
190      case OP_DOLL:      case OP_DOLL:
191        case OP_DOLLM:
192      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
193      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
194      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
195      break;      break;
196    
197      /* Skip over a subpattern that has a {0} or {0,x} quantifier */      /* Skip over a subpattern that has a {0} or {0,x} quantifier */
198    
199      case OP_BRAZERO:      case OP_BRAZERO:
200      case OP_BRAMINZERO:      case OP_BRAMINZERO:
201        case OP_BRAPOSZERO:
202      case OP_SKIPZERO:      case OP_SKIPZERO:
203      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
204      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
205      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
206      break;      break;
# Line 186  for (;;) Line 208  for (;;)
208      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
209    
210      case OP_CHAR:      case OP_CHAR:
211      case OP_CHARNC:      case OP_CHARI:
212      case OP_NOT:      case OP_NOT:
213        case OP_NOTI:
214      case OP_PLUS:      case OP_PLUS:
215        case OP_PLUSI:
216      case OP_MINPLUS:      case OP_MINPLUS:
217        case OP_MINPLUSI:
218      case OP_POSPLUS:      case OP_POSPLUS:
219        case OP_POSPLUSI:
220      case OP_NOTPLUS:      case OP_NOTPLUS:
221        case OP_NOTPLUSI:
222      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
223        case OP_NOTMINPLUSI:
224      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
225        case OP_NOTPOSPLUSI:
226      branchlength++;      branchlength++;
227      cc += 2;      cc += 2;
228  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
229      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
230  #endif  #endif
231      break;      break;
232    
# Line 212  for (;;) Line 241  for (;;)
241      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
242    
243      case OP_EXACT:      case OP_EXACT:
244        case OP_EXACTI:
245      case OP_NOTEXACT:      case OP_NOTEXACT:
246        case OP_NOTEXACTI:
247      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
248      cc += 4;      cc += 2 + IMM2_SIZE;
249  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
250      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
251  #endif  #endif
252      break;      break;
253    
254      case OP_TYPEEXACT:      case OP_TYPEEXACT:
255      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
256      cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;      cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
257          || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
258      break;      break;
259    
260      /* Handle single-char non-literal matchers */      /* Handle single-char non-literal matchers */
# Line 249  for (;;) Line 281  for (;;)
281      cc++;      cc++;
282      break;      break;
283    
284      /* "Any newline" might match two characters */      /* "Any newline" might match two characters, but it also might match just
285        one. */
286    
287      case OP_ANYNL:      case OP_ANYNL:
288      branchlength += 2;      branchlength += 1;
289      cc++;      cc++;
290      break;      break;
291    
292      /* The single-byte matcher means we can't proceed in UTF-8 mode */      /* The single-byte matcher means we can't proceed in UTF-8 mode. (In
293        non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever
294        appear, but leave the code, just in case.) */
295    
296      case OP_ANYBYTE:      case OP_ANYBYTE:
297  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
298      if (utf8) return -1;      if (utf) return -1;
299  #endif  #endif
300      branchlength++;      branchlength++;
301      cc++;      cc++;
# Line 276  for (;;) Line 311  for (;;)
311      case OP_TYPEPOSSTAR:      case OP_TYPEPOSSTAR:
312      case OP_TYPEPOSQUERY:      case OP_TYPEPOSQUERY:
313      if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;      if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
314      cc += _pcre_OP_lengths[op];      cc += PRIV(OP_lengths)[op];
315      break;      break;
316    
317      case OP_TYPEUPTO:      case OP_TYPEUPTO:
318      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
319      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
320      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP
321      cc += _pcre_OP_lengths[op];        || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
322        cc += PRIV(OP_lengths)[op];
323      break;      break;
324    
325      /* Check a class for variable quantification */      /* Check a class for variable quantification */
326    
 #ifdef SUPPORT_UTF8  
     case OP_XCLASS:  
     cc += GET(cc, 1) - 33;  
     /* Fall through */  
 #endif  
   
327      case OP_CLASS:      case OP_CLASS:
328      case OP_NCLASS:      case OP_NCLASS:
329      cc += 33;  #if defined SUPPORT_UTF || defined COMPILE_PCRE16 || defined COMPILE_PCRE32
330        case OP_XCLASS:
331        /* The original code caused an unsigned overflow in 64 bit systems,
332        so now we use a conditional statement. */
333        if (op == OP_XCLASS)
334          cc += GET(cc, 1);
335        else
336          cc += PRIV(OP_lengths)[OP_CLASS];
337    #else
338        cc += PRIV(OP_lengths)[OP_CLASS];
339    #endif
340    
341      switch (*cc)      switch (*cc)
342        {        {
# Line 315  for (;;) Line 355  for (;;)
355        case OP_CRRANGE:        case OP_CRRANGE:
356        case OP_CRMINRANGE:        case OP_CRMINRANGE:
357        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
358        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
359        break;        break;
360    
361        default:        default:
# Line 335  for (;;) Line 375  for (;;)
375      If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket      If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
376      matches an empty string (by default it causes a matching failure), so in      matches an empty string (by default it causes a matching failure), so in
377      that case we must set the minimum length to zero. */      that case we must set the minimum length to zero. */
378    
379        case OP_DNREF:     /* Duplicate named pattern back reference */
380        case OP_DNREFI:
381        if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
382          {
383          int count = GET2(cc, 1+IMM2_SIZE);
384          pcre_uchar *slot = (pcre_uchar *)re +
385            re->name_table_offset + GET2(cc, 1) * re->name_entry_size;
386          d = INT_MAX;
387          while (count-- > 0)
388            {
389            ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(slot, 0));
390            if (cs == NULL) return -2;
391            do ce += GET(ce, 1); while (*ce == OP_ALT);
392            if (cc > cs && cc < ce)
393              {
394              d = 0;
395              had_recurse = TRUE;
396              break;
397              }
398            else
399              {
400              int dd = find_minlength(re, cs, startcode, options, recurse_depth);
401              if (dd < d) d = dd;
402              }
403            slot += re->name_entry_size;
404            }
405          }
406        else d = 0;
407        cc += 1 + 2*IMM2_SIZE;
408        goto REPEAT_BACK_REFERENCE;
409    
410      case OP_REF:      case OP_REF:      /* Single back reference */
411        case OP_REFI:
412      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
413        {        {
414        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));        ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf, GET2(cc, 1));
415        if (cs == NULL) return -2;        if (cs == NULL) return -2;
416        do ce += GET(ce, 1); while (*ce == OP_ALT);        do ce += GET(ce, 1); while (*ce == OP_ALT);
417        if (cc > cs && cc < ce)        if (cc > cs && cc < ce)
# Line 347  for (;;) Line 419  for (;;)
419          d = 0;          d = 0;
420          had_recurse = TRUE;          had_recurse = TRUE;
421          }          }
422        else d = find_minlength(cs, startcode, options);        else
423            {
424            d = find_minlength(re, cs, startcode, options, recurse_depth);
425            }
426        }        }
427      else d = 0;      else d = 0;
428      cc += 3;      cc += 1 + IMM2_SIZE;
429    
430      /* Handle repeated back references */      /* Handle repeated back references */
431    
432        REPEAT_BACK_REFERENCE:
433      switch (*cc)      switch (*cc)
434        {        {
435        case OP_CRSTAR:        case OP_CRSTAR:
# Line 364  for (;;) Line 440  for (;;)
440        cc++;        cc++;
441        break;        break;
442    
443          case OP_CRPLUS:
444          case OP_CRMINPLUS:
445          min = 1;
446          cc++;
447          break;
448    
449        case OP_CRRANGE:        case OP_CRRANGE:
450        case OP_CRMINRANGE:        case OP_CRMINRANGE:
451        min = GET2(cc, 1);        min = GET2(cc, 1);
452        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
453        break;        break;
454    
455        default:        default:
# Line 378  for (;;) Line 460  for (;;)
460      branchlength += min * d;      branchlength += min * d;
461      break;      break;
462    
463        /* We can easily detect direct recursion, but not mutual recursion. This is
464        caught by a recursion depth count. */
465    
466      case OP_RECURSE:      case OP_RECURSE:
467      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (pcre_uchar *)startcode + GET(cc, 1);
     if (cs == NULL) return -2;  
468      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
469      if (cc > cs && cc < ce)      if ((cc > cs && cc < ce) || recurse_depth > 10)
470        had_recurse = TRUE;        had_recurse = TRUE;
471      else      else
472        branchlength += find_minlength(cs, startcode, options);        {
473          branchlength += find_minlength(re, cs, startcode, options,
474            recurse_depth + 1);
475          }
476      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
477      break;      break;
478    
479      /* 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
480      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
481      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
482        happens, the "NOT" versions of these opcodes are used at present only for
483        ASCII characters, so they could be omitted from this list. However, in
484        future that may change, so we include them here so as not to leave a
485        gotcha for a future maintainer. */
486    
487      case OP_UPTO:      case OP_UPTO:
488        case OP_UPTOI:
489      case OP_NOTUPTO:      case OP_NOTUPTO:
490        case OP_NOTUPTOI:
491      case OP_MINUPTO:      case OP_MINUPTO:
492        case OP_MINUPTOI:
493      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
494        case OP_NOTMINUPTOI:
495      case OP_POSUPTO:      case OP_POSUPTO:
496        case OP_POSUPTOI:
497        case OP_NOTPOSUPTO:
498        case OP_NOTPOSUPTOI:
499    
500      case OP_STAR:      case OP_STAR:
501        case OP_STARI:
502        case OP_NOTSTAR:
503        case OP_NOTSTARI:
504      case OP_MINSTAR:      case OP_MINSTAR:
505        case OP_MINSTARI:
506      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
507        case OP_NOTMINSTARI:
508      case OP_POSSTAR:      case OP_POSSTAR:
509        case OP_POSSTARI:
510      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
511        case OP_NOTPOSSTARI:
512    
513      case OP_QUERY:      case OP_QUERY:
514        case OP_QUERYI:
515        case OP_NOTQUERY:
516        case OP_NOTQUERYI:
517      case OP_MINQUERY:      case OP_MINQUERY:
518        case OP_MINQUERYI:
519      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
520        case OP_NOTMINQUERYI:
521      case OP_POSQUERY:      case OP_POSQUERY:
522        case OP_POSQUERYI:
523      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
524      cc += _pcre_OP_lengths[op];      case OP_NOTPOSQUERYI:
525  #ifdef SUPPORT_UTF8  
526      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      cc += PRIV(OP_lengths)[op];
527    #ifdef SUPPORT_UTF
528        if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
529  #endif  #endif
530      break;      break;
531    
# Line 419  for (;;) Line 534  for (;;)
534      case OP_MARK:      case OP_MARK:
535      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
536      case OP_SKIP_ARG:      case OP_SKIP_ARG:
537      cc += _pcre_OP_lengths[op] + cc[1];      case OP_THEN_ARG:
538        cc += PRIV(OP_lengths)[op] + cc[1];
539      break;      break;
540    
541      case OP_THEN_ARG:      /* The remaining opcodes are just skipped over. */
542      cc += _pcre_OP_lengths[op] + cc[1+LINK_SIZE];  
543        case OP_CLOSE:
544        case OP_COMMIT:
545        case OP_FAIL:
546        case OP_PRUNE:
547        case OP_SET_SOM:
548        case OP_SKIP:
549        case OP_THEN:
550        cc += PRIV(OP_lengths)[op];
551      break;      break;
552    
553      /* For the record, these are the opcodes that are matched by "default":      /* This should not occur: we list all opcodes explicitly so that when
554      OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,      new ones get added they are properly considered. */
     OP_THEN. */  
555    
556      default:      default:
557      cc += _pcre_OP_lengths[op];      return -3;
     break;  
558      }      }
559    }    }
560  /* Control never gets here */  /* Control never gets here */
# Line 454  Arguments: Line 576  Arguments:
576    p             points to the character    p             points to the character
577    caseless      the caseless flag    caseless      the caseless flag
578    cd            the block with char table pointers    cd            the block with char table pointers
579    utf8          TRUE for UTF-8 mode    utf           TRUE for UTF-8 / UTF-16 / UTF-32 mode
580    
581  Returns:        pointer after the character  Returns:        pointer after the character
582  */  */
583    
584  static const uschar *  static const pcre_uchar *
585  set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,  set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless,
586    compile_data *cd, BOOL utf8)    compile_data *cd, BOOL utf)
587  {  {
588  unsigned int c = *p;  pcre_uint32 c = *p;
589    
590    #ifdef COMPILE_PCRE8
591  SET_BIT(c);  SET_BIT(c);
592    
593  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF
594  if (utf8 && c > 127)  if (utf && c > 127)
595    {    {
596    GETCHARINC(c, p);    GETCHARINC(c, p);
597  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
598    if (caseless)    if (caseless)
599      {      {
600      uschar buff[8];      pcre_uchar buff[6];
601      c = UCD_OTHERCASE(c);      c = UCD_OTHERCASE(c);
602      (void)_pcre_ord2utf8(c, buff);      (void)PRIV(ord2utf)(c, buff);
603      SET_BIT(buff[0]);      SET_BIT(buff[0]);
604      }      }
605  #endif  #endif  /* Not SUPPORT_UCP */
606    return p;    return p;
607    }    }
608  #endif  #else   /* Not SUPPORT_UTF */
609    (void)(utf);   /* Stops warning for unused parameter */
610    #endif  /* SUPPORT_UTF */
611    
612  /* Not UTF-8 mode, or character is less than 127. */  /* Not UTF-8 mode, or character is less than 127. */
613    
614  if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);  if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
615  return p + 1;  return p + 1;
616    #endif  /* COMPILE_PCRE8 */
617    
618    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
619    if (c > 0xff)
620      {
621      c = 0xff;
622      caseless = FALSE;
623      }
624    SET_BIT(c);
625    
626    #ifdef SUPPORT_UTF
627    if (utf && c > 127)
628      {
629      GETCHARINC(c, p);
630    #ifdef SUPPORT_UCP
631      if (caseless)
632        {
633        c = UCD_OTHERCASE(c);
634        if (c > 0xff)
635          c = 0xff;
636        SET_BIT(c);
637        }
638    #endif  /* SUPPORT_UCP */
639      return p;
640      }
641    #else   /* Not SUPPORT_UTF */
642    (void)(utf);   /* Stops warning for unused parameter */
643    #endif  /* SUPPORT_UTF */
644    
645    if (caseless && (cd->ctypes[c] & ctype_letter) != 0) SET_BIT(cd->fcc[c]);
646    return p + 1;
647    #endif
648  }  }
649    
650    
# Line 513  Returns:         nothing Line 670  Returns:         nothing
670  */  */
671    
672  static void  static void
673  set_type_bits(uschar *start_bits, int cbit_type, int table_limit,  set_type_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit,
674    compile_data *cd)    compile_data *cd)
675  {  {
676  register int c;  register pcre_uint32 c;
677  for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];  for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
678    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
679  if (table_limit == 32) return;  if (table_limit == 32) return;
680  for (c = 128; c < 256; c++)  for (c = 128; c < 256; c++)
681    {    {
682    if ((cd->cbits[c/8] & (1 << (c&7))) != 0)    if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
683      {      {
684      uschar buff[8];      pcre_uchar buff[6];
685      (void)_pcre_ord2utf8(c, buff);      (void)PRIV(ord2utf)(c, buff);
686      SET_BIT(buff[0]);      SET_BIT(buff[0]);
687      }      }
688    }    }
689    #endif
690  }  }
691    
692    
# Line 553  Returns:         nothing Line 712  Returns:         nothing
712  */  */
713    
714  static void  static void
715  set_nottype_bits(uschar *start_bits, int cbit_type, int table_limit,  set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, unsigned int table_limit,
716    compile_data *cd)    compile_data *cd)
717  {  {
718  register int c;  register pcre_uint32 c;
719  for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];  for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
720    #if defined SUPPORT_UTF && defined COMPILE_PCRE8
721  if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;  if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
722    #endif
723  }  }
724    
725    
# Line 578  function fails unless the result is SSB_ Line 739  function fails unless the result is SSB_
739  Arguments:  Arguments:
740    code         points to an expression    code         points to an expression
741    start_bits   points to a 32-byte table, initialized to 0    start_bits   points to a 32-byte table, initialized to 0
742    caseless     the current state of the caseless flag    utf          TRUE if in UTF-8 / UTF-16 / UTF-32 mode
   utf8         TRUE if in UTF-8 mode  
743    cd           the block with char table pointers    cd           the block with char table pointers
744    
745  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
746                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
747                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
748                   SSB_UNKNOWN  => Hit an unrecognized opcode
749  */  */
750    
751  static int  static int
752  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf,
753    BOOL utf8, compile_data *cd)    compile_data *cd)
754  {  {
755  register int c;  register pcre_uint32 c;
756  int yield = SSB_DONE;  int yield = SSB_DONE;
757  int table_limit = utf8? 16:32;  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
758    int table_limit = utf? 16:32;
759    #else
760    int table_limit = 32;
761    #endif
762    
763  #if 0  #if 0
764  /* ========================================================================= */  /* ========================================================================= */
# Line 614  volatile int dummy; Line 779  volatile int dummy;
779    
780  do  do
781    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
782    BOOL try_next = TRUE;    BOOL try_next = TRUE;
783      const pcre_uchar *tcode = code + 1 + LINK_SIZE;
784    
785      if (*code == OP_CBRA || *code == OP_SCBRA ||
786          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
787    
788    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
789      {      {
790      int rc;      int rc;
791    
792      switch(*tcode)      switch(*tcode)
793        {        {
794        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
795          been created that hasn't been added to this code. Hopefully this problem
796          will be discovered during testing. */
797    
798        default:        default:
799          return SSB_UNKNOWN;
800    
801          /* Fail for a valid opcode that implies no starting bits. */
802    
803          case OP_ACCEPT:
804          case OP_ASSERT_ACCEPT:
805          case OP_ALLANY:
806          case OP_ANY:
807          case OP_ANYBYTE:
808          case OP_CIRC:
809          case OP_CIRCM:
810          case OP_CLOSE:
811          case OP_COMMIT:
812          case OP_COND:
813          case OP_CREF:
814          case OP_DEF:
815          case OP_DOLL:
816          case OP_DOLLM:
817          case OP_END:
818          case OP_EOD:
819          case OP_EODN:
820          case OP_EXTUNI:
821          case OP_FAIL:
822          case OP_MARK:
823          case OP_NCREF:
824          case OP_NOT:
825          case OP_NOTEXACT:
826          case OP_NOTEXACTI:
827          case OP_NOTI:
828          case OP_NOTMINPLUS:
829          case OP_NOTMINPLUSI:
830          case OP_NOTMINQUERY:
831          case OP_NOTMINQUERYI:
832          case OP_NOTMINSTAR:
833          case OP_NOTMINSTARI:
834          case OP_NOTMINUPTO:
835          case OP_NOTMINUPTOI:
836          case OP_NOTPLUS:
837          case OP_NOTPLUSI:
838          case OP_NOTPOSPLUS:
839          case OP_NOTPOSPLUSI:
840          case OP_NOTPOSQUERY:
841          case OP_NOTPOSQUERYI:
842          case OP_NOTPOSSTAR:
843          case OP_NOTPOSSTARI:
844          case OP_NOTPOSUPTO:
845          case OP_NOTPOSUPTOI:
846          case OP_NOTPROP:
847          case OP_NOTQUERY:
848          case OP_NOTQUERYI:
849          case OP_NOTSTAR:
850          case OP_NOTSTARI:
851          case OP_NOTUPTO:
852          case OP_NOTUPTOI:
853          case OP_NOT_HSPACE:
854          case OP_NOT_VSPACE:
855          case OP_NRREF:
856          case OP_PROP:
857          case OP_PRUNE:
858          case OP_PRUNE_ARG:
859          case OP_RECURSE:
860          case OP_REF:
861          case OP_REFI:
862          case OP_DNREF:
863          case OP_DNREFI:
864          case OP_REVERSE:
865          case OP_RREF:
866          case OP_SCOND:
867          case OP_SET_SOM:
868          case OP_SKIP:
869          case OP_SKIP_ARG:
870          case OP_SOD:
871          case OP_SOM:
872          case OP_THEN:
873          case OP_THEN_ARG:
874    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
875          case OP_XCLASS:
876    #endif
877        return SSB_FAIL;        return SSB_FAIL;
878    
879          /* We can ignore word boundary tests. */
880    
881          case OP_WORD_BOUNDARY:
882          case OP_NOT_WORD_BOUNDARY:
883          tcode++;
884          break;
885    
886        /* 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
887        bits from within the subpattern. If it can't find anything, we have to        bits from within the subpattern. If it can't find anything, we have to
888        give up. If it finds some mandatory character(s), we are done for this        give up. If it finds some mandatory character(s), we are done for this
# Line 636  do Line 892  do
892        case OP_SBRA:        case OP_SBRA:
893        case OP_CBRA:        case OP_CBRA:
894        case OP_SCBRA:        case OP_SCBRA:
895          case OP_BRAPOS:
896          case OP_SBRAPOS:
897          case OP_CBRAPOS:
898          case OP_SCBRAPOS:
899        case OP_ONCE:        case OP_ONCE:
900          case OP_ONCE_NC:
901        case OP_ASSERT:        case OP_ASSERT:
902        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf, cd);
903        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
904        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
905          {          {
906          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 662  do Line 923  do
923        case OP_KET:        case OP_KET:
924        case OP_KETRMAX:        case OP_KETRMAX:
925        case OP_KETRMIN:        case OP_KETRMIN:
926          case OP_KETRPOS:
927        return SSB_CONTINUE;        return SSB_CONTINUE;
928    
929        /* Skip over callout */        /* Skip over callout */
# Line 679  do Line 941  do
941        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
942        break;        break;
943    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
944        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
945    
946        case OP_BRAZERO:        case OP_BRAZERO:
947        case OP_BRAMINZERO:        case OP_BRAMINZERO:
948        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
949          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf, cd);
950          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
951  /* =========================================================================  /* =========================================================================
952        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,
953        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 717  do Line 973  do
973        case OP_QUERY:        case OP_QUERY:
974        case OP_MINQUERY:        case OP_MINQUERY:
975        case OP_POSQUERY:        case OP_POSQUERY:
976        tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
977          break;
978    
979          case OP_STARI:
980          case OP_MINSTARI:
981          case OP_POSSTARI:
982          case OP_QUERYI:
983          case OP_MINQUERYI:
984          case OP_POSQUERYI:
985          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
986        break;        break;
987    
988        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 725  do Line 990  do
990        case OP_UPTO:        case OP_UPTO:
991        case OP_MINUPTO:        case OP_MINUPTO:
992        case OP_POSUPTO:        case OP_POSUPTO:
993        tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf);
994        break;        break;
995    
996        /* At least one single char sets the bit and stops */        case OP_UPTOI:
997          case OP_MINUPTOI:
998          case OP_POSUPTOI:
999          tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf);
1000          break;
1001    
1002        case OP_EXACT:       /* Fall through */        /* At least one single char sets the bit and stops */
       tcode += 2;  
1003    
1004          case OP_EXACT:
1005          tcode += IMM2_SIZE;
1006          /* Fall through */
1007        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
1008        case OP_PLUS:        case OP_PLUS:
1009        case OP_MINPLUS:        case OP_MINPLUS:
1010        case OP_POSPLUS:        case OP_POSPLUS:
1011        (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf);
1012          try_next = FALSE;
1013          break;
1014    
1015          case OP_EXACTI:
1016          tcode += IMM2_SIZE;
1017          /* Fall through */
1018          case OP_CHARI:
1019          case OP_PLUSI:
1020          case OP_MINPLUSI:
1021          case OP_POSPLUSI:
1022          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf);
1023        try_next = FALSE;        try_next = FALSE;
1024        break;        break;
1025    
# Line 749  do Line 1030  do
1030        identical. */        identical. */
1031    
1032        case OP_HSPACE:        case OP_HSPACE:
1033        SET_BIT(0x09);        SET_BIT(CHAR_HT);
1034        SET_BIT(0x20);        SET_BIT(CHAR_SPACE);
1035        if (utf8)  #ifdef SUPPORT_UTF
1036          if (utf)
1037          {          {
1038    #ifdef COMPILE_PCRE8
1039          SET_BIT(0xC2);  /* For U+00A0 */          SET_BIT(0xC2);  /* For U+00A0 */
1040          SET_BIT(0xE1);  /* For U+1680, U+180E */          SET_BIT(0xE1);  /* For U+1680, U+180E */
1041          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1042          SET_BIT(0xE3);  /* For U+3000 */          SET_BIT(0xE3);  /* For U+3000 */
1043    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1044            SET_BIT(0xA0);
1045            SET_BIT(0xFF);  /* For characters > 255 */
1046    #endif  /* COMPILE_PCRE[8|16|32] */
1047            }
1048          else
1049    #endif /* SUPPORT_UTF */
1050            {
1051    #ifndef EBCDIC
1052            SET_BIT(0xA0);
1053    #endif  /* Not EBCDIC */
1054    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1055            SET_BIT(0xFF);  /* For characters > 255 */
1056    #endif  /* COMPILE_PCRE[16|32] */
1057          }          }
       else SET_BIT(0xA0);  
1058        try_next = FALSE;        try_next = FALSE;
1059        break;        break;
1060    
1061        case OP_ANYNL:        case OP_ANYNL:
1062        case OP_VSPACE:        case OP_VSPACE:
1063        SET_BIT(0x0A);        SET_BIT(CHAR_LF);
1064        SET_BIT(0x0B);        SET_BIT(CHAR_VT);
1065        SET_BIT(0x0C);        SET_BIT(CHAR_FF);
1066        SET_BIT(0x0D);        SET_BIT(CHAR_CR);
1067        if (utf8)  #ifdef SUPPORT_UTF
1068          if (utf)
1069          {          {
1070    #ifdef COMPILE_PCRE8
1071          SET_BIT(0xC2);  /* For U+0085 */          SET_BIT(0xC2);  /* For U+0085 */
1072          SET_BIT(0xE2);  /* For U+2028, U+2029 */          SET_BIT(0xE2);  /* For U+2028, U+2029 */
1073    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1074            SET_BIT(CHAR_NEL);
1075            SET_BIT(0xFF);  /* For characters > 255 */
1076    #endif  /* COMPILE_PCRE[8|16|32] */
1077            }
1078          else
1079    #endif /* SUPPORT_UTF */
1080            {
1081            SET_BIT(CHAR_NEL);
1082    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1083            SET_BIT(0xFF);  /* For characters > 255 */
1084    #endif
1085          }          }
       else SET_BIT(0x85);  
1086        try_next = FALSE;        try_next = FALSE;
1087        break;        break;
1088    
# Line 793  do Line 1102  do
1102        break;        break;
1103    
1104        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
1105        ensure it is set as not whitespace. */        ensure it is set as not whitespace. Luckily, the code value is the same
1106          (0x0b) in ASCII and EBCDIC, so we can just adjust the appropriate bit. */
1107    
1108        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
1109        set_nottype_bits(start_bits, cbit_space, table_limit, cd);        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
# Line 801  do Line 1111  do
1111        try_next = FALSE;        try_next = FALSE;
1112        break;        break;
1113    
1114        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to not
1115        not set it from the table. */        set it from the table. Luckily, the code value is the same (0x0b) in
1116          ASCII and EBCDIC, so we can just adjust the appropriate bit. */
1117    
1118        case OP_WHITESPACE:        case OP_WHITESPACE:
1119        c = start_bits[1];    /* Save in case it was already set */        c = start_bits[1];    /* Save in case it was already set */
# Line 831  do Line 1142  do
1142        break;        break;
1143    
1144        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1145        tcode += 3;        tcode += 1 + IMM2_SIZE;
1146        break;        break;
1147    
1148        /* Zero or more repeats of character types set the bits and then        /* Zero or more repeats of character types set the bits and then
# Line 840  do Line 1151  do
1151        case OP_TYPEUPTO:        case OP_TYPEUPTO:
1152        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1153        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1154        tcode += 2;               /* Fall through */        tcode += IMM2_SIZE;  /* Fall through */
1155    
1156        case OP_TYPESTAR:        case OP_TYPESTAR:
1157        case OP_TYPEMINSTAR:        case OP_TYPEMINSTAR:
# Line 856  do Line 1167  do
1167          return SSB_FAIL;          return SSB_FAIL;
1168    
1169          case OP_HSPACE:          case OP_HSPACE:
1170          SET_BIT(0x09);          SET_BIT(CHAR_HT);
1171          SET_BIT(0x20);          SET_BIT(CHAR_SPACE);
1172          if (utf8)  #ifdef SUPPORT_UTF
1173            if (utf)
1174            {            {
1175    #ifdef COMPILE_PCRE8
1176            SET_BIT(0xC2);  /* For U+00A0 */            SET_BIT(0xC2);  /* For U+00A0 */
1177            SET_BIT(0xE1);  /* For U+1680, U+180E */            SET_BIT(0xE1);  /* For U+1680, U+180E */
1178            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1179            SET_BIT(0xE3);  /* For U+3000 */            SET_BIT(0xE3);  /* For U+3000 */
1180    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1181              SET_BIT(0xA0);
1182              SET_BIT(0xFF);  /* For characters > 255 */
1183    #endif  /* COMPILE_PCRE[8|16|32] */
1184            }            }
1185          else SET_BIT(0xA0);          else
1186    #endif /* SUPPORT_UTF */
1187    #ifndef EBCDIC
1188              SET_BIT(0xA0);
1189    #endif  /* Not EBCDIC */
1190          break;          break;
1191    
1192          case OP_ANYNL:          case OP_ANYNL:
1193          case OP_VSPACE:          case OP_VSPACE:
1194          SET_BIT(0x0A);          SET_BIT(CHAR_LF);
1195          SET_BIT(0x0B);          SET_BIT(CHAR_VT);
1196          SET_BIT(0x0C);          SET_BIT(CHAR_FF);
1197          SET_BIT(0x0D);          SET_BIT(CHAR_CR);
1198          if (utf8)  #ifdef SUPPORT_UTF
1199            if (utf)
1200            {            {
1201    #ifdef COMPILE_PCRE8
1202            SET_BIT(0xC2);  /* For U+0085 */            SET_BIT(0xC2);  /* For U+0085 */
1203            SET_BIT(0xE2);  /* For U+2028, U+2029 */            SET_BIT(0xE2);  /* For U+2028, U+2029 */
1204    #elif defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1205              SET_BIT(CHAR_NEL);
1206              SET_BIT(0xFF);  /* For characters > 255 */
1207    #endif  /* COMPILE_PCRE16 */
1208            }            }
1209          else SET_BIT(0x85);          else
1210    #endif /* SUPPORT_UTF */
1211              SET_BIT(CHAR_NEL);
1212          break;          break;
1213    
1214          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
# Line 890  do Line 1219  do
1219          set_type_bits(start_bits, cbit_digit, table_limit, cd);          set_type_bits(start_bits, cbit_digit, table_limit, cd);
1220          break;          break;
1221    
1222          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we no longer
1223          ensure it gets set as not whitespace. */          have to play fancy tricks because Perl added VT to its whitespace at
1224            release 5.18. PCRE added it at release 8.34. */
1225    
1226          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
1227          set_nottype_bits(start_bits, cbit_space, table_limit, cd);          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
         start_bits[1] |= 0x08;  
1228          break;          break;
1229    
         /* The cbit_space table has vertical tab as whitespace; we have to  
         avoid setting it. */  
   
1230          case OP_WHITESPACE:          case OP_WHITESPACE:
         c = start_bits[1];    /* Save in case it was already set */  
1231          set_type_bits(start_bits, cbit_space, table_limit, cd);          set_type_bits(start_bits, cbit_space, table_limit, cd);
         start_bits[1] = (start_bits[1] & ~0x08) | c;  
1232          break;          break;
1233    
1234          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
# Line 926  do Line 1250  do
1250        character with a value > 255. */        character with a value > 255. */
1251    
1252        case OP_NCLASS:        case OP_NCLASS:
1253  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1254        if (utf8)        if (utf)
1255          {          {
1256          start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */          start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */
1257          memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */          memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */
1258          }          }
1259  #endif  #endif
1260    #if defined COMPILE_PCRE16 || defined COMPILE_PCRE32
1261          SET_BIT(0xFF);                         /* For characters > 255 */
1262    #endif
1263        /* Fall through */        /* Fall through */
1264    
1265        case OP_CLASS:        case OP_CLASS:
1266          {          {
1267            pcre_uint8 *map;
1268          tcode++;          tcode++;
1269            map = (pcre_uint8 *)tcode;
1270    
1271          /* In UTF-8 mode, the bits in a bit map correspond to character          /* In UTF-8 mode, the bits in a bit map correspond to character
1272          values, not to byte values. However, the bit map we are constructing is          values, not to byte values. However, the bit map we are constructing is
# Line 945  do Line 1274  do
1274          value is > 127. In fact, there are only two possible starting bytes for          value is > 127. In fact, there are only two possible starting bytes for
1275          characters in the range 128 - 255. */          characters in the range 128 - 255. */
1276    
1277  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF && defined COMPILE_PCRE8
1278          if (utf8)          if (utf)
1279            {            {
1280            for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 16; c++) start_bits[c] |= map[c];
1281            for (c = 128; c < 256; c++)            for (c = 128; c < 256; c++)
1282              {              {
1283              if ((tcode[c/8] && (1 << (c&7))) != 0)              if ((map[c/8] && (1 << (c&7))) != 0)
1284                {                {
1285                int d = (c >> 6) | 0xc0;            /* Set bit for this starter */                int d = (c >> 6) | 0xc0;            /* Set bit for this starter */
1286                start_bits[d/8] |= (1 << (d&7));    /* and then skip on to the */                start_bits[d/8] |= (1 << (d&7));    /* and then skip on to the */
# Line 959  do Line 1288  do
1288                }                }
1289              }              }
1290            }            }
   
         /* In non-UTF-8 mode, the two bit maps are completely compatible. */  
   
1291          else          else
1292  #endif  #endif
1293            {            {
1294            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            /* In non-UTF-8 mode, the two bit maps are completely compatible. */
1295              for (c = 0; c < 32; c++) start_bits[c] |= map[c];
1296            }            }
1297    
1298          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1299            minimum repeat, continue; otherwise stop processing. */
1300    
1301          tcode += 32;          tcode += 32 / sizeof(pcre_uchar);
1302          switch (*tcode)          switch (*tcode)
1303            {            {
1304            case OP_CRSTAR:            case OP_CRSTAR:
# Line 982  do Line 1310  do
1310    
1311            case OP_CRRANGE:            case OP_CRRANGE:
1312            case OP_CRMINRANGE:            case OP_CRMINRANGE:
1313            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;            if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1314              else try_next = FALSE;              else try_next = FALSE;
1315            break;            break;
1316    
# Line 1004  return yield; Line 1332  return yield;
1332    
1333    
1334    
1335    
1336    
1337  /*************************************************  /*************************************************
1338  *          Study a compiled expression           *  *          Study a compiled expression           *
1339  *************************************************/  *************************************************/
1340    
1341  /* This function is handed a compiled expression that it must study to produce  /* This function is handed a compiled expression that it must study to produce
1342  information that will speed up the matching. It returns a pcre_extra block  information that will speed up the matching. It returns a pcre[16]_extra block
1343  which then gets handed back to pcre_exec().  which then gets handed back to pcre_exec().
1344    
1345  Arguments:  Arguments:
# Line 1018  Arguments: Line 1348  Arguments:
1348    errorptr  points to where to place error messages;    errorptr  points to where to place error messages;
1349              set NULL unless error              set NULL unless error
1350    
1351  Returns:    pointer to a pcre_extra block, with study_data filled in and the  Returns:    pointer to a pcre[16]_extra block, with study_data filled in and
1352                appropriate flags set;                the appropriate flags set;
1353              NULL on error or if no optimization possible              NULL on error or if no optimization possible
1354  */  */
1355    
1356    #if defined COMPILE_PCRE8
1357  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1358  pcre_study(const pcre *external_re, int options, const char **errorptr)  pcre_study(const pcre *external_re, int options, const char **errorptr)
1359    #elif defined COMPILE_PCRE16
1360    PCRE_EXP_DEFN pcre16_extra * PCRE_CALL_CONVENTION
1361    pcre16_study(const pcre16 *external_re, int options, const char **errorptr)
1362    #elif defined COMPILE_PCRE32
1363    PCRE_EXP_DEFN pcre32_extra * PCRE_CALL_CONVENTION
1364    pcre32_study(const pcre32 *external_re, int options, const char **errorptr)
1365    #endif
1366  {  {
1367  int min;  int min;
1368  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1369  uschar start_bits[32];  pcre_uint8 start_bits[32];
1370  pcre_extra *extra;  PUBL(extra) *extra = NULL;
1371  pcre_study_data *study;  pcre_study_data *study;
1372  const uschar *tables;  const pcre_uint8 *tables;
1373  uschar *code;  pcre_uchar *code;
1374  compile_data compile_block;  compile_data compile_block;
1375  const real_pcre *re = (const real_pcre *)external_re;  const REAL_PCRE *re = (const REAL_PCRE *)external_re;
1376    
1377    
1378  *errorptr = NULL;  *errorptr = NULL;
1379    
# Line 1044  if (re == NULL || re->magic_number != MA Line 1383  if (re == NULL || re->magic_number != MA
1383    return NULL;    return NULL;
1384    }    }
1385    
1386    if ((re->flags & PCRE_MODE) == 0)
1387      {
1388    #if defined COMPILE_PCRE8
1389      *errorptr = "argument not compiled in 8 bit mode";
1390    #elif defined COMPILE_PCRE16
1391      *errorptr = "argument not compiled in 16 bit mode";
1392    #elif defined COMPILE_PCRE32
1393      *errorptr = "argument not compiled in 32 bit mode";
1394    #endif
1395      return NULL;
1396      }
1397    
1398  if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)  if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
1399    {    {
1400    *errorptr = "unknown or incorrect option bit(s) set";    *errorptr = "unknown or incorrect option bit(s) set";
1401    return NULL;    return NULL;
1402    }    }
1403    
1404  code = (uschar *)re + re->name_table_offset +  code = (pcre_uchar *)re + re->name_table_offset +
1405    (re->name_count * re->name_entry_size);    (re->name_count * re->name_entry_size);
1406    
1407  /* For an anchored pattern, or an unanchored pattern that has a first char, or  /* For an anchored pattern, or an unanchored pattern that has a first char, or
# Line 1060  seeking a list of starting bytes. */ Line 1411  seeking a list of starting bytes. */
1411  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1412      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1413    {    {
1414      int rc;
1415    
1416    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1417    
1418    tables = re->tables;    tables = re->tables;
1419    
1420    #if defined COMPILE_PCRE8
1421    if (tables == NULL)    if (tables == NULL)
1422      (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,      (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1423      (void *)(&tables));      (void *)(&tables));
1424    #elif defined COMPILE_PCRE16
1425      if (tables == NULL)
1426        (void)pcre16_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1427        (void *)(&tables));
1428    #elif defined COMPILE_PCRE32
1429      if (tables == NULL)
1430        (void)pcre32_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
1431        (void *)(&tables));
1432    #endif
1433    
1434    compile_block.lcc = tables + lcc_offset;    compile_block.lcc = tables + lcc_offset;
1435    compile_block.fcc = tables + fcc_offset;    compile_block.fcc = tables + fcc_offset;
# Line 1074  if ((re->options & PCRE_ANCHORED) == 0 & Line 1438  if ((re->options & PCRE_ANCHORED) == 0 &
1438    
1439    /* 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. */
1440    
1441    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(pcre_uint8));
1442    bits_set = set_start_bits(code, start_bits,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1443      (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,      &compile_block);
1444      &compile_block) == SSB_DONE;    bits_set = rc == SSB_DONE;
1445      if (rc == SSB_UNKNOWN)
1446        {
1447        *errorptr = "internal error: opcode not recognized";
1448        return NULL;
1449        }
1450    }    }
1451    
1452  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1453    
1454  min = find_minlength(code, code, re->options);  switch(min = find_minlength(re, code, code, re->options, 0))
1455      {
1456      case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1457      case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1458      default: break;
1459      }
1460    
1461  /* Return NULL if no optimization is possible. */  /* If a set of starting bytes has been identified, or if the minimum length is
1462    greater than zero, or if JIT optimization has been requested, or if
1463    PCRE_STUDY_EXTRA_NEEDED is set, get a pcre[16]_extra block and a
1464    pcre_study_data block. The study data is put in the latter, which is pointed to
1465    by the former, which may also get additional data set later by the calling
1466    program. At the moment, the size of pcre_study_data is fixed. We nevertheless
1467    save it in a field for returning via the pcre_fullinfo() function so that if it
1468    becomes variable in the future, we don't have to change that code. */
1469    
1470    if (bits_set || min > 0 || (options & (
1471    #ifdef SUPPORT_JIT
1472        PCRE_STUDY_JIT_COMPILE | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE |
1473        PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE |
1474    #endif
1475        PCRE_STUDY_EXTRA_NEEDED)) != 0)
1476      {
1477      extra = (PUBL(extra) *)(PUBL(malloc))
1478        (sizeof(PUBL(extra)) + sizeof(pcre_study_data));
1479      if (extra == NULL)
1480        {
1481        *errorptr = "failed to get memory";
1482        return NULL;
1483        }
1484    
1485  if (!bits_set && min < 0) return NULL;    study = (pcre_study_data *)((char *)extra + sizeof(PUBL(extra)));
1486      extra->flags = PCRE_EXTRA_STUDY_DATA;
1487      extra->study_data = study;
1488    
1489      study->size = sizeof(pcre_study_data);
1490      study->flags = 0;
1491    
1492      /* Set the start bits always, to avoid unset memory errors if the
1493      study data is written to a file, but set the flag only if any of the bits
1494      are set, to save time looking when none are. */
1495    
1496  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    if (bits_set)
1497  the latter, which is pointed to by the former, which may also get additional      {
1498  data set later by the calling program. At the moment, the size of      study->flags |= PCRE_STUDY_MAPPED;
1499  pcre_study_data is fixed. We nevertheless save it in a field for returning via      memcpy(study->start_bits, start_bits, sizeof(start_bits));
1500  the pcre_fullinfo() function so that if it becomes variable in the future, we      }
1501  don't have to change that code. */    else memset(study->start_bits, 0, 32 * sizeof(pcre_uint8));
1502    
1503  extra = (pcre_extra *)(pcre_malloc)  #ifdef PCRE_DEBUG
1504    (sizeof(pcre_extra) + sizeof(pcre_study_data));    if (bits_set)
1505        {
1506        pcre_uint8 *ptr = start_bits;
1507        int i;
1508    
1509  if (extra == NULL)      printf("Start bits:\n");
1510    {      for (i = 0; i < 32; i++)
1511    *errorptr = "failed to get memory";        printf("%3d: %02x%s", i * 8, *ptr++, ((i + 1) & 0x7) != 0? " " : "\n");
1512    return NULL;      }
1513    }  #endif
1514    
1515  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));    /* Always set the minlength value in the block, because the JIT compiler
1516  extra->flags = PCRE_EXTRA_STUDY_DATA;    makes use of it. However, don't set the bit unless the length is greater than
1517  extra->study_data = study;    zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1518      checking the zero case. */
1519    
1520  study->size = sizeof(pcre_study_data);    if (min > 0)
1521  study->flags = 0;      {
1522        study->flags |= PCRE_STUDY_MINLEN;
1523        study->minlength = min;
1524        }
1525      else study->minlength = 0;
1526    
1527  if (bits_set)    /* If JIT support was compiled and requested, attempt the JIT compilation.
1528    {    If no starting bytes were found, and the minimum length is zero, and JIT
1529    study->flags |= PCRE_STUDY_MAPPED;    compilation fails, abandon the extra block and return NULL, unless
1530    memcpy(study->start_bits, start_bits, sizeof(start_bits));    PCRE_STUDY_EXTRA_NEEDED is set. */
1531    }  
1532    #ifdef SUPPORT_JIT
1533      extra->executable_jit = NULL;
1534      if ((options & PCRE_STUDY_JIT_COMPILE) != 0)
1535        PRIV(jit_compile)(re, extra, JIT_COMPILE);
1536      if ((options & PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE) != 0)
1537        PRIV(jit_compile)(re, extra, JIT_PARTIAL_SOFT_COMPILE);
1538      if ((options & PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE) != 0)
1539        PRIV(jit_compile)(re, extra, JIT_PARTIAL_HARD_COMPILE);
1540    
1541  if (min >= 0)    if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0 &&
1542    {        (options & PCRE_STUDY_EXTRA_NEEDED) == 0)
1543    study->flags |= PCRE_STUDY_MINLEN;      {
1544    study->minlength = min;  #if defined COMPILE_PCRE8
1545        pcre_free_study(extra);
1546    #elif defined COMPILE_PCRE16
1547        pcre16_free_study(extra);
1548    #elif defined COMPILE_PCRE32
1549        pcre32_free_study(extra);
1550    #endif
1551        extra = NULL;
1552        }
1553    #endif
1554    }    }
1555    
1556  return extra;  return extra;
1557  }  }
1558    
1559    
1560    /*************************************************
1561    *          Free the study data                   *
1562    *************************************************/
1563    
1564    /* This function frees the memory that was obtained by pcre_study().
1565    
1566    Argument:   a pointer to the pcre[16]_extra block
1567    Returns:    nothing
1568    */
1569    
1570    #if defined COMPILE_PCRE8
1571    PCRE_EXP_DEFN void
1572    pcre_free_study(pcre_extra *extra)
1573    #elif defined COMPILE_PCRE16
1574    PCRE_EXP_DEFN void
1575    pcre16_free_study(pcre16_extra *extra)
1576    #elif defined COMPILE_PCRE32
1577    PCRE_EXP_DEFN void
1578    pcre32_free_study(pcre32_extra *extra)
1579    #endif
1580    {
1581    if (extra == NULL)
1582      return;
1583    #ifdef SUPPORT_JIT
1584    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1585         extra->executable_jit != NULL)
1586      PRIV(jit_free)(extra->executable_jit);
1587    #endif
1588    PUBL(free)(extra);
1589    }
1590    
1591  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.550  
changed lines
  Added in v.1364

  ViewVC Help
Powered by ViewVC 1.1.5