bunkum

an old and silly c99 web server with some fun features
Log | Files | Refs | README

commit 931a0ae751e552bed3abd8c428c7b2483f28269c
parent 3f66d6c58d16dd0065b63e0f9f03d947048be793
Author: quantumish <freifeld.david@gmail.com>
Date:   Sun,  9 Apr 2023 16:58:13 -0700

Add Makefile, phf util, formatting tweaks

Diffstat:
AMakefile | 14++++++++++++++
Mhttp.c | 2+-
Aphf.py | 43+++++++++++++++++++++++++++++++++++++++++++
Mutils/log.c | 2+-
Mutils/shitvec.c | 2--
5 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,14 @@ +CFLAGS?=-O2 -g +LIBS:=-lz -lpthread +SOURCES = $(wildcard utils/*.c) $(wildcard http/*.c) http.c +OBJ = $(wildcard build/*.o) + +.PHONY: all clean + +all: serv + +serv: $(SOURCES) + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +clean: + rm serv diff --git a/http.c b/http.c @@ -73,7 +73,7 @@ void* handle_conn(void* ctxt) { int fd = open(pathbuf+1, O_RDONLY); if (fd != -1) { response_t r = resp_new(OK); - + struct stat st; fstat(fd, &st); char* fbuf = malloc(st.st_size); diff --git a/phf.py b/phf.py @@ -0,0 +1,43 @@ +import random +inputs = ["GET", "HEAD", "POST", "PUT", "DELETE", + "CONNECT", "OPTIONS", "TRACE"] + +int_inputs = [int.from_bytes(bytes(i, 'ascii'), 'little') for i in inputs] + +answers = list(range(8)) + +def is_phf(h, inputs): + return len({h(x) for x in inputs}) == len(inputs) + +print(next(m for m in range(9, 2**32) if is_phf(lambda x: x % m, int_inputs))) + +def h(x, c): + m = (x * c) % 2**32 + return m >> 28 + +out = [0]*8 +idxs = list(h(x, 0x1b8b6e6d) for x in int_inputs) + +for i, idx in enumerate(idxs): + out[idx] = answers[i] + +print([inputs[i] for i in out]) +print(list(out[h(x, 0x1b8b6e6d)] for x in int_inputs)) + +# best = float('inf') +# while best >= len(int_inputs): +# c = random.randrange(2**32) +# max_idx = max(h(x, c) for x in int_inputs) +# if max_idx < best and is_phf(lambda x: h(x, c), int_inputs): +# print(max_idx, hex(c)) +# best = max_idx + + + +# for i in inputs: +# print(i, " ") +# for c in i[::-1]: +# print(hex(ord(c))[2:], end=" ") +# print(" ") +# print(int.from_bytes(bytes(i, 'ascii'), 'little')) +# print("\n") diff --git a/utils/log.c b/utils/log.c @@ -35,7 +35,7 @@ void log_msg(enum log_level lvl, char* lvl_name, char* fmt, ...) { time_t now = time(0); struct tm* local = localtime(&now); - char* lvl_color = lvl_colors[lvl]; + const char* lvl_color = lvl_colors[lvl]; printf(ANSI_GREY "%02d/%02d/%02d %02d:%02d:%02d %s%s" ANSI_RESET ": %s\n", local->tm_mon, local->tm_mday, local->tm_year, diff --git a/utils/shitvec.c b/utils/shitvec.c @@ -28,7 +28,6 @@ void shitvec_push(shitvec_t* sv, void* item) { sv->vec_sz += 1; } - void shitvec_subpush(shitvec_t* sv, void* item, size_t sz) { sv->vec_sz += 1; if ((sv->arr+(sv->e_sz * sv->vec_sz)) > sv->arr+sv->alloc_sz) { @@ -37,7 +36,6 @@ void shitvec_subpush(shitvec_t* sv, void* item, size_t sz) { memcpy(sv->arr+(sv->vec_sz * sv->e_sz), item, sz); } - bool shitvec_check(shitvec_t* sv, void* item, int(cmp)(void*, void*)) { for (size_t i = 0; i < sv->vec_sz; i++) { if (cmp(sv->arr+(i*sv->e_sz), item) == 0) {