diff --git a/cdatapack/cdatapack.c b/cdatapack/cdatapack.c --- a/cdatapack/cdatapack.c +++ b/cdatapack/cdatapack.c @@ -18,8 +18,6 @@ #include #include #include -#include -#include #include #if defined(__linux__) @@ -38,6 +36,8 @@ #endif /* #if defined(__APPLE__) */ #include +#include "portability/inet.h" +#include "portability/unistd.h" #include "cdatapack.h" #include "buffer.h" @@ -47,11 +47,8 @@ /** * This is an exact representation of an index entry on disk. Do not consume * the fields directly, as they may need processing. - * - * NOTE: this uses gcc's __attribute__((packed)) syntax to indicate a packed - * data structure, which obviously has potential portability issues. */ -typedef struct _disk_index_entry_t { +PACKEDSTRUCT(typedef struct _disk_index_entry_t { uint8_t node[NODE_SZ]; // offset of the next element in the delta chain in the index file @@ -61,7 +58,7 @@ // file. data_offset_t data_offset; data_offset_t data_sz; -} __attribute__((packed)) disk_index_entry_t; +}) disk_index_entry_t; /** * This represents offsets into the index indicating the range of a fanout @@ -95,13 +92,13 @@ * NOTE: this uses gcc's __attribute__((packed)) syntax to indicate a packed * data structure, which obviously has potential portability issues. */ -typedef struct _disk_index_header_t { +PACKEDSTRUCT(typedef struct _disk_index_header_t { #define VERSION 0 uint8_t version; #define LARGE_FANOUT 0x80 uint8_t config; -} __attribute__((packed)) disk_index_header_t; +}) disk_index_header_t; static void unpack_disk_deltachunk( const disk_index_entry_t *disk_deltachunk, diff --git a/clib/portability/inet.h b/clib/portability/inet.h new file mode 100644 --- /dev/null +++ b/clib/portability/inet.h @@ -0,0 +1,12 @@ +#ifndef PORTABILITY_INET_H +#define PORTABILITY_INET_H + +#if defined(_MSC_VER) + #include + #pragma comment(lib, "Ws2_32.lib") +#else + #include +#endif + +#endif /* PORTABILITY_INET_H */ + diff --git a/clib/portability/portability.h b/clib/portability/portability.h --- a/clib/portability/portability.h +++ b/clib/portability/portability.h @@ -13,4 +13,11 @@ #define COMPOUND_LITERAL(typename_) (typename_) #endif /* #if defined(_MSC_VER) */ +#if defined(_MSC_VER) + #define PACKEDSTRUCT(__Declaration__) __pragma(pack(push, 1)) \ + __Declaration__ __pragma(pack(pop)) +#else + #define PACKEDSTRUCT(__Declaration__) __Declaration__ __attribute__((packed)) +#endif + #endif /* #ifndef PORTABILITY_PORTABILITY_H */ diff --git a/clib/portability/unistd.h b/clib/portability/unistd.h new file mode 100644 --- /dev/null +++ b/clib/portability/unistd.h @@ -0,0 +1,16 @@ +#ifndef PORTABILITY_UNISTD_H +#define PORTABILITY_UNISTD_H + +#if defined(_MSC_VER) + #include + /* MSVC's io.h header shows deprecation + warnings on these without underscore */ + #define lseek _lseek + #define open _open + #define close _close +#else + #include +#endif + +#endif /* PORTABILITY_UNISTD_H */ +