Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #148 from maralla/byteswap_bug
Browse files Browse the repository at this point in the history
fix i16 byte swap bug on mac platforms
  • Loading branch information
lxyu committed Jul 30, 2015
2 parents 3788d49 + 7aad72e commit a9a65d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
9 changes: 9 additions & 0 deletions tests/test_protocol_cybinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def test_read_i16():
assert 12345 == val


def test_byteswap_i16():
i = 128
b = TCyMemoryBuffer()
proto.write_val(b, TType.I16, i)
b.flush()
v = proto.read_val(b, TType.I16)
assert v == i


def test_write_i32():
b = TCyMemoryBuffer()
proto.write_val(b, TType.I32, 1234567890)
Expand Down
37 changes: 9 additions & 28 deletions thriftpy/protocol/cybin/endian_port.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@

#if defined(__APPLE__)
static inline int16_t bswap16(int16_t n) {
return (n << 8) | (n >> 8);
}

static inline int32_t bswap32(int32_t n) {
return ((n & 0xff000000) >> 24) | \
((n & 0x00ff0000) >> 8) | \
((n & 0x0000ff00) << 8) | \
((n & 0x000000ff) << 24);
}

static inline int64_t bswap64(int64_t n) {
return ((n & 0xff00000000000000ull) >> 56) | \
((n & 0x00ff000000000000ull) >> 40) | \
((n & 0x0000ff0000000000ull) >> 24) | \
((n & 0x000000ff00000000ull) >> 8) | \
((n & 0x00000000ff000000ull) << 8) | \
((n & 0x0000000000ff0000ull) << 24) | \
((n & 0x000000000000ff00ull) << 40) | \
((n & 0x00000000000000ffull) << 56);
}

#define htobe16(n) bswap16(n)
#define htobe32(n) bswap32(n)
#define htobe64(n) bswap64(n)
#define be16toh(n) bswap16(n)
#define be32toh(n) bswap32(n)
#define be64toh(n) bswap64(n)

#include <libkern/OSByteOrder.h>

#define htobe16(x) OSSwapHostToBigInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define be64toh(x) OSSwapBigToHostInt64(x)

#else

Expand Down

0 comments on commit a9a65d4

Please sign in to comment.