From d447993a135cff73dcd16294f78c0e4a7f3e040f Mon Sep 17 00:00:00 2001 From: Siddharth Date: Thu, 15 Feb 2024 18:30:04 +0100 Subject: [PATCH] Add support for windows builds For allowing python and rust users to develop OSDP apps on windows, we need to fix this repo in multiple places. Some of it looks uglier than it ought to be but that's the cost of usability :( Signed-off-by: Siddharth --- include/utils/utils.h | 16 +++++++++++++++- src/disjoint_set.c | 8 ++++++-- src/logger.c | 6 ++++-- src/slab.c | 4 ++-- src/utils.c | 41 ++++++++++++++++++++++++++++++++++++++--- 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/include/utils/utils.h b/include/utils/utils.h index 354147f..5ee309f 100644 --- a/include/utils/utils.h +++ b/include/utils/utils.h @@ -119,9 +119,23 @@ extern "C" { #define __packed __attribute__((__packed__)) #endif +#ifdef __GNUC__ +#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__)) +#endif + +#ifdef _MSC_VER +#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop)) +#endif + #undef __weak #define __weak __attribute__((weak)) +#if (defined(_WIN32) || defined(_WIN64)) +#define __format_printf(x, y) +#else +#define __format_printf(x, y) __attribute__((format(printf, x, y))) +#endif + /** * @brief Return random number between 0 and `limit` both inclusive. * @@ -193,7 +207,7 @@ void dump_trace(void); static inline bool char_is_space(int c) { - unsigned char d = c - 9; + unsigned char d = (unsigned char)(c - 9); return (0x80001FU >> (d & 31)) & (1U >> (d >> 5)); } diff --git a/src/disjoint_set.c b/src/disjoint_set.c index f5dabf8..08c7d0e 100644 --- a/src/disjoint_set.c +++ b/src/disjoint_set.c @@ -49,8 +49,12 @@ void disjoint_set_union(struct disjoint_set *set, int a, int b) if (a == b) return; - if (set->rank[a] < set->rank[b]) - SWAP(a, b); + if (set->rank[a] < set->rank[b]) { + int tmp; + tmp = a; + a = b; + b = tmp; + } set->parent[b] = a; if (set->rank[a] == set->rank[b]) diff --git a/src/logger.c b/src/logger.c index 0f53f25..2e9a48a 100644 --- a/src/logger.c +++ b/src/logger.c @@ -6,10 +6,12 @@ #include #include #include -#include #include #include #include +#if (!defined(_WIN32) && !defined(_WIN64)) +#include +#endif #include @@ -98,7 +100,7 @@ static int terminate_log_line(char *buf, int len) #define LOG_BUF_LEN 192 -__attribute__((format(printf, 5, 6))) +__format_printf(5, 6) int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long line, const char *fmt, ...) { diff --git a/src/slab.c b/src/slab.c index 0a621b8..c8d86e4 100644 --- a/src/slab.c +++ b/src/slab.c @@ -10,11 +10,11 @@ #include -struct slab_unit { +PACK(struct slab_unit { uint32_t leased; uint32_t canary; uint8_t data[]; -} __packed; +}); int slab_init(slab_t *slab, size_t slab_size, uint8_t *blob, size_t blob_size) diff --git a/src/utils.c b/src/utils.c index 01a4ac6..72e3147 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,8 +9,6 @@ #include #include #include -#include -#include #include @@ -49,7 +47,7 @@ int num_digits_in_number(int num) return digits; } -__attribute__((format(printf, 3, 4))) +__format_printf(3, 4) void hexdump(const void *p, size_t len, const char *fmt, ...) { size_t i; @@ -87,6 +85,43 @@ void hexdump(const void *p, size_t len, const char *fmt, ...) printf("\n"); } +#if (defined(_WIN32) || defined(_WIN64)) +#define WIN32_LEAN_AND_MEAN +#include +#include // portable: uint64_t MSVC: __int64 + +// MSVC defines this in winsock2.h!? +typedef struct timeval { + long tv_sec; + long tv_usec; +} timeval; + +int gettimeofday(struct timeval * tp, struct timezone * tzp) +{ + // Note: some broken versions only have 8 trailing zero's, the correct + // epoch has 9 trailing zero's This magic number is the number of 100 + // nanosecond intervals since January 1, 1601 (UTC) until 00:00:00 + // January 1, 1970 + static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL); + + SYSTEMTIME system_time; + FILETIME file_time; + uint64_t time; + + GetSystemTime( &system_time ); + SystemTimeToFileTime( &system_time, &file_time ); + time = ((uint64_t)file_time.dwLowDateTime ) ; + time += ((uint64_t)file_time.dwHighDateTime) << 32; + + tp->tv_sec = (long) ((time - EPOCH) / 10000000L); + tp->tv_usec = (long) (system_time.wMilliseconds * 1000); + return 0; +} +#else +#include +#include +#endif + int64_t usec_now() { int64_t usec;