• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/pcre/pcre_study.c

00001 /*************************************************
00002 *      Perl-Compatible Regular Expressions       *
00003 *************************************************/
00004 
00005 /* PCRE is a library of functions to support regular expressions whose syntax
00006 and semantics are as close as possible to those of the Perl 5 language.
00007 
00008                        Written by Philip Hazel
00009            Copyright (c) 1997-2009 University of Cambridge
00010 
00011 -----------------------------------------------------------------------------
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014 
00015     * Redistributions of source code must retain the above copyright notice,
00016       this list of conditions and the following disclaimer.
00017 
00018     * Redistributions in binary form must reproduce the above copyright
00019       notice, this list of conditions and the following disclaimer in the
00020       documentation and/or other materials provided with the distribution.
00021 
00022     * Neither the name of the University of Cambridge nor the names of its
00023       contributors may be used to endorse or promote products derived from
00024       this software without specific prior written permission.
00025 
00026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 POSSIBILITY OF SUCH DAMAGE.
00037 -----------------------------------------------------------------------------
00038 */
00039 
00040 
00041 /* This module contains the external function pcre_study(), along with local
00042 supporting functions. */
00043 
00044 
00045 #ifdef HAVE_CONFIG_H
00046 #include "config.h"
00047 #else if defined(_WINDOWS)
00048 #include <spl/configwin32.h>
00049 #endif
00050 
00051 
00052 #include "pcre_internal.h"
00053 
00054 
00055 /* Returns from set_start_bits() */
00056 
00057 enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE };
00058 
00059 
00060 
00061 /*************************************************
00062 *   Find the minimum subject length for a group  *
00063 *************************************************/
00064 
00065 /* Scan a parenthesized group and compute the minimum length of subject that
00066 is needed to match it. This is a lower bound; it does not mean there is a
00067 string of that length that matches. In UTF8 mode, the result is in characters
00068 rather than bytes.
00069 
00070 Arguments:
00071   code       pointer to start of group (the bracket)
00072   startcode  pointer to start of the whole pattern
00073   options    the compiling options
00074 
00075 Returns:   the minimum length
00076            -1 if \C was encountered
00077            -2 internal error (missing capturing bracket)
00078 */
00079 
00080 static int
00081 find_minlength(const uschar *code, const uschar *startcode, int options)
00082 {
00083 int length = -1;
00084 BOOL utf8 = (options & PCRE_UTF8) != 0;
00085 BOOL had_recurse = FALSE;
00086 register int branchlength = 0;
00087 register uschar *cc = (uschar *)code + 1 + LINK_SIZE;
00088 
00089 if (*code == OP_CBRA || *code == OP_SCBRA) cc += 2;
00090 
00091 /* Scan along the opcodes for this branch. If we get to the end of the
00092 branch, check the length against that of the other branches. */
00093 
00094 for (;;)
00095   {
00096   int d, min;
00097   uschar *cs, *ce;
00098   register int op = *cc;
00099 
00100   switch (op)
00101     {
00102     case OP_CBRA:
00103     case OP_SCBRA:
00104     case OP_BRA:
00105     case OP_SBRA:
00106     case OP_ONCE:
00107     case OP_COND:
00108     case OP_SCOND:
00109     d = find_minlength(cc, startcode, options);
00110     if (d < 0) return d;
00111     branchlength += d;
00112     do cc += GET(cc, 1); while (*cc == OP_ALT);
00113     cc += 1 + LINK_SIZE;
00114     break;
00115 
00116     /* Reached end of a branch; if it's a ket it is the end of a nested
00117     call. If it's ALT it is an alternation in a nested call. If it is
00118     END it's the end of the outer call. All can be handled by the same code. */
00119 
00120     case OP_ALT:
00121     case OP_KET:
00122     case OP_KETRMAX:
00123     case OP_KETRMIN:
00124     case OP_END:
00125     if (length < 0 || (!had_recurse && branchlength < length))
00126       length = branchlength;
00127     if (*cc != OP_ALT) return length;
00128     cc += 1 + LINK_SIZE;
00129     branchlength = 0;
00130     had_recurse = FALSE;
00131     break;
00132 
00133     /* Skip over assertive subpatterns */
00134 
00135     case OP_ASSERT:
00136     case OP_ASSERT_NOT:
00137     case OP_ASSERTBACK:
00138     case OP_ASSERTBACK_NOT:
00139     do cc += GET(cc, 1); while (*cc == OP_ALT);
00140     /* Fall through */
00141 
00142     /* Skip over things that don't match chars */
00143 
00144     case OP_REVERSE:
00145     case OP_CREF:
00146     case OP_NCREF:
00147     case OP_RREF:
00148     case OP_NRREF:
00149     case OP_DEF:
00150     case OP_OPT:
00151     case OP_CALLOUT:
00152     case OP_SOD:
00153     case OP_SOM:
00154     case OP_EOD:
00155     case OP_EODN:
00156     case OP_CIRC:
00157     case OP_DOLL:
00158     case OP_NOT_WORD_BOUNDARY:
00159     case OP_WORD_BOUNDARY:
00160     cc += _pcre_OP_lengths[*cc];
00161     break;
00162 
00163     /* Skip over a subpattern that has a {0} or {0,x} quantifier */
00164 
00165     case OP_BRAZERO:
00166     case OP_BRAMINZERO:
00167     case OP_SKIPZERO:
00168     cc += _pcre_OP_lengths[*cc];
00169     do cc += GET(cc, 1); while (*cc == OP_ALT);
00170     cc += 1 + LINK_SIZE;
00171     break;
00172 
00173     /* Handle literal characters and + repetitions */
00174 
00175     case OP_CHAR:
00176     case OP_CHARNC:
00177     case OP_NOT:
00178     case OP_PLUS:
00179     case OP_MINPLUS:
00180     case OP_POSPLUS:
00181     case OP_NOTPLUS:
00182     case OP_NOTMINPLUS:
00183     case OP_NOTPOSPLUS:
00184     branchlength++;
00185     cc += 2;
00186 #ifdef SUPPORT_UTF8
00187     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
00188 #endif
00189     break;
00190 
00191     case OP_TYPEPLUS:
00192     case OP_TYPEMINPLUS:
00193     case OP_TYPEPOSPLUS:
00194     branchlength++;
00195     cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
00196     break;
00197 
00198     /* Handle exact repetitions. The count is already in characters, but we
00199     need to skip over a multibyte character in UTF8 mode.  */
00200 
00201     case OP_EXACT:
00202     case OP_NOTEXACT:
00203     branchlength += GET2(cc,1);
00204     cc += 4;
00205 #ifdef SUPPORT_UTF8
00206     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
00207 #endif
00208     break;
00209 
00210     case OP_TYPEEXACT:
00211     branchlength += GET2(cc,1);
00212     cc += (cc[3] == OP_PROP || cc[3] == OP_NOTPROP)? 6 : 4;
00213     break;
00214 
00215     /* Handle single-char non-literal matchers */
00216 
00217     case OP_PROP:
00218     case OP_NOTPROP:
00219     cc += 2;
00220     /* Fall through */
00221 
00222     case OP_NOT_DIGIT:
00223     case OP_DIGIT:
00224     case OP_NOT_WHITESPACE:
00225     case OP_WHITESPACE:
00226     case OP_NOT_WORDCHAR:
00227     case OP_WORDCHAR:
00228     case OP_ANY:
00229     case OP_ALLANY:
00230     case OP_EXTUNI:
00231     case OP_HSPACE:
00232     case OP_NOT_HSPACE:
00233     case OP_VSPACE:
00234     case OP_NOT_VSPACE:
00235     branchlength++;
00236     cc++;
00237     break;
00238 
00239     /* "Any newline" might match two characters */
00240 
00241     case OP_ANYNL:
00242     branchlength += 2;
00243     cc++;
00244     break;
00245 
00246     /* The single-byte matcher means we can't proceed in UTF-8 mode */
00247 
00248     case OP_ANYBYTE:
00249 #ifdef SUPPORT_UTF8
00250     if (utf8) return -1;
00251 #endif
00252     branchlength++;
00253     cc++;
00254     break;
00255 
00256     /* For repeated character types, we have to test for \p and \P, which have
00257     an extra two bytes of parameters. */
00258 
00259     case OP_TYPESTAR:
00260     case OP_TYPEMINSTAR:
00261     case OP_TYPEQUERY:
00262     case OP_TYPEMINQUERY:
00263     case OP_TYPEPOSSTAR:
00264     case OP_TYPEPOSQUERY:
00265     if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
00266     cc += _pcre_OP_lengths[op];
00267     break;
00268 
00269     case OP_TYPEUPTO:
00270     case OP_TYPEMINUPTO:
00271     case OP_TYPEPOSUPTO:
00272     if (cc[3] == OP_PROP || cc[3] == OP_NOTPROP) cc += 2;
00273     cc += _pcre_OP_lengths[op];
00274     break;
00275 
00276     /* Check a class for variable quantification */
00277 
00278 #ifdef SUPPORT_UTF8
00279     case OP_XCLASS:
00280     cc += GET(cc, 1) - 33;
00281     /* Fall through */
00282 #endif
00283 
00284     case OP_CLASS:
00285     case OP_NCLASS:
00286     cc += 33;
00287 
00288     switch (*cc)
00289       {
00290       case OP_CRPLUS:
00291       case OP_CRMINPLUS:
00292       branchlength++;
00293       /* Fall through */
00294 
00295       case OP_CRSTAR:
00296       case OP_CRMINSTAR:
00297       case OP_CRQUERY:
00298       case OP_CRMINQUERY:
00299       cc++;
00300       break;
00301 
00302       case OP_CRRANGE:
00303       case OP_CRMINRANGE:
00304       branchlength += GET2(cc,1);
00305       cc += 5;
00306       break;
00307 
00308       default:
00309       branchlength++;
00310       break;
00311       }
00312     break;
00313 
00314     /* Backreferences and subroutine calls are treated in the same way: we find
00315     the minimum length for the subpattern. A recursion, however, causes an
00316     a flag to be set that causes the length of this branch to be ignored. The
00317     logic is that a recursion can only make sense if there is another
00318     alternation that stops the recursing. That will provide the minimum length
00319     (when no recursion happens). A backreference within the group that it is
00320     referencing behaves in the same way.
00321 
00322     If PCRE_JAVASCRIPT_COMPAT is set, a backreference to an unset bracket
00323     matches an empty string (by default it causes a matching failure), so in
00324     that case we must set the minimum length to zero. */
00325 
00326     case OP_REF:
00327     if ((options & PCRE_JAVASCRIPT_COMPAT) == 0)
00328       {
00329       ce = cs = (uschar *)_pcre_find_bracket(startcode, utf8, GET2(cc, 1));
00330       if (cs == NULL) return -2;
00331       do ce += GET(ce, 1); while (*ce == OP_ALT);
00332       if (cc > cs && cc < ce)
00333         {
00334         d = 0;
00335         had_recurse = TRUE;
00336         }
00337       else d = find_minlength(cs, startcode, options);
00338       }
00339     else d = 0;
00340     cc += 3;
00341 
00342     /* Handle repeated back references */
00343 
00344     switch (*cc)
00345       {
00346       case OP_CRSTAR:
00347       case OP_CRMINSTAR:
00348       case OP_CRQUERY:
00349       case OP_CRMINQUERY:
00350       min = 0;
00351       cc++;
00352       break;
00353 
00354       case OP_CRRANGE:
00355       case OP_CRMINRANGE:
00356       min = GET2(cc, 1);
00357       cc += 5;
00358       break;
00359 
00360       default:
00361       min = 1;
00362       break;
00363       }
00364 
00365     branchlength += min * d;
00366     break;
00367 
00368     case OP_RECURSE:
00369     cs = ce = (uschar *)startcode + GET(cc, 1);
00370     if (cs == NULL) return -2;
00371     do ce += GET(ce, 1); while (*ce == OP_ALT);
00372     if (cc > cs && cc < ce)
00373       had_recurse = TRUE;
00374     else
00375       branchlength += find_minlength(cs, startcode, options);
00376     cc += 1 + LINK_SIZE;
00377     break;
00378 
00379     /* Anything else does not or need not match a character. We can get the
00380     item's length from the table, but for those that can match zero occurrences
00381     of a character, we must take special action for UTF-8 characters. */
00382 
00383     case OP_UPTO:
00384     case OP_NOTUPTO:
00385     case OP_MINUPTO:
00386     case OP_NOTMINUPTO:
00387     case OP_POSUPTO:
00388     case OP_STAR:
00389     case OP_MINSTAR:
00390     case OP_NOTMINSTAR:
00391     case OP_POSSTAR:
00392     case OP_NOTPOSSTAR:
00393     case OP_QUERY:
00394     case OP_MINQUERY:
00395     case OP_NOTMINQUERY:
00396     case OP_POSQUERY:
00397     case OP_NOTPOSQUERY:
00398     cc += _pcre_OP_lengths[op];
00399 #ifdef SUPPORT_UTF8
00400     if (utf8 && cc[-1] >= 0xc0) cc += _pcre_utf8_table4[cc[-1] & 0x3f];
00401 #endif
00402     break;
00403 
00404     /* For the record, these are the opcodes that are matched by "default":
00405     OP_ACCEPT, OP_CLOSE, OP_COMMIT, OP_FAIL, OP_PRUNE, OP_SET_SOM, OP_SKIP,
00406     OP_THEN. */
00407 
00408     default:
00409     cc += _pcre_OP_lengths[op];
00410     break;
00411     }
00412   }
00413 /* Control never gets here */
00414 }
00415 
00416 
00417 
00418 /*************************************************
00419 *      Set a bit and maybe its alternate case    *
00420 *************************************************/
00421 
00422 /* Given a character, set its bit in the table, and also the bit for the other
00423 version of a letter if we are caseless.
00424 
00425 Arguments:
00426   start_bits    points to the bit map
00427   c             is the character
00428   caseless      the caseless flag
00429   cd            the block with char table pointers
00430 
00431 Returns:        nothing
00432 */
00433 
00434 static void
00435 set_bit(uschar *start_bits, unsigned int c, BOOL caseless, compile_data *cd)
00436 {
00437 start_bits[c/8] |= (1 << (c&7));
00438 if (caseless && (cd->ctypes[c] & ctype_letter) != 0)
00439   start_bits[cd->fcc[c]/8] |= (1 << (cd->fcc[c]&7));
00440 }
00441 
00442 
00443 
00444 /*************************************************
00445 *          Create bitmap of starting bytes       *
00446 *************************************************/
00447 
00448 /* This function scans a compiled unanchored expression recursively and
00449 attempts to build a bitmap of the set of possible starting bytes. As time goes
00450 by, we may be able to get more clever at doing this. The SSB_CONTINUE return is
00451 useful for parenthesized groups in patterns such as (a*)b where the group
00452 provides some optional starting bytes but scanning must continue at the outer
00453 level to find at least one mandatory byte. At the outermost level, this
00454 function fails unless the result is SSB_DONE.
00455 
00456 Arguments:
00457   code         points to an expression
00458   start_bits   points to a 32-byte table, initialized to 0
00459   caseless     the current state of the caseless flag
00460   utf8         TRUE if in UTF-8 mode
00461   cd           the block with char table pointers
00462 
00463 Returns:       SSB_FAIL     => Failed to find any starting bytes
00464                SSB_DONE     => Found mandatory starting bytes
00465                SSB_CONTINUE => Found optional starting bytes
00466 */
00467 
00468 static int
00469 set_start_bits(const uschar *code, uschar *start_bits, BOOL caseless,
00470   BOOL utf8, compile_data *cd)
00471 {
00472 register int c;
00473 int yield = SSB_DONE;
00474 
00475 #if 0
00476 /* ========================================================================= */
00477 /* The following comment and code was inserted in January 1999. In May 2006,
00478 when it was observed to cause compiler warnings about unused values, I took it
00479 out again. If anybody is still using OS/2, they will have to put it back
00480 manually. */
00481 
00482 /* This next statement and the later reference to dummy are here in order to
00483 trick the optimizer of the IBM C compiler for OS/2 into generating correct
00484 code. Apparently IBM isn't going to fix the problem, and we would rather not
00485 disable optimization (in this module it actually makes a big difference, and
00486 the pcre module can use all the optimization it can get). */
00487 
00488 volatile int dummy;
00489 /* ========================================================================= */
00490 #endif
00491 
00492 do
00493   {
00494   const uschar *tcode = code + (((int)*code == OP_CBRA)? 3:1) + LINK_SIZE;
00495   BOOL try_next = TRUE;
00496 
00497   while (try_next)    /* Loop for items in this branch */
00498     {
00499     int rc;
00500     switch(*tcode)
00501       {
00502       /* Fail if we reach something we don't understand */
00503 
00504       default:
00505       return SSB_FAIL;
00506 
00507       /* If we hit a bracket or a positive lookahead assertion, recurse to set
00508       bits from within the subpattern. If it can't find anything, we have to
00509       give up. If it finds some mandatory character(s), we are done for this
00510       branch. Otherwise, carry on scanning after the subpattern. */
00511 
00512       case OP_BRA:
00513       case OP_SBRA:
00514       case OP_CBRA:
00515       case OP_SCBRA:
00516       case OP_ONCE:
00517       case OP_ASSERT:
00518       rc = set_start_bits(tcode, start_bits, caseless, utf8, cd);
00519       if (rc == SSB_FAIL) return SSB_FAIL;
00520       if (rc == SSB_DONE) try_next = FALSE; else
00521         {
00522         do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
00523         tcode += 1 + LINK_SIZE;
00524         }
00525       break;
00526 
00527       /* If we hit ALT or KET, it means we haven't found anything mandatory in
00528       this branch, though we might have found something optional. For ALT, we
00529       continue with the next alternative, but we have to arrange that the final
00530       result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
00531       return SSB_CONTINUE: if this is the top level, that indicates failure,
00532       but after a nested subpattern, it causes scanning to continue. */
00533 
00534       case OP_ALT:
00535       yield = SSB_CONTINUE;
00536       try_next = FALSE;
00537       break;
00538 
00539       case OP_KET:
00540       case OP_KETRMAX:
00541       case OP_KETRMIN:
00542       return SSB_CONTINUE;
00543 
00544       /* Skip over callout */
00545 
00546       case OP_CALLOUT:
00547       tcode += 2 + 2*LINK_SIZE;
00548       break;
00549 
00550       /* Skip over lookbehind and negative lookahead assertions */
00551 
00552       case OP_ASSERT_NOT:
00553       case OP_ASSERTBACK:
00554       case OP_ASSERTBACK_NOT:
00555       do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
00556       tcode += 1 + LINK_SIZE;
00557       break;
00558 
00559       /* Skip over an option setting, changing the caseless flag */
00560 
00561       case OP_OPT:
00562       caseless = (tcode[1] & PCRE_CASELESS) != 0;
00563       tcode += 2;
00564       break;
00565 
00566       /* BRAZERO does the bracket, but carries on. */
00567 
00568       case OP_BRAZERO:
00569       case OP_BRAMINZERO:
00570       if (set_start_bits(++tcode, start_bits, caseless, utf8, cd) == SSB_FAIL)
00571         return SSB_FAIL;
00572 /* =========================================================================
00573       See the comment at the head of this function concerning the next line,
00574       which was an old fudge for the benefit of OS/2.
00575       dummy = 1;
00576   ========================================================================= */
00577       do tcode += GET(tcode,1); while (*tcode == OP_ALT);
00578       tcode += 1 + LINK_SIZE;
00579       break;
00580 
00581       /* SKIPZERO skips the bracket. */
00582 
00583       case OP_SKIPZERO:
00584       tcode++;
00585       do tcode += GET(tcode,1); while (*tcode == OP_ALT);
00586       tcode += 1 + LINK_SIZE;
00587       break;
00588 
00589       /* Single-char * or ? sets the bit and tries the next item */
00590 
00591       case OP_STAR:
00592       case OP_MINSTAR:
00593       case OP_POSSTAR:
00594       case OP_QUERY:
00595       case OP_MINQUERY:
00596       case OP_POSQUERY:
00597       set_bit(start_bits, tcode[1], caseless, cd);
00598       tcode += 2;
00599 #ifdef SUPPORT_UTF8
00600       if (utf8 && tcode[-1] >= 0xc0)
00601         tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
00602 #endif
00603       break;
00604 
00605       /* Single-char upto sets the bit and tries the next */
00606 
00607       case OP_UPTO:
00608       case OP_MINUPTO:
00609       case OP_POSUPTO:
00610       set_bit(start_bits, tcode[3], caseless, cd);
00611       tcode += 4;
00612 #ifdef SUPPORT_UTF8
00613       if (utf8 && tcode[-1] >= 0xc0)
00614         tcode += _pcre_utf8_table4[tcode[-1] & 0x3f];
00615 #endif
00616       break;
00617 
00618       /* At least one single char sets the bit and stops */
00619 
00620       case OP_EXACT:       /* Fall through */
00621       tcode += 2;
00622 
00623       case OP_CHAR:
00624       case OP_CHARNC:
00625       case OP_PLUS:
00626       case OP_MINPLUS:
00627       case OP_POSPLUS:
00628       set_bit(start_bits, tcode[1], caseless, cd);
00629       try_next = FALSE;
00630       break;
00631 
00632       /* Single character type sets the bits and stops */
00633 
00634       case OP_NOT_DIGIT:
00635       for (c = 0; c < 32; c++)
00636         start_bits[c] |= ~cd->cbits[c+cbit_digit];
00637       try_next = FALSE;
00638       break;
00639 
00640       case OP_DIGIT:
00641       for (c = 0; c < 32; c++)
00642         start_bits[c] |= cd->cbits[c+cbit_digit];
00643       try_next = FALSE;
00644       break;
00645 
00646       /* The cbit_space table has vertical tab as whitespace; we have to
00647       discard it. */
00648 
00649       case OP_NOT_WHITESPACE:
00650       for (c = 0; c < 32; c++)
00651         {
00652         int d = cd->cbits[c+cbit_space];
00653         if (c == 1) d &= ~0x08;
00654         start_bits[c] |= ~d;
00655         }
00656       try_next = FALSE;
00657       break;
00658 
00659       /* The cbit_space table has vertical tab as whitespace; we have to
00660       discard it. */
00661 
00662       case OP_WHITESPACE:
00663       for (c = 0; c < 32; c++)
00664         {
00665         int d = cd->cbits[c+cbit_space];
00666         if (c == 1) d &= ~0x08;
00667         start_bits[c] |= d;
00668         }
00669       try_next = FALSE;
00670       break;
00671 
00672       case OP_NOT_WORDCHAR:
00673       for (c = 0; c < 32; c++)
00674         start_bits[c] |= ~cd->cbits[c+cbit_word];
00675       try_next = FALSE;
00676       break;
00677 
00678       case OP_WORDCHAR:
00679       for (c = 0; c < 32; c++)
00680         start_bits[c] |= cd->cbits[c+cbit_word];
00681       try_next = FALSE;
00682       break;
00683 
00684       /* One or more character type fudges the pointer and restarts, knowing
00685       it will hit a single character type and stop there. */
00686 
00687       case OP_TYPEPLUS:
00688       case OP_TYPEMINPLUS:
00689       tcode++;
00690       break;
00691 
00692       case OP_TYPEEXACT:
00693       tcode += 3;
00694       break;
00695 
00696       /* Zero or more repeats of character types set the bits and then
00697       try again. */
00698 
00699       case OP_TYPEUPTO:
00700       case OP_TYPEMINUPTO:
00701       case OP_TYPEPOSUPTO:
00702       tcode += 2;               /* Fall through */
00703 
00704       case OP_TYPESTAR:
00705       case OP_TYPEMINSTAR:
00706       case OP_TYPEPOSSTAR:
00707       case OP_TYPEQUERY:
00708       case OP_TYPEMINQUERY:
00709       case OP_TYPEPOSQUERY:
00710       switch(tcode[1])
00711         {
00712         case OP_ANY:
00713         case OP_ALLANY:
00714         return SSB_FAIL;
00715 
00716         case OP_NOT_DIGIT:
00717         for (c = 0; c < 32; c++)
00718           start_bits[c] |= ~cd->cbits[c+cbit_digit];
00719         break;
00720 
00721         case OP_DIGIT:
00722         for (c = 0; c < 32; c++)
00723           start_bits[c] |= cd->cbits[c+cbit_digit];
00724         break;
00725 
00726         /* The cbit_space table has vertical tab as whitespace; we have to
00727         discard it. */
00728 
00729         case OP_NOT_WHITESPACE:
00730         for (c = 0; c < 32; c++)
00731           {
00732           int d = cd->cbits[c+cbit_space];
00733           if (c == 1) d &= ~0x08;
00734           start_bits[c] |= ~d;
00735           }
00736         break;
00737 
00738         /* The cbit_space table has vertical tab as whitespace; we have to
00739         discard it. */
00740 
00741         case OP_WHITESPACE:
00742         for (c = 0; c < 32; c++)
00743           {
00744           int d = cd->cbits[c+cbit_space];
00745           if (c == 1) d &= ~0x08;
00746           start_bits[c] |= d;
00747           }
00748         break;
00749 
00750         case OP_NOT_WORDCHAR:
00751         for (c = 0; c < 32; c++)
00752           start_bits[c] |= ~cd->cbits[c+cbit_word];
00753         break;
00754 
00755         case OP_WORDCHAR:
00756         for (c = 0; c < 32; c++)
00757           start_bits[c] |= cd->cbits[c+cbit_word];
00758         break;
00759         }
00760 
00761       tcode += 2;
00762       break;
00763 
00764       /* Character class where all the information is in a bit map: set the
00765       bits and either carry on or not, according to the repeat count. If it was
00766       a negative class, and we are operating with UTF-8 characters, any byte
00767       with a value >= 0xc4 is a potentially valid starter because it starts a
00768       character with a value > 255. */
00769 
00770       case OP_NCLASS:
00771 #ifdef SUPPORT_UTF8
00772       if (utf8)
00773         {
00774         start_bits[24] |= 0xf0;              /* Bits for 0xc4 - 0xc8 */
00775         memset(start_bits+25, 0xff, 7);      /* Bits for 0xc9 - 0xff */
00776         }
00777 #endif
00778       /* Fall through */
00779 
00780       case OP_CLASS:
00781         {
00782         tcode++;
00783 
00784         /* In UTF-8 mode, the bits in a bit map correspond to character
00785         values, not to byte values. However, the bit map we are constructing is
00786         for byte values. So we have to do a conversion for characters whose
00787         value is > 127. In fact, there are only two possible starting bytes for
00788         characters in the range 128 - 255. */
00789 
00790 #ifdef SUPPORT_UTF8
00791         if (utf8)
00792           {
00793           for (c = 0; c < 16; c++) start_bits[c] |= tcode[c];
00794           for (c = 128; c < 256; c++)
00795             {
00796             if ((tcode[c/8] && (1 << (c&7))) != 0)
00797               {
00798               int d = (c >> 6) | 0xc0;            /* Set bit for this starter */
00799               start_bits[d/8] |= (1 << (d&7));    /* and then skip on to the */
00800               c = (c & 0xc0) + 0x40 - 1;          /* next relevant character. */
00801               }
00802             }
00803           }
00804 
00805         /* In non-UTF-8 mode, the two bit maps are completely compatible. */
00806 
00807         else
00808 #endif
00809           {
00810           for (c = 0; c < 32; c++) start_bits[c] |= tcode[c];
00811           }
00812 
00813         /* Advance past the bit map, and act on what follows */
00814 
00815         tcode += 32;
00816         switch (*tcode)
00817           {
00818           case OP_CRSTAR:
00819           case OP_CRMINSTAR:
00820           case OP_CRQUERY:
00821           case OP_CRMINQUERY:
00822           tcode++;
00823           break;
00824 
00825           case OP_CRRANGE:
00826           case OP_CRMINRANGE:
00827           if (((tcode[1] << 8) + tcode[2]) == 0) tcode += 5;
00828             else try_next = FALSE;
00829           break;
00830 
00831           default:
00832           try_next = FALSE;
00833           break;
00834           }
00835         }
00836       break; /* End of bitmap class handling */
00837 
00838       }      /* End of switch */
00839     }        /* End of try_next loop */
00840 
00841   code += GET(code, 1);   /* Advance to next branch */
00842   }
00843 while (*code == OP_ALT);
00844 return yield;
00845 }
00846 
00847 
00848 
00849 /*************************************************
00850 *          Study a compiled expression           *
00851 *************************************************/
00852 
00853 /* This function is handed a compiled expression that it must study to produce
00854 information that will speed up the matching. It returns a pcre_extra block
00855 which then gets handed back to pcre_exec().
00856 
00857 Arguments:
00858   re        points to the compiled expression
00859   options   contains option bits
00860   errorptr  points to where to place error messages;
00861             set NULL unless error
00862 
00863 Returns:    pointer to a pcre_extra block, with study_data filled in and the
00864               appropriate flags set;
00865             NULL on error or if no optimization possible
00866 */
00867 
00868 PCRE_EXP_DEFN pcre_extra * PCRE_CALL_CONVENTION
00869 pcre_study(const pcre *external_re, int options, const char **errorptr)
00870 {
00871 int min;
00872 BOOL bits_set = FALSE;
00873 uschar start_bits[32];
00874 pcre_extra *extra;
00875 pcre_study_data *study;
00876 const uschar *tables;
00877 uschar *code;
00878 compile_data compile_block;
00879 const real_pcre *re = (const real_pcre *)external_re;
00880 
00881 *errorptr = NULL;
00882 
00883 if (re == NULL || re->magic_number != MAGIC_NUMBER)
00884   {
00885   *errorptr = "argument is not a compiled regular expression";
00886   return NULL;
00887   }
00888 
00889 if ((options & ~PUBLIC_STUDY_OPTIONS) != 0)
00890   {
00891   *errorptr = "unknown or incorrect option bit(s) set";
00892   return NULL;
00893   }
00894 
00895 code = (uschar *)re + re->name_table_offset +
00896   (re->name_count * re->name_entry_size);
00897 
00898 /* For an anchored pattern, or an unanchored pattern that has a first char, or
00899 a multiline pattern that matches only at "line starts", there is no point in
00900 seeking a list of starting bytes. */
00901 
00902 if ((re->options & PCRE_ANCHORED) == 0 &&
00903     (re->flags & (PCRE_FIRSTSET|PCRE_STARTLINE)) == 0)
00904   {
00905   /* Set the character tables in the block that is passed around */
00906 
00907   tables = re->tables;
00908   if (tables == NULL)
00909     (void)pcre_fullinfo(external_re, NULL, PCRE_INFO_DEFAULT_TABLES,
00910     (void *)(&tables));
00911 
00912   compile_block.lcc = tables + lcc_offset;
00913   compile_block.fcc = tables + fcc_offset;
00914   compile_block.cbits = tables + cbits_offset;
00915   compile_block.ctypes = tables + ctypes_offset;
00916 
00917   /* See if we can find a fixed set of initial characters for the pattern. */
00918 
00919   memset(start_bits, 0, 32 * sizeof(uschar));
00920   bits_set = set_start_bits(code, start_bits,
00921     (re->options & PCRE_CASELESS) != 0, (re->options & PCRE_UTF8) != 0,
00922     &compile_block) == SSB_DONE;
00923   }
00924 
00925 /* Find the minimum length of subject string. */
00926 
00927 min = find_minlength(code, code, re->options);
00928 
00929 /* Return NULL if no optimization is possible. */
00930 
00931 if (!bits_set && min < 0) return NULL;
00932 
00933 /* Get a pcre_extra block and a pcre_study_data block. The study data is put in
00934 the latter, which is pointed to by the former, which may also get additional
00935 data set later by the calling program. At the moment, the size of
00936 pcre_study_data is fixed. We nevertheless save it in a field for returning via
00937 the pcre_fullinfo() function so that if it becomes variable in the future, we
00938 don't have to change that code. */
00939 
00940 extra = (pcre_extra *)(pcre_malloc)
00941   (sizeof(pcre_extra) + sizeof(pcre_study_data));
00942 
00943 if (extra == NULL)
00944   {
00945   *errorptr = "failed to get memory";
00946   return NULL;
00947   }
00948 
00949 study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
00950 extra->flags = PCRE_EXTRA_STUDY_DATA;
00951 extra->study_data = study;
00952 
00953 study->size = sizeof(pcre_study_data);
00954 study->flags = 0;
00955 
00956 if (bits_set)
00957   {
00958   study->flags |= PCRE_STUDY_MAPPED;
00959   memcpy(study->start_bits, start_bits, sizeof(start_bits));
00960   }
00961 
00962 if (min >= 0)
00963   {
00964   study->flags |= PCRE_STUDY_MINLEN;
00965   study->minlength = min;
00966   }
00967 
00968 return extra;
00969 }
00970 
00971 /* End of pcre_study.c */