Skip to content

Commit

Permalink
Use sysdep.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Marsh committed Feb 9, 2016
1 parent 03d300c commit ab14508
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
10 changes: 8 additions & 2 deletions cpp/sysdep.h
Expand Up @@ -36,6 +36,10 @@ typedef unsigned __int64 uint64_t;
#include <stdbool.h>
#endif

#if defined(__linux__)
#include <endian.h>
#endif

#ifdef _WIN32

#ifdef __cplusplus
Expand Down Expand Up @@ -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 ) | \
Expand All @@ -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 */
#endif /* sysdep.h */
19 changes: 5 additions & 14 deletions js/decoder.h
Expand Up @@ -4,16 +4,7 @@
#include <zlib.h>
#include <cstdio>

#if defined(_WIN32)
// assuming _WIN32 is LE
#include <cstdlib>
#define ntohs(x) (_byteswap_ushort(x))
#define ntohl(x) (_byteswap_ulong(x))
#define ntohll(x) (_byteswap_uint64(x))
#elif defined(__linux__)
#include <endian.h>
#define ntohll(x) be64toh(x)
#endif
#include "../cpp/sysdep.h"

using namespace v8;

Expand Down Expand Up @@ -65,7 +56,7 @@ class Decoder {
return 0;
}

uint16_t val = ntohs(*reinterpret_cast<const uint16_t*>(data + offset));
uint16_t val = _erlpack_be16(*reinterpret_cast<const uint16_t*>(data + offset));
offset += sizeof(uint16_t);
return val;
}
Expand All @@ -76,7 +67,7 @@ class Decoder {
return 0;
}

uint32_t val = ntohl(*reinterpret_cast<const uint32_t*>(data + offset));
uint32_t val = _erlpack_be32(*reinterpret_cast<const uint32_t*>(data + offset));
offset += sizeof(uint32_t);
return val;
}
Expand All @@ -87,7 +78,7 @@ class Decoder {
return 0;
}

uint64_t val = ntohll(*reinterpret_cast<const uint64_t*>(data + offset));
uint64_t val = _erlpack_be64(*reinterpret_cast<const uint64_t*>(data + offset));
offset += sizeof(val);
return val;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ab14508

Please sign in to comment.