forked from hkc/sfxd
1
0
Fork 0
sfxd/src/common.h

49 lines
1.2 KiB
C
Raw Normal View History

2024-02-21 15:36:58 +03:00
#ifndef _SFXD_COMMON_H_
#define _SFXD_COMMON_H_
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 8192
#define DAEMON_SOCKET_PATH "/tmp/sfxd-ipc"
#define CLIENT_SOCKET_PATH "/tmp/sfxd-client-%ld"
#define PANIC_FMT(FMT, ...) _panic(__LINE__, __FILE__, errno, FMT, __VA_ARGS__)
#define PANIC(TXT) _panic(__LINE__, __FILE__, errno, TXT)
#define EXPECT(CODE, CONDITION) { \
ssize_t __res = (CODE); \
if (!(__res CONDITION)) \
PANIC_FMT("%s failed condition: %zd %s", #CODE, __res, #CONDITION); \
}
static inline void _panic(int line, const char *filename, int _errno, const char *fmt, ...) {
fprintf(stderr, "Panic at %s:%d\nMessage: ", filename, line);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\nerrno: %d (%s)\n", _errno, strerror(_errno));
exit(EXIT_FAILURE);
}
static inline uint32_t adler32(const void *buf, size_t buflength) {
const uint8_t *buffer = (const uint8_t*)buf;
uint32_t s1 = 1;
uint32_t s2 = 0;
for (size_t n = 0; n < buflength; n++) {
s1 = (s1 + buffer[n]) % 65521;
s2 = (s2 + s1) % 65521;
}
return (s2 << 16) | s1;
}
#endif