feat: implement funky tree lookup
This commit is contained in:
commit
86e131db0c
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
compile_commands.json
|
||||
*.o
|
||||
streecmp
|
||||
test
|
23
Makefile
Normal file
23
Makefile
Normal file
|
@ -0,0 +1,23 @@
|
|||
CC=gcc
|
||||
CFLAGS=-g3
|
||||
LIBFLAGS=-c
|
||||
CFILES=streecmp.c
|
||||
TFILES=test.c
|
||||
OUTPUT=streecmp
|
||||
TOUTPUT=test
|
||||
|
||||
all:
|
||||
$(CC) $(LIBFLAGS) $(CFLAGS) $(CFILES) -o $(OUTPUT)
|
||||
make test
|
||||
|
||||
test:
|
||||
$(CC) $(CFLAGS) $(CFILES) $(TFILES) -o $(TOUTPUT)
|
||||
|
||||
run:
|
||||
./$(OUTPUT)
|
||||
|
||||
run_test:
|
||||
./$(TOUTPUT)
|
||||
|
||||
clean:
|
||||
rm -rf $(OUTPUT) $(TOUTPUT)
|
85
streecmp.c
Normal file
85
streecmp.c
Normal file
|
@ -0,0 +1,85 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "streecmp.h"
|
||||
|
||||
int strs_cnt = 0;
|
||||
|
||||
struct nod *mknod(struct nod *prnt) {
|
||||
if (prnt == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
prnt->pool_size++;
|
||||
|
||||
prnt->pool = (struct nod *) realloc(prnt->pool, sizeof(struct nod)*prnt->pool_size);
|
||||
if (prnt->pool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct nod *child = &prnt->pool[prnt->pool_size-1];
|
||||
child->pool_size = 0;
|
||||
child->pool = NULL;
|
||||
child->val.cval = 0;
|
||||
child->val.ival = 0;
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
int mkstr(struct nod *rot, char *str) {
|
||||
for (int i = 0; i < rot->pool_size; i++) {
|
||||
struct nod *child = &rot->pool[i];
|
||||
if (child->val.cval != *str) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return mkstr(child, str+1);
|
||||
}
|
||||
|
||||
struct nod *new = mknod(rot);
|
||||
if (new == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
new->val.cval = *str;
|
||||
if (*str != '\0') {
|
||||
return mkstr(new, str+1);
|
||||
}
|
||||
|
||||
strs_cnt++;
|
||||
new->val.ival = strs_cnt;
|
||||
return strs_cnt;
|
||||
}
|
||||
|
||||
int gentree(struct nod *rot, char *strs) {
|
||||
char *strs_cpy = strdup(strs);
|
||||
if (strs_cpy == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n")) {
|
||||
int ret = mkstr(rot,tok);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int streecmp(struct nod *rot, char *str) {
|
||||
for (int i = 0; i < rot->pool_size; i++) {
|
||||
struct nod *child = &rot->pool[i];
|
||||
if (child->val.cval != *str) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child->val.cval == '\0') {
|
||||
return child->val.ival;
|
||||
}
|
||||
|
||||
return streecmp(child, str+1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
20
streecmp.h
Normal file
20
streecmp.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef STREECMP_H
|
||||
#define STREECMP_H
|
||||
|
||||
struct nodval {
|
||||
int ival;
|
||||
char cval;
|
||||
};
|
||||
|
||||
struct nod {
|
||||
struct nodval val;
|
||||
int pool_size;
|
||||
struct nod *pool;
|
||||
};
|
||||
|
||||
struct nod *mknod(struct nod *prnt);
|
||||
int mkstr(struct nod *rot, char *str);
|
||||
int gentree(struct nod *rot, char *strs);
|
||||
int streecmp(struct nod *rot, char *str);
|
||||
|
||||
#endif
|
151
test.c
Normal file
151
test.c
Normal file
|
@ -0,0 +1,151 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "streecmp.h"
|
||||
|
||||
char *strs = "A-IM\n"
|
||||
"Accept\n"
|
||||
"Accept-Charset\n"
|
||||
"Accept-Datetime\n"
|
||||
"Accept-Encoding\n"
|
||||
"Accept-Language\n"
|
||||
"Access-Control-Request-Method\n"
|
||||
"Access-Control-Request-Headers\n"
|
||||
"Authorization\n"
|
||||
"Cache-Control\n"
|
||||
"Connection\n"
|
||||
"Content-Encoding\n"
|
||||
"Content-Length\n"
|
||||
"Content-MD5\n"
|
||||
"Content-Type\n"
|
||||
"Cookie\n"
|
||||
"Date\n"
|
||||
"Expect\n"
|
||||
"Forwarded\n"
|
||||
"From\n"
|
||||
"Host\n"
|
||||
"HTTP2-Settings\n"
|
||||
"If-Match\n"
|
||||
"If-Modified-Since\n"
|
||||
"If-None-Match\n"
|
||||
"If-Range\n"
|
||||
"If-Unmodified-Since\n"
|
||||
"Max-Forwards\n"
|
||||
"Origin\n"
|
||||
"Pragma\n"
|
||||
"Prefer\n"
|
||||
"Proxy-Authorization\n"
|
||||
"Range\n"
|
||||
"Referer\n"
|
||||
"TE\n"
|
||||
"Trailer\n"
|
||||
"Transfer-Encoding\n"
|
||||
"User-Agent\n"
|
||||
"Upgrade\n"
|
||||
"Via\n"
|
||||
"Warning\n"
|
||||
"Upgrade-Insecure-Requests\n"
|
||||
"X-Requested-With\n"
|
||||
"DNT\n"
|
||||
"X-Forwarded-For\n"
|
||||
"X-Forwarded-Host\n"
|
||||
"X-Forwarded-Proto\n"
|
||||
"Front-End-Https\n"
|
||||
"X-Http-Method-Override\n"
|
||||
"X-ATT-DeviceID\n"
|
||||
"X-Wap-Profile\n"
|
||||
"Proxy-Connection\n"
|
||||
"X-UIDH\n"
|
||||
"X-Csrf-Token\n"
|
||||
"X-Request-ID\n"
|
||||
"X-Correlation-ID\n"
|
||||
"Correlation-ID\n"
|
||||
"Save-Data\n"
|
||||
"Sec-GPC\n"
|
||||
"Accept-CH\n"
|
||||
"Access-Control-Allow-Origin\n"
|
||||
"Access-Control-Allow-Credentials\n"
|
||||
"Access-Control-Expose-Headers\n"
|
||||
"Access-Control-Max-Age\n"
|
||||
"Access-Control-Allow-Methods\n"
|
||||
"Access-Control-Allow-Headers\n"
|
||||
"Accept-Patch\n"
|
||||
"Accept-Ranges\n"
|
||||
"Age\n"
|
||||
"Allow\n"
|
||||
"Alt-Svc\n"
|
||||
"Content-Disposition\n"
|
||||
"Content-Language\n"
|
||||
"Content-Location\n"
|
||||
"Content-Range\n"
|
||||
"Delta-Base\n"
|
||||
"ETag\n"
|
||||
"Expires\n"
|
||||
"IM\n"
|
||||
"Last-Modified\n"
|
||||
"Link\n"
|
||||
"Location\n"
|
||||
"P3P\n"
|
||||
"Preference-Applied\n"
|
||||
"Proxy-Authenticate\n"
|
||||
"Public-Key-Pins\n"
|
||||
"Retry-After\n"
|
||||
"Server\n"
|
||||
"Set-Cookie\n"
|
||||
"Strict-Transport-Security\n"
|
||||
"Tk\n"
|
||||
"Vary\n"
|
||||
"WWW-Authenticate\n"
|
||||
"X-Frame-Options\n"
|
||||
"Content-Security-Policy\n"
|
||||
"Expect-CT\n"
|
||||
"NEL\n"
|
||||
"Permissions-Policy\n"
|
||||
"Refresh\n"
|
||||
"Report-To\n"
|
||||
"Status\n"
|
||||
"Timing-Allow-Origin\n"
|
||||
"X-Content-Duration\n"
|
||||
"X-Content-Type-Options\n"
|
||||
"X-Powered-By\n"
|
||||
"X-Redirect-By\n"
|
||||
"X-UA-Compatible\n"
|
||||
"X-XSS-Protection";
|
||||
|
||||
int main(void) {
|
||||
int ret;
|
||||
struct nod *rot = (struct nod *) calloc(1, sizeof(struct nod));
|
||||
if (rot == NULL) {
|
||||
fprintf(stderr, "[-] Failed: not enough dynamic memory\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = gentree(rot, strs);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "[-] Failed: could not generate tree\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *strs_cpy = strdup(strs);
|
||||
if (strs_cpy == NULL) {
|
||||
fprintf(stderr, "[-] Failed: not enough dynamic memory\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(stdout, "------------------------\n");
|
||||
|
||||
int cnt = 1;
|
||||
for (char *tok = strtok(strs_cpy, "\n"); tok != NULL; tok = strtok(NULL, "\n"), cnt++) {
|
||||
int found = streecmp(rot, tok);
|
||||
if (found == cnt) {
|
||||
fprintf(stdout, "[+][%d\t] Success: could find '%s' in tree\n", cnt, tok);
|
||||
} else {
|
||||
fprintf(stderr, "[-][%d\t] Error: could not find '%s' in tree\n", cnt, tok);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stdout, "------------------------\n");
|
||||
fprintf(stdout, "[*] Done\n");
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user