diff options
| author | 2026-01-06 03:27:12 -0600 | |
|---|---|---|
| committer | 2026-01-06 03:27:12 -0600 | |
| commit | 4cf8b35097a131abcfc8e0d04d35294be13943ac (patch) | |
| tree | bb384a58c53d7b4ab9faf45e84677ae5cdb42ac1 /lib/include | |
initial commit
Diffstat (limited to 'lib/include')
| -rw-r--r-- | lib/include/arena.h | 28 | ||||
| -rw-r--r-- | lib/include/log.h | 52 | ||||
| -rw-r--r-- | lib/include/net.h | 8 | ||||
| -rw-r--r-- | lib/include/sha1.h | 42 |
4 files changed, 130 insertions, 0 deletions
diff --git a/lib/include/arena.h b/lib/include/arena.h new file mode 100644 index 0000000..fa1674b --- /dev/null +++ b/lib/include/arena.h @@ -0,0 +1,28 @@ +#ifndef VL_ARENA_H_INCLUDED +#define VL_ARENA_H_INCLUDED + +/* implements a basic arena allocator */ + +#include <stddef.h> +#include <stdarg.h> + +typedef struct vl__arena_tag vl_arena; + +/* returns NULL if an arena could not be allocated */ +vl_arena *vl_arena_new(size_t cap); + +/* aborts if the arena is overflowing */ +void *vl_arena_push(vl_arena *parena, size_t len); + +/* resets the arena (but does not free it) */ +void vl_arena_reset(vl_arena *parena); + +/* frees the arena */ +void vl_arena_free(vl_arena *parena); + +char *vl_arena_strdup(vl_arena *parena, const char *str); + +char *vl_arena_sprintf(vl_arena *parena, const char *fmt, ...); +char *vl_arena_vsprintf(vl_arena *parena, const char *fmt, va_list ap); + +#endif /* include guard */ diff --git a/lib/include/log.h b/lib/include/log.h new file mode 100644 index 0000000..a8549e0 --- /dev/null +++ b/lib/include/log.h @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <stdarg.h> + +#define LOG_TRACE 0u +#define LOG_DEBUG 1u +#define LOG_INFO 2u +#define LOG_WARN 3u +#define LOG_ERROR 4u + +void vl_log_setlevel(unsigned level); + +void vl_logv(unsigned level, const char *fmt, va_list args); +void vl_log(unsigned level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); + +#ifndef LOG_MIN_LEVEL +#define LOG_MIN_LEVEL LOG_TRACE +#endif + +#if LOG_TRACE >= LOG_MIN_LEVEL +#define LOG_TRACE_ENABLED +#define vl_trace(...) vl_log(LOG_TRACE, __VA_ARGS__) +#define vl_tracev(...) vl_logv(LOG_TRACE, __VA_ARGS__) +#else +#define vl_trace(...) +#define vl_tracev(...) +#endif + +#if LOG_DEBUG >= LOG_MIN_LEVEL +#define LOG_DEBUG_ENABLED +#define vl_debug(...) vl_log(LOG_DEBUG, __VA_ARGS__) +#define vl_debugv(...) vl_logv(LOG_DEBUG, __VA_ARGS__) +#else +#define vl_debug(...) +#define vl_debugv(...) +#endif + +#if LOG_INFO >= LOG_MIN_LEVEL +#define LOG_INFO_ENABLED +#define vl_info(...) vl_log(LOG_INFO, __VA_ARGS__) +#define vl_infov(...) vl_logv(LOG_INFO, __VA_ARGS__) +#else +#define vl_info(...) +#define vl_infov(...) +#endif + +#define LOG_WARN_ENABLED +#define vl_warn(...) vl_log(LOG_WARN, __VA_ARGS__) +#define vl_warnv(...) vl_logv(LOG_WARN, __VA_ARGS__) + +#define LOG_ERROR_ENABLED +#define vl_error(...) vl_log(LOG_ERROR, __VA_ARGS__) +#define vl_errorv(...) vl_logv(LOG_ERROR, __VA_ARGS__) diff --git a/lib/include/net.h b/lib/include/net.h new file mode 100644 index 0000000..d5f1482 --- /dev/null +++ b/lib/include/net.h @@ -0,0 +1,8 @@ +#ifndef VL_NET_H_INCLUDED +#define VL_NET_H_INCLUDED + +#include "arena.h" + +int vl_net_ensure_cached(vl_arena *arena, const char *url, const char *target_path); + +#endif diff --git a/lib/include/sha1.h b/lib/include/sha1.h new file mode 100644 index 0000000..05cedd4 --- /dev/null +++ b/lib/include/sha1.h @@ -0,0 +1,42 @@ +#ifndef VL_SHA1_H_INCLUDED +#define VL_SHA1_H_INCLUDED + +#include <stdint.h> +#include <stddef.h> + +#define VL_SHA1_STATELEN_32 ( 5u) /* 160 bits / 32 (bits per int) = 5 ints */ +#define VL_SHA1_CHUNKLEN_BYTES (64u) /* 512 bits / 8 (bits per byte) = 64 bytes */ + +#define VL_SHA1_DIGEST_BYTES (20u) +#define VL_SHA1_DIGEST_HEX_STRLEN (VL_SHA1_DIGEST_BYTES << 1) + +typedef struct { + /* struct fields are internal (struct size is ABI doe) */ + size_t vl_nchunk; + uint64_t vl_total; + uint32_t vl_state[VL_SHA1_STATELEN_32]; + uint8_t vl_chunk[VL_SHA1_CHUNKLEN_BYTES]; +} vl_sha1_st; + +typedef uint8_t vl_sha1[VL_SHA1_DIGEST_BYTES]; + +/* initializes a SHA1 state struct */ +void vl_sha1_init(vl_sha1_st *st); + +/* updates a SHA1 state struct with new data (must have been initialized with vl_sha1_init first) */ +void vl_sha1_update(vl_sha1_st *st, const void *data, size_t sz); + +/* finalizes a SHA1 state struct, writing the final digest to the second argument. the state struct + * is now invalid, and vl_sha1_init must be used on it again before it can be reused. */ +void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out); + +/* shortcut for hashing a buffer */ +void vl_sha1_buffer(vl_sha1 odigest, const void *data, size_t sz); + +/* converts digest bytes to a hex string (does NOT place a NUL byte) */ +void vl_sha1_encode(const vl_sha1 dig, char hex[VL_SHA1_DIGEST_HEX_STRLEN]); + +/* converts a hex string to digest bytes */ +int vl_sha1_decode(vl_sha1 odigest, const char hex[VL_SHA1_DIGEST_HEX_STRLEN]); + +#endif /* include guard */ |
