diff --git a/streecmp.a b/streecmp.a new file mode 100644 index 0000000..9eacb54 Binary files /dev/null and b/streecmp.a differ diff --git a/streecmp.c b/streecmp.c index 5d8c5f0..67634e7 100644 --- a/streecmp.c +++ b/streecmp.c @@ -51,31 +51,35 @@ struct nod *mknod(struct nod *nod, int loc) { return nod->pool[loc]; } -int mkstr(struct nod *nod, char *str, int val) { - struct nod *target = nod->pool[*str]; +int mkstr(struct nod *nod, char *str, int val, int flag) { + char ch = (flag & LOOSE_CHECK) && + (*str >= 'a') && + (*str <= 'z') ? *str-32 : *str; + + struct nod *target = nod->pool[ch]; if (target != NULL) { if (*str == '\0') { return 0; } - return mkstr(target, str+1, val); + return mkstr(target, str+1, val, flag); } - struct nod *new = mknod(nod, *str); + struct nod *new = mknod(nod, ch); if (new == NULL) { return -1; } - new->val.cval = *str; + new->val.cval = ch; if (*str != '\0') { - return mkstr(new, str+1, val); + return mkstr(new, str+1, val, flag); } new->val.ival = val; return val; } -int gentree(struct nod *nod, char *strs, char *delim) { +int gentree(struct nod *nod, char *strs, char *delim, int flag) { strs_cnt = 1; char *strs_cpy = strdup(strs); @@ -89,7 +93,7 @@ int gentree(struct nod *nod, char *strs, char *delim) { for (char *tok = strtok(strs_cpy, delim); tok != NULL; tok = strtok(NULL, delim)) { - int ret = mkstr(nod, tok, strs_cnt); + int ret = mkstr(nod, tok, strs_cnt, flag); if (ret < 0) { goto _err_mkstr; } @@ -105,8 +109,12 @@ _err: return -1; } -int streecmp(struct nod *nod, char *str) { - struct nod *target = nod->pool[*str]; +int streecmp(struct nod *nod, char *str, int flag) { + char ch = (flag & LOOSE_CHECK) && + (*str >= 'a') && + (*str <= 'z') ? *str-32 : *str; + + struct nod *target = nod->pool[ch]; if (target == NULL) { return 0; } @@ -115,21 +123,22 @@ int streecmp(struct nod *nod, char *str) { return target->val.ival; } - return streecmp(target, str+1); + return streecmp(target, str+1, flag); } -int streencmp(struct nod *nod, char *str, int len) { +int streencmp(struct nod *nod, char *str, int len, int flag) { struct nod *nptr = nod; - char *sptr = str; + for (char *sptr = str; sptr < str+len; sptr++) { + char ch = (flag & LOOSE_CHECK) && + (*sptr >= 'a') && + (*sptr <= 'z') ? *sptr-32 : *sptr; - for (int i = 0; i < len; i++) { - struct nod *target = nptr->pool[*sptr]; + struct nod *target = nptr->pool[ch]; if (target == NULL) { goto _not_found; } nptr = target; - sptr++; } nptr = nptr->pool['\0']; diff --git a/streecmp.h b/streecmp.h index 6d35e0d..67d00e6 100644 --- a/streecmp.h +++ b/streecmp.h @@ -4,6 +4,8 @@ #define POOL_SIZE 256 #define DEF_DELIM "\n" +#define LOOSE_CHECK 0b00000001 + struct nodval { int ival; char cval; @@ -18,9 +20,9 @@ struct nod { void frenod(struct nod *nod); struct nod *allocnod(void); struct nod *mknod(struct nod *nod, int loc); -int mkstr(struct nod *nod, char *str, int val); -int gentree(struct nod *nod, char *strs, char *delim); -int streecmp(struct nod *nod, char *str); -int streencmp(struct nod *nod, char *str, int len); +int mkstr(struct nod *nod, char *str, int val, int flag); +int gentree(struct nod *nod, char *strs, char *delim, int flag); +int streecmp(struct nod *nod, char *str, int flag); +int streencmp(struct nod *nod, char *str, int len, int flag); #endif diff --git a/test.c b/test.c index 7a5fcfb..87ea936 100644 --- a/test.c +++ b/test.c @@ -127,7 +127,7 @@ int main(void) { return -1; } - ret = gentree(rot, strs, NULL); + ret = gentree(rot, strs, NULL, LOOSE_CHECK); if (ret < 0) { fprintf(stderr, "[-] Failed: could not generate tree\n"); return -1; @@ -138,7 +138,7 @@ int main(void) { int cnt = 1; for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n")) { - int found = streecmp(rot, tok); + int found = streecmp(rot, tok, LOOSE_CHECK); if (found == cnt) { fprintf(stdout, "[+][%d/%d] Success: could find" " '%s' in tree\n", cnt, found, tok);