/[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

code/trunk/pcre_study.c revision 524 by ph10, Mon May 24 17:06:28 2010 UTC code/branches/pcre16/pcre_study.c revision 770 by zherczeg, Mon Nov 28 20:39:30 2011 UTC
# 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)    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      int             RECURSE depth
73    
74  Returns:   the minimum length  Returns:   the minimum length
75             -1 if \C was encountered             -1 if \C in UTF-8 mode or (*ACCEPT) 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 pcre_uchar *code, const pcre_uchar *startcode, int options,
82      int recurse_depth)
83  {  {
84  int length = -1;  int length = -1;
85  BOOL utf8 = (options & PCRE_UTF8) != 0;  BOOL utf8 = (options & PCRE_UTF8) != 0;
86  BOOL had_recurse = FALSE;  BOOL had_recurse = FALSE;
87  register int branchlength = 0;  register int branchlength = 0;
88  register uschar *cc = (uschar *)code + 1 + LINK_SIZE;  register pcre_uchar *cc = (pcre_uchar *)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 += IMM2_SIZE;
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 92  branch, check the length against that of Line 96  branch, check the length against that of
96  for (;;)  for (;;)
97    {    {
98    int d, min;    int d, min;
99    uschar *cs, *ce;    pcre_uchar *cs, *ce;
100    register int op = *cc;    register int op = *cc;
101    
102    switch (op)    switch (op)
# Line 118  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);      case OP_ONCE_NC:
131        d = find_minlength(cc, startcode, options, recurse_depth);
132      if (d < 0) return d;      if (d < 0) return d;
133      branchlength += d;      branchlength += d;
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        /* ACCEPT makes things far too complicated; we have to give up. */
139    
140        case OP_ACCEPT:
141        case OP_ASSERT_ACCEPT:
142        return -1;
143    
144      /* 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
145      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
146      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
147        ACCEPT was previously encountered, use the length that was in force at that
148        time, and pass back the shortest ACCEPT length. */
149    
150      case OP_ALT:      case OP_ALT:
151      case OP_KET:      case OP_KET:
152      case OP_KETRMAX:      case OP_KETRMAX:
153      case OP_KETRMIN:      case OP_KETRMIN:
154        case OP_KETRPOS:
155      case OP_END:      case OP_END:
156      if (length < 0 || (!had_recurse && branchlength < length))      if (length < 0 || (!had_recurse && branchlength < length))
157        length = branchlength;        length = branchlength;
158      if (*cc != OP_ALT) return length;      if (op != OP_ALT) return length;
159      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
160      branchlength = 0;      branchlength = 0;
161      had_recurse = FALSE;      had_recurse = FALSE;
# Line 160  for (;;) Line 178  for (;;)
178      case OP_RREF:      case OP_RREF:
179      case OP_NRREF:      case OP_NRREF:
180      case OP_DEF:      case OP_DEF:
     case OP_OPT:  
181      case OP_CALLOUT:      case OP_CALLOUT:
182      case OP_SOD:      case OP_SOD:
183      case OP_SOM:      case OP_SOM:
184      case OP_EOD:      case OP_EOD:
185      case OP_EODN:      case OP_EODN:
186      case OP_CIRC:      case OP_CIRC:
187        case OP_CIRCM:
188      case OP_DOLL:      case OP_DOLL:
189        case OP_DOLLM:
190      case OP_NOT_WORD_BOUNDARY:      case OP_NOT_WORD_BOUNDARY:
191      case OP_WORD_BOUNDARY:      case OP_WORD_BOUNDARY:
192      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
193      break;      break;
194    
195      /* Skip over a subpattern that has a {0} or {0,x} quantifier */      /* Skip over a subpattern that has a {0} or {0,x} quantifier */
196    
197      case OP_BRAZERO:      case OP_BRAZERO:
198      case OP_BRAMINZERO:      case OP_BRAMINZERO:
199        case OP_BRAPOSZERO:
200      case OP_SKIPZERO:      case OP_SKIPZERO:
201      cc += _pcre_OP_lengths[*cc];      cc += PRIV(OP_lengths)[*cc];
202      do cc += GET(cc, 1); while (*cc == OP_ALT);      do cc += GET(cc, 1); while (*cc == OP_ALT);
203      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
204      break;      break;
# Line 186  for (;;) Line 206  for (;;)
206      /* Handle literal characters and + repetitions */      /* Handle literal characters and + repetitions */
207    
208      case OP_CHAR:      case OP_CHAR:
209      case OP_CHARNC:      case OP_CHARI:
210      case OP_NOT:      case OP_NOT:
211        case OP_NOTI:
212      case OP_PLUS:      case OP_PLUS:
213        case OP_PLUSI:
214      case OP_MINPLUS:      case OP_MINPLUS:
215        case OP_MINPLUSI:
216      case OP_POSPLUS:      case OP_POSPLUS:
217        case OP_POSPLUSI:
218      case OP_NOTPLUS:      case OP_NOTPLUS:
219        case OP_NOTPLUSI:
220      case OP_NOTMINPLUS:      case OP_NOTMINPLUS:
221        case OP_NOTMINPLUSI:
222      case OP_NOTPOSPLUS:      case OP_NOTPOSPLUS:
223        case OP_NOTPOSPLUSI:
224      branchlength++;      branchlength++;
225      cc += 2;      cc += 2;
226  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
227      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
228  #endif  #endif
229      break;      break;
230    
# Line 212  for (;;) Line 239  for (;;)
239      need to skip over a multibyte character in UTF8 mode.  */      need to skip over a multibyte character in UTF8 mode.  */
240    
241      case OP_EXACT:      case OP_EXACT:
242        case OP_EXACTI:
243      case OP_NOTEXACT:      case OP_NOTEXACT:
244        case OP_NOTEXACTI:
245      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
246      cc += 4;      cc += 2 + IMM2_SIZE;
247  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
248      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
249  #endif  #endif
250      break;      break;
251    
252      case OP_TYPEEXACT:      case OP_TYPEEXACT:
253      branchlength += GET2(cc,1);      branchlength += GET2(cc,1);
254      cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;      cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
255          || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
256      break;      break;
257    
258      /* Handle single-char non-literal matchers */      /* Handle single-char non-literal matchers */
# Line 249  for (;;) Line 279  for (;;)
279      cc++;      cc++;
280      break;      break;
281    
282      /* "Any newline" might match two characters */      /* "Any newline" might match two characters, but it also might match just
283        one. */
284    
285      case OP_ANYNL:      case OP_ANYNL:
286      branchlength += 2;      branchlength += 1;
287      cc++;      cc++;
288      break;      break;
289    
290      /* 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
291        non-UTF-8 mode \C will actually be turned into OP_ALLANY, so won't ever
292        appear, but leave the code, just in case.) */
293    
294      case OP_ANYBYTE:      case OP_ANYBYTE:
295  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
# Line 276  for (;;) Line 309  for (;;)
309      case OP_TYPEPOSSTAR:      case OP_TYPEPOSSTAR:
310      case OP_TYPEPOSQUERY:      case OP_TYPEPOSQUERY:
311      if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;      if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
312      cc += _pcre_OP_lengths[op];      cc += PRIV(OP_lengths)[op];
313      break;      break;
314    
315      case OP_TYPEUPTO:      case OP_TYPEUPTO:
316      case OP_TYPEMINUPTO:      case OP_TYPEMINUPTO:
317      case OP_TYPEPOSUPTO:      case OP_TYPEPOSUPTO:
318      if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;      if (cc[1 + IMM2_SIZE] == OP_PROP
319      cc += _pcre_OP_lengths[op];        || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
320        cc += PRIV(OP_lengths)[op];
321      break;      break;
322    
323      /* Check a class for variable quantification */      /* Check a class for variable quantification */
324    
325  #ifdef SUPPORT_UTF8  #if defined SUPPORT_UTF8 || !defined COMPILE_PCRE8
326      case OP_XCLASS:      case OP_XCLASS:
327      cc += GET(cc, 1) - 33;      cc += GET(cc, 1) - PRIV(OP_lengths)[OP_CLASS];
328      /* Fall through */      /* Fall through */
329  #endif  #endif
330    
331      case OP_CLASS:      case OP_CLASS:
332      case OP_NCLASS:      case OP_NCLASS:
333      cc += 33;      cc += PRIV(OP_lengths)[OP_CLASS];
334    
335      switch (*cc)      switch (*cc)
336        {        {
# Line 315  for (;;) Line 349  for (;;)
349        case OP_CRRANGE:        case OP_CRRANGE:
350        case OP_CRMINRANGE:        case OP_CRMINRANGE:
351        branchlength += GET2(cc,1);        branchlength += GET2(cc,1);
352        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
353        break;        break;
354    
355        default:        default:
# Line 337  for (;;) Line 371  for (;;)
371      that case we must set the minimum length to zero. */      that case we must set the minimum length to zero. */
372    
373      case OP_REF:      case OP_REF:
374        case OP_REFI:
375      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)      if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
376        {        {
377        ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));        ce = cs = (pcre_uchar *)PRIV(find_bracket)(startcode, utf8, GET2(cc, 1));
378        if (cs == NULL) return -2;        if (cs == NULL) return -2;
379        do ce += GET(ce, 1); while (*ce == OP_ALT);        do ce += GET(ce, 1); while (*ce == OP_ALT);
380        if (cc > cs && cc < ce)        if (cc > cs && cc < ce)
# Line 347  for (;;) Line 382  for (;;)
382          d = 0;          d = 0;
383          had_recurse = TRUE;          had_recurse = TRUE;
384          }          }
385        else d = find_minlength(cs, startcode, options);        else
386            {
387            d = find_minlength(cs, startcode, options, recurse_depth);
388            }
389        }        }
390      else d = 0;      else d = 0;
391      cc += 3;      cc += 1 + IMM2_SIZE;
392    
393      /* Handle repeated back references */      /* Handle repeated back references */
394    
# Line 364  for (;;) Line 402  for (;;)
402        cc++;        cc++;
403        break;        break;
404    
405          case OP_CRPLUS:
406          case OP_CRMINPLUS:
407          min = 1;
408          cc++;
409          break;
410    
411        case OP_CRRANGE:        case OP_CRRANGE:
412        case OP_CRMINRANGE:        case OP_CRMINRANGE:
413        min = GET2(cc, 1);        min = GET2(cc, 1);
414        cc += 5;        cc += 1 + 2 * IMM2_SIZE;
415        break;        break;
416    
417        default:        default:
# Line 378  for (;;) Line 422  for (;;)
422      branchlength += min * d;      branchlength += min * d;
423      break;      break;
424    
425        /* We can easily detect direct recursion, but not mutual recursion. This is
426        caught by a recursion depth count. */
427    
428      case OP_RECURSE:      case OP_RECURSE:
429      cs = ce = (uschar *)startcode + GET(cc, 1);      cs = ce = (pcre_uchar *)startcode + GET(cc, 1);
     if (cs == NULL) return -2;  
430      do ce += GET(ce, 1); while (*ce == OP_ALT);      do ce += GET(ce, 1); while (*ce == OP_ALT);
431      if (cc > cs && cc < ce)      if ((cc > cs && cc < ce) || recurse_depth > 10)
432        had_recurse = TRUE;        had_recurse = TRUE;
433      else      else
434        branchlength += find_minlength(cs, startcode, options);        {
435          branchlength += find_minlength(cs, startcode, options, recurse_depth + 1);
436          }
437      cc += 1 + LINK_SIZE;      cc += 1 + LINK_SIZE;
438      break;      break;
439    
440      /* 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
441      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
442      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
443        happens, the "NOT" versions of these opcodes are used at present only for
444        ASCII characters, so they could be omitted from this list. However, in
445        future that may change, so we include them here so as not to leave a
446        gotcha for a future maintainer. */
447    
448      case OP_UPTO:      case OP_UPTO:
449        case OP_UPTOI:
450      case OP_NOTUPTO:      case OP_NOTUPTO:
451        case OP_NOTUPTOI:
452      case OP_MINUPTO:      case OP_MINUPTO:
453        case OP_MINUPTOI:
454      case OP_NOTMINUPTO:      case OP_NOTMINUPTO:
455        case OP_NOTMINUPTOI:
456      case OP_POSUPTO:      case OP_POSUPTO:
457        case OP_POSUPTOI:
458        case OP_NOTPOSUPTO:
459        case OP_NOTPOSUPTOI:
460    
461      case OP_STAR:      case OP_STAR:
462        case OP_STARI:
463        case OP_NOTSTAR:
464        case OP_NOTSTARI:
465      case OP_MINSTAR:      case OP_MINSTAR:
466        case OP_MINSTARI:
467      case OP_NOTMINSTAR:      case OP_NOTMINSTAR:
468        case OP_NOTMINSTARI:
469      case OP_POSSTAR:      case OP_POSSTAR:
470        case OP_POSSTARI:
471      case OP_NOTPOSSTAR:      case OP_NOTPOSSTAR:
472        case OP_NOTPOSSTARI:
473    
474      case OP_QUERY:      case OP_QUERY:
475        case OP_QUERYI:
476        case OP_NOTQUERY:
477        case OP_NOTQUERYI:
478      case OP_MINQUERY:      case OP_MINQUERY:
479        case OP_MINQUERYI:
480      case OP_NOTMINQUERY:      case OP_NOTMINQUERY:
481        case OP_NOTMINQUERYI:
482      case OP_POSQUERY:      case OP_POSQUERY:
483        case OP_POSQUERYI:
484      case OP_NOTPOSQUERY:      case OP_NOTPOSQUERY:
485      cc += _pcre_OP_lengths[op];      case OP_NOTPOSQUERYI:
486    
487        cc += PRIV(OP_lengths)[op];
488  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
489      if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];      if (utf8 && cc[-1] >= 0xc0) cc += PRIV(utf8_table4)[cc[-1] & 0x3f];
490  #endif  #endif
491      break;      break;
492    
# Line 420  for (;;) Line 496  for (;;)
496      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
497      case OP_SKIP_ARG:      case OP_SKIP_ARG:
498      case OP_THEN_ARG:      case OP_THEN_ARG:
499      cc += _pcre_OP_lengths[op] + cc[1];      cc += PRIV(OP_lengths)[op] + cc[1];
500      break;      break;
501    
502      /* For the record, these are the opcodes that are matched by "default":      /* The remaining opcodes are just skipped over. */
     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,  
     OP_THEN. */  
503    
504      default:      case OP_CLOSE:
505      cc += _pcre_OP_lengths[op];      case OP_COMMIT:
506        case OP_FAIL:
507        case OP_PRUNE:
508        case OP_SET_SOM:
509        case OP_SKIP:
510        case OP_THEN:
511        cc += PRIV(OP_lengths)[op];
512      break;      break;
513    
514        /* This should not occur: we list all opcodes explicitly so that when
515        new ones get added they are properly considered. */
516    
517        default:
518        return -3;
519      }      }
520    }    }
521  /* Control never gets here */  /* Control never gets here */
# Line 441  for (;;) Line 527  for (;;)
527  *      Set a bit and maybe its alternate case    *  *      Set a bit and maybe its alternate case    *
528  *************************************************/  *************************************************/
529    
530  /* Given a character, set its first byte's bit in the table, and also the  /* Given a character, set its first byte's bit in the table, and also the
531  corresponding bit for the other version of a letter if we are caseless. In  corresponding bit for the other version of a letter if we are caseless. In
532  UTF-8 mode, for characters greater than 127, we can only do the caseless thing  UTF-8 mode, for characters greater than 127, we can only do the caseless thing
533  when Unicode property support is available.  when Unicode property support is available.
# Line 451  Arguments: Line 537  Arguments:
537    p             points to the character    p             points to the character
538    caseless      the caseless flag    caseless      the caseless flag
539    cd            the block with char table pointers    cd            the block with char table pointers
540    utf8          TRUE for UTF-8 mode    utf8          TRUE for UTF-8 mode
541    
542  Returns:        pointer after the character  Returns:        pointer after the character
543  */  */
544    
545  static const uschar *  static const pcre_uchar *
546  set_table_bit(uschar *start_bits, const uschar *p, BOOL caseless,  set_table_bit(pcre_uint8 *start_bits, const pcre_uchar *p, BOOL caseless,
547    compile_data *cd, BOOL utf8)    compile_data *cd, BOOL utf8)
548  {  {
549  unsigned int c = *p;  unsigned int c = *p;
# Line 471  if (utf8 && c > 127) Line 557  if (utf8 && c > 127)
557  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
558    if (caseless)    if (caseless)
559      {      {
560      uschar buff[8];      pcre_uint8 buff[8];
561      c = UCD_OTHERCASE(c);      c = UCD_OTHERCASE(c);
562      (void)_pcre_ord2utf8(c, buff);      (void)PRIV(ord2utf8)(c, buff);
563      SET_BIT(buff[0]);      SET_BIT(buff[0]);
564      }      }
565  #endif  #endif
566    return p;    return p;
567    }    }
568  #endif  #endif
569    
570  /* Not UTF-8 mode, or character is less than 127. */  /* Not UTF-8 mode, or character is less than 127. */
571    
# Line 490  return p + 1; Line 576  return p + 1;
576    
577    
578  /*************************************************  /*************************************************
579    *     Set bits for a positive character type     *
580    *************************************************/
581    
582    /* This function sets starting bits for a character type. In UTF-8 mode, we can
583    only do a direct setting for bytes less than 128, as otherwise there can be
584    confusion with bytes in the middle of UTF-8 characters. In a "traditional"
585    environment, the tables will only recognize ASCII characters anyway, but in at
586    least one Windows environment, some higher bytes bits were set in the tables.
587    So we deal with that case by considering the UTF-8 encoding.
588    
589    Arguments:
590      start_bits     the starting bitmap
591      cbit type      the type of character wanted
592      table_limit    32 for non-UTF-8; 16 for UTF-8
593      cd             the block with char table pointers
594    
595    Returns:         nothing
596    */
597    
598    static void
599    set_type_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit,
600      compile_data *cd)
601    {
602    register int c;
603    for (c = 0; c < table_limit; c++) start_bits[c] |= cd->cbits[c+cbit_type];
604    #ifdef SUPPORT_UTF8
605    if (table_limit == 32) return;
606    for (c = 128; c < 256; c++)
607      {
608      if ((cd->cbits[c/8] & (1 << (c&7))) != 0)
609        {
610        pcre_uint8 buff[8];
611        (void)PRIV(ord2utf8)(c, buff);
612        SET_BIT(buff[0]);
613        }
614      }
615    #endif
616    }
617    
618    
619    /*************************************************
620    *     Set bits for a negative character type     *
621    *************************************************/
622    
623    /* This function sets starting bits for a negative character type such as \D.
624    In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
625    otherwise there can be confusion with bytes in the middle of UTF-8 characters.
626    Unlike in the positive case, where we can set appropriate starting bits for
627    specific high-valued UTF-8 characters, in this case we have to set the bits for
628    all high-valued characters. The lowest is 0xc2, but we overkill by starting at
629    0xc0 (192) for simplicity.
630    
631    Arguments:
632      start_bits     the starting bitmap
633      cbit type      the type of character wanted
634      table_limit    32 for non-UTF-8; 16 for UTF-8
635      cd             the block with char table pointers
636    
637    Returns:         nothing
638    */
639    
640    static void
641    set_nottype_bits(pcre_uint8 *start_bits, int cbit_type, int table_limit,
642      compile_data *cd)
643    {
644    register int c;
645    for (c = 0; c < table_limit; c++) start_bits[c] |= ~cd->cbits[c+cbit_type];
646    if (table_limit != 32) for (c = 24; c < 32; c++) start_bits[c] = 0xff;
647    }
648    
649    
650    
651    /*************************************************
652  *          Create bitmap of starting bytes       *  *          Create bitmap of starting bytes       *
653  *************************************************/  *************************************************/
654    
# Line 504  function fails unless the result is SSB_ Line 663  function fails unless the result is SSB_
663  Arguments:  Arguments:
664    code         points to an expression    code         points to an expression
665    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  
666    utf8         TRUE if in UTF-8 mode    utf8         TRUE if in UTF-8 mode
667    cd           the block with char table pointers    cd           the block with char table pointers
668    
669  Returns:       SSB_FAIL     => Failed to find any starting bytes  Returns:       SSB_FAIL     => Failed to find any starting bytes
670                 SSB_DONE     => Found mandatory starting bytes                 SSB_DONE     => Found mandatory starting bytes
671                 SSB_CONTINUE => Found optional starting bytes                 SSB_CONTINUE => Found optional starting bytes
672                   SSB_UNKNOWN  => Hit an unrecognized opcode
673  */  */
674    
675  static int  static int
676  set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,  set_start_bits(const pcre_uchar *code, pcre_uint8 *start_bits, BOOL utf8,
677    BOOL utf8, compile_data *cd)    compile_data *cd)
678  {  {
679  register int c;  register int c;
680  int yield = SSB_DONE;  int yield = SSB_DONE;
681    int table_limit = utf8? 16:32;
682    
683  #if 0  #if 0
684  /* ========================================================================= */  /* ========================================================================= */
# Line 539  volatile int dummy; Line 699  volatile int dummy;
699    
700  do  do
701    {    {
   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;  
702    BOOL try_next = TRUE;    BOOL try_next = TRUE;
703      const pcre_uchar *tcode = code + 1 + LINK_SIZE;
704    
705      if (*code == OP_CBRA || *code == OP_SCBRA ||
706          *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
707    
708    while (try_next)    /* Loop for items in this branch */    while (try_next)    /* Loop for items in this branch */
709      {      {
710      int rc;      int rc;
711    
712      switch(*tcode)      switch(*tcode)
713        {        {
714        /* Fail if we reach something we don't understand */        /* If we reach something we don't understand, it means a new opcode has
715          been created that hasn't been added to this code. Hopefully this problem
716          will be discovered during testing. */
717    
718        default:        default:
719          return SSB_UNKNOWN;
720    
721          /* Fail for a valid opcode that implies no starting bits. */
722    
723          case OP_ACCEPT:
724          case OP_ASSERT_ACCEPT:
725          case OP_ALLANY:
726          case OP_ANY:
727          case OP_ANYBYTE:
728          case OP_CIRC:
729          case OP_CIRCM:
730          case OP_CLOSE:
731          case OP_COMMIT:
732          case OP_COND:
733          case OP_CREF:
734          case OP_DEF:
735          case OP_DOLL:
736          case OP_DOLLM:
737          case OP_END:
738          case OP_EOD:
739          case OP_EODN:
740          case OP_EXTUNI:
741          case OP_FAIL:
742          case OP_MARK:
743          case OP_NCREF:
744          case OP_NOT:
745          case OP_NOTEXACT:
746          case OP_NOTEXACTI:
747          case OP_NOTI:
748          case OP_NOTMINPLUS:
749          case OP_NOTMINPLUSI:
750          case OP_NOTMINQUERY:
751          case OP_NOTMINQUERYI:
752          case OP_NOTMINSTAR:
753          case OP_NOTMINSTARI:
754          case OP_NOTMINUPTO:
755          case OP_NOTMINUPTOI:
756          case OP_NOTPLUS:
757          case OP_NOTPLUSI:
758          case OP_NOTPOSPLUS:
759          case OP_NOTPOSPLUSI:
760          case OP_NOTPOSQUERY:
761          case OP_NOTPOSQUERYI:
762          case OP_NOTPOSSTAR:
763          case OP_NOTPOSSTARI:
764          case OP_NOTPOSUPTO:
765          case OP_NOTPOSUPTOI:
766          case OP_NOTPROP:
767          case OP_NOTQUERY:
768          case OP_NOTQUERYI:
769          case OP_NOTSTAR:
770          case OP_NOTSTARI:
771          case OP_NOTUPTO:
772          case OP_NOTUPTOI:
773          case OP_NOT_HSPACE:
774          case OP_NOT_VSPACE:
775          case OP_NRREF:
776          case OP_PROP:
777          case OP_PRUNE:
778          case OP_PRUNE_ARG:
779          case OP_RECURSE:
780          case OP_REF:
781          case OP_REFI:
782          case OP_REVERSE:
783          case OP_RREF:
784          case OP_SCOND:
785          case OP_SET_SOM:
786          case OP_SKIP:
787          case OP_SKIP_ARG:
788          case OP_SOD:
789          case OP_SOM:
790          case OP_THEN:
791          case OP_THEN_ARG:
792    #if defined SUPPORT_UTF8 || !defined COMPILE_PCRE8
793          case OP_XCLASS:
794    #endif
795        return SSB_FAIL;        return SSB_FAIL;
796    
797          /* We can ignore word boundary tests. */
798    
799          case OP_WORD_BOUNDARY:
800          case OP_NOT_WORD_BOUNDARY:
801          tcode++;
802          break;
803    
804        /* 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
805        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
806        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 561  do Line 810  do
810        case OP_SBRA:        case OP_SBRA:
811        case OP_CBRA:        case OP_CBRA:
812        case OP_SCBRA:        case OP_SCBRA:
813          case OP_BRAPOS:
814          case OP_SBRAPOS:
815          case OP_CBRAPOS:
816          case OP_SCBRAPOS:
817        case OP_ONCE:        case OP_ONCE:
818          case OP_ONCE_NC:
819        case OP_ASSERT:        case OP_ASSERT:
820        rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);        rc = set_start_bits(tcode, start_bits, utf8, cd);
821        if (rc == SSB_FAIL) return SSB_FAIL;        if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
822        if (rc == SSB_DONE) try_next = FALSE; else        if (rc == SSB_DONE) try_next = FALSE; else
823          {          {
824          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);          do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
# Line 587  do Line 841  do
841        case OP_KET:        case OP_KET:
842        case OP_KETRMAX:        case OP_KETRMAX:
843        case OP_KETRMIN:        case OP_KETRMIN:
844          case OP_KETRPOS:
845        return SSB_CONTINUE;        return SSB_CONTINUE;
846    
847        /* Skip over callout */        /* Skip over callout */
# Line 604  do Line 859  do
859        tcode += 1 + LINK_SIZE;        tcode += 1 + LINK_SIZE;
860        break;        break;
861    
       /* Skip over an option setting, changing the caseless flag */  
   
       case OP_OPT:  
       caseless = (tcode[1] & PCRE_CASELESS) != 0;  
       tcode += 2;  
       break;  
   
862        /* BRAZERO does the bracket, but carries on. */        /* BRAZERO does the bracket, but carries on. */
863    
864        case OP_BRAZERO:        case OP_BRAZERO:
865        case OP_BRAMINZERO:        case OP_BRAMINZERO:
866        if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)        case OP_BRAPOSZERO:
867          return SSB_FAIL;        rc = set_start_bits(++tcode, start_bits, utf8, cd);
868          if (rc == SSB_FAIL || rc == SSB_UNKNOWN) return rc;
869  /* =========================================================================  /* =========================================================================
870        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,
871        which was an old fudge for the benefit of OS/2.        which was an old fudge for the benefit of OS/2.
# Line 642  do Line 891  do
891        case OP_QUERY:        case OP_QUERY:
892        case OP_MINQUERY:        case OP_MINQUERY:
893        case OP_POSQUERY:        case OP_POSQUERY:
894        tcode = set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
895          break;
896    
897          case OP_STARI:
898          case OP_MINSTARI:
899          case OP_POSSTARI:
900          case OP_QUERYI:
901          case OP_MINQUERYI:
902          case OP_POSQUERYI:
903          tcode = set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
904        break;        break;
905    
906        /* Single-char upto sets the bit and tries the next */        /* Single-char upto sets the bit and tries the next */
# Line 650  do Line 908  do
908        case OP_UPTO:        case OP_UPTO:
909        case OP_MINUPTO:        case OP_MINUPTO:
910        case OP_POSUPTO:        case OP_POSUPTO:
911        tcode = set_table_bit(start_bits, tcode + 3, caseless, cd, utf8);        tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, FALSE, cd, utf8);
912        break;        break;
913    
914        /* At least one single char sets the bit and stops */        case OP_UPTOI:
915          case OP_MINUPTOI:
916          case OP_POSUPTOI:
917          tcode = set_table_bit(start_bits, tcode + 1 + IMM2_SIZE, TRUE, cd, utf8);
918          break;
919    
920        case OP_EXACT:       /* Fall through */        /* At least one single char sets the bit and stops */
       tcode += 2;  
921    
922          case OP_EXACT:
923          tcode += IMM2_SIZE;
924          /* Fall through */
925        case OP_CHAR:        case OP_CHAR:
       case OP_CHARNC:  
926        case OP_PLUS:        case OP_PLUS:
927        case OP_MINPLUS:        case OP_MINPLUS:
928        case OP_POSPLUS:        case OP_POSPLUS:
929        (void)set_table_bit(start_bits, tcode + 1, caseless, cd, utf8);        (void)set_table_bit(start_bits, tcode + 1, FALSE, cd, utf8);
930          try_next = FALSE;
931          break;
932    
933          case OP_EXACTI:
934          tcode += IMM2_SIZE;
935          /* Fall through */
936          case OP_CHARI:
937          case OP_PLUSI:
938          case OP_MINPLUSI:
939          case OP_POSPLUSI:
940          (void)set_table_bit(start_bits, tcode + 1, TRUE, cd, utf8);
941        try_next = FALSE;        try_next = FALSE;
942        break;        break;
943    
944        /* Special spacing and line-terminating items. These recognize specific        /* Special spacing and line-terminating items. These recognize specific
945        lists of characters. The difference between VSPACE and ANYNL is that the        lists of characters. The difference between VSPACE and ANYNL is that the
946        latter can match the two-character CRLF sequence, but that is not        latter can match the two-character CRLF sequence, but that is not
947        relevant for finding the first character, so their code here is        relevant for finding the first character, so their code here is
948        identical. */        identical. */
949    
950        case OP_HSPACE:        case OP_HSPACE:
951        SET_BIT(0x09);        SET_BIT(0x09);
952        SET_BIT(0x20);        SET_BIT(0x20);
       SET_BIT(0xA0);  
953        if (utf8)        if (utf8)
954          {          {
955            SET_BIT(0xC2);  /* For U+00A0 */
956          SET_BIT(0xE1);  /* For U+1680, U+180E */          SET_BIT(0xE1);  /* For U+1680, U+180E */
957          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
958          SET_BIT(0xE3);  /* For U+3000 */          SET_BIT(0xE3);  /* For U+3000 */
959          }          }
960          else SET_BIT(0xA0);
961        try_next = FALSE;        try_next = FALSE;
962        break;        break;
963    
964        case OP_ANYNL:        case OP_ANYNL:
965        case OP_VSPACE:        case OP_VSPACE:
966        SET_BIT(0x0A);        SET_BIT(0x0A);
967        SET_BIT(0x0B);        SET_BIT(0x0B);
968        SET_BIT(0x0C);        SET_BIT(0x0C);
969        SET_BIT(0x0D);        SET_BIT(0x0D);
970        SET_BIT(0x85);        if (utf8)
971        if (utf8) SET_BIT(0xE2);    /* For U+2028, U+2029 */          {
972        try_next = FALSE;          SET_BIT(0xC2);  /* For U+0085 */
973        break;          SET_BIT(0xE2);  /* For U+2028, U+2029 */
974            }
975        /* Single character types set the bits and stop. Note that if PCRE_UCP        else SET_BIT(0x85);
976        is set, we do not see these op codes because \d etc are converted to        try_next = FALSE;
977        properties. Therefore, these apply in the case when only ASCII characters        break;
978        are recognized to match the types. */  
979          /* Single character types set the bits and stop. Note that if PCRE_UCP
980          is set, we do not see these op codes because \d etc are converted to
981          properties. Therefore, these apply in the case when only characters less
982          than 256 are recognized to match the types. */
983    
984        case OP_NOT_DIGIT:        case OP_NOT_DIGIT:
985        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_digit];  
986        try_next = FALSE;        try_next = FALSE;
987        break;        break;
988    
989        case OP_DIGIT:        case OP_DIGIT:
990        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_digit, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_digit];  
991        try_next = FALSE;        try_next = FALSE;
992        break;        break;
993    
994        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
995        discard it. */        ensure it is set as not whitespace. */
996    
997        case OP_NOT_WHITESPACE:        case OP_NOT_WHITESPACE:
998        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_space, table_limit, cd);
999          {        start_bits[1] |= 0x08;
         int d = cd->cbits[c+cbit_space];  
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= ~d;  
         }  
1000        try_next = FALSE;        try_next = FALSE;
1001        break;        break;
1002    
1003        /* The cbit_space table has vertical tab as whitespace; we have to        /* The cbit_space table has vertical tab as whitespace; we have to
1004        discard it. */        not set it from the table. */
1005    
1006        case OP_WHITESPACE:        case OP_WHITESPACE:
1007        for (c = 0; c < 32; c++)        c = start_bits[1];    /* Save in case it was already set */
1008          {        set_type_bits(start_bits, cbit_space, table_limit, cd);
1009          int d = cd->cbits[c+cbit_space];        start_bits[1] = (start_bits[1] & ~0x08) | c;
         if (c == 1) d &= ~0x08;  
         start_bits[c] |= d;  
         }  
1010        try_next = FALSE;        try_next = FALSE;
1011        break;        break;
1012    
1013        case OP_NOT_WORDCHAR:        case OP_NOT_WORDCHAR:
1014        for (c = 0; c < 32; c++)        set_nottype_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= ~cd->cbits[c+cbit_word];  
1015        try_next = FALSE;        try_next = FALSE;
1016        break;        break;
1017    
1018        case OP_WORDCHAR:        case OP_WORDCHAR:
1019        for (c = 0; c < 32; c++)        set_type_bits(start_bits, cbit_word, table_limit, cd);
         start_bits[c] |= cd->cbits[c+cbit_word];  
1020        try_next = FALSE;        try_next = FALSE;
1021        break;        break;
1022    
# Line 757  do Line 1025  do
1025    
1026        case OP_TYPEPLUS:        case OP_TYPEPLUS:
1027        case OP_TYPEMINPLUS:        case OP_TYPEMINPLUS:
1028        case OP_TYPEPOSPLUS:        case OP_TYPEPOSPLUS:
1029        tcode++;        tcode++;
1030        break;        break;
1031    
1032        case OP_TYPEEXACT:        case OP_TYPEEXACT:
1033        tcode += 3;        tcode += 1 + IMM2_SIZE;
1034        break;        break;
1035    
1036        /* 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 771  do Line 1039  do
1039        case OP_TYPEUPTO:        case OP_TYPEUPTO:
1040        case OP_TYPEMINUPTO:        case OP_TYPEMINUPTO:
1041        case OP_TYPEPOSUPTO:        case OP_TYPEPOSUPTO:
1042        tcode += 2;               /* Fall through */        tcode += IMM2_SIZE;  /* Fall through */
1043    
1044        case OP_TYPESTAR:        case OP_TYPESTAR:
1045        case OP_TYPEMINSTAR:        case OP_TYPEMINSTAR:
# Line 785  do Line 1053  do
1053          case OP_ANY:          case OP_ANY:
1054          case OP_ALLANY:          case OP_ALLANY:
1055          return SSB_FAIL;          return SSB_FAIL;
1056    
1057          case OP_HSPACE:          case OP_HSPACE:
1058          SET_BIT(0x09);          SET_BIT(0x09);
1059          SET_BIT(0x20);          SET_BIT(0x20);
         SET_BIT(0xA0);  
1060          if (utf8)          if (utf8)
1061            {            {
1062              SET_BIT(0xC2);  /* For U+00A0 */
1063            SET_BIT(0xE1);  /* For U+1680, U+180E */            SET_BIT(0xE1);  /* For U+1680, U+180E */
1064            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */            SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1065            SET_BIT(0xE3);  /* For U+3000 */            SET_BIT(0xE3);  /* For U+3000 */
1066            }            }
1067          break;          else SET_BIT(0xA0);
1068            break;
1069          case OP_ANYNL:  
1070            case OP_ANYNL:
1071          case OP_VSPACE:          case OP_VSPACE:
1072          SET_BIT(0x0A);          SET_BIT(0x0A);
1073          SET_BIT(0x0B);          SET_BIT(0x0B);
1074          SET_BIT(0x0C);          SET_BIT(0x0C);
1075          SET_BIT(0x0D);          SET_BIT(0x0D);
1076          SET_BIT(0x85);          if (utf8)
1077          if (utf8) SET_BIT(0xE2);    /* For U+2028, U+2029 */            {
1078          break;            SET_BIT(0xC2);  /* For U+0085 */
1079              SET_BIT(0xE2);  /* For U+2028, U+2029 */
1080              }
1081            else SET_BIT(0x85);
1082            break;
1083    
1084          case OP_NOT_DIGIT:          case OP_NOT_DIGIT:
1085          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_digit];  
1086          break;          break;
1087    
1088          case OP_DIGIT:          case OP_DIGIT:
1089          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_digit, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_digit];  
1090          break;          break;
1091    
1092          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1093          discard it. */          ensure it gets set as not whitespace. */
1094    
1095          case OP_NOT_WHITESPACE:          case OP_NOT_WHITESPACE:
1096          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_space, table_limit, cd);
1097            {          start_bits[1] |= 0x08;
           int d = cd->cbits[c+cbit_space];  
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= ~d;  
           }  
1098          break;          break;
1099    
1100          /* The cbit_space table has vertical tab as whitespace; we have to          /* The cbit_space table has vertical tab as whitespace; we have to
1101          discard it. */          avoid setting it. */
1102    
1103          case OP_WHITESPACE:          case OP_WHITESPACE:
1104          for (c = 0; c < 32; c++)          c = start_bits[1];    /* Save in case it was already set */
1105            {          set_type_bits(start_bits, cbit_space, table_limit, cd);
1106            int d = cd->cbits[c+cbit_space];          start_bits[1] = (start_bits[1] & ~0x08) | c;
           if (c == 1) d &= ~0x08;  
           start_bits[c] |= d;  
           }  
1107          break;          break;
1108    
1109          case OP_NOT_WORDCHAR:          case OP_NOT_WORDCHAR:
1110          for (c = 0; c < 32; c++)          set_nottype_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= ~cd->cbits[c+cbit_word];  
1111          break;          break;
1112    
1113          case OP_WORDCHAR:          case OP_WORDCHAR:
1114          for (c = 0; c < 32; c++)          set_type_bits(start_bits, cbit_word, table_limit, cd);
           start_bits[c] |= cd->cbits[c+cbit_word];  
1115          break;          break;
1116          }          }
1117    
# Line 874  do Line 1136  do
1136    
1137        case OP_CLASS:        case OP_CLASS:
1138          {          {
1139            pcre_uint8 *map;
1140          tcode++;          tcode++;
1141            map = (pcre_uint8 *)tcode;
1142    
1143          /* 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
1144          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 885  do Line 1149  do
1149  #ifdef SUPPORT_UTF8  #ifdef SUPPORT_UTF8
1150          if (utf8)          if (utf8)
1151            {            {
1152            for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 16; c++) start_bits[c] |= map[c];
1153            for (c = 128; c < 256; c++)            for (c = 128; c < 256; c++)
1154              {              {
1155              if ((tcode[c/8] && (1 << (c&7))) != 0)              if ((map[c/8] && (1 << (c&7))) != 0)
1156                {                {
1157                int d = (c >> 6) | 0xc0;            /* Set bit for this starter */                int d = (c >> 6) | 0xc0;            /* Set bit for this starter */
1158                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 902  do Line 1166  do
1166          else          else
1167  #endif  #endif
1168            {            {
1169            for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];            for (c = 0; c < 32; c++) start_bits[c] |= map[c];
1170            }            }
1171    
1172          /* Advance past the bit map, and act on what follows */          /* Advance past the bit map, and act on what follows. For a zero
1173            minimum repeat, continue; otherwise stop processing. */
1174    
1175          tcode += 32;          tcode += 32 / sizeof(pcre_uchar);
1176          switch (*tcode)          switch (*tcode)
1177            {            {
1178            case OP_CRSTAR:            case OP_CRSTAR:
# Line 919  do Line 1184  do
1184    
1185            case OP_CRRANGE:            case OP_CRRANGE:
1186            case OP_CRMINRANGE:            case OP_CRMINRANGE:
1187            if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;            if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1188              else try_next = FALSE;              else try_next = FALSE;
1189            break;            break;
1190    
# Line 941  return yield; Line 1206  return yield;
1206    
1207    
1208    
1209    
1210    
1211  /*************************************************  /*************************************************
1212  *          Study a compiled expression           *  *          Study a compiled expression           *
1213  *************************************************/  *************************************************/
# Line 960  Returns: pointer to a pcre_extra bloc Line 1227  Returns: pointer to a pcre_extra bloc
1227              NULL on error or if no optimization possible              NULL on error or if no optimization possible
1228  */  */
1229    
1230    #ifdef COMPILE_PCRE8
1231  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION  PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1232  pcre_study(const pcre *external_re, int options, const char **errorptr)  pcre_study(const pcre *external_re, int options, const char **errorptr)
1233    #else
1234    PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
1235    pcre16_study(const pcre *external_re, int options, const char **errorptr)
1236    #endif
1237  {  {
1238  int min;  int min;
1239  BOOL bits_set = FALSE;  BOOL bits_set = FALSE;
1240  uschar start_bits[32];  pcre_uint8 start_bits[32];
1241  pcre_extra *extra;  pcre_extra *extra = NULL;
1242  pcre_study_data *study;  pcre_study_data *study;
1243  const uschar *tables;  const pcre_uint8 *tables;
1244  uschar *code;  pcre_uchar *code;
1245  compile_data compile_block;  compile_data compile_block;
1246  const real_pcre *re = (const real_pcre *)external_re;  const real_pcre *re = (const real_pcre *)external_re;
1247    
# Line 987  if ((options & ~PUBLIC_STUDY_OPTIONS) != Line 1259  if ((options & ~PUBLIC_STUDY_OPTIONS) !=
1259    return NULL;    return NULL;
1260    }    }
1261    
1262  code = (uschar *)re + re->name_table_offset +  code = (pcre_uchar *)re + re->name_table_offset +
1263    (re->name_count * re->name_entry_size);    (re->name_count * re->name_entry_size);
1264    
1265  /* 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 997  seeking a list of starting bytes. */ Line 1269  seeking a list of starting bytes. */
1269  if ((re->options & PCRE_ANCHORED) == 0 &&  if ((re->options & PCRE_ANCHORED) == 0 &&
1270      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)      (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
1271    {    {
1272      int rc;
1273    
1274    /* Set the character tables in the block that is passed around */    /* Set the character tables in the block that is passed around */
1275    
1276    tables = re->tables;    tables = re->tables;
# Line 1011  if ((re->options & PCRE_ANCHORED) == 0 & Line 1285  if ((re->options & PCRE_ANCHORED) == 0 &
1285    
1286    /* 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. */
1287    
1288    memset(start_bits, 0, 32 * sizeof(uschar));    memset(start_bits, 0, 32 * sizeof(pcre_uint8));
1289    bits_set = set_start_bits(code, start_bits,    rc = set_start_bits(code, start_bits, (re->options & PCRE_UTF8) != 0,
1290      (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,      &compile_block);
1291      &compile_block) == SSB_DONE;    bits_set = rc == SSB_DONE;
1292      if (rc == SSB_UNKNOWN)
1293        {
1294        *errorptr = "internal error: opcode not recognized";
1295        return NULL;
1296        }
1297    }    }
1298    
1299  /* Find the minimum length of subject string. */  /* Find the minimum length of subject string. */
1300    
1301  min = find_minlength(code, code, re->options);  switch(min = find_minlength(code, code, re->options, 0))
1302      {
1303      case -2: *errorptr = "internal error: missing capturing bracket"; return NULL;
1304      case -3: *errorptr = "internal error: opcode not recognized"; return NULL;
1305      default: break;
1306      }
1307    
1308    /* If a set of starting bytes has been identified, or if the minimum length is
1309    greater than zero, or if JIT optimization has been requested, get a pcre_extra
1310    block and a pcre_study_data block. The study data is put in the latter, which
1311    is pointed to by the former, which may also get additional data set later by
1312    the calling program. At the moment, the size of pcre_study_data is fixed. We
1313    nevertheless save it in a field for returning via the pcre_fullinfo() function
1314    so that if it becomes variable in the future, we don't have to change that
1315    code. */
1316    
1317    if (bits_set || min > 0
1318    #ifdef SUPPORT_JIT
1319        || (options & PCRE_STUDY_JIT_COMPILE) != 0
1320    #endif
1321      )
1322      {
1323      extra = (pcre_extra *)(pcre_malloc)
1324        (sizeof(pcre_extra) + sizeof(pcre_study_data));
1325      if (extra == NULL)
1326        {
1327        *errorptr = "failed to get memory";
1328        return NULL;
1329        }
1330    
1331  /* Return NULL if no optimization is possible. */    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
1332      extra->flags = PCRE_EXTRA_STUDY_DATA;
1333      extra->study_data = study;
1334    
1335  if (!bits_set && min < 0) return NULL;    study->size = sizeof(pcre_study_data);
1336      study->flags = 0;
1337    
1338  /* Get a pcre_extra block and a pcre_study_data block. The study data is put in    if (bits_set)
1339  the latter, which is pointed to by the former, which may also get additional      {
1340  data set later by the calling program. At the moment, the size of      study->flags |= PCRE_STUDY_MAPPED;
1341  pcre_study_data is fixed. We nevertheless save it in a field for returning via      memcpy(study->start_bits, start_bits, sizeof(start_bits));
1342  the pcre_fullinfo() function so that if it becomes variable in the future, we      }
 don't have to change that code. */  
1343    
1344  extra = (pcre_extra *)(pcre_malloc)    /* Always set the minlength value in the block, because the JIT compiler
1345    (sizeof(pcre_extra) + sizeof(pcre_study_data));    makes use of it. However, don't set the bit unless the length is greater than
1346      zero - the interpretive pcre_exec() and pcre_dfa_exec() needn't waste time
1347      checking the zero case. */
1348    
1349  if (extra == NULL)    if (min > 0)
1350    {      {
1351    *errorptr = "failed to get memory";      study->flags |= PCRE_STUDY_MINLEN;
1352    return NULL;      study->minlength = min;
1353        }
1354      else study->minlength = 0;
1355    
1356      /* If JIT support was compiled and requested, attempt the JIT compilation.
1357      If no starting bytes were found, and the minimum length is zero, and JIT
1358      compilation fails, abandon the extra block and return NULL. */
1359    
1360    #ifdef SUPPORT_JIT
1361      extra->executable_jit = NULL;
1362      if ((options & PCRE_STUDY_JIT_COMPILE) != 0) PRIV(jit_compile)(re, extra);
1363      if (study->flags == 0 && (extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) == 0)
1364        {
1365        pcre_free_study(extra);
1366        extra = NULL;
1367        }
1368    #endif
1369    }    }
1370    
1371  study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));  return extra;
1372  extra->flags = PCRE_EXTRA_STUDY_DATA;  }
 extra->study_data = study;  
1373    
 study->size = sizeof(pcre_study_data);  
 study->flags = 0;  
1374    
1375  if (bits_set)  /*************************************************
1376    {  *          Free the study data                   *
1377    study->flags |= PCRE_STUDY_MAPPED;  *************************************************/
   memcpy(study->start_bits, start_bits, sizeof(start_bits));  
   }  
1378    
1379  if (min >= 0)  /* This function frees the memory that was obtained by pcre_study().
   {  
   study->flags |= PCRE_STUDY_MINLEN;  
   study->minlength = min;  
   }  
1380    
1381  return extra;  Argument:   a pointer to the pcre_extra block
1382    Returns:    nothing
1383    */
1384    
1385    #ifdef COMPILE_PCRE8
1386    PCRE_EXP_DEFN void
1387    pcre_free_study(pcre_extra *extra)
1388    #else
1389    PCRE_EXP_DEFN void
1390    pcre16_free_study(pcre_extra *extra)
1391    #endif
1392    {
1393    #ifdef SUPPORT_JIT
1394    if ((extra->flags & PCRE_EXTRA_EXECUTABLE_JIT) != 0 &&
1395         extra->executable_jit != NULL)
1396      PRIV(jit_free)(extra->executable_jit);
1397    #endif
1398    pcre_free(extra);
1399  }  }
1400    
1401  /* End of pcre_study.c */  /* End of pcre_study.c */

Legend:
Removed from v.524  
changed lines
  Added in v.770

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12