From ab14508636f0fa9bf965986d393d1995cd27b484 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Tue, 9 Feb 2016 12:15:59 -0800 Subject: [PATCH] Use sysdep.h --- cpp/sysdep.h | 10 ++++++++-- js/decoder.h | 19 +++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cpp/sysdep.h b/cpp/sysdep.h index fa179ae..fe1d96a 100644 --- a/cpp/sysdep.h +++ b/cpp/sysdep.h @@ -36,6 +36,10 @@ typedef unsigned __int64 uint64_t; #include #endif +#if defined(__linux__) +#include +#endif + #ifdef _WIN32 #ifdef __cplusplus @@ -101,8 +105,10 @@ typedef unsigned __int64 uint64_t; # define _erlpack_be64(x) bswap_64(x) #elif defined(__DARWIN_OSSwapInt64) # define _erlpack_be64(x) __DARWIN_OSSwapInt64(x) +#elif defined(__linux__) +# define _erlpack_be64(x) be64toh(x) #else -#define _erlpack_be64(x) \ +# define _erlpack_be64(x) \ ( ((((uint64_t)x) << 56) ) | \ ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \ ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \ @@ -127,4 +133,4 @@ typedef unsigned __int64 uint64_t; do { uint64_t val = _erlpack_be64(num); memcpy(to, &val, 8); } while(0) -#endif /* sysdep.h */ \ No newline at end of file +#endif /* sysdep.h */ diff --git a/js/decoder.h b/js/decoder.h index 2c8aae6..21e9868 100644 --- a/js/decoder.h +++ b/js/decoder.h @@ -4,16 +4,7 @@ #include #include -#if defined(_WIN32) -// assuming _WIN32 is LE -#include -#define ntohs(x) (_byteswap_ushort(x)) -#define ntohl(x) (_byteswap_ulong(x)) -#define ntohll(x) (_byteswap_uint64(x)) -#elif defined(__linux__) -#include -#define ntohll(x) be64toh(x) -#endif +#include "../cpp/sysdep.h" using namespace v8; @@ -65,7 +56,7 @@ class Decoder { return 0; } - uint16_t val = ntohs(*reinterpret_cast(data + offset)); + uint16_t val = _erlpack_be16(*reinterpret_cast(data + offset)); offset += sizeof(uint16_t); return val; } @@ -76,7 +67,7 @@ class Decoder { return 0; } - uint32_t val = ntohl(*reinterpret_cast(data + offset)); + uint32_t val = _erlpack_be32(*reinterpret_cast(data + offset)); offset += sizeof(uint32_t); return val; } @@ -87,7 +78,7 @@ class Decoder { return 0; } - uint64_t val = ntohll(*reinterpret_cast(data + offset)); + uint64_t val = _erlpack_be64(*reinterpret_cast(data + offset)); offset += sizeof(val); return val; } @@ -309,7 +300,7 @@ class Decoder { unsigned long sourceSize = uncompressedSize; uint8_t* outBuffer = (uint8_t*)malloc(uncompressedSize); - const int ret = uncompress(outBuffer, &sourceSize, (const unsigned char*)(data + offset), size - offset); + const int ret = uncompress(outBuffer, &sourceSize, (const unsigned char*)(data + offset), (uLong)(size - offset)); offset += sourceSize; if (ret != Z_OK) {