From 451b1d1b6b4943f4df5bc7c509bd14ceefe017d1 Mon Sep 17 00:00:00 2001 From: depsterr Date: Sat, 11 Dec 2021 14:26:39 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ Makefile | 18 ++++++++++ src/dbg.h | 31 +++++++++++++++++ src/main.c | 5 +++ src/util.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 15 +++++++++ 6 files changed, 168 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/dbg.h create mode 100644 src/main.c create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9bb5861 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +secd diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..85ca0a5 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.POSIX: +.PHONY: all clean +DEPS=src/main.o src/util.o + +all: secd + +secd: $(DEPS) + $(CC) $(CFLAGS) $(DEPS) -o secd + +run: secd + @./secd + +clean: + rm -f $(DEPS) + +.SUFFIXES: .c .o +.c.o: + $(CC) -c $(CFLAGS) $< -o $@ diff --git a/src/dbg.h b/src/dbg.h new file mode 100644 index 0000000..fc9b72b --- /dev/null +++ b/src/dbg.h @@ -0,0 +1,31 @@ +#ifndef __DBG_H +#define __DBG_H + +#include + +#ifdef ENABLE_DBG_H + +# define DEBUG(fmt, ...) fprintf(stderr, "[%s:%s:%d]: " fmt "\n", \ + __FILE__, __func__, __LINE__, __VA_ARGS__) + +# define VALFMTS "[%s:%s():%d] %s: " +# define VALFMTP "\n" + +# define VAL(X) fprintf(stderr, _Generic((X), \ + char: VALFMTS "%c" VALFMTP, \ + unsigned char: VALFMTS "%hhu" VALFMTP, \ + short: VALFMTS "%h" VALFMTP, \ + unsigned short: VALFMTS "%hu" VALFMTP, \ + int: VALFMTS "%d" VALFMTP, \ + unsigned int: VALFMTS "%u" VALFMTP, \ + long: VALFMTS "%l" VALFMTP, \ + unsigned long: VALFMTS "%lu" VALFMTP, \ + char*: VALFMTS "'%s'" VALFMTP, \ + default: VALFMTS "0x%x" VALFMTP\ + ), __FILE__, __func__, __LINE__, #X, X) +#else +# define DEBUG(...) +# define VAL(_) +#endif + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..5cc36eb --- /dev/null +++ b/src/main.c @@ -0,0 +1,5 @@ +#include +#include "dbg.h" + +int main(int argc, char** argv) { +} diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..9b16e60 --- /dev/null +++ b/src/util.c @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "dbg.h" +#include "util.h" + +#ifdef POSIX +#include +#include +#include +#endif + +// print message to stderr and then exit +void die(const char* s) { + fprintf(stderr, "FATAL ERROR: %s.\n", s); + exit(1); +} + +// print perror message to stderr and then exit +void diePerror(const char* s) { + perror(s); + exit(1); +} + +// malloc which dies on failure +void* xmalloc(size_t bytes) { + void* p = malloc(bytes); + GUARD(p); + return p; +} + +// realloc which dies on failure +void* xrealloc(void* p, size_t bytes) { + p = realloc(p, bytes); + GUARD(p); + return p; +} + +// free which is garantueed null pointer safe +void xfree(void* p) { + if (p) { + free(p); + } +} + +// read the file at the given path and return a byte buffer +// pointing to the contents of the file, the length of which +// is stored in rsize +uint8_t* readFile(char* path, size_t* rsize) { + size_t size = 0; + uint8_t* buf = 0; +#ifdef POSIX + int fd = 0; + + fd = open(path, O_RDONLY); + SGUARD(fd); + + size = lseek(fd, 0, SEEK_END); + SGUARD(size); + + buf = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0); + SGUARD(buf); + + SGUARD(close(fd)); +#else + FILE* fp = 0; + + fp = fopen(path, "rb"); + GUARD(fp); + + GUARD(fseek(fp, 0, SEEK_END)); + + size = ftell(fp); + + rewind(fp); + + buf = xmalloc(size); + + GUARD(fread(buf, 1, size, fp) == size); + + GUARD(fclose(fp)); +#endif + + *rsize = size; + + return buf; +} + +// free read file +void freeFile(uint8_t* buf, size_t size) { +#ifdef POSIX + SGUARD(munmap(buf, size)); +#else + xfree(buf); +#endif +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..01c2700 --- /dev/null +++ b/src/util.h @@ -0,0 +1,15 @@ +#ifndef _UTIL_H +#define _UTIL_H + +#define GUARD(p) if (!p) { diePerror(__func__); } +#define SGUARD(p) if (p < 0) { diePerror(__func__); } + +#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) +#define POSIX +#endif + +#ifdef __WIN32__ +#define WINDOWS +#endif + +#endif