| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "rsync.h" |
| 13 |
|
| 14 |
|
| 15 |
#define NEGATE_CLASS '!' |
| 16 |
#define NEGATE_CLASS2 '^' |
| 17 |
|
| 18 |
#define FALSE 0 |
| 19 |
#define TRUE 1 |
| 20 |
#define ABORT_ALL -1 |
| 21 |
#define ABORT_TO_STARSTAR -2 |
| 22 |
|
| 23 |
#define CC_EQ(class, len, litmatch) ((len) == sizeof (litmatch)-1 \ |
| 24 |
&& *(class) == *(litmatch) \ |
| 25 |
&& strncmp((char*)class, litmatch, len) == 0) |
| 26 |
|
| 27 |
#if defined STDC_HEADERS || !defined isascii |
| 28 |
# define ISASCII(c) 1 |
| 29 |
#else |
| 30 |
# define ISASCII(c) isascii(c) |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef isblank |
| 34 |
# define ISBLANK(c) (ISASCII(c) && isblank(c)) |
| 35 |
#else |
| 36 |
# define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef isgraph |
| 40 |
# define ISGRAPH(c) (ISASCII(c) && isgraph(c)) |
| 41 |
#else |
| 42 |
# define ISGRAPH(c) (ISASCII(c) && isprint(c) && !isspace(c)) |
| 43 |
#endif |
| 44 |
|
| 45 |
#define ISPRINT(c) (ISASCII(c) && isprint(c)) |
| 46 |
#define ISDIGIT(c) (ISASCII(c) && isdigit(c)) |
| 47 |
#define ISALNUM(c) (ISASCII(c) && isalnum(c)) |
| 48 |
#define ISALPHA(c) (ISASCII(c) && isalpha(c)) |
| 49 |
#define ISCNTRL(c) (ISASCII(c) && iscntrl(c)) |
| 50 |
#define ISLOWER(c) (ISASCII(c) && islower(c)) |
| 51 |
#define ISPUNCT(c) (ISASCII(c) && ispunct(c)) |
| 52 |
#define ISSPACE(c) (ISASCII(c) && isspace(c)) |
| 53 |
#define ISUPPER(c) (ISASCII(c) && isupper(c)) |
| 54 |
#define ISXDIGIT(c) (ISASCII(c) && isxdigit(c)) |
| 55 |
|
| 56 |
#ifdef WILD_TEST_ITERATIONS |
| 57 |
int wildmatch_iteration_count; |
| 58 |
#endif |
| 59 |
|
| 60 |
static int force_lower_case = 0; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static int dowild(const uchar *p, const uchar *text, const uchar*const *a) |
| 65 |
{ |
| 66 |
uchar p_ch; |
| 67 |
|
| 68 |
#ifdef WILD_TEST_ITERATIONS |
| 69 |
wildmatch_iteration_count++; |
| 70 |
#endif |
| 71 |
|
| 72 |
for ( ; (p_ch = *p) != '\0'; text++, p++) { |
| 73 |
int matched, special; |
| 74 |
uchar t_ch, prev_ch; |
| 75 |
while ((t_ch = *text) == '\0') { |
| 76 |
if (*a == NULL) { |
| 77 |
if (p_ch != '*') |
| 78 |
return ABORT_ALL; |
| 79 |
break; |
| 80 |
} |
| 81 |
text = *a++; |
| 82 |
} |
| 83 |
if (force_lower_case && ISUPPER(t_ch)) |
| 84 |
t_ch = tolower(t_ch); |
| 85 |
switch (p_ch) { |
| 86 |
case '\\': |
| 87 |
|
| 88 |
|
| 89 |
p_ch = *++p; |
| 90 |
|
| 91 |
default: |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (force_lower_case && ISUPPER(p_ch)) |
| 96 |
p_ch = tolower(p_ch); |
| 97 |
if (t_ch != p_ch) |
| 98 |
return FALSE; |
| 99 |
continue; |
| 100 |
case '?': |
| 101 |
|
| 102 |
if (t_ch == '/') |
| 103 |
return FALSE; |
| 104 |
continue; |
| 105 |
case '*': |
| 106 |
if (*++p == '*') { |
| 107 |
while (*++p == '*') {} |
| 108 |
special = TRUE; |
| 109 |
} else |
| 110 |
special = FALSE; |
| 111 |
if (*p == '\0') { |
| 112 |
|
| 113 |
|
| 114 |
if (!special) { |
| 115 |
do { |
| 116 |
if (strchr((char*)text, '/') != NULL) |
| 117 |
return FALSE; |
| 118 |
} while ((text = *a++) != NULL); |
| 119 |
} |
| 120 |
return TRUE; |
| 121 |
} |
| 122 |
while (1) { |
| 123 |
if (t_ch == '\0') { |
| 124 |
if ((text = *a++) == NULL) |
| 125 |
break; |
| 126 |
t_ch = *text; |
| 127 |
continue; |
| 128 |
} |
| 129 |
if ((matched = dowild(p, text, a)) != FALSE) { |
| 130 |
if (!special || matched != ABORT_TO_STARSTAR) |
| 131 |
return matched; |
| 132 |
} else if (!special && t_ch == '/') |
| 133 |
return ABORT_TO_STARSTAR; |
| 134 |
t_ch = *++text; |
| 135 |
} |
| 136 |
return ABORT_ALL; |
| 137 |
case '[': |
| 138 |
p_ch = *++p; |
| 139 |
#ifdef NEGATE_CLASS2 |
| 140 |
if (p_ch == NEGATE_CLASS2) |
| 141 |
p_ch = NEGATE_CLASS; |
| 142 |
#endif |
| 143 |
|
| 144 |
special = p_ch == NEGATE_CLASS? TRUE : FALSE; |
| 145 |
if (special) { |
| 146 |
|
| 147 |
p_ch = *++p; |
| 148 |
} |
| 149 |
prev_ch = 0; |
| 150 |
matched = FALSE; |
| 151 |
do { |
| 152 |
if (!p_ch) |
| 153 |
return ABORT_ALL; |
| 154 |
if (p_ch == '\\') { |
| 155 |
p_ch = *++p; |
| 156 |
if (!p_ch) |
| 157 |
return ABORT_ALL; |
| 158 |
if (force_lower_case && ISUPPER(p_ch)) |
| 159 |
p_ch = tolower(p_ch); |
| 160 |
if (t_ch == p_ch) |
| 161 |
matched = TRUE; |
| 162 |
} else if (p_ch == '-' && prev_ch && p[1] && p[1] != ']') { |
| 163 |
p_ch = *++p; |
| 164 |
if (p_ch == '\\') { |
| 165 |
p_ch = *++p; |
| 166 |
if (!p_ch) |
| 167 |
return ABORT_ALL; |
| 168 |
} |
| 169 |
if (force_lower_case && ISUPPER(p_ch)) |
| 170 |
p_ch = tolower(p_ch); |
| 171 |
if (t_ch <= p_ch && t_ch >= prev_ch) |
| 172 |
matched = TRUE; |
| 173 |
p_ch = 0; |
| 174 |
} else if (p_ch == '[' && p[1] == ':') { |
| 175 |
const uchar *s; |
| 176 |
int i; |
| 177 |
for (s = p += 2; (p_ch = *p) && p_ch != ']'; p++) {} |
| 178 |
if (!p_ch) |
| 179 |
return ABORT_ALL; |
| 180 |
i = p - s - 1; |
| 181 |
if (i < 0 || p[-1] != ':') { |
| 182 |
|
| 183 |
p = s - 2; |
| 184 |
p_ch = '['; |
| 185 |
if (t_ch == p_ch) |
| 186 |
matched = TRUE; |
| 187 |
continue; |
| 188 |
} |
| 189 |
if (CC_EQ(s,i, "alnum")) { |
| 190 |
if (ISALNUM(t_ch)) |
| 191 |
matched = TRUE; |
| 192 |
} else if (CC_EQ(s,i, "alpha")) { |
| 193 |
if (ISALPHA(t_ch)) |
| 194 |
matched = TRUE; |
| 195 |
} else if (CC_EQ(s,i, "blank")) { |
| 196 |
if (ISBLANK(t_ch)) |
| 197 |
matched = TRUE; |
| 198 |
} else if (CC_EQ(s,i, "cntrl")) { |
| 199 |
if (ISCNTRL(t_ch)) |
| 200 |
matched = TRUE; |
| 201 |
} else if (CC_EQ(s,i, "digit")) { |
| 202 |
if (ISDIGIT(t_ch)) |
| 203 |
matched = TRUE; |
| 204 |
} else if (CC_EQ(s,i, "graph")) { |
| 205 |
if (ISGRAPH(t_ch)) |
| 206 |
matched = TRUE; |
| 207 |
} else if (CC_EQ(s,i, "lower")) { |
| 208 |
if (ISLOWER(t_ch)) |
| 209 |
matched = TRUE; |
| 210 |
} else if (CC_EQ(s,i, "print")) { |
| 211 |
if (ISPRINT(t_ch)) |
| 212 |
matched = TRUE; |
| 213 |
} else if (CC_EQ(s,i, "punct")) { |
| 214 |
if (ISPUNCT(t_ch)) |
| 215 |
matched = TRUE; |
| 216 |
} else if (CC_EQ(s,i, "space")) { |
| 217 |
if (ISSPACE(t_ch)) |
| 218 |
matched = TRUE; |
| 219 |
} else if (CC_EQ(s,i, "upper")) { |
| 220 |
if (ISUPPER(t_ch)) |
| 221 |
matched = TRUE; |
| 222 |
} else if (CC_EQ(s,i, "xdigit")) { |
| 223 |
if (ISXDIGIT(t_ch)) |
| 224 |
matched = TRUE; |
| 225 |
} else |
| 226 |
return ABORT_ALL; |
| 227 |
p_ch = 0; |
| 228 |
} else { |
| 229 |
if (force_lower_case && ISUPPER(p_ch)) |
| 230 |
p_ch = tolower(p_ch); |
| 231 |
if (t_ch == p_ch) |
| 232 |
matched = TRUE; |
| 233 |
} |
| 234 |
} while (prev_ch = p_ch, (p_ch = *++p) != ']'); |
| 235 |
if (matched == special || t_ch == '/') |
| 236 |
return FALSE; |
| 237 |
continue; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
do { |
| 242 |
if (*text) |
| 243 |
return FALSE; |
| 244 |
} while ((text = *a++) != NULL); |
| 245 |
|
| 246 |
return TRUE; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static int doliteral(const uchar *s, const uchar *text, const uchar*const *a) |
| 252 |
{ |
| 253 |
for ( ; *s != '\0'; text++, s++) { |
| 254 |
while (*text == '\0') { |
| 255 |
if ((text = *a++) == NULL) |
| 256 |
return FALSE; |
| 257 |
} |
| 258 |
if (*text != *s) |
| 259 |
return FALSE; |
| 260 |
} |
| 261 |
|
| 262 |
do { |
| 263 |
if (*text) |
| 264 |
return FALSE; |
| 265 |
} while ((text = *a++) != NULL); |
| 266 |
|
| 267 |
return TRUE; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static const uchar *trailing_N_elements(const uchar*const **a_ptr, int count) |
| 274 |
{ |
| 275 |
const uchar*const *a = *a_ptr; |
| 276 |
const uchar*const *first_a = a; |
| 277 |
|
| 278 |
while (*a) |
| 279 |
a++; |
| 280 |
|
| 281 |
while (a != first_a) { |
| 282 |
const uchar *s = *--a; |
| 283 |
s += strlen((char*)s); |
| 284 |
while (--s >= *a) { |
| 285 |
if (*s == '/' && !--count) { |
| 286 |
*a_ptr = a+1; |
| 287 |
return s+1; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if (count == 1) { |
| 293 |
*a_ptr = a+1; |
| 294 |
return *a; |
| 295 |
} |
| 296 |
|
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
int wildmatch(const char *pattern, const char *text) |
| 302 |
{ |
| 303 |
static const uchar *nomore[1]; |
| 304 |
#ifdef WILD_TEST_ITERATIONS |
| 305 |
wildmatch_iteration_count = 0; |
| 306 |
#endif |
| 307 |
return dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
int iwildmatch(const char *pattern, const char *text) |
| 312 |
{ |
| 313 |
static const uchar *nomore[1]; |
| 314 |
int ret; |
| 315 |
#ifdef WILD_TEST_ITERATIONS |
| 316 |
wildmatch_iteration_count = 0; |
| 317 |
#endif |
| 318 |
force_lower_case = 1; |
| 319 |
ret = dowild((const uchar*)pattern, (const uchar*)text, nomore) == TRUE; |
| 320 |
force_lower_case = 0; |
| 321 |
return ret; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
int wildmatch_array(const char *pattern, const char*const *texts, int where) |
| 330 |
{ |
| 331 |
const uchar *p = (const uchar*)pattern; |
| 332 |
const uchar*const *a = (const uchar*const*)texts; |
| 333 |
const uchar *text; |
| 334 |
int matched; |
| 335 |
|
| 336 |
#ifdef WILD_TEST_ITERATIONS |
| 337 |
wildmatch_iteration_count = 0; |
| 338 |
#endif |
| 339 |
|
| 340 |
if (where > 0) |
| 341 |
text = trailing_N_elements(&a, where); |
| 342 |
else |
| 343 |
text = *a++; |
| 344 |
if (!text) |
| 345 |
return FALSE; |
| 346 |
|
| 347 |
if ((matched = dowild(p, text, a)) != TRUE && where < 0 |
| 348 |
&& matched != ABORT_ALL) { |
| 349 |
while (1) { |
| 350 |
if (*text == '\0') { |
| 351 |
if ((text = (uchar*)*a++) == NULL) |
| 352 |
return FALSE; |
| 353 |
continue; |
| 354 |
} |
| 355 |
if (*text++ == '/' && (matched = dowild(p, text, a)) != FALSE |
| 356 |
&& matched != ABORT_TO_STARSTAR) |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
return matched == TRUE; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
int litmatch_array(const char *string, const char*const *texts, int where) |
| 368 |
{ |
| 369 |
const uchar *s = (const uchar*)string; |
| 370 |
const uchar*const *a = (const uchar* const*)texts; |
| 371 |
const uchar *text; |
| 372 |
|
| 373 |
if (where > 0) |
| 374 |
text = trailing_N_elements(&a, where); |
| 375 |
else |
| 376 |
text = *a++; |
| 377 |
if (!text) |
| 378 |
return FALSE; |
| 379 |
|
| 380 |
return doliteral(s, text, a) == TRUE; |
| 381 |
} |