andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * shexp.c: shell-like wildcard match routines andre@0: * andre@0: * See shexp.h for public documentation. andre@0: */ andre@0: andre@0: #include "seccomon.h" andre@0: #include "portreg.h" andre@0: andre@0: /* ----------------------------- shexp_valid ------------------------------ */ andre@0: andre@0: andre@0: static int andre@0: _valid_subexp(const char *exp, char stop1, char stop2) andre@0: { andre@0: register int x; andre@0: int nsc = 0; /* Number of special characters */ andre@0: int np; /* Number of pipe characters in union */ andre@0: int tld = 0; /* Number of tilde characters */ andre@0: andre@0: for (x = 0; exp[x] && (exp[x] != stop1) && (exp[x] != stop2); ++x) { andre@0: switch(exp[x]) { andre@0: case '~': andre@0: if(tld) /* at most one exclusion */ andre@0: return INVALID_SXP; andre@0: if (stop1) /* no exclusions within unions */ andre@0: return INVALID_SXP; andre@0: if (!exp[x+1]) /* exclusion cannot be last character */ andre@0: return INVALID_SXP; andre@0: if (!x) /* exclusion cannot be first character */ andre@0: return INVALID_SXP; andre@0: ++tld; andre@0: /* fall through */ andre@0: case '*': andre@0: case '?': andre@0: case '$': andre@0: ++nsc; andre@0: break; andre@0: case '[': andre@0: ++nsc; andre@0: if((!exp[++x]) || (exp[x] == ']')) andre@0: return INVALID_SXP; andre@0: for(; exp[x] && (exp[x] != ']'); ++x) { andre@0: if(exp[x] == '\\' && !exp[++x]) andre@0: return INVALID_SXP; andre@0: } andre@0: if(!exp[x]) andre@0: return INVALID_SXP; andre@0: break; andre@0: case '(': andre@0: ++nsc; andre@0: if (stop1) /* no nested unions */ andre@0: return INVALID_SXP; andre@0: np = -1; andre@0: do { andre@0: int t = _valid_subexp(&exp[++x], ')', '|'); andre@0: if(t == 0 || t == INVALID_SXP) andre@0: return INVALID_SXP; andre@0: x+=t; andre@0: if(!exp[x]) andre@0: return INVALID_SXP; andre@0: ++np; andre@0: } while (exp[x] == '|' ); andre@0: if(np < 1) /* must be at least one pipe */ andre@0: return INVALID_SXP; andre@0: break; andre@0: case ')': andre@0: case '|': andre@0: case ']': andre@0: return INVALID_SXP; andre@0: case '\\': andre@0: ++nsc; andre@0: if(!exp[++x]) andre@0: return INVALID_SXP; andre@0: break; andre@0: default: andre@0: break; andre@0: } andre@0: } andre@0: if((!stop1) && (!nsc)) /* must be at least one special character */ andre@0: return NON_SXP; andre@0: return ((exp[x] == stop1 || exp[x] == stop2) ? x : INVALID_SXP); andre@0: } andre@0: andre@0: int andre@0: PORT_RegExpValid(const char *exp) andre@0: { andre@0: int x; andre@0: andre@0: x = _valid_subexp(exp, '\0', '\0'); andre@0: return (x < 0 ? x : VALID_SXP); andre@0: } andre@0: andre@0: andre@0: /* ----------------------------- shexp_match ----------------------------- */ andre@0: andre@0: andre@0: #define MATCH 0 andre@0: #define NOMATCH 1 andre@0: #define ABORTED -1 andre@0: andre@0: static int andre@0: _shexp_match(const char *str, const char *exp, PRBool case_insensitive, andre@0: unsigned int level); andre@0: andre@0: /* Count characters until we reach a NUL character or either of the andre@0: * two delimiter characters, stop1 or stop2. If we encounter a bracketed andre@0: * expression, look only for NUL or ']' inside it. Do not look for stop1 andre@0: * or stop2 inside it. Return ABORTED if bracketed expression is unterminated. andre@0: * Handle all escaping. andre@0: * Return index in input string of first stop found, or ABORTED if not found. andre@0: * If "dest" is non-NULL, copy counted characters to it and NUL terminate. andre@0: */ andre@0: static int andre@0: _scan_and_copy(const char *exp, char stop1, char stop2, char *dest) andre@0: { andre@0: register int sx; /* source index */ andre@0: register char cc; andre@0: andre@0: for (sx = 0; (cc = exp[sx]) && cc != stop1 && cc != stop2; sx++) { andre@0: if (cc == '\\') { andre@0: if (!exp[++sx]) andre@0: return ABORTED; /* should be impossible */ andre@0: } else if (cc == '[') { andre@0: while ((cc = exp[++sx]) && cc != ']') { andre@0: if(cc == '\\' && !exp[++sx]) andre@0: return ABORTED; andre@0: } andre@0: if (!cc) andre@0: return ABORTED; /* should be impossible */ andre@0: } andre@0: } andre@0: if (dest && sx) { andre@0: /* Copy all but the closing delimiter. */ andre@0: memcpy(dest, exp, sx); andre@0: dest[sx] = 0; andre@0: } andre@0: return cc ? sx : ABORTED; /* index of closing delimiter */ andre@0: } andre@0: andre@0: /* On input, exp[0] is the opening parenthesis of a union. andre@0: * See if any of the alternatives in the union matches as a pattern. andre@0: * The strategy is to take each of the alternatives, in turn, and append andre@0: * the rest of the expression (after the closing ')' that marks the end of andre@0: * this union) to that alternative, and then see if the resultant expression andre@0: * matches the input string. Repeat this until some alternative matches, andre@0: * or we have an abort. andre@0: */ andre@0: static int andre@0: _handle_union(const char *str, const char *exp, PRBool case_insensitive, andre@0: unsigned int level) andre@0: { andre@0: register int sx; /* source index */ andre@0: int cp; /* source index of closing parenthesis */ andre@0: int count; andre@0: int ret = NOMATCH; andre@0: char *e2; andre@0: andre@0: /* Find the closing parenthesis that ends this union in the expression */ andre@0: cp = _scan_and_copy(exp, ')', '\0', NULL); andre@0: if (cp == ABORTED || cp < 4) /* must be at least "(a|b" before ')' */ andre@0: return ABORTED; andre@0: ++cp; /* now index of char after closing parenthesis */ andre@0: e2 = (char *) PORT_Alloc(1 + strlen(exp)); andre@0: if (!e2) andre@0: return ABORTED; andre@0: for (sx = 1; ; ++sx) { andre@0: /* Here, exp[sx] is one character past the preceding '(' or '|'. */ andre@0: /* Copy everything up to the next delimiter to e2 */ andre@0: count = _scan_and_copy(exp + sx, ')', '|', e2); andre@0: if (count == ABORTED || !count) { andre@0: ret = ABORTED; andre@0: break; andre@0: } andre@0: sx += count; andre@0: /* Append everything after closing parenthesis to e2. This is safe. */ andre@0: strcpy(e2+count, exp+cp); andre@0: ret = _shexp_match(str, e2, case_insensitive, level + 1); andre@0: if (ret != NOMATCH || !exp[sx] || exp[sx] == ')') andre@0: break; andre@0: } andre@0: PORT_Free(e2); andre@0: if (sx < 2) andre@0: ret = ABORTED; andre@0: return ret; andre@0: } andre@0: andre@0: /* returns 1 if val is in range from start..end, case insensitive. */ andre@0: static int andre@0: _is_char_in_range(int start, int end, int val) andre@0: { andre@0: char map[256]; andre@0: memset(map, 0, sizeof map); andre@0: while (start <= end) andre@0: map[tolower(start++)] = 1; andre@0: return map[tolower(val)]; andre@0: } andre@0: andre@0: static int andre@0: _shexp_match(const char *str, const char *exp, PRBool case_insensitive, andre@0: unsigned int level) andre@0: { andre@0: register int x; /* input string index */ andre@0: register int y; /* expression index */ andre@0: int ret,neg; andre@0: andre@0: if (level > 20) /* Don't let the stack get too deep. */ andre@0: return ABORTED; andre@0: for(x = 0, y = 0; exp[y]; ++y, ++x) { andre@0: if((!str[x]) && (exp[y] != '$') && (exp[y] != '*')) { andre@0: return NOMATCH; andre@0: } andre@0: switch(exp[y]) { andre@0: case '$': andre@0: if(str[x]) andre@0: return NOMATCH; andre@0: --x; /* we don't want loop to increment x */ andre@0: break; andre@0: case '*': andre@0: while(exp[++y] == '*'){} andre@0: if(!exp[y]) andre@0: return MATCH; andre@0: while(str[x]) { andre@0: ret = _shexp_match(&str[x++], &exp[y], case_insensitive, andre@0: level + 1); andre@0: switch(ret) { andre@0: case NOMATCH: andre@0: continue; andre@0: case ABORTED: andre@0: return ABORTED; andre@0: default: andre@0: return MATCH; andre@0: } andre@0: } andre@0: if((exp[y] == '$') && (exp[y+1] == '\0') && (!str[x])) andre@0: return MATCH; andre@0: else andre@0: return NOMATCH; andre@0: case '[': { andre@0: int start, end = 0, i; andre@0: neg = ((exp[++y] == '^') && (exp[y+1] != ']')); andre@0: if (neg) andre@0: ++y; andre@0: i = y; andre@0: start = (unsigned char)(exp[i++]); andre@0: if (start == '\\') andre@0: start = (unsigned char)(exp[i++]); andre@0: if (isalnum(start) && exp[i++] == '-') { andre@0: end = (unsigned char)(exp[i++]); andre@0: if (end == '\\') andre@0: end = (unsigned char)(exp[i++]); andre@0: } andre@0: if (isalnum(end) && exp[i] == ']') { andre@0: /* This is a range form: a-b */ andre@0: int val = (unsigned char)(str[x]); andre@0: if (end < start) { /* swap them */ andre@0: start ^= end; andre@0: end ^= start; andre@0: start ^= end; andre@0: } andre@0: if (case_insensitive && isalpha(val)) { andre@0: val = _is_char_in_range(start, end, val); andre@0: if (neg == val) andre@0: return NOMATCH; andre@0: } else if (neg != ((val < start) || (val > end))) { andre@0: return NOMATCH; andre@0: } andre@0: y = i; andre@0: } else { andre@0: /* Not range form */ andre@0: int matched = 0; andre@0: for (; exp[y] != ']'; y++) { andre@0: if (exp[y] == '\\') andre@0: ++y; andre@0: if(case_insensitive) { andre@0: matched |= (toupper(str[x]) == toupper(exp[y])); andre@0: } else { andre@0: matched |= (str[x] == exp[y]); andre@0: } andre@0: } andre@0: if (neg == matched) andre@0: return NOMATCH; andre@0: } andre@0: } andre@0: break; andre@0: case '(': andre@0: if (!exp[y+1]) andre@0: return ABORTED; andre@0: return _handle_union(&str[x], &exp[y], case_insensitive, level); andre@0: case '?': andre@0: break; andre@0: case '|': andre@0: case ']': andre@0: case ')': andre@0: return ABORTED; andre@0: case '\\': andre@0: ++y; andre@0: /* fall through */ andre@0: default: andre@0: if(case_insensitive) { andre@0: if(toupper(str[x]) != toupper(exp[y])) andre@0: return NOMATCH; andre@0: } else { andre@0: if(str[x] != exp[y]) andre@0: return NOMATCH; andre@0: } andre@0: break; andre@0: } andre@0: } andre@0: return (str[x] ? NOMATCH : MATCH); andre@0: } andre@0: andre@0: static int andre@0: port_RegExpMatch(const char *str, const char *xp, PRBool case_insensitive) andre@0: { andre@0: char *exp = 0; andre@0: int x, ret = MATCH; andre@0: andre@0: if (!strchr(xp, '~')) andre@0: return _shexp_match(str, xp, case_insensitive, 0); andre@0: andre@0: exp = PORT_Strdup(xp); andre@0: if(!exp) andre@0: return NOMATCH; andre@0: andre@0: x = _scan_and_copy(exp, '~', '\0', NULL); andre@0: if (x != ABORTED && exp[x] == '~') { andre@0: exp[x++] = '\0'; andre@0: ret = _shexp_match(str, &exp[x], case_insensitive, 0); andre@0: switch (ret) { andre@0: case NOMATCH: ret = MATCH; break; andre@0: case MATCH: ret = NOMATCH; break; andre@0: default: break; andre@0: } andre@0: } andre@0: if (ret == MATCH) andre@0: ret = _shexp_match(str, exp, case_insensitive, 0); andre@0: andre@0: PORT_Free(exp); andre@0: return ret; andre@0: } andre@0: andre@0: andre@0: /* ------------------------------ shexp_cmp ------------------------------- */ andre@0: andre@0: int andre@0: PORT_RegExpSearch(const char *str, const char *exp) andre@0: { andre@0: switch(PORT_RegExpValid(exp)) andre@0: { andre@0: case INVALID_SXP: andre@0: return -1; andre@0: case NON_SXP: andre@0: return (strcmp(exp,str) ? 1 : 0); andre@0: default: andre@0: return port_RegExpMatch(str, exp, PR_FALSE); andre@0: } andre@0: } andre@0: andre@0: int andre@0: PORT_RegExpCaseSearch(const char *str, const char *exp) andre@0: { andre@0: switch(PORT_RegExpValid(exp)) andre@0: { andre@0: case INVALID_SXP: andre@0: return -1; andre@0: case NON_SXP: andre@0: return (PORT_Strcasecmp(exp,str) ? 1 : 0); andre@0: default: andre@0: return port_RegExpMatch(str, exp, PR_TRUE); andre@0: } andre@0: } andre@0: