#include "includes.h"
static int null_match(const smb_ucs2_t *p)
{
for (;*p;p++) {
if (*p != UCS2_CHAR('*') &&
*p != UCS2_CHAR('<') &&
*p != UCS2_CHAR('"') &&
*p != UCS2_CHAR('>')) return -1;
}
return 0;
}
struct max_n {
const smb_ucs2_t *predot;
const smb_ucs2_t *postdot;
};
static int ms_fnmatch_core(const smb_ucs2_t *p, const smb_ucs2_t *n,
struct max_n *max_n, const smb_ucs2_t *ldot,
BOOL is_case_sensitive)
{
smb_ucs2_t c;
int i;
while ((c = *p++)) {
switch (c) {
case UCS2_CHAR('*'):
if (max_n->predot && max_n->predot <= n) {
return null_match(p);
}
for (i=0; n[i]; i++) {
if (ms_fnmatch_core(p, n+i, max_n+1, ldot, is_case_sensitive) == 0) {
return 0;
}
}
if (!max_n->predot || max_n->predot > n) max_n->predot = n;
return null_match(p);
case UCS2_CHAR('<'):
if (max_n->predot && max_n->predot <= n) {
return null_match(p);
}
if (max_n->postdot && max_n->postdot <= n && n <= ldot) {
return -1;
}
for (i=0; n[i]; i++) {
if (ms_fnmatch_core(p, n+i, max_n+1, ldot, is_case_sensitive) == 0) return 0;
if (n+i == ldot) {
if (ms_fnmatch_core(p, n+i+1, max_n+1, ldot, is_case_sensitive) == 0) return 0;
if (!max_n->postdot || max_n->postdot > n) max_n->postdot = n;
return -1;
}
}
if (!max_n->predot || max_n->predot > n) max_n->predot = n;
return null_match(p);
case UCS2_CHAR('?'):
if (! *n) {
return -1;
}
n++;
break;
case UCS2_CHAR('>'):
if (n[0] == UCS2_CHAR('.')) {
if (! n[1] && null_match(p) == 0) {
return 0;
}
break;
}
if (! *n) return null_match(p);
n++;
break;
case UCS2_CHAR('"'):
if (*n == 0 && null_match(p) == 0) {
return 0;
}
if (*n != UCS2_CHAR('.')) return -1;
n++;
break;
default:
if (c != *n) {
if (is_case_sensitive) {
return -1;
}
if (toupper_w(c) != toupper_w(*n)) {
return -1;
}
}
n++;
break;
}
}
if (! *n) {
return 0;
}
return -1;
}
int ms_fnmatch(const char *pattern, const char *string, enum protocol_types protocol,
BOOL is_case_sensitive)
{
wpstring p, s;
int ret, count, i;
struct max_n *max_n = NULL;
if (strcmp(string, "..") == 0) {
string = ".";
}
if (strpbrk(pattern, "<>*?\"") == NULL) {
if (is_case_sensitive) {
return strcmp(pattern, string);
} else {
return StrCaseCmp(pattern, string);
}
}
pstrcpy_wa(p, pattern);
pstrcpy_wa(s, string);
if (protocol <= PROTOCOL_LANMAN2) {
for (i=0;p[i];i++) {
if (p[i] == UCS2_CHAR('?')) {
p[i] = UCS2_CHAR('>');
} else if (p[i] == UCS2_CHAR('.') &&
(p[i+1] == UCS2_CHAR('?') ||
p[i+1] == UCS2_CHAR('*') ||
p[i+1] == 0)) {
p[i] = UCS2_CHAR('"');
} else if (p[i] == UCS2_CHAR('*') && p[i+1] == UCS2_CHAR('.')) {
p[i] = UCS2_CHAR('<');
}
}
}
for (count=i=0;p[i];i++) {
if (p[i] == UCS2_CHAR('*') || p[i] == UCS2_CHAR('<')) count++;
}
if (count != 0) {
max_n = SMB_CALLOC_ARRAY(struct max_n, count);
if (!max_n) {
return -1;
}
}
ret = ms_fnmatch_core(p, s, max_n, strrchr_w(s, UCS2_CHAR('.')), is_case_sensitive);
if (max_n) {
free(max_n);
}
return ret;
}
int gen_fnmatch(const char *pattern, const char *string)
{
return ms_fnmatch(pattern, string, PROTOCOL_NT1, False);
}