📄 pcre_exec.c.svn-base
字号:
HEAP_RECURSE:
/* Macros make the argument variables come from the current frame */
#define eptr frame->Xeptr
#define ecode frame->Xecode
#define offset_top frame->Xoffset_top
#define ims frame->Xims
#define eptrb frame->Xeptrb
#define flags frame->Xflags
#define rdepth frame->Xrdepth
/* Ditto for the local variables */
#ifdef SUPPORT_UTF8
#define charptr frame->Xcharptr
#endif
#define callpat frame->Xcallpat
#define data frame->Xdata
#define next frame->Xnext
#define pp frame->Xpp
#define prev frame->Xprev
#define saved_eptr frame->Xsaved_eptr
#define new_recursive frame->Xnew_recursive
#define cur_is_word frame->Xcur_is_word
#define condition frame->Xcondition
#define prev_is_word frame->Xprev_is_word
#define original_ims frame->Xoriginal_ims
#ifdef SUPPORT_UCP
#define prop_type frame->Xprop_type
#define prop_value frame->Xprop_value
#define prop_fail_result frame->Xprop_fail_result
#define prop_category frame->Xprop_category
#define prop_chartype frame->Xprop_chartype
#define prop_script frame->Xprop_script
#define oclength frame->Xoclength
#define occhars frame->Xocchars
#endif
#define ctype frame->Xctype
#define fc frame->Xfc
#define fi frame->Xfi
#define length frame->Xlength
#define max frame->Xmax
#define min frame->Xmin
#define number frame->Xnumber
#define offset frame->Xoffset
#define op frame->Xop
#define save_capture_last frame->Xsave_capture_last
#define save_offset1 frame->Xsave_offset1
#define save_offset2 frame->Xsave_offset2
#define save_offset3 frame->Xsave_offset3
#define stacksave frame->Xstacksave
#define newptrb frame->Xnewptrb
/* When recursion is being used, local variables are allocated on the stack and
get preserved during recursion in the normal way. In this environment, fi and
i, and fc and c, can be the same variables. */
#else /* NO_RECURSE not defined */
#define fi i
#define fc c
#ifdef SUPPORT_UTF8 /* Many of these variables are used only */
const uschar *charptr; /* in small blocks of the code. My normal */
#endif /* style of coding would have declared */
const uschar *callpat; /* them within each of those blocks. */
const uschar *data; /* However, in order to accommodate the */
const uschar *next; /* version of this code that uses an */
USPTR pp; /* external "stack" implemented on the */
const uschar *prev; /* heap, it is easier to declare them all */
USPTR saved_eptr; /* here, so the declarations can be cut */
/* out in a block. The only declarations */
recursion_info new_recursive; /* within blocks below are for variables */
/* that do not have to be preserved over */
BOOL cur_is_word; /* a recursive call to RMATCH(). */
BOOL condition;
BOOL prev_is_word;
unsigned long int original_ims;
#ifdef SUPPORT_UCP
int prop_type;
int prop_value;
int prop_fail_result;
int prop_category;
int prop_chartype;
int prop_script;
int oclength;
uschar occhars[8];
#endif
int ctype;
int length;
int max;
int min;
int number;
int offset;
int op;
int save_capture_last;
int save_offset1, save_offset2, save_offset3;
int stacksave[REC_STACK_SAVE_MAX];
eptrblock newptrb;
#endif /* NO_RECURSE */
/* These statements are here to stop the compiler complaining about unitialized
variables. */
#ifdef SUPPORT_UCP
prop_value = 0;
prop_fail_result = 0;
#endif
/* This label is used for tail recursion, which is used in a few cases even
when NO_RECURSE is not defined, in order to reduce the amount of stack that is
used. Thanks to Ian Taylor for noticing this possibility and sending the
original patch. */
TAIL_RECURSE:
/* OK, now we can get on with the real code of the function. Recursive calls
are specified by the macro RMATCH and RRETURN is used to return. When
NO_RECURSE is *not* defined, these just turn into a recursive call to match()
and a "return", respectively (possibly with some debugging if DEBUG is
defined). However, RMATCH isn't like a function call because it's quite a
complicated macro. It has to be used in one particular way. This shouldn't,
however, impact performance when true recursion is being used. */
/* First check that we haven't called match() too many times, or that we
haven't exceeded the recursive call limit. */
if (md->match_call_count++ >= md->match_limit) RRETURN(PCRE_ERROR_MATCHLIMIT);
if (rdepth >= md->match_limit_recursion) RRETURN(PCRE_ERROR_RECURSIONLIMIT);
original_ims = ims; /* Save for resetting on ')' */
#ifdef SUPPORT_UTF8
utf8 = md->utf8; /* Local copy of the flag */
#else
utf8 = FALSE;
#endif
/* At the start of a group with an unlimited repeat that may match an empty
string, the match_cbegroup flag is set. When this is the case, add the current
subject pointer to the chain of such remembered pointers, to be checked when we
hit the closing ket, in order to break infinite loops that match no characters.
When match() is called in other circumstances, don't add to the chain. If this
is a tail recursion, use a block from the workspace, as the one on the stack is
already used. */
if ((flags & match_cbegroup) != 0)
{
eptrblock *p;
if ((flags & match_tail_recursed) != 0)
{
if (md->eptrn >= EPTR_WORK_SIZE) RRETURN(PCRE_ERROR_NULLWSLIMIT);
p = md->eptrchain + md->eptrn++;
}
else p = &newptrb;
p->epb_saved_eptr = eptr;
p->epb_prev = eptrb;
eptrb = p;
}
/* Now start processing the opcodes. */
for (;;)
{
minimize = possessive = FALSE;
op = *ecode;
/* For partial matching, remember if we ever hit the end of the subject after
matching at least one subject character. */
if (md->partial &&
eptr >= md->end_subject &&
eptr > md->start_match)
md->hitend = TRUE;
switch(op)
{
/* Handle a capturing bracket. If there is space in the offset vector, save
the current subject position in the working slot at the top of the vector.
We mustn't change the current values of the data slot, because they may be
set from a previous iteration of this group, and be referred to by a
reference inside the group.
If the bracket fails to match, we need to restore this value and also the
values of the final offsets, in case they were set by a previous iteration
of the same bracket.
If there isn't enough space in the offset vector, treat this as if it were
a non-capturing bracket. Don't worry about setting the flag for the error
case here; that is handled in the code for KET. */
case OP_CBRA:
case OP_SCBRA:
number = GET2(ecode, 1+LINK_SIZE);
offset = number << 1;
#ifdef DEBUG
printf("start bracket %d\n", number);
printf("subject=");
pchars(eptr, 16, TRUE, md);
printf("\n");
#endif
if (offset < md->offset_max)
{
save_offset1 = md->offset_vector[offset];
save_offset2 = md->offset_vector[offset+1];
save_offset3 = md->offset_vector[md->offset_end - number];
save_capture_last = md->capture_last;
DPRINTF(("saving %d %d %d\n", save_offset1, save_offset2, save_offset3));
md->offset_vector[md->offset_end - number] = eptr - md->start_subject;
flags = (op == OP_SCBRA)? match_cbegroup : 0;
do
{
RMATCH(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md,
ims, eptrb, flags);
if (rrc != MATCH_NOMATCH) RRETURN(rrc);
md->capture_last = save_capture_last;
ecode += GET(ecode, 1);
}
while (*ecode == OP_ALT);
DPRINTF(("bracket %d failed\n", number));
md->offset_vector[offset] = save_offset1;
md->offset_vector[offset+1] = save_offset2;
md->offset_vector[md->offset_end - number] = save_offset3;
RRETURN(MATCH_NOMATCH);
}
/* Insufficient room for saving captured contents. Treat as a non-capturing
bracket. */
DPRINTF(("insufficient capture room: treat as non-capturing\n"));
/* Non-capturing bracket. Loop for all the alternatives. When we get to the
final alternative within the brackets, we would return the result of a
recursive call to match() whatever happened. We can reduce stack usage by
turning this into a tail recursion. */
case OP_BRA:
case OP_SBRA:
DPRINTF(("start non-capturing bracket\n"));
flags = (op >= OP_SBRA)? match_cbegroup : 0;
for (;;)
{
if (ecode[GET(ecode, 1)] != OP_ALT)
{
ecode += _pcre_OP_lengths[*ecode];
flags |= match_tail_recursed;
DPRINTF(("bracket 0 tail recursion\n"));
goto TAIL_RECURSE;
}
/* For non-final alternatives, continue the loop for a NOMATCH result;
otherwise return. */
RMATCH(rrc, eptr, ecode + _pcre_OP_lengths[*ecode], offset_top, md, ims,
eptrb, flags);
if (rrc != MATCH_NOMATCH) RRETURN(rrc);
ecode += GET(ecode, 1);
}
/* Control never reaches here. */
/* Conditional group: compilation checked that there are no more than
two branches. If the condition is false, skipping the first branch takes us
past the end if there is only one branch, but that's OK because that is
exactly what going to the ket would do. As there is only one branch to be
obeyed, we can use tail recursion to avoid using another stack frame. */
case OP_COND:
case OP_SCOND:
if (ecode[LINK_SIZE+1] == OP_RREF) /* Recursion test */
{
offset = GET2(ecode, LINK_SIZE + 2); /* Recursion group number*/
condition = md->recursive != NULL &&
(offset == RREF_ANY || offset == md->recursive->group_num);
ecode += condition? 3 : GET(ecode, 1);
}
else if (ecode[LINK_SIZE+1] == OP_CREF) /* Group used test */
{
offset = GET2(ecode, LINK_SIZE+2) << 1; /* Doubled ref number */
condition = offset < offset_top && md->offset_vector[offset] >= 0;
ecode += condition? 3 : GET(ecode, 1);
}
else if (ecode[LINK_SIZE+1] == OP_DEF) /* DEFINE - always false */
{
condition = FALSE;
ecode += GET(ecode, 1);
}
/* The condition is an assertion. Call match() to evaluate it - setting
the final argument match_condassert causes it to stop at the end of an
assertion. */
else
{
RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL,
match_condassert);
if (rrc == MATCH_MATCH)
{
condition = TRUE;
ecode += 1 + LINK_SIZE + GET(ecode, LINK_SIZE + 2);
while (*ecode == OP_ALT) ecode += GET(ecode, 1);
}
else if (rrc != MATCH_NOMATCH)
{
RRETURN(rrc); /* Need braces because of following else */
}
else
{
condition = FALSE;
ecode += GET(ecode, 1);
}
}
/* We are now at the branch that is to be obeyed. As there is only one,
we can use tail recursion to avoid using another stack frame. If the second
alternative doesn't exist, we can just plough on. */
if (condition || *ecode == OP_ALT)
{
ecode += 1 + LINK_SIZE;
flags = match_tail_recursed | ((op == OP_SCOND)? match_cbegroup : 0);
goto TAIL_RECURSE;
}
else
{
ecode += 1 + LINK_SIZE;
}
break;
/* End of the pattern. If we are in a top-level recursion, we should
restore the offsets appropriately and continue from after the call. */
case OP_END:
if (md->recursive != NULL && md->recursive->group_num == 0)
{
recursion_info *rec = md->recursive;
DPRINTF(("End of pattern in a (?0) recursion\n"));
md->recursive = rec->prevrec;
memmove(md->offset_vector, rec->offset_save,
rec->saved_max * sizeof(int));
md->start_match = rec->save_start;
ims = original_ims;
ecode = rec->after_call;
break;
}
/* Otherwise, if PCRE_NOTEMPTY is set, fail if we have matched an empty
string - backtracking will then try other alternatives, if any. */
if (md->notempty && eptr == md->start_match) RRETURN(MATCH_NOMATCH);
md->end_match_ptr = eptr; /* Record where we ended */
md->end_offset_top = offset_top; /* and how many extracts were taken */
RRETURN(MATCH_MATCH);
/* Change option settings */
case OP_OPT:
ims = ecode[1];
ecode += 2;
DPRINTF(("ims set to %02lx\n", ims));
break;
/* Assertion brackets. Check the alternative branches in turn - the
matching won't pass the KET for an assertion. If any one branch matches,
the assertion is true. Lookbehind assertions have an OP_REVERSE item at the
start of each branch to move the current point backwards, so the code at
this level is identical to the lookahead case. */
case OP_ASSERT:
case OP_ASSERTBACK:
do
{
RMATCH(rrc, eptr, ecode + 1 + LINK_SIZE, offset_top, md, ims, NULL, 0);
if (rrc == MATCH_MATCH) break;
if (rrc != MATCH_NOMATCH) RRETURN(rrc);
ecode += GET(ecode, 1);
}
while (*ecode == OP_ALT);
if (*ecode == OP_KET) RRETURN(MATCH_NOMATCH);
/* If checking an assertion for a condition, return MATCH_MATCH. */
if ((flags & match_condassert) != 0) RRETURN(MATCH_MATCH);
/* Continue from after the assertion, updating the offsets high water
mark, since extracts may have been taken during the assertion. */
do ecode += GET(ecode,1); while (*ecode == OP_ALT);
ecode += 1 + LINK_SIZE;
offset_top = md->end_offset_top;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -