2013-10-26 05:18:42 -03:00
|
|
|
/*
|
|
|
|
* Secret Labs' Regular Expression Engine
|
|
|
|
*
|
|
|
|
* regular expression matching engine
|
|
|
|
*
|
|
|
|
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
|
|
|
*
|
2022-04-04 04:53:26 -03:00
|
|
|
* See the sre.c file for information on usage and redistribution.
|
2013-10-26 05:18:42 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* String matching engine */
|
|
|
|
|
|
|
|
/* This file is included three times, with different character settings */
|
|
|
|
|
|
|
|
LOCAL(int)
|
2020-04-11 04:48:40 -03:00
|
|
|
SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at)
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
|
|
|
/* check if pointer is at given position */
|
|
|
|
|
|
|
|
Py_ssize_t thisp, thatp;
|
|
|
|
|
|
|
|
switch (at) {
|
|
|
|
|
|
|
|
case SRE_AT_BEGINNING:
|
|
|
|
case SRE_AT_BEGINNING_STRING:
|
|
|
|
return ((void*) ptr == state->beginning);
|
|
|
|
|
|
|
|
case SRE_AT_BEGINNING_LINE:
|
|
|
|
return ((void*) ptr == state->beginning ||
|
|
|
|
SRE_IS_LINEBREAK((int) ptr[-1]));
|
|
|
|
|
|
|
|
case SRE_AT_END:
|
2015-07-06 07:58:33 -03:00
|
|
|
return (((SRE_CHAR *)state->end - ptr == 1 &&
|
2013-10-26 05:18:42 -03:00
|
|
|
SRE_IS_LINEBREAK((int) ptr[0])) ||
|
|
|
|
((void*) ptr == state->end));
|
|
|
|
|
|
|
|
case SRE_AT_END_LINE:
|
|
|
|
return ((void*) ptr == state->end ||
|
|
|
|
SRE_IS_LINEBREAK((int) ptr[0]));
|
|
|
|
|
|
|
|
case SRE_AT_END_STRING:
|
|
|
|
return ((void*) ptr == state->end);
|
|
|
|
|
|
|
|
case SRE_AT_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp != thatp;
|
|
|
|
|
|
|
|
case SRE_AT_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp == thatp;
|
|
|
|
|
|
|
|
case SRE_AT_LOC_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp != thatp;
|
|
|
|
|
|
|
|
case SRE_AT_LOC_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp == thatp;
|
|
|
|
|
|
|
|
case SRE_AT_UNI_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp != thatp;
|
|
|
|
|
|
|
|
case SRE_AT_UNI_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
thisp = ((void*) ptr < state->end) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return thisp == thatp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(int)
|
2020-04-11 04:48:40 -03:00
|
|
|
SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
|
|
|
/* check if character is a member of the given set */
|
|
|
|
|
|
|
|
int ok = 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (*set++) {
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
return !ok;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
/* <LITERAL> <code> */
|
|
|
|
if (ch == set[0])
|
|
|
|
return ok;
|
|
|
|
set++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
/* <CATEGORY> <code> */
|
|
|
|
if (sre_category(set[0], (int) ch))
|
|
|
|
return ok;
|
|
|
|
set++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CHARSET:
|
|
|
|
/* <CHARSET> <bitmap> */
|
|
|
|
if (ch < 256 &&
|
|
|
|
(set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1)))))
|
|
|
|
return ok;
|
|
|
|
set += 256/SRE_CODE_BITS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_RANGE:
|
|
|
|
/* <RANGE> <lower> <upper> */
|
|
|
|
if (set[0] <= ch && ch <= set[1])
|
|
|
|
return ok;
|
|
|
|
set += 2;
|
|
|
|
break;
|
|
|
|
|
2017-10-24 17:31:42 -03:00
|
|
|
case SRE_OP_RANGE_UNI_IGNORE:
|
|
|
|
/* <RANGE_UNI_IGNORE> <lower> <upper> */
|
2014-10-31 07:36:56 -03:00
|
|
|
{
|
|
|
|
SRE_CODE uch;
|
|
|
|
/* ch is already lower cased */
|
|
|
|
if (set[0] <= ch && ch <= set[1])
|
|
|
|
return ok;
|
2017-10-24 17:31:42 -03:00
|
|
|
uch = sre_upper_unicode(ch);
|
2014-10-31 07:36:56 -03:00
|
|
|
if (set[0] <= uch && uch <= set[1])
|
|
|
|
return ok;
|
|
|
|
set += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
case SRE_OP_NEGATE:
|
|
|
|
ok = !ok;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_BIGCHARSET:
|
|
|
|
/* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
|
|
|
|
{
|
|
|
|
Py_ssize_t count, block;
|
|
|
|
count = *(set++);
|
|
|
|
|
|
|
|
if (ch < 0x10000u)
|
|
|
|
block = ((unsigned char*)set)[ch >> 8];
|
|
|
|
else
|
|
|
|
block = -1;
|
|
|
|
set += 256/sizeof(SRE_CODE);
|
|
|
|
if (block >=0 &&
|
|
|
|
(set[(block * 256 + (ch & 255))/SRE_CODE_BITS] &
|
|
|
|
(1u << (ch & (SRE_CODE_BITS-1)))))
|
|
|
|
return ok;
|
|
|
|
set += count * (256/SRE_CODE_BITS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* internal error -- there's not much we can do about it
|
|
|
|
here, so let's just pretend it didn't match... */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 02:53:40 -03:00
|
|
|
LOCAL(int)
|
2020-04-11 04:48:40 -03:00
|
|
|
SRE(charset_loc_ignore)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
|
2017-05-05 02:53:40 -03:00
|
|
|
{
|
|
|
|
SRE_CODE lo, up;
|
2017-10-24 17:31:42 -03:00
|
|
|
lo = sre_lower_locale(ch);
|
2017-05-05 02:53:40 -03:00
|
|
|
if (SRE(charset)(state, set, lo))
|
|
|
|
return 1;
|
|
|
|
|
2017-10-24 17:31:42 -03:00
|
|
|
up = sre_upper_locale(ch);
|
2017-05-05 02:53:40 -03:00
|
|
|
return up != lo && SRE(charset)(state, set, up);
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:48:40 -03:00
|
|
|
LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel);
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
LOCAL(Py_ssize_t)
|
2020-04-11 04:48:40 -03:00
|
|
|
SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
|
|
|
SRE_CODE chr;
|
|
|
|
SRE_CHAR c;
|
2020-04-11 04:48:40 -03:00
|
|
|
const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
|
|
|
|
const SRE_CHAR* end = (const SRE_CHAR *)state->end;
|
2013-10-26 05:18:42 -03:00
|
|
|
Py_ssize_t i;
|
|
|
|
|
|
|
|
/* adjust end */
|
|
|
|
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
|
|
|
|
end = ptr + maxcount;
|
|
|
|
|
|
|
|
switch (pattern[0]) {
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
/* repeated set */
|
|
|
|
TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
|
2014-10-31 07:36:56 -03:00
|
|
|
while (ptr < end && SRE(charset)(state, pattern + 2, *ptr))
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY:
|
|
|
|
/* repeated dot wildcard. */
|
|
|
|
TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
|
|
|
|
while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY_ALL:
|
|
|
|
/* repeated dot wildcard. skip to the end of the target
|
|
|
|
string, and backtrack from there */
|
|
|
|
TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
|
|
|
|
ptr = end;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
|
|
|
|
c = (SRE_CHAR) chr;
|
|
|
|
#if SIZEOF_SRE_CHAR < 4
|
|
|
|
if ((SRE_CODE) c != chr)
|
|
|
|
; /* literal can't match: doesn't fit in char width */
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
while (ptr < end && *ptr == c)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
|
2017-10-24 17:31:42 -03:00
|
|
|
while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_UNI_IGNORE:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
|
|
|
|
while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr)
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2017-05-05 02:53:40 -03:00
|
|
|
case SRE_OP_LITERAL_LOC_IGNORE:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
|
2017-10-24 17:31:42 -03:00
|
|
|
while (ptr < end && char_loc_ignore(chr, *ptr))
|
2017-05-05 02:53:40 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
|
|
|
|
c = (SRE_CHAR) chr;
|
|
|
|
#if SIZEOF_SRE_CHAR < 4
|
|
|
|
if ((SRE_CODE) c != chr)
|
|
|
|
ptr = end; /* literal can't match: doesn't fit in char width */
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
while (ptr < end && *ptr != c)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
|
2017-10-24 17:31:42 -03:00
|
|
|
while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL_UNI_IGNORE:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr));
|
|
|
|
while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr)
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2017-05-05 02:53:40 -03:00
|
|
|
case SRE_OP_NOT_LITERAL_LOC_IGNORE:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr));
|
2017-10-24 17:31:42 -03:00
|
|
|
while (ptr < end && !char_loc_ignore(chr, *ptr))
|
2017-05-05 02:53:40 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
default:
|
|
|
|
/* repeated single character pattern */
|
|
|
|
TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
|
|
|
|
while ((SRE_CHAR*) state->ptr < end) {
|
2014-05-14 15:48:17 -03:00
|
|
|
i = SRE(match)(state, pattern, 0);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
|
|
|
break;
|
|
|
|
}
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
|
2013-10-26 05:18:42 -03:00
|
|
|
(SRE_CHAR*) state->ptr - ptr));
|
|
|
|
return (SRE_CHAR*) state->ptr - ptr;
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr,
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr - (SRE_CHAR*) state->ptr));
|
|
|
|
return ptr - (SRE_CHAR*) state->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The macros below should be used to protect recursive SRE(match)()
|
|
|
|
* calls that *failed* and do *not* return immediately (IOW, those
|
|
|
|
* that will backtrack). Explaining:
|
|
|
|
*
|
|
|
|
* - Recursive SRE(match)() returned true: that's usually a success
|
|
|
|
* (besides atypical cases like ASSERT_NOT), therefore there's no
|
|
|
|
* reason to restore lastmark;
|
|
|
|
*
|
|
|
|
* - Recursive SRE(match)() returned false but the current SRE(match)()
|
|
|
|
* is returning to the caller: If the current SRE(match)() is the
|
|
|
|
* top function of the recursion, returning false will be a matching
|
|
|
|
* failure, and it doesn't matter where lastmark is pointing to.
|
|
|
|
* If it's *not* the top function, it will be a recursive SRE(match)()
|
|
|
|
* failure by itself, and the calling SRE(match)() will have to deal
|
|
|
|
* with the failure by the same rules explained here (it will restore
|
|
|
|
* lastmark by itself if necessary);
|
|
|
|
*
|
|
|
|
* - Recursive SRE(match)() returned false, and will continue the
|
|
|
|
* outside 'for' loop: must be protected when breaking, since the next
|
|
|
|
* OP could potentially depend on lastmark;
|
|
|
|
*
|
|
|
|
* - Recursive SRE(match)() returned false, and will be called again
|
|
|
|
* inside a local for/while loop: must be protected between each
|
|
|
|
* loop iteration, since the recursive SRE(match)() could do anything,
|
|
|
|
* and could potentially depend on lastmark.
|
|
|
|
*
|
|
|
|
* For more information, check the discussion at SF patch #712900.
|
|
|
|
*/
|
|
|
|
#define LASTMARK_SAVE() \
|
|
|
|
do { \
|
|
|
|
ctx->lastmark = state->lastmark; \
|
|
|
|
ctx->lastindex = state->lastindex; \
|
|
|
|
} while (0)
|
|
|
|
#define LASTMARK_RESTORE() \
|
|
|
|
do { \
|
|
|
|
state->lastmark = ctx->lastmark; \
|
|
|
|
state->lastindex = ctx->lastindex; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define RETURN_ERROR(i) do { return i; } while(0)
|
|
|
|
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
|
|
|
|
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
|
|
|
|
|
|
|
|
#define RETURN_ON_ERROR(i) \
|
|
|
|
do { if (i < 0) RETURN_ERROR(i); } while (0)
|
|
|
|
#define RETURN_ON_SUCCESS(i) \
|
|
|
|
do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
|
|
|
|
#define RETURN_ON_FAILURE(i) \
|
|
|
|
do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_ALLOC(state, type, ptr) \
|
|
|
|
do { \
|
|
|
|
alloc_pos = state->data_stack_base; \
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("allocating %s in %zd (%zd)\n", \
|
2015-11-05 11:43:42 -04:00
|
|
|
Py_STRINGIFY(type), alloc_pos, sizeof(type))); \
|
2013-10-26 05:18:42 -03:00
|
|
|
if (sizeof(type) > state->data_stack_size - alloc_pos) { \
|
|
|
|
int j = data_stack_grow(state, sizeof(type)); \
|
|
|
|
if (j < 0) return j; \
|
|
|
|
if (ctx_pos != -1) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
|
|
|
|
} \
|
|
|
|
ptr = (type*)(state->data_stack+alloc_pos); \
|
|
|
|
state->data_stack_base += sizeof(type); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
|
|
|
|
do { \
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr = (type*)(state->data_stack+pos); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_PUSH(state, data, size) \
|
|
|
|
do { \
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("copy data in %p to %zd (%zd)\n", \
|
2013-10-26 05:18:42 -03:00
|
|
|
data, state->data_stack_base, size)); \
|
|
|
|
if (size > state->data_stack_size - state->data_stack_base) { \
|
|
|
|
int j = data_stack_grow(state, size); \
|
|
|
|
if (j < 0) return j; \
|
|
|
|
if (ctx_pos != -1) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \
|
|
|
|
} \
|
|
|
|
memcpy(state->data_stack+state->data_stack_base, data, size); \
|
|
|
|
state->data_stack_base += size; \
|
|
|
|
} while (0)
|
|
|
|
|
2020-06-01 14:21:43 -03:00
|
|
|
/* We add an explicit cast to memcpy here because MSVC has a bug when
|
|
|
|
compiling C code where it believes that `const void**` cannot be
|
|
|
|
safely casted to `void*`, see bpo-39943 for details. */
|
2013-10-26 05:18:42 -03:00
|
|
|
#define DATA_STACK_POP(state, data, size, discard) \
|
|
|
|
do { \
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("copy data to %p from %zd (%zd)\n", \
|
2013-10-26 05:18:42 -03:00
|
|
|
data, state->data_stack_base-size, size)); \
|
2020-06-01 14:21:43 -03:00
|
|
|
memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \
|
2013-10-26 05:18:42 -03:00
|
|
|
if (discard) \
|
|
|
|
state->data_stack_base -= size; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_POP_DISCARD(state, size) \
|
|
|
|
do { \
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("discard data from %zd (%zd)\n", \
|
2013-10-26 05:18:42 -03:00
|
|
|
state->data_stack_base-size, size)); \
|
|
|
|
state->data_stack_base -= size; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define DATA_PUSH(x) \
|
|
|
|
DATA_STACK_PUSH(state, (x), sizeof(*(x)))
|
|
|
|
#define DATA_POP(x) \
|
|
|
|
DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
|
|
|
|
#define DATA_POP_DISCARD(x) \
|
|
|
|
DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
|
|
|
|
#define DATA_ALLOC(t,p) \
|
|
|
|
DATA_STACK_ALLOC(state, t, p)
|
|
|
|
#define DATA_LOOKUP_AT(t,p,pos) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state,t,p,pos)
|
|
|
|
|
|
|
|
#define MARK_PUSH(lastmark) \
|
2022-03-29 11:31:01 -03:00
|
|
|
do if (lastmark >= 0) { \
|
2022-04-18 10:50:40 -03:00
|
|
|
size_t _marks_size = (lastmark+1) * sizeof(void*); \
|
|
|
|
DATA_STACK_PUSH(state, state->mark, _marks_size); \
|
2013-10-26 05:18:42 -03:00
|
|
|
} while (0)
|
|
|
|
#define MARK_POP(lastmark) \
|
2022-03-29 11:31:01 -03:00
|
|
|
do if (lastmark >= 0) { \
|
2022-04-18 10:50:40 -03:00
|
|
|
size_t _marks_size = (lastmark+1) * sizeof(void*); \
|
|
|
|
DATA_STACK_POP(state, state->mark, _marks_size, 1); \
|
2013-10-26 05:18:42 -03:00
|
|
|
} while (0)
|
|
|
|
#define MARK_POP_KEEP(lastmark) \
|
2022-03-29 11:31:01 -03:00
|
|
|
do if (lastmark >= 0) { \
|
2022-04-18 10:50:40 -03:00
|
|
|
size_t _marks_size = (lastmark+1) * sizeof(void*); \
|
|
|
|
DATA_STACK_POP(state, state->mark, _marks_size, 0); \
|
2013-10-26 05:18:42 -03:00
|
|
|
} while (0)
|
|
|
|
#define MARK_POP_DISCARD(lastmark) \
|
2022-03-29 11:31:01 -03:00
|
|
|
do if (lastmark >= 0) { \
|
2022-04-18 10:50:40 -03:00
|
|
|
size_t _marks_size = (lastmark+1) * sizeof(void*); \
|
|
|
|
DATA_STACK_POP_DISCARD(state, _marks_size); \
|
2013-10-26 05:18:42 -03:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define JUMP_NONE 0
|
|
|
|
#define JUMP_MAX_UNTIL_1 1
|
|
|
|
#define JUMP_MAX_UNTIL_2 2
|
|
|
|
#define JUMP_MAX_UNTIL_3 3
|
|
|
|
#define JUMP_MIN_UNTIL_1 4
|
|
|
|
#define JUMP_MIN_UNTIL_2 5
|
|
|
|
#define JUMP_MIN_UNTIL_3 6
|
|
|
|
#define JUMP_REPEAT 7
|
|
|
|
#define JUMP_REPEAT_ONE_1 8
|
|
|
|
#define JUMP_REPEAT_ONE_2 9
|
|
|
|
#define JUMP_MIN_REPEAT_ONE 10
|
|
|
|
#define JUMP_BRANCH 11
|
|
|
|
#define JUMP_ASSERT 12
|
|
|
|
#define JUMP_ASSERT_NOT 13
|
2022-03-21 13:28:22 -03:00
|
|
|
#define JUMP_POSS_REPEAT_1 14
|
|
|
|
#define JUMP_POSS_REPEAT_2 15
|
|
|
|
#define JUMP_ATOMIC_GROUP 16
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2017-12-04 08:29:05 -04:00
|
|
|
#define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \
|
2022-04-15 13:26:44 -03:00
|
|
|
ctx->pattern = pattern; \
|
|
|
|
ctx->ptr = ptr; \
|
2013-10-26 05:18:42 -03:00
|
|
|
DATA_ALLOC(SRE(match_context), nextctx); \
|
|
|
|
nextctx->pattern = nextpattern; \
|
2017-12-04 08:29:05 -04:00
|
|
|
nextctx->toplevel = toplevel_; \
|
2022-04-18 10:50:40 -03:00
|
|
|
nextctx->jump = jumpvalue; \
|
|
|
|
nextctx->last_ctx_pos = ctx_pos; \
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern = nextpattern; \
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx_pos = alloc_pos; \
|
|
|
|
ctx = nextctx; \
|
|
|
|
goto entrance; \
|
|
|
|
jumplabel: \
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern = ctx->pattern; \
|
|
|
|
ptr = ctx->ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2013-11-23 17:20:30 -04:00
|
|
|
#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
|
2017-12-04 08:29:05 -04:00
|
|
|
DO_JUMPX(jumpvalue, jumplabel, nextpattern, ctx->toplevel)
|
2013-11-23 17:20:30 -04:00
|
|
|
|
|
|
|
#define DO_JUMP0(jumpvalue, jumplabel, nextpattern) \
|
|
|
|
DO_JUMPX(jumpvalue, jumplabel, nextpattern, 0)
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
typedef struct {
|
|
|
|
Py_ssize_t count;
|
|
|
|
union {
|
|
|
|
SRE_CODE chr;
|
|
|
|
SRE_REPEAT* rep;
|
|
|
|
} u;
|
2022-04-18 10:50:40 -03:00
|
|
|
int lastmark;
|
|
|
|
int lastindex;
|
|
|
|
const SRE_CODE* pattern;
|
|
|
|
const SRE_CHAR* ptr;
|
2017-12-04 08:29:05 -04:00
|
|
|
int toplevel;
|
2022-04-18 10:50:40 -03:00
|
|
|
int jump;
|
|
|
|
Py_ssize_t last_ctx_pos;
|
2013-10-26 05:18:42 -03:00
|
|
|
} SRE(match_context);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
#define MAYBE_CHECK_SIGNALS \
|
|
|
|
do { \
|
|
|
|
if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \
|
|
|
|
RETURN_ERROR(SRE_ERROR_INTERRUPTED); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#ifdef HAVE_COMPUTED_GOTOS
|
|
|
|
#ifndef USE_COMPUTED_GOTOS
|
|
|
|
#define USE_COMPUTED_GOTOS 1
|
|
|
|
#endif
|
|
|
|
#elif defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
|
|
|
|
#error "Computed gotos are not supported on this compiler."
|
|
|
|
#else
|
|
|
|
#undef USE_COMPUTED_GOTOS
|
|
|
|
#define USE_COMPUTED_GOTOS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_COMPUTED_GOTOS
|
|
|
|
#define TARGET(OP) TARGET_ ## OP
|
|
|
|
#define DISPATCH \
|
|
|
|
do { \
|
|
|
|
MAYBE_CHECK_SIGNALS; \
|
|
|
|
goto *sre_targets[*pattern++]; \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define TARGET(OP) case OP
|
|
|
|
#define DISPATCH goto dispatch
|
|
|
|
#endif
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
/* check if string matches the given pattern. returns <0 for
|
|
|
|
error, 0 for failure, and 1 for success */
|
|
|
|
LOCAL(Py_ssize_t)
|
2020-04-11 04:48:40 -03:00
|
|
|
SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
2020-04-11 04:48:40 -03:00
|
|
|
const SRE_CHAR* end = (const SRE_CHAR *)state->end;
|
2013-10-26 05:18:42 -03:00
|
|
|
Py_ssize_t alloc_pos, ctx_pos = -1;
|
2022-04-18 10:50:40 -03:00
|
|
|
Py_ssize_t ret = 0;
|
|
|
|
int jump;
|
2013-10-26 05:18:42 -03:00
|
|
|
unsigned int sigcount=0;
|
|
|
|
|
|
|
|
SRE(match_context)* ctx;
|
|
|
|
SRE(match_context)* nextctx;
|
|
|
|
|
|
|
|
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
|
|
|
|
|
|
|
|
DATA_ALLOC(SRE(match_context), ctx);
|
|
|
|
ctx->last_ctx_pos = -1;
|
|
|
|
ctx->jump = JUMP_NONE;
|
2017-12-04 08:29:05 -04:00
|
|
|
ctx->toplevel = toplevel;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx_pos = alloc_pos;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
#if USE_COMPUTED_GOTOS
|
|
|
|
#include "sre_targets.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
entrance:
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
; // Fashion statement.
|
|
|
|
const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[0] == SRE_OP_INFO) {
|
2013-10-26 05:18:42 -03:00
|
|
|
/* optimization info block */
|
|
|
|
/* <INFO> <1=skip> <2=flags> <3=min> ... */
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) {
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("reject (got %zd chars, need %zd)\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
end - ptr, (Py_ssize_t) pattern[3]));
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[1] + 1;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
#if USE_COMPUTED_GOTOS
|
|
|
|
DISPATCH;
|
|
|
|
#else
|
|
|
|
dispatch:
|
|
|
|
MAYBE_CHECK_SIGNALS;
|
|
|
|
switch (*pattern++)
|
|
|
|
#endif
|
|
|
|
{
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_MARK):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* set mark */
|
|
|
|
/* <MARK> <gid> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|MARK %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2022-04-18 10:50:40 -03:00
|
|
|
{
|
|
|
|
int i = pattern[0];
|
|
|
|
if (i & 1)
|
|
|
|
state->lastindex = i/2 + 1;
|
|
|
|
if (i > state->lastmark) {
|
|
|
|
/* state->lastmark is the highest valid index in the
|
|
|
|
state->mark array. If it is increased by more than 1,
|
|
|
|
the intervening marks must be set to NULL to signal
|
|
|
|
that these marks have not been encountered. */
|
|
|
|
int j = state->lastmark + 1;
|
|
|
|
while (j < i)
|
|
|
|
state->mark[j++] = NULL;
|
|
|
|
state->lastmark = i;
|
|
|
|
}
|
|
|
|
state->mark[i] = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_LITERAL):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match literal string */
|
|
|
|
/* <LITERAL> <code> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL %d\n", pattern,
|
|
|
|
ptr, *pattern));
|
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_NOT_LITERAL):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match anything that is not literal character */
|
|
|
|
/* <NOT_LITERAL> <code> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern,
|
|
|
|
ptr, *pattern));
|
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_SUCCESS):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* end of pattern */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
|
2017-12-04 08:29:05 -04:00
|
|
|
if (ctx->toplevel &&
|
2022-04-15 13:26:44 -03:00
|
|
|
((state->match_all && ptr != state->end) ||
|
|
|
|
(state->must_advance && ptr == state->start)))
|
2017-12-04 08:29:05 -04:00
|
|
|
{
|
|
|
|
RETURN_FAILURE;
|
2013-11-23 17:20:30 -04:00
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2017-12-04 08:29:05 -04:00
|
|
|
RETURN_SUCCESS;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_AT):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match at given position */
|
|
|
|
/* <AT> <code> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
|
|
|
|
if (!SRE(at)(state, ptr, *pattern))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_CATEGORY):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match at given category */
|
|
|
|
/* <CATEGORY> <code> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|CATEGORY %d\n", pattern,
|
|
|
|
ptr, *pattern));
|
|
|
|
if (ptr >= end || !sre_category(pattern[0], ptr[0]))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_ANY):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match anything (except a newline) */
|
|
|
|
/* <ANY> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|ANY\n", pattern, ptr));
|
|
|
|
if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_ANY_ALL):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match anything */
|
|
|
|
/* <ANY_ALL> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
|
|
|
|
if (ptr >= end)
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_IN):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match set member (or non_member) */
|
|
|
|
/* <IN> <skip> <set> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|IN\n", pattern, ptr));
|
|
|
|
if (ptr >= end ||
|
|
|
|
!SRE(charset)(state, pattern + 1, *ptr))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_LITERAL_IGNORE):
|
2013-10-26 05:18:42 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, pattern[0]));
|
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_ascii(*ptr) != *pattern)
|
2017-10-24 17:31:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-10-24 17:31:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_LITERAL_UNI_IGNORE):
|
2017-10-24 17:31:42 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, pattern[0]));
|
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_unicode(*ptr) != *pattern)
|
2017-05-05 02:53:40 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-05-05 02:53:40 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_LITERAL_LOC_IGNORE):
|
2017-05-05 02:53:40 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, pattern[0]));
|
|
|
|
if (ptr >= end
|
|
|
|
|| !char_loc_ignore(*pattern, *ptr))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_NOT_LITERAL_IGNORE):
|
2013-10-26 05:18:42 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, *pattern));
|
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_ascii(*ptr) == *pattern)
|
2017-10-24 17:31:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-10-24 17:31:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE):
|
2017-10-24 17:31:42 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, *pattern));
|
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_unicode(*ptr) == *pattern)
|
2017-05-05 02:53:40 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-05-05 02:53:40 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE):
|
2017-05-05 02:53:40 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n",
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern, ptr, *pattern));
|
|
|
|
if (ptr >= end
|
|
|
|
|| char_loc_ignore(*pattern, *ptr))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_IN_IGNORE):
|
|
|
|
TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
|
|
|
|
if (ptr >= end
|
|
|
|
|| !SRE(charset)(state, pattern+1,
|
|
|
|
(SRE_CODE)sre_lower_ascii(*ptr)))
|
2017-10-24 17:31:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-10-24 17:31:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_IN_UNI_IGNORE):
|
|
|
|
TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
|
|
|
|
if (ptr >= end
|
|
|
|
|| !SRE(charset)(state, pattern+1,
|
|
|
|
(SRE_CODE)sre_lower_unicode(*ptr)))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_IN_LOC_IGNORE):
|
|
|
|
TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr));
|
|
|
|
if (ptr >= end
|
|
|
|
|| !SRE(charset_loc_ignore)(state, pattern+1, *ptr))
|
2017-05-05 02:53:40 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
DISPATCH;
|
2017-05-05 02:53:40 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_JUMP):
|
|
|
|
TARGET(SRE_OP_INFO):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* jump forward */
|
|
|
|
/* <JUMP> <offset> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
|
|
|
pattern += pattern[0];
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_BRANCH):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* alternation */
|
|
|
|
/* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
LASTMARK_SAVE();
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
2013-10-26 05:18:42 -03:00
|
|
|
MARK_PUSH(ctx->lastmark);
|
2022-04-15 13:26:44 -03:00
|
|
|
for (; pattern[0]; pattern += pattern[0]) {
|
|
|
|
if (pattern[1] == SRE_OP_LITERAL &&
|
|
|
|
(ptr >= end ||
|
|
|
|
(SRE_CODE) *ptr != pattern[2]))
|
2013-10-26 05:18:42 -03:00
|
|
|
continue;
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[1] == SRE_OP_IN &&
|
|
|
|
(ptr >= end ||
|
|
|
|
!SRE(charset)(state, pattern + 3,
|
|
|
|
(SRE_CODE) *ptr)))
|
2013-10-26 05:18:42 -03:00
|
|
|
continue;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
|
|
|
DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
2013-10-26 05:18:42 -03:00
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
2013-10-26 05:18:42 -03:00
|
|
|
MARK_POP_KEEP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
2013-10-26 05:18:42 -03:00
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_REPEAT_ONE):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match repeated sequence (maximizing regexp) */
|
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
|
|
|
use the MAX_REPEAT operator */
|
|
|
|
|
|
|
|
/* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
|
|
|
|
pattern[1], pattern[2]));
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if ((Py_ssize_t) pattern[1] > end - ptr)
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE; /* cannot match */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
ret = SRE(count)(state, pattern+3, pattern[2]);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
|
|
|
|
ctx->count = ret;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr += ctx->count;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
/* when we arrive here, count contains the number of
|
2022-04-15 13:26:44 -03:00
|
|
|
matches, and ptr points to the tail of the target
|
2013-10-26 05:18:42 -03:00
|
|
|
string. check if the rest of the pattern matches,
|
|
|
|
and backtrack if not. */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) pattern[1])
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
|
|
|
|
ptr == state->end &&
|
|
|
|
!(ctx->toplevel && state->must_advance && ptr == state->start))
|
2017-12-04 08:29:05 -04:00
|
|
|
{
|
2013-10-26 05:18:42 -03:00
|
|
|
/* tail is empty. we're finished */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
LASTMARK_SAVE();
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[pattern[0]] == SRE_OP_LITERAL) {
|
2013-10-26 05:18:42 -03:00
|
|
|
/* tail starts with a literal. skip positions where
|
|
|
|
the rest of the pattern cannot possibly match */
|
2022-04-15 13:26:44 -03:00
|
|
|
ctx->u.chr = pattern[pattern[0]+1];
|
2013-10-26 05:18:42 -03:00
|
|
|
for (;;) {
|
2022-04-15 13:26:44 -03:00
|
|
|
while (ctx->count >= (Py_ssize_t) pattern[1] &&
|
|
|
|
(ptr >= end || *ptr != ctx->u.chr)) {
|
|
|
|
ptr--;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->count--;
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) pattern[1])
|
2013-10-26 05:18:42 -03:00
|
|
|
break;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern+pattern[0]);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_KEEP(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
LASTMARK_RESTORE();
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr--;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->count--;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
} else {
|
|
|
|
/* general case */
|
2022-04-15 13:26:44 -03:00
|
|
|
while (ctx->count >= (Py_ssize_t) pattern[1]) {
|
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern+pattern[0]);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_KEEP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr--;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->count--;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_MIN_REPEAT_ONE):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match repeated sequence (minimizing regexp) */
|
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
|
|
|
use the MIN_REPEAT operator */
|
|
|
|
|
|
|
|
/* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr,
|
|
|
|
pattern[1], pattern[2]));
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if ((Py_ssize_t) pattern[1] > end - ptr)
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE; /* cannot match */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[1] == 0)
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->count = 0;
|
|
|
|
else {
|
|
|
|
/* count using pattern min as the maximum */
|
2022-04-15 13:26:44 -03:00
|
|
|
ret = SRE(count)(state, pattern+3, pattern[1]);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ret < (Py_ssize_t) pattern[1])
|
2013-10-26 05:18:42 -03:00
|
|
|
/* didn't match minimum number of times */
|
|
|
|
RETURN_FAILURE;
|
|
|
|
/* advance past minimum matches of repeat */
|
|
|
|
ctx->count = ret;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr += ctx->count;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (pattern[pattern[0]] == SRE_OP_SUCCESS &&
|
2017-12-04 08:29:05 -04:00
|
|
|
!(ctx->toplevel &&
|
2022-04-15 13:26:44 -03:00
|
|
|
((state->match_all && ptr != state->end) ||
|
|
|
|
(state->must_advance && ptr == state->start))))
|
2017-12-04 08:29:05 -04:00
|
|
|
{
|
2013-10-26 05:18:42 -03:00
|
|
|
/* tail is empty. we're finished */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_SUCCESS;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* general case */
|
|
|
|
LASTMARK_SAVE();
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT
|
|
|
|
|| ctx->count <= (Py_ssize_t)pattern[2]) {
|
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern+pattern[0]);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_KEEP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
|
|
|
ret = SRE(count)(state, pattern+3, 1);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
assert(ret == 1);
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->count++;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE):
|
2022-03-21 13:28:22 -03:00
|
|
|
/* match repeated sequence (maximizing regexp) without
|
|
|
|
backtracking */
|
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
|
|
|
use the MAX_REPEAT operator */
|
|
|
|
|
|
|
|
/* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
|
|
|
|
tail */
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern,
|
|
|
|
ptr, pattern[1], pattern[2]));
|
2022-03-21 13:28:22 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ptr + pattern[1] > end) {
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_FAILURE; /* cannot match */
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
ret = SRE(count)(state, pattern + 3, pattern[2]);
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
|
|
|
|
ctx->count = ret;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr += ctx->count;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* when we arrive here, count contains the number of
|
2022-04-15 13:26:44 -03:00
|
|
|
matches, and ptr points to the tail of the target
|
2022-03-21 13:28:22 -03:00
|
|
|
string. check if the rest of the pattern matches,
|
|
|
|
and fail if not. */
|
|
|
|
|
|
|
|
/* Test for not enough repetitions in match */
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) pattern[1]) {
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the pattern to point to the next op code */
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Let the tail be evaluated separately and consider this
|
|
|
|
match successful. */
|
2022-04-15 13:26:44 -03:00
|
|
|
if (*pattern == SRE_OP_SUCCESS &&
|
|
|
|
ptr == state->end &&
|
|
|
|
!(ctx->toplevel && state->must_advance && ptr == state->start))
|
2022-03-21 13:28:22 -03:00
|
|
|
{
|
|
|
|
/* tail is empty. we're finished */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to match the rest of the string */
|
2022-04-15 13:26:44 -03:00
|
|
|
DISPATCH;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_REPEAT):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* create repeat context. all the hard work is done
|
|
|
|
by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
|
2022-04-03 13:16:20 -03:00
|
|
|
/* <REPEAT> <skip> <1=min> <2=max>
|
|
|
|
<3=repeat_index> item <UNTIL> tail */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT %d %d %d\n", pattern, ptr,
|
|
|
|
pattern[1], pattern[2], pattern[3]));
|
2022-04-03 13:16:20 -03:00
|
|
|
|
|
|
|
/* install repeat context */
|
2022-04-15 13:26:44 -03:00
|
|
|
ctx->u.rep = &state->repeats_array[pattern[3]];
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
ctx->u.rep->count = -1;
|
2022-04-15 13:26:44 -03:00
|
|
|
ctx->u.rep->pattern = pattern;
|
2013-10-26 05:18:42 -03:00
|
|
|
ctx->u.rep->prev = state->repeat;
|
|
|
|
ctx->u.rep->last_ptr = NULL;
|
|
|
|
state->repeat = ctx->u.rep;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
|
|
|
DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
|
2013-10-26 05:18:42 -03:00
|
|
|
state->repeat = ctx->u.rep->prev;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_MAX_UNTIL):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* maximizing repeat */
|
2022-04-03 13:16:20 -03:00
|
|
|
/* <REPEAT> <skip> <1=min> <2=max>
|
|
|
|
<3=repeat_index> item <MAX_UNTIL> tail */
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
/* FIXME: we probably need to deal with zero-width
|
|
|
|
matches in here... */
|
|
|
|
|
|
|
|
ctx->u.rep = state->repeat;
|
|
|
|
if (!ctx->u.rep)
|
|
|
|
RETURN_ERROR(SRE_ERROR_STATE);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
ctx->count = ctx->u.rep->count+1;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern,
|
|
|
|
ptr, ctx->count));
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
|
|
|
|
/* not enough matches */
|
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
|
2022-04-03 13:16:20 -03:00
|
|
|
ctx->u.rep->pattern+4);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
|
|
|
|
ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
|
|
|
|
state->ptr != ctx->u.rep->last_ptr) {
|
|
|
|
/* we may have enough matches, but if we can
|
|
|
|
match another item, do so */
|
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
LASTMARK_SAVE();
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
/* zero-width match protection */
|
|
|
|
DATA_PUSH(&ctx->u.rep->last_ptr);
|
|
|
|
ctx->u.rep->last_ptr = state->ptr;
|
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
|
2022-04-03 13:16:20 -03:00
|
|
|
ctx->u.rep->pattern+4);
|
2013-10-26 05:18:42 -03:00
|
|
|
DATA_POP(&ctx->u.rep->last_ptr);
|
|
|
|
if (ret) {
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
MARK_POP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cannot match more repeated items here. make sure the
|
|
|
|
tail matches */
|
|
|
|
state->repeat = ctx->u.rep->prev;
|
2022-04-15 13:26:44 -03:00
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern);
|
2022-03-29 11:31:01 -03:00
|
|
|
state->repeat = ctx->u.rep; // restore repeat before return
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_SUCCESS(ret);
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_MIN_UNTIL):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* minimizing repeat */
|
2022-04-03 13:16:20 -03:00
|
|
|
/* <REPEAT> <skip> <1=min> <2=max>
|
|
|
|
<3=repeat_index> item <MIN_UNTIL> tail */
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
ctx->u.rep = state->repeat;
|
|
|
|
if (!ctx->u.rep)
|
|
|
|
RETURN_ERROR(SRE_ERROR_STATE);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
ctx->count = ctx->u.rep->count+1;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern,
|
|
|
|
ptr, ctx->count, ctx->u.rep->pattern));
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
|
|
|
|
/* not enough matches */
|
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
|
2022-04-03 13:16:20 -03:00
|
|
|
ctx->u.rep->pattern+4);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see if the tail matches */
|
|
|
|
state->repeat = ctx->u.rep->prev;
|
2022-03-29 11:31:01 -03:00
|
|
|
|
|
|
|
LASTMARK_SAVE();
|
|
|
|
if (state->repeat)
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern);
|
2022-03-29 11:31:01 -03:00
|
|
|
SRE_REPEAT *repeat_of_tail = state->repeat;
|
|
|
|
state->repeat = ctx->u.rep; // restore repeat before return
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (repeat_of_tail)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (repeat_of_tail)
|
|
|
|
MARK_POP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
|
|
|
|
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
|
|
|
|
state->ptr == ctx->u.rep->last_ptr)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
|
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
/* zero-width match protection */
|
|
|
|
DATA_PUSH(&ctx->u.rep->last_ptr);
|
|
|
|
ctx->u.rep->last_ptr = state->ptr;
|
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
|
2022-04-03 13:16:20 -03:00
|
|
|
ctx->u.rep->pattern+4);
|
2013-10-26 05:18:42 -03:00
|
|
|
DATA_POP(&ctx->u.rep->last_ptr);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_POSSESSIVE_REPEAT):
|
2022-03-21 13:28:22 -03:00
|
|
|
/* create possessive repeat contexts. */
|
|
|
|
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
|
|
|
|
<SUCCESS> tail */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
|
|
|
|
ptr, pattern[1], pattern[2]));
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Set the global Input pointer to this context's Input
|
|
|
|
pointer */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Initialize Count to 0 */
|
|
|
|
ctx->count = 0;
|
|
|
|
|
|
|
|
/* Check for minimum required matches. */
|
2022-04-15 13:26:44 -03:00
|
|
|
while (ctx->count < (Py_ssize_t)pattern[1]) {
|
2022-03-21 13:28:22 -03:00
|
|
|
/* not enough matches */
|
2022-04-19 11:49:36 -03:00
|
|
|
DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
|
|
|
|
&pattern[3]);
|
2022-03-21 13:28:22 -03:00
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
ctx->count++;
|
|
|
|
}
|
|
|
|
else {
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the context's Input stream pointer so that it
|
|
|
|
doesn't match the global state so that the while loop can
|
|
|
|
be entered. */
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr = NULL;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Keep trying to parse the <pattern> sub-pattern until the
|
|
|
|
end is reached, creating a new context each time. */
|
2022-04-15 13:26:44 -03:00
|
|
|
while ((ctx->count < (Py_ssize_t)pattern[2] ||
|
|
|
|
(Py_ssize_t)pattern[2] == SRE_MAXREPEAT) &&
|
|
|
|
state->ptr != ptr) {
|
2022-03-21 13:28:22 -03:00
|
|
|
/* Save the Capture Group Marker state into the current
|
|
|
|
Context and back up the current highest number
|
|
|
|
Capture Group marker. */
|
|
|
|
LASTMARK_SAVE();
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
|
|
|
|
/* zero-width match protection */
|
|
|
|
/* Set the context's Input Stream pointer to be the
|
|
|
|
current Input Stream pointer from the global
|
|
|
|
state. When the loop reaches the next iteration,
|
|
|
|
the context will then store the last known good
|
|
|
|
position with the global state holding the Input
|
|
|
|
Input Stream position that has been updated with
|
|
|
|
the most recent match. Thus, if state's Input
|
|
|
|
stream remains the same as the one stored in the
|
|
|
|
current Context, we know we have successfully
|
|
|
|
matched an empty string and that all subsequent
|
|
|
|
matches will also be the empty string until the
|
|
|
|
maximum number of matches are counted, and because
|
|
|
|
of this, we could immediately stop at that point and
|
|
|
|
consider this match successful. */
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr = state->ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* We have not reached the maximin matches, so try to
|
|
|
|
match once more. */
|
2022-04-19 11:49:36 -03:00
|
|
|
DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2,
|
|
|
|
&pattern[3]);
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Check to see if the last attempted match
|
|
|
|
succeeded. */
|
|
|
|
if (ret) {
|
|
|
|
/* Drop the saved highest number Capture Group
|
|
|
|
marker saved above and use the newly updated
|
|
|
|
value. */
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
|
|
|
|
/* Success, increment the count. */
|
|
|
|
ctx->count++;
|
|
|
|
}
|
|
|
|
/* Last attempted match failed. */
|
|
|
|
else {
|
|
|
|
/* Restore the previously saved highest number
|
|
|
|
Capture Group marker since the last iteration
|
|
|
|
did not match, then restore that to the global
|
|
|
|
state. */
|
|
|
|
MARK_POP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
|
|
|
|
|
|
|
/* We have sufficient matches, so exit loop. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Evaluate Tail */
|
|
|
|
/* Jump to end of pattern indicated by skip, and then skip
|
|
|
|
the SUCCESS op code that follows it. */
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0] + 1;
|
|
|
|
ptr = state->ptr;
|
|
|
|
DISPATCH;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_ATOMIC_GROUP):
|
2022-03-21 13:28:22 -03:00
|
|
|
/* Atomic Group Sub Pattern */
|
|
|
|
/* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr));
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Set the global Input pointer to this context's Input
|
2022-04-19 11:49:36 -03:00
|
|
|
pointer */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Evaluate the Atomic Group in a new context, terminating
|
|
|
|
when the end of the group, represented by a SUCCESS op
|
|
|
|
code, is reached. */
|
|
|
|
/* Group Pattern begins at an offset of 1 code. */
|
2022-04-19 11:49:36 -03:00
|
|
|
DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group,
|
|
|
|
&pattern[1]);
|
2022-03-21 13:28:22 -03:00
|
|
|
|
|
|
|
/* Test Exit Condition */
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
/* Atomic Group failed to Match. */
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr;
|
2022-03-21 13:28:22 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Evaluate Tail */
|
|
|
|
/* Jump to end of pattern indicated by skip, and then skip
|
|
|
|
the SUCCESS op code that follows it. */
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr = state->ptr;
|
|
|
|
DISPATCH;
|
2022-03-21 13:28:22 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_GROUPREF):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match backreference */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
2022-04-18 10:50:40 -03:00
|
|
|
int groupref = pattern[0] * 2;
|
2013-10-26 05:18:42 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ptr >= end || *ptr != *p)
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
p++;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_GROUPREF_IGNORE):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* match backreference */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2013-10-26 05:18:42 -03:00
|
|
|
{
|
2022-04-18 10:50:40 -03:00
|
|
|
int groupref = pattern[0] * 2;
|
2013-10-26 05:18:42 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_ascii(*ptr) != sre_lower_ascii(*p))
|
2017-10-24 17:31:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
p++;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
2017-10-24 17:31:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2017-10-24 17:31:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_GROUPREF_UNI_IGNORE):
|
2017-10-24 17:31:42 -03:00
|
|
|
/* match backreference */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2017-10-24 17:31:42 -03:00
|
|
|
{
|
2022-04-18 10:50:40 -03:00
|
|
|
int groupref = pattern[0] * 2;
|
2017-10-24 17:31:42 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_unicode(*ptr) != sre_lower_unicode(*p))
|
2017-10-24 17:31:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
p++;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
2017-10-24 17:31:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2017-10-24 17:31:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_GROUPREF_LOC_IGNORE):
|
2017-10-24 17:31:42 -03:00
|
|
|
/* match backreference */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2017-10-24 17:31:42 -03:00
|
|
|
{
|
2022-04-18 10:50:40 -03:00
|
|
|
int groupref = pattern[0] * 2;
|
2017-10-24 17:31:42 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
2022-04-15 13:26:44 -03:00
|
|
|
if (ptr >= end ||
|
|
|
|
sre_lower_locale(*ptr) != sre_lower_locale(*p))
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
p++;
|
2022-04-15 13:26:44 -03:00
|
|
|
ptr++;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern++;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_GROUPREF_EXISTS):
|
|
|
|
TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern,
|
|
|
|
ptr, pattern[0]));
|
2013-10-26 05:18:42 -03:00
|
|
|
/* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
|
|
|
|
{
|
2022-04-18 10:50:40 -03:00
|
|
|
int groupref = pattern[0] * 2;
|
2013-10-26 05:18:42 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[1];
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p) {
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[1];
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += 2;
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_ASSERT):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* assert subpattern */
|
|
|
|
/* <ASSERT> <skip> <back> <pattern> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|ASSERT %d\n", pattern,
|
|
|
|
ptr, pattern[1]));
|
|
|
|
if (ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)pattern[1])
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
2022-04-15 13:26:44 -03:00
|
|
|
state->ptr = ptr - pattern[1];
|
|
|
|
DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_FAILURE(ret);
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_ASSERT_NOT):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* assert not subpattern */
|
|
|
|
/* <ASSERT_NOT> <skip> <back> <pattern> */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern,
|
|
|
|
ptr, pattern[1]));
|
|
|
|
if (ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)pattern[1]) {
|
|
|
|
state->ptr = ptr - pattern[1];
|
2022-03-29 11:31:01 -03:00
|
|
|
LASTMARK_SAVE();
|
|
|
|
if (state->repeat)
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (ret) {
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
2022-03-29 11:31:01 -03:00
|
|
|
if (state->repeat)
|
|
|
|
MARK_POP(ctx->lastmark);
|
|
|
|
LASTMARK_RESTORE();
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
2022-04-15 13:26:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
DISPATCH;
|
2013-10-26 05:18:42 -03:00
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
TARGET(SRE_OP_FAILURE):
|
2013-10-26 05:18:42 -03:00
|
|
|
/* immediate failure */
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
|
2022-04-15 13:26:44 -03:00
|
|
|
#if !USE_COMPUTED_GOTOS
|
2013-10-26 05:18:42 -03:00
|
|
|
default:
|
2022-04-15 13:26:44 -03:00
|
|
|
#endif
|
|
|
|
// Also any unused opcodes:
|
|
|
|
TARGET(SRE_OP_RANGE_UNI_IGNORE):
|
|
|
|
TARGET(SRE_OP_SUBPATTERN):
|
|
|
|
TARGET(SRE_OP_RANGE):
|
|
|
|
TARGET(SRE_OP_NEGATE):
|
|
|
|
TARGET(SRE_OP_BIGCHARSET):
|
|
|
|
TARGET(SRE_OP_CHARSET):
|
|
|
|
TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr,
|
|
|
|
pattern[-1]));
|
2013-10-26 05:18:42 -03:00
|
|
|
RETURN_ERROR(SRE_ERROR_ILLEGAL);
|
2022-04-15 13:26:44 -03:00
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
ctx_pos = ctx->last_ctx_pos;
|
|
|
|
jump = ctx->jump;
|
|
|
|
DATA_POP_DISCARD(ctx);
|
|
|
|
if (ctx_pos == -1)
|
|
|
|
return ret;
|
|
|
|
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
|
|
|
|
|
|
|
|
switch (jump) {
|
|
|
|
case JUMP_MAX_UNTIL_2:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_max_until_2;
|
|
|
|
case JUMP_MAX_UNTIL_3:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_max_until_3;
|
|
|
|
case JUMP_MIN_UNTIL_2:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_min_until_2;
|
|
|
|
case JUMP_MIN_UNTIL_3:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_min_until_3;
|
|
|
|
case JUMP_BRANCH:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_branch;
|
|
|
|
case JUMP_MAX_UNTIL_1:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_max_until_1;
|
|
|
|
case JUMP_MIN_UNTIL_1:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_min_until_1;
|
2022-03-21 13:28:22 -03:00
|
|
|
case JUMP_POSS_REPEAT_1:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr));
|
2022-03-21 13:28:22 -03:00
|
|
|
goto jump_poss_repeat_1;
|
|
|
|
case JUMP_POSS_REPEAT_2:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr));
|
2022-03-21 13:28:22 -03:00
|
|
|
goto jump_poss_repeat_2;
|
2013-10-26 05:18:42 -03:00
|
|
|
case JUMP_REPEAT:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_repeat;
|
|
|
|
case JUMP_REPEAT_ONE_1:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_repeat_one_1;
|
|
|
|
case JUMP_REPEAT_ONE_2:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_repeat_one_2;
|
|
|
|
case JUMP_MIN_REPEAT_ONE:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_min_repeat_one;
|
2022-03-21 13:28:22 -03:00
|
|
|
case JUMP_ATOMIC_GROUP:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr));
|
2022-03-21 13:28:22 -03:00
|
|
|
goto jump_atomic_group;
|
2013-10-26 05:18:42 -03:00
|
|
|
case JUMP_ASSERT:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_assert;
|
|
|
|
case JUMP_ASSERT_NOT:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr));
|
2013-10-26 05:18:42 -03:00
|
|
|
goto jump_assert_not;
|
|
|
|
case JUMP_NONE:
|
2022-04-15 13:26:44 -03:00
|
|
|
TRACE(("|%p|%p|RETURN %zd\n", pattern,
|
|
|
|
ptr, ret));
|
2013-10-26 05:18:42 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret; /* should never get here */
|
|
|
|
}
|
|
|
|
|
2019-02-18 09:26:37 -04:00
|
|
|
/* need to reset capturing groups between two SRE(match) callings in loops */
|
|
|
|
#define RESET_CAPTURE_GROUP() \
|
|
|
|
do { state->lastmark = state->lastindex = -1; } while (0)
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
LOCAL(Py_ssize_t)
|
|
|
|
SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
|
|
|
SRE_CHAR* ptr = (SRE_CHAR *)state->start;
|
|
|
|
SRE_CHAR* end = (SRE_CHAR *)state->end;
|
|
|
|
Py_ssize_t status = 0;
|
|
|
|
Py_ssize_t prefix_len = 0;
|
|
|
|
Py_ssize_t prefix_skip = 0;
|
|
|
|
SRE_CODE* prefix = NULL;
|
|
|
|
SRE_CODE* charset = NULL;
|
|
|
|
SRE_CODE* overlap = NULL;
|
|
|
|
int flags = 0;
|
|
|
|
|
2015-07-06 07:58:33 -03:00
|
|
|
if (ptr > end)
|
|
|
|
return 0;
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
if (pattern[0] == SRE_OP_INFO) {
|
|
|
|
/* optimization info block */
|
|
|
|
/* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
|
|
|
|
|
|
|
|
flags = pattern[2];
|
|
|
|
|
2015-07-06 07:58:33 -03:00
|
|
|
if (pattern[3] && end - ptr < (Py_ssize_t)pattern[3]) {
|
|
|
|
TRACE(("reject (got %u chars, need %u)\n",
|
|
|
|
(unsigned int)(end - ptr), pattern[3]));
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-26 05:18:42 -03:00
|
|
|
if (pattern[3] > 1) {
|
|
|
|
/* adjust end point (but make sure we leave at least one
|
|
|
|
character in there, so literal search will work) */
|
|
|
|
end -= pattern[3] - 1;
|
|
|
|
if (end <= ptr)
|
|
|
|
end = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & SRE_INFO_PREFIX) {
|
|
|
|
/* pattern starts with a known prefix */
|
|
|
|
/* <length> <skip> <prefix data> <overlap data> */
|
|
|
|
prefix_len = pattern[5];
|
|
|
|
prefix_skip = pattern[6];
|
|
|
|
prefix = pattern + 7;
|
|
|
|
overlap = prefix + prefix_len - 1;
|
|
|
|
} else if (flags & SRE_INFO_CHARSET)
|
|
|
|
/* pattern starts with a character from a known set */
|
|
|
|
/* <charset> */
|
|
|
|
charset = pattern + 5;
|
|
|
|
|
|
|
|
pattern += 1 + pattern[1];
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:38:05 -03:00
|
|
|
TRACE(("prefix = %p %zd %zd\n",
|
2013-10-26 05:18:42 -03:00
|
|
|
prefix, prefix_len, prefix_skip));
|
|
|
|
TRACE(("charset = %p\n", charset));
|
|
|
|
|
2015-06-21 08:06:55 -03:00
|
|
|
if (prefix_len == 1) {
|
|
|
|
/* pattern starts with a literal character */
|
|
|
|
SRE_CHAR c = (SRE_CHAR) prefix[0];
|
|
|
|
#if SIZEOF_SRE_CHAR < 4
|
|
|
|
if ((SRE_CODE) c != prefix[0])
|
|
|
|
return 0; /* literal can't match: doesn't fit in char width */
|
|
|
|
#endif
|
|
|
|
end = (SRE_CHAR *)state->end;
|
2017-12-04 08:29:05 -04:00
|
|
|
state->must_advance = 0;
|
2015-06-21 08:06:55 -03:00
|
|
|
while (ptr < end) {
|
|
|
|
while (*ptr != c) {
|
|
|
|
if (++ptr >= end)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
|
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ptr + prefix_skip;
|
|
|
|
if (flags & SRE_INFO_LITERAL)
|
|
|
|
return 1; /* we got all of it */
|
|
|
|
status = SRE(match)(state, pattern + 2*prefix_skip, 0);
|
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
++ptr;
|
2019-02-18 09:26:37 -04:00
|
|
|
RESET_CAPTURE_GROUP();
|
2015-06-21 08:06:55 -03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-26 05:18:42 -03:00
|
|
|
if (prefix_len > 1) {
|
|
|
|
/* pattern starts with a known prefix. use the overlap
|
|
|
|
table to skip forward as fast as we possibly can */
|
|
|
|
Py_ssize_t i = 0;
|
|
|
|
|
|
|
|
end = (SRE_CHAR *)state->end;
|
|
|
|
if (prefix_len > end - ptr)
|
|
|
|
return 0;
|
|
|
|
#if SIZEOF_SRE_CHAR < 4
|
|
|
|
for (i = 0; i < prefix_len; i++)
|
|
|
|
if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i])
|
|
|
|
return 0; /* literal can't match: doesn't fit in char width */
|
|
|
|
#endif
|
|
|
|
while (ptr < end) {
|
|
|
|
SRE_CHAR c = (SRE_CHAR) prefix[0];
|
|
|
|
while (*ptr++ != c) {
|
|
|
|
if (ptr >= end)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ptr >= end)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
i = 1;
|
2017-12-04 08:29:05 -04:00
|
|
|
state->must_advance = 0;
|
2013-10-26 05:18:42 -03:00
|
|
|
do {
|
|
|
|
if (*ptr == (SRE_CHAR) prefix[i]) {
|
|
|
|
if (++i != prefix_len) {
|
|
|
|
if (++ptr >= end)
|
|
|
|
return 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* found a potential match */
|
|
|
|
TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
|
|
|
|
state->start = ptr - (prefix_len - 1);
|
|
|
|
state->ptr = ptr - (prefix_len - prefix_skip - 1);
|
|
|
|
if (flags & SRE_INFO_LITERAL)
|
|
|
|
return 1; /* we got all of it */
|
2014-05-14 15:48:17 -03:00
|
|
|
status = SRE(match)(state, pattern + 2*prefix_skip, 0);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
/* close but no cigar -- try again */
|
|
|
|
if (++ptr >= end)
|
|
|
|
return 0;
|
2019-02-18 09:26:37 -04:00
|
|
|
RESET_CAPTURE_GROUP();
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
|
|
|
i = overlap[i];
|
|
|
|
} while (i != 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:06:55 -03:00
|
|
|
if (charset) {
|
2013-10-26 05:18:42 -03:00
|
|
|
/* pattern starts with a character from a known set */
|
|
|
|
end = (SRE_CHAR *)state->end;
|
2017-12-04 08:29:05 -04:00
|
|
|
state->must_advance = 0;
|
2013-10-26 05:18:42 -03:00
|
|
|
for (;;) {
|
2014-10-31 07:36:56 -03:00
|
|
|
while (ptr < end && !SRE(charset)(state, charset, *ptr))
|
2013-10-26 05:18:42 -03:00
|
|
|
ptr++;
|
|
|
|
if (ptr >= end)
|
|
|
|
return 0;
|
|
|
|
TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
|
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ptr;
|
2014-05-14 15:48:17 -03:00
|
|
|
status = SRE(match)(state, pattern, 0);
|
2013-10-26 05:18:42 -03:00
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
ptr++;
|
2019-02-18 09:26:37 -04:00
|
|
|
RESET_CAPTURE_GROUP();
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
2015-07-06 07:58:33 -03:00
|
|
|
} else {
|
2013-10-26 05:18:42 -03:00
|
|
|
/* general case */
|
2015-07-06 07:58:33 -03:00
|
|
|
assert(ptr <= end);
|
2017-12-04 08:29:05 -04:00
|
|
|
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
|
|
|
|
state->start = state->ptr = ptr;
|
|
|
|
status = SRE(match)(state, pattern, 1);
|
|
|
|
state->must_advance = 0;
|
2022-03-22 12:27:55 -03:00
|
|
|
if (status == 0 && pattern[0] == SRE_OP_AT &&
|
|
|
|
(pattern[1] == SRE_AT_BEGINNING ||
|
|
|
|
pattern[1] == SRE_AT_BEGINNING_STRING))
|
|
|
|
{
|
|
|
|
state->start = state->ptr = ptr = end;
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-04 08:29:05 -04:00
|
|
|
while (status == 0 && ptr < end) {
|
|
|
|
ptr++;
|
2019-02-18 09:26:37 -04:00
|
|
|
RESET_CAPTURE_GROUP();
|
2013-10-26 05:18:42 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
|
2015-07-06 07:58:33 -03:00
|
|
|
state->start = state->ptr = ptr;
|
2014-05-14 15:48:17 -03:00
|
|
|
status = SRE(match)(state, pattern, 0);
|
2013-10-26 05:18:42 -03:00
|
|
|
}
|
2015-07-06 07:58:33 -03:00
|
|
|
}
|
2013-10-26 05:18:42 -03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SRE_CHAR
|
|
|
|
#undef SIZEOF_SRE_CHAR
|
|
|
|
#undef SRE
|
|
|
|
|
|
|
|
/* vim:ts=4:sw=4:et
|
|
|
|
*/
|