53
_valid_subexp(const char *exp, char stop)
53
_valid_subexp(const char *exp, char stop1, char stop2)
60
while(exp[x] && (exp[x] != stop)) {
56
int nsc = 0; /* Number of special characters */
57
int np; /* Number of pipe characters in union */
58
int tld = 0; /* Number of tilde characters */
60
for (x = 0; exp[x] && (exp[x] != stop1) && (exp[x] != stop2); ++x) {
63
if(tld) return INVALID_SXP;
63
if(tld) /* at most one exclusion */
65
if (stop1) /* no exclusions within unions */
67
if (!exp[x+1]) /* exclusion cannot be last character */
69
if (!x) /* exclusion cannot be first character */
73
80
if((!exp[++x]) || (exp[x] == ']'))
74
81
return INVALID_SXP;
75
for(++x;exp[x] && (exp[x] != ']');++x)
82
for(; exp[x] && (exp[x] != ']'); ++x) {
83
if(exp[x] == '\\' && !exp[++x])
80
87
return INVALID_SXP;
87
for(y=x;(exp[y]) && (exp[y] != '|') && (exp[y] != ')');++y)
95
t = _valid_subexp(&exp[x],exp[y]);
91
if (stop1) /* no nested unions */
95
int t = _valid_subexp(&exp[++x], ')', '|');
96
if(t == 0 || t == INVALID_SXP)
97
97
return INVALID_SXP;
102
} while (exp[x] == '|' );
103
if(np < 1) /* must be at least one pipe */
108
109
return INVALID_SXP;
111
113
return INVALID_SXP;
117
if((!stop) && (!nsc))
119
if((!stop1) && (!nsc)) /* must be at least one special character */
119
return ((exp[x] == stop) ? x : INVALID_SXP);
121
return ((exp[x] == stop1 || exp[x] == stop2) ? x : INVALID_SXP);
136
138
#define NOMATCH 1
137
139
#define ABORTED -1
139
static int _shexp_match(const char *str, const char *exp, PRBool case_insensitive);
142
_handle_union(const char *str, const char *exp, PRBool case_insensitive)
144
char *e2 = (char *) PORT_Alloc(sizeof(char)*strlen(exp));
145
register int t,p2,p1 = 1;
149
for(cp=1;exp[cp] != ')';cp++)
152
for(p2 = 0;(exp[p1] != '|') && (p1 != cp);p1++,p2++) {
154
e2[p2++] = exp[p1++];
157
for (t=cp+1; ((e2[p2] = exp[t]) != 0); ++t,++p2) {}
158
if(_shexp_match(str,e2, case_insensitive) == MATCH) {
142
_shexp_match(const char *str, const char *exp, PRBool case_insensitive,
145
/* Count characters until we reach a NUL character or either of the
146
* two delimiter characters, stop1 or stop2. If we encounter a bracketed
147
* expression, look only for NUL or ']' inside it. Do not look for stop1
148
* or stop2 inside it. Return ABORTED if bracketed expression is unterminated.
149
* Handle all escaping.
150
* Return index in input string of first stop found, or ABORTED if not found.
151
* If "dest" is non-NULL, copy counted characters to it and NUL terminate.
154
_scan_and_copy(const char *exp, char stop1, char stop2, char *dest)
156
register int sx; /* source index */
159
for (sx = 0; (cc = exp[sx]) && cc != stop1 && cc != stop2; sx++) {
162
return ABORTED; /* should be impossible */
163
} else if (cc == '[') {
164
while ((cc = exp[++sx]) && cc != ']') {
165
if(cc == '\\' && !exp[++sx])
169
return ABORTED; /* should be impossible */
173
/* Copy all but the closing delimiter. */
174
memcpy(dest, exp, sx);
177
return cc ? sx : ABORTED; /* index of closing delimiter */
180
/* On input, exp[0] is the opening parenthesis of a union.
181
* See if any of the alternatives in the union matches as a pattern.
182
* The strategy is to take each of the alternatives, in turn, and append
183
* the rest of the expression (after the closing ')' that marks the end of
184
* this union) to that alternative, and then see if the resultant expression
185
* matches the input string. Repeat this until some alternative matches,
186
* or we have an abort.
189
_handle_union(const char *str, const char *exp, PRBool case_insensitive,
192
register int sx; /* source index */
193
int cp; /* source index of closing parenthesis */
198
/* Find the closing parenthesis that ends this union in the expression */
199
cp = _scan_and_copy(exp, ')', '\0', NULL);
200
if (cp == ABORTED || cp < 4) /* must be at least "(a|b" before ')' */
202
++cp; /* now index of char after closing parenthesis */
203
e2 = (char *) PORT_Alloc(1 + strlen(exp));
206
for (sx = 1; ; ++sx) {
207
/* Here, exp[sx] is one character past the preceeding '(' or '|'. */
208
/* Copy everything up to the next delimiter to e2 */
209
count = _scan_and_copy(exp + sx, ')', '|', e2);
210
if (count == ABORTED || !count) {
215
/* Append everything after closing parenthesis to e2. This is safe. */
216
strcpy(e2+count, exp+cp);
217
ret = _shexp_match(str, e2, case_insensitive, level + 1);
218
if (ret != NOMATCH || !exp[sx] || exp[sx] == ')')
227
/* returns 1 if val is in range from start..end, case insensitive. */
229
_is_char_in_range(int start, int end, int val)
232
memset(map, 0, sizeof map);
234
map[tolower(start++)] = 1;
235
return map[tolower(val)];
239
_shexp_match(const char *str, const char *exp, PRBool case_insensitive,
242
register int x; /* input string index */
243
register int y; /* expression index */
246
if (level > 20) /* Don't let the stack get too deep. */
248
for(x = 0, y = 0; exp[y]; ++y, ++x) {
249
if((!str[x]) && (exp[y] != '$') && (exp[y] != '*')) {
256
--x; /* we don't want loop to increment x */
259
while(exp[++y] == '*'){}
263
ret = _shexp_match(&str[x++], &exp[y], case_insensitive,
274
if((exp[y] == '$') && (exp[y+1] == '\0') && (!str[x]))
279
int start, end = 0, i;
280
neg = ((exp[++y] == '^') && (exp[y+1] != ']'));
284
start = (unsigned char)(exp[i++]);
286
start = (unsigned char)(exp[i++]);
287
if (isalnum(start) && exp[i++] == '-') {
288
end = (unsigned char)(exp[i++]);
290
end = (unsigned char)(exp[i++]);
292
if (isalnum(end) && exp[i] == ']') {
293
/* This is a range form: a-b */
294
int val = (unsigned char)(str[x]);
295
if (end < start) { /* swap them */
300
if (case_insensitive && isalpha(val)) {
301
val = _is_char_in_range(start, end, val);
304
} else if (neg != ((val < start) || (val > end))) {
311
for (; exp[y] != ']'; y++) {
314
if(case_insensitive) {
315
matched |= (toupper(str[x]) == toupper(exp[y]));
317
matched |= (str[x] == exp[y]);
328
return _handle_union(&str[x], &exp[y], case_insensitive, level);
339
if(case_insensitive) {
340
if(toupper(str[x]) != toupper(exp[y]))
349
return (str[x] ? NOMATCH : MATCH);
172
_shexp_match(const char *str, const char *exp, PRBool case_insensitive)
353
port_RegExpMatch(const char *str, const char *xp, PRBool case_insensitive)
178
for(x=0,y=0;exp[y];++y,++x) {
179
if((!str[x]) && (exp[y] != '(') && (exp[y] != '$') && (exp[y] != '*'))
187
--x; /* we don't want loop to increment x */
190
while(exp[++y] == '*'){}
194
switch(_shexp_match(&str[x++],&exp[y], case_insensitive)) {
205
if((exp[y] == '$') && (exp[y+1] == '\0') && (!str[x]))
211
neg = ((exp[++y] == '^') && (exp[y+1] != ']'));
215
if ((isalnum(exp[y])) && (exp[y+1] == '-') &&
216
(isalnum(exp[y+2])) && (exp[y+3] == ']'))
218
int start = exp[y], end = exp[y+2];
220
/* no safeguards here */
221
if(neg ^ ((str[x] < start) || (str[x] > end))) {
230
for (matched=0;exp[y] != ']';y++)
231
matched |= (str[x] == exp[y]);
232
if (neg ^ (!matched))
237
return _handle_union(&str[x],&exp[y], case_insensitive);
246
if(toupper(str[x]) != toupper(exp[y]))
260
return (ret ? ret : (str[x] ? NOMATCH : MATCH));
264
port_RegExpMatch(const char *str, const char *xp, PRBool case_insensitive) {
358
if (!strchr(xp, '~'))
359
return _shexp_match(str, xp, case_insensitive, 0);
268
361
exp = PORT_Strdup(xp);
273
for(x=strlen(exp)-1;x;--x) {
274
if((exp[x] == '~') && (exp[x-1] != '\\')) {
276
if(_shexp_match(str,&exp[++x], case_insensitive) == MATCH)
365
x = _scan_and_copy(exp, '~', '\0', NULL);
366
if (x != ABORTED && exp[x] == '~') {
368
ret = _shexp_match(str, &exp[x], case_insensitive, 0);
370
case NOMATCH: ret = MATCH; break;
371
case MATCH: ret = NOMATCH; break;
281
if(_shexp_match(str,exp, case_insensitive) == MATCH) {
376
ret = _shexp_match(str, exp, case_insensitive, 0);