Skip to content

Commit

Permalink
Add support for windows builds
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
sidcha committed Feb 20, 2024
1 parent 06259c4 commit d447993
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
16 changes: 15 additions & 1 deletion include/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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));
}

Expand Down
8 changes: 6 additions & 2 deletions src/disjoint_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 4 additions & 2 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#if (!defined(_WIN32) && !defined(_WIN64))
#include <unistd.h>
#endif

#include <utils/logger.h>

Expand Down Expand Up @@ -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, ...)
{
Expand Down
4 changes: 2 additions & 2 deletions src/slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

#include <utils/slab.h>

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)
Expand Down
41 changes: 38 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <stddef.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#include <utils/utils.h>

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <Windows.h>
#include <stdint.h> // 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 <sys/time.h>
#include <time.h>
#endif

int64_t usec_now()
{
int64_t usec;
Expand Down

0 comments on commit d447993

Please sign in to comment.