?? pcre_dfa_exec.c
字號:
/************************************************** Perl-Compatible Regular Expressions **************************************************//* PCRE is a library of functions to support regular expressions whose syntaxand semantics are as close as possible to those of the Perl 5 language. Written by Philip Hazel Copyright (c) 1997-2007 University of Cambridge-----------------------------------------------------------------------------Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BELIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OFSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER INCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF SUCH DAMAGE.-----------------------------------------------------------------------------*//* This module contains the external function pcre_dfa_exec(), which is analternative matching function that uses a sort of DFA algorithm (not a trueFSM). This is NOT Perl- compatible, but it has advantages in certainapplications. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#define NLBLOCK md /* Block containing newline information */#define PSSTART start_subject /* Field containing processed string start */#define PSEND end_subject /* Field containing processed string end */#include "pcre_internal.h"/* For use to indent debugging output */#define SP " "/************************************************** Code parameters and static tables **************************************************//* These are offsets that are used to turn the OP_TYPESTAR and friends opcodesinto others, under special conditions. A gap of 20 between the blocks should beenough. The resulting opcodes don't have to be less than 256 because they arenever stored, so we push them well clear of the normal opcodes. */#define OP_PROP_EXTRA 300#define OP_EXTUNI_EXTRA 320#define OP_ANYNL_EXTRA 340#define OP_HSPACE_EXTRA 360#define OP_VSPACE_EXTRA 380/* This table identifies those opcodes that are followed immediately by acharacter that is to be tested in some way. This makes is possible tocentralize the loading of these characters. In the case of Type * etc, the"character" is the opcode for \D, \d, \S, \s, \W, or \w, which will always be asmall value. ***NOTE*** If the start of this table is modified, the two tablesthat follow must also be modified. */static uschar coptable[] = { 0, /* End */ 0, 0, 0, 0, 0, /* \A, \G, \K, \B, \b */ 0, 0, 0, 0, 0, 0, /* \D, \d, \S, \s, \W, \w */ 0, 0, /* Any, Anybyte */ 0, 0, 0, /* NOTPROP, PROP, EXTUNI */ 0, 0, 0, 0, 0, /* \R, \H, \h, \V, \v */ 0, 0, 0, 0, 0, /* \Z, \z, Opt, ^, $ */ 1, /* Char */ 1, /* Charnc */ 1, /* not */ /* Positive single-char repeats */ 1, 1, 1, 1, 1, 1, /* *, *?, +, +?, ?, ?? */ 3, 3, 3, /* upto, minupto, exact */ 1, 1, 1, 3, /* *+, ++, ?+, upto+ */ /* Negative single-char repeats - only for chars < 256 */ 1, 1, 1, 1, 1, 1, /* NOT *, *?, +, +?, ?, ?? */ 3, 3, 3, /* NOT upto, minupto, exact */ 1, 1, 1, 3, /* NOT *+, ++, ?+, updo+ */ /* Positive type repeats */ 1, 1, 1, 1, 1, 1, /* Type *, *?, +, +?, ?, ?? */ 3, 3, 3, /* Type upto, minupto, exact */ 1, 1, 1, 3, /* Type *+, ++, ?+, upto+ */ /* Character class & ref repeats */ 0, 0, 0, 0, 0, 0, /* *, *?, +, +?, ?, ?? */ 0, 0, /* CRRANGE, CRMINRANGE */ 0, /* CLASS */ 0, /* NCLASS */ 0, /* XCLASS - variable length */ 0, /* REF */ 0, /* RECURSE */ 0, /* CALLOUT */ 0, /* Alt */ 0, /* Ket */ 0, /* KetRmax */ 0, /* KetRmin */ 0, /* Assert */ 0, /* Assert not */ 0, /* Assert behind */ 0, /* Assert behind not */ 0, /* Reverse */ 0, 0, 0, 0, /* ONCE, BRA, CBRA, COND */ 0, 0, 0, /* SBRA, SCBRA, SCOND */ 0, /* CREF */ 0, /* RREF */ 0, /* DEF */ 0, 0, /* BRAZERO, BRAMINZERO */ 0, 0, 0, 0, /* PRUNE, SKIP, THEN, COMMIT */ 0, 0 /* FAIL, ACCEPT */};/* These 2 tables allow for compact code for testing for \D, \d, \S, \s, \W,and \w */static uschar toptable1[] = { 0, 0, 0, 0, 0, 0, ctype_digit, ctype_digit, ctype_space, ctype_space, ctype_word, ctype_word, 0 /* OP_ANY */};static uschar toptable2[] = { 0, 0, 0, 0, 0, 0, ctype_digit, 0, ctype_space, 0, ctype_word, 0, 1 /* OP_ANY */};/* Structure for holding data about a particular state, which is in effect thecurrent data for an active path through the match tree. It must consistentirely of ints because the working vector we are passed, and which we putthese structures in, is a vector of ints. */typedef struct stateblock { int offset; /* Offset to opcode */ int count; /* Count for repeats */ int ims; /* ims flag bits */ int data; /* Some use extra data */} stateblock;#define INTS_PER_STATEBLOCK (sizeof(stateblock)/sizeof(int))#ifdef DEBUG/************************************************** Print character string **************************************************//* Character string printing function for debugging.Arguments: p points to string length number of bytes f where to printReturns: nothing*/static voidpchars(unsigned char *p, int length, FILE *f){int c;while (length-- > 0) { if (isprint(c = *(p++))) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c); }}#endif/************************************************** Execute a Regular Expression - DFA engine **************************************************//* This internal function applies a compiled pattern to a subject string,starting at a given point, using a DFA engine. This function is called from theexternal one, possibly multiple times if the pattern is not anchored. Thefunction calls itself recursively for some kinds of subpattern.Arguments: md the match_data block with fixed information this_start_code the opening bracket of this subexpression's code current_subject where we currently are in the subject string start_offset start offset in the subject string offsets vector to contain the matching string offsets offsetcount size of same workspace vector of workspace wscount size of same ims the current ims flags rlevel function call recursion level recursing regex recursive call levelReturns: > 0 => = 0 => -1 => failed to match < -1 => some kind of unexpected problemThe following macros are used for adding states to the two state vectors (onefor the current character, one for the following character). */#define ADD_ACTIVE(x,y) \ if (active_count++ < wscount) \ { \ next_active_state->offset = (x); \ next_active_state->count = (y); \ next_active_state->ims = ims; \ next_active_state++; \ DPRINTF(("%.*sADD_ACTIVE(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \ } \ else return PCRE_ERROR_DFA_WSSIZE#define ADD_ACTIVE_DATA(x,y,z) \ if (active_count++ < wscount) \ { \ next_active_state->offset = (x); \ next_active_state->count = (y); \ next_active_state->ims = ims; \ next_active_state->data = (z); \ next_active_state++; \ DPRINTF(("%.*sADD_ACTIVE_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \ } \ else return PCRE_ERROR_DFA_WSSIZE#define ADD_NEW(x,y) \ if (new_count++ < wscount) \ { \ next_new_state->offset = (x); \ next_new_state->count = (y); \ next_new_state->ims = ims; \ next_new_state++; \ DPRINTF(("%.*sADD_NEW(%d,%d)\n", rlevel*2-2, SP, (x), (y))); \ } \ else return PCRE_ERROR_DFA_WSSIZE#define ADD_NEW_DATA(x,y,z) \ if (new_count++ < wscount) \ { \ next_new_state->offset = (x); \ next_new_state->count = (y); \ next_new_state->ims = ims; \ next_new_state->data = (z); \ next_new_state++; \ DPRINTF(("%.*sADD_NEW_DATA(%d,%d,%d)\n", rlevel*2-2, SP, (x), (y), (z))); \ } \ else return PCRE_ERROR_DFA_WSSIZE/* And now, here is the code */static intinternal_dfa_exec( dfa_match_data *md, const uschar *this_start_code, const uschar *current_subject, int start_offset, int *offsets, int offsetcount, int *workspace, int wscount, int ims, int rlevel, int recursing){stateblock *active_states, *new_states, *temp_states;stateblock *next_active_state, *next_new_state;const uschar *ctypes, *lcc, *fcc;const uschar *ptr;const uschar *end_code, *first_op;int active_count, new_count, match_count;/* Some fields in the md block are frequently referenced, so we load them intoindependent variables in the hope that this will perform better. */const uschar *start_subject = md->start_subject;const uschar *end_subject = md->end_subject;const uschar *start_code = md->start_code;#ifdef SUPPORT_UTF8BOOL utf8 = (md->poptions & PCRE_UTF8) != 0;#elseBOOL utf8 = FALSE;#endifrlevel++;offsetcount &= (-2);wscount -= 2;wscount = (wscount - (wscount % (INTS_PER_STATEBLOCK * 2))) / (2 * INTS_PER_STATEBLOCK);DPRINTF(("\n%.*s---------------------\n" "%.*sCall to internal_dfa_exec f=%d r=%d\n", rlevel*2-2, SP, rlevel*2-2, SP, rlevel, recursing));ctypes = md->tables + ctypes_offset;lcc = md->tables + lcc_offset;fcc = md->tables + fcc_offset;match_count = PCRE_ERROR_NOMATCH; /* A negative number */active_states = (stateblock *)(workspace + 2);next_new_state = new_states = active_states + wscount;new_count = 0;first_op = this_start_code + 1 + LINK_SIZE + ((*this_start_code == OP_CBRA || *this_start_code == OP_SCBRA)? 2:0);/* The first thing in any (sub) pattern is a bracket of some sort. Push allthe alternative states onto the list, and find out where the end is. Thismakes is possible to use this function recursively, when we want to stop at amatching internal ket rather than at the end.If the first opcode in the first alternative is OP_REVERSE, we are dealing witha backward assertion. In that case, we have to find out the maximum amount tomove back, and set up each alternative appropriately. */if (*first_op == OP_REVERSE) { int max_back = 0; int gone_back; end_code = this_start_code; do { int back = GET(end_code, 2+LINK_SIZE); if (back > max_back) max_back = back; end_code += GET(end_code, 1); } while (*end_code == OP_ALT); /* If we can't go back the amount required for the longest lookbehind pattern, go back as far as we can; some alternatives may still be viable. */#ifdef SUPPORT_UTF8 /* In character mode we have to step back character by character */ if (utf8) { for (gone_back = 0; gone_back < max_back; gone_back++) { if (current_subject <= start_subject) break; current_subject--; while (current_subject > start_subject && (*current_subject & 0xc0) == 0x80) current_subject--; } } else#endif /* In byte-mode we can do this quickly. */ { gone_back = (current_subject - max_back < start_subject)? current_subject - start_subject : max_back; current_subject -= gone_back; } /* Now we can process the individual branches. */ end_code = this_start_code; do { int back = GET(end_code, 2+LINK_SIZE); if (back <= gone_back) { int bstate = end_code - start_code + 2 + 2*LINK_SIZE; ADD_NEW_DATA(-bstate, 0, gone_back - back); } end_code += GET(end_code, 1); } while (*end_code == OP_ALT); }/* This is the code for a "normal" subpattern (not a backward assertion). Thestart of a whole pattern is always one of these. If we are at the top level,we may be asked to restart matching from the same point that we reached for aprevious partial match. We still have to scan through the top-level branches tofind the end state. */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -