DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/* 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 */
    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 */
                return INVALID_SXP;
            if (stop1)              /* no exclusions within unions */
                return INVALID_SXP;
            if (!exp[x+1])          /* exclusion cannot be last character */
                return INVALID_SXP;
            if (!x)                 /* exclusion cannot be first character */
                return INVALID_SXP;
            ++tld;
	    /* fall through */
	case '*':
	case '?':
	case '$':
            ++nsc;
            break;
	case '[':
            ++nsc;
            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
 * matches the input string.  Repeat this until some alternative matches, 
 * or we have an abort. 
 */
static int 
_handle_union(const char *str, const char *exp, PRBool case_insensitive,
              unsigned int level) 
{
    register int sx;     /* source index */
    int cp;              /* source index of closing parenthesis */
    int count;
    int ret   = NOMATCH;
    char *e2;

    /* Find the closing parenthesis that ends this union in the expression */
    cp = _scan_and_copy(exp, ')', '\0', NULL);
    if (cp == ABORTED || cp < 4) /* must be at least "(a|b" before ')' */
    	return ABORTED;
    ++cp;                /* now index of char after closing parenthesis */
    e2 = (char *) PORT_Alloc(1 + strlen(exp));
    if (!e2)
    	return ABORTED;
    for (sx = 1; ; ++sx) {
	/* Here, exp[sx] is one character past the preceding '(' or '|'. */
	/* Copy everything up to the next delimiter to e2 */
	count = _scan_and_copy(exp + sx, ')', '|', e2);
	if (count == ABORTED || !count) {
	    ret = ABORTED;
	    break;
	}
	sx += count;
	/* Append everything after closing parenthesis to e2. This is safe. */
	strcpy(e2+count, exp+cp);
        ret = _shexp_match(str, e2, case_insensitive, level + 1);
	if (ret != NOMATCH || !exp[sx] || exp[sx] == ')')
            break;
    }
    PORT_Free(e2);
    if (sx < 2)
    	ret = ABORTED;
    return ret;
}

/* returns 1 if val is in range from start..end, case insensitive. */
static int
_is_char_in_range(int start, int end, int val)
{
    char map[256];
    memset(map, 0, sizeof map);
    while (start <= end)
	map[tolower(start++)] = 1;
    return map[tolower(val)];
}

static int 
_shexp_match(const char *str, const char *exp, PRBool case_insensitive, 
             unsigned int level) 
{
    register int x;   /* input string index */
    register int y;   /* expression index */
    int ret,neg;

    if (level > 20)      /* Don't let the stack get too deep. */
    	return ABORTED;
    for(x = 0, y = 0; exp[y]; ++y, ++x) {
        if((!str[x]) && (exp[y] != '$') && (exp[y] != '*')) {
            return NOMATCH;
	}
	switch(exp[y]) {
	case '$':
	    if(str[x])
		return NOMATCH;
	    --x;                 /* we don't want loop to increment x */
	    break;
	case '*':
	    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 val   = (unsigned char)(str[x]);
		if (end < start) { /* swap them */
		    start ^= end;
		    end ^= start;
		    start ^= end;
		}
		if (case_insensitive && isalpha(val)) {
		    val = _is_char_in_range(start, end, val);
		    if (neg == val)
			return NOMATCH;
		} else if (neg != ((val < start) || (val > end))) {
		    return NOMATCH;
		}
		y = i;
	    } else {
		/* Not range form */
		int matched = 0;
		for (; exp[y] != ']'; y++) {
		    if (exp[y] == '\\')
			++y;
		    if(case_insensitive) {
			matched |= (toupper(str[x]) == toupper(exp[y]));
		    } else {
			matched |= (str[x] == exp[y]);
		    }
		}
		if (neg == matched)
		    return NOMATCH;
	    }
	}
	break;
	case '(':
	    if (!exp[y+1])
	    	return ABORTED;
	    return _handle_union(&str[x], &exp[y], case_insensitive, level);
	case '?':
	    break;
	case '|':
	case ']':
	case ')':
	    return ABORTED;
	case '\\':
	    ++y;
	    /* fall through */
	default:
	    if(case_insensitive) {
		if(toupper(str[x]) != toupper(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 (ret == MATCH)
	ret = _shexp_match(str, exp, case_insensitive, 0);

    PORT_Free(exp);
    return ret;
}


/* ------------------------------ shexp_cmp ------------------------------- */

int 
PORT_RegExpSearch(const char *str, const char *exp)
{
    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))
      {
        case INVALID_SXP:
            return -1;
        case NON_SXP:
            return (PORT_Strcasecmp(exp,str) ? 1 : 0);
        default:
            return port_RegExpMatch(str, exp, PR_TRUE);
      }
}