bplist.c
#ifndef _MSC_VER
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#else
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
#endif
2.get_unaligned的win适配
#ifdef _MSC_VER
uint64_t get_unaligned_64(uint64_t *ptr)
{
uint64_t temp;
memcpy(&temp, ptr, sizeof(temp));
return temp;
}
uint32_t get_unaligned_32(uint32_t *ptr)
{
uint32_t temp;
memcpy(&temp, ptr, sizeof(temp));
return temp;
}
uint16_t get_unaligned_16(uint16_t *ptr)
{
uint16_t temp;
memcpy(&temp, ptr, sizeof(temp));
return temp;
}
#else
#define get_unaligned(ptr) \
({ \
struct __attribute__((packed)) { \
typeof(*(ptr)) __v; \
} *__p = (void *) (ptr); \
__p->__v; \
})
#endif
3.UINT_TO_HOST的vs适配
#ifdef _MSC_VER
uint64_t UINT_TO_HOST(void* x, uint8_t n)
{
union plist_uint_ptr __up;
// Adding to void* is a GCC extension, see http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Pointer-Arith.html
__up.src = (n > 8) ? (char *)x + (n - 8) : x;
return (n >= 8 ? be64toh(get_unaligned_64(__up.u64ptr)) :
(n == 4 ? be32toh(get_unaligned_32(__up.u32ptr)) :
(n == 2 ? be16toh(get_unaligned_16(__up.u16ptr)) :
(n == 1 ? *__up.u8ptr :
beNtoh(get_unaligned_64(__up.u64ptr), n)
))));
}
#else
#define UINT_TO_HOST(x, n) \
({ \
union plist_uint_ptr __up; \
__up.src = (n > 8) ? x + (n - 8) : x; \
(n >= 8 ? be64toh( get_unaligned(__up.u64ptr) ) : \
(n == 4 ? be32toh( get_unaligned(__up.u32ptr) ) : \
(n == 2 ? be16toh( get_unaligned(__up.u16ptr) ) : \
(n == 1 ? *__up.u8ptr : \
beNtoh( get_unaligned(__up.u64ptr), n) \
)))); \
})
#endif
4.plist.h的接口声明
#ifdef _MSC_VER
#define PLIST_API_MSC __declspec( dllexport )
#else
#define PLIST_API_MSC
#endif