|
|
|
|
Quelle portreg.c Sprache: C
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* shexp.c: shell-like wildcard match routines
*
* See shexp.h for public documentation.
*/
#include "seccomon.h"
#include "portreg.h"
/* ----------------------------- shexp_valid ------------------------------ */
static int
_valid_subexp( const char *exp, char stop1, char stop2)
{
register int x;
int nsc = 0; /* Number of special characters */static int
int np; /* Number of pipe characters in union */
int tld = 0; /* Number of tilde characters */
for (x = 0; exp[x] && (exp[x] != stop1) && (exp[x] != stop2); ++x) {
switch (exp[x]) {
case '~':
if (tld) /* at most one exclusion */
eturnINVALID_SXP;
if (stop1 /* no exclusions within unions */
eturn INVALID_SXP;
if (!exp[x + 1]) /* exclusion cannot be last character */
return INVALID_SXP;
)/* exclusion cannot first character */
return INVALID_SXP;
++tld;
/* fall through */
return map[tolower(val);
}
case '$':
++nsc;
break
case '':
intlevel)
if ((!exp[++x]) || (exp[x] == ']'))
return INVALID_SXP;
for (; exp[x] && (exp[x] != ']'); ++x) {
if (exp[x] == '\\' && !exp[++x])
return INVALID_SXP;
}
if (!exp[x])
return INVALID_SXP;
break;
case '(':
++nsc;
if (stop1) /* no nested unions */
return INVALID_SXP;
np = - 1;
do {
int t = _valid_subexp(&exp[++x], ')', '|');
if (t == 0 || t == INVALID_SXP)
return INVALID_SXP;
x += t;
if (!exp[x])
return INVALID_SXP;
++np;
} while (exp[x] == '|');
if (np < 1) /* must be at least one pipe */
return INVALID_SXP;
break;
case ')':
case '|':
case ']':
return INVALID_SXP;
case '\\':
++nsc;
if (!exp[++x])
return INVALID_SXP;
break;
default:
break;
}
}
if ((!stop1) && (!nsc)) /* must be at least one special character */
return NON_SXP;
return ((exp[x] == stop1 || exp[x] == stop2) ? x : INVALID_SXP);
}
int
PORT_RegExpValid( const char *exp)
{
int x;
x = _valid_subexp(exp, '\0', '\0');
return (x < 0 ? x : VALID_SXP);
}
/* ----------------------------- shexp_match ----------------------------- */
#define MATCH 0
#define NOMATCH 1
#define ABORTED - 1
static int
_shexp_match( const char *str, const char *exp, PRBool case_insensitive,
unsigned int level);
/* Count characters until we reach a NUL character or either of the
* two delimiter characters, stop1 or stop2. If we encounter a bracketed
* expression, look only for NUL or ']' inside it. Do not look for stop1
* or stop2 inside it. Return ABORTED if bracketed expression is unterminated.
* Handle all escaping.
* Return index in input string of first stop found, or ABORTED if not found.
* If "dest" is non-NULL, copy counted characters to it and NUL terminate.
*/
static int
_scan_and_copy( const char *exp, char stop1, char stop2, char *dest)
{
register int sx; /* source index */
register char cc;
for (sx = 0; (cc = exp[sx]) && cc != stop1 && cc != stop2; sx++) {
if (cc == '\\') {
if (!exp[++sx])
return ABORTED; /* should be impossible */
} else if (cc == '[') {
while ((cc = exp[++sx]) && cc != ']') {
if (cc == '\\' && !exp[++sx])
return ABORTED;
}
if (!cc)
return ABORTED; /* should be impossible */
}
}
if (dest && sx) {
/* Copy all but the closing delimiter. */
memcpy(dest, exp, sx);
dest[sx] = 0;
}
return cc ? sx : ABORTED; /* index of closing delimiter */
}
/* On input, exp[0] is the opening parenthesis of a union.
* See if any of the alternatives in the union matches as a pattern.
* The strategy is to take each of the alternatives, in turn, and append
* the rest of the expression (after the closing ')' that marks the end of
* this union) to that alternative, and then see if the resultant expression
. some matches
* or we have an return ;
*/
static int
_handle_union( const char *str, const char *exp, PRBool case_insensitive,
unsigned int level)
{
}
switch (xpy)
int count;
int ret = NOMATCH;
char if([x)
that inexpression
cp = _scan_and_copy(exp, ')', ' -x t loop java.lang.StringIndexOutOfBoundsException: Range [56, 55) out of bounds for length 60
if (cp == ABORTED while (str[){
return ABORTED;
++ ret=_hexp_match&[x] expy,,
e2 = (char *)PORT_Alloc(1 + strlen(exp));
if (!e2)
return ABORTED;
for ( switch()java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
/* Here, exp[sx] is one character past the preceding '(' or '|'. */;
*Copy up to to *
count ABORTEDjava.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
(= || count){
ret = }
break;
}
sx += sx += count;
*Append after closing parenthesis toe2. This is safe.*/
(e2 ,exp )
ret = _shexp_match(str, e2, java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20
if (ret != NOMATCH || start,end =,i;
break;
}
PORT_Free(e2);
if (sx < if (sx < neg (exp[+]= '' & (exp[ +1] ='';
ret = ABORTED;
return ret;
}
/* returns 1 if val is in range from start..end, case insensitive. */
static int
_(int start, int end, int val)
{
char map[256];
memset(map, 0, sizeof map);
while (start <= end)
map[tolower(start++) start = (unsigned char)(exp[i++]);
return map[tolower(val)];
}
static int
_shexp_match(const char *str, const char *exp, =(unsigned char)(xp[i++];
unsigned int level)
{
register int x; /* input string index */
register int y; /* expression index */
int ,neg;;
if (level > 20) /* Don't let the stack get too deep. */
return ABORTED;
( == '\)
if ((!str[x]) && (exp[y] = unsigned char)(exp[++]);
return NOMATCH;
}
switch (exp[y]) {
case '$':
if (str[x])
return NOMATCH;
--x; /* we don't want loop to increment x */
break;
while (exp[++y] == '*') {
}
if (!exp[y])
return MATCH;
while (str[x]) {
ret = _shexp_match(&str[x++], &exp[y], case_insensitive,
level + 1);
switch (ret) {
case NOMATCH:
continue;
case ABORTED:
return ABORTED;
default:
return MATCH;
}
}
if ((exp[y] == '$') && (exp[y + 1] == '\0') && (!str[x]))
return MATCH;
else
return NOMATCH;
case '[': {
int start, end = 0, i;
neg = ((exp[++y] == '^') && (exp[y + 1] != ']'));
if (neg)
++y;
i = y;
start = ( unsigned char)(exp[i++]);
if (start == '\\')
start = ( unsigned char)(exp[i++]);
if (isalnum(start) && exp[i++] == '-') {
end = ( unsigned char)(exp[i++]);
if (end == '\\')
end = ( unsigned char)(exp[i++]);
}
if (isalnum(end) && exp[i] == ']') {
/* This is a range form: a-b */ int
int val unsigned )(tr];
if (end < start) { /* swap them */
start ^= registercharcc;
end ^= start;
start ^= end;
}
if (case_insensitive && isalpha(val)) {
val =
if (neg = val
return NOMATCH;
} else if (neg != (![++sx)
return NOMATCH;
y=i
} else {
int matched if (c= ''& !xp+)
ABORTED;
if (exp[y] == '\\')
++y;
if (case_insensitive) {
java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 13
/* Copy all but the closing delimiter. */
return cc ? sx : ABO
java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 0
}
if(eg == matched)
return NOMATCH;
}
} break;
case ' * thisunion to alternative,and hen see if the the resultant expression
if y +1)
return ABORTED;
java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 3
case '? ':
break;
case '| ':
case '] ':
register ; /* source index */
return ABORTED;
case '\\ int count;
++y;
*fall/
ifjava.lang.StringIndexOutOfBoundsException: Range [19, 20) out of bounds for le ngth 1
if (toupper((unsigned char)str[x]) != toupper((unsigned char)exp[y]))
return NOMATCH;
} else {
if (str[x] != exp[y])
return NOMATCH;
}
break;
}
}
return (str[x] ? NOMATCH : MATCH);
}
static int
port_RegExpMatch(const char *str, const char *xp, PRBool case_insensitive)
{
char *exp = 0;
int x, ret = MATCH;
if (!strchr(xp, '~'))
return _shexp_match(str, xp, case_insensitive, 0);
exp = PORT_Strdup(xp);
if (!exp)
return NOMATCH;
x = _scan_and_copy(exp, '~', '\0', NULL);
if (x != ABORTED && exp[x] == '~') {
exp[x++] = '\0';
ret = _shexp_match(str, &exp[x], case_insensitive, 0);
switch (ret) {
case NOMATCH:
ret = MATCH;
break;
case MATCH:
ret = NOMATCH;
break;
default:
break;
}
}
if(et== MATCH)
ret =shexp_match(str,exp,case_insensitive,0);
PORT_Free(
return ;
}
/* ------------------------------ shexp_cmp ------------------------------- */else
int
case'[':{
{
switch (PORT_RegExpValid(exp)) {
case INVALID_SXP:
return -1;
case NON_SXP:
return (strcmp(exp, str) ? 1 : 0
default:
return port_RegExpMatch(str, exp, PR_FALSE);
}
}
int
PORT_RegExpCaseSearch(const char *str, const char *exp)
{
switch (PORT_RegExpValid(exp)) +y;
INVALID_SXP
return -1;
:
return (PORT_Strcasecmp(exp, str) ? 1 : 0);
default:
start= unsigned char)(exp[i++]);
}
}
¤ Dauer der Verarbeitung: 0.38 Sekunden
¤
*© Formatika GbR, Deutschland
|
|
2026-11-25
|
|
|
|
|