This repository has been archived by the owner on May 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Binary.h
53 lines (41 loc) · 2.64 KB
/
Binary.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifdef COMPILE_64
#define Binary::readSignedShort(data) unpack("n", data)[1] << 48 >> 48
#define Binary::readSignedLShort(data) unpack("v", data)[1] << 48 >> 48
#define Binary::readInt(data) unpack("N", data)[1] << 32 >> 32
#define Binary::readLInt(data) unpack("V", data)[1] << 32 >> 32
#define Binary::writeLong(data) pack("NN", data >> 32, data & 0xFFFFFFFF)
#else
#ifdef COMPILE_32
#define Binary::readSignedShort(data) unpack("n", data)[1] << 16 >> 16
#define Binary::readSignedLShort(data) unpack("v", data)[1] << 16 >> 16
#define Binary::readInt(data) unpack("N", data)[1]
#define Binary::readLInt(data) unpack("V", data)[1]
#else
#define Binary::readSignedShort(data) (PHP_INT_SIZE === 8 ? unpack("n", data)[1] << 48 >> 48 : unpack("n", data)[1] << 16 >> 16)
#define Binary::readSignedLShort(data) (PHP_INT_SIZE === 8 ? unpack("v", data)[1] << 48 >> 48 : unpack("v", data)[1] << 16 >> 16)
#define Binary::readInt(data) (PHP_INT_SIZE === 8 ? unpack("N", data)[1] << 32 >> 32 : unpack("N", data)[1])
#define Binary::readLInt(data) (PHP_INT_SIZE === 8 ? unpack("V", data)[1] << 32 >> 32 : unpack("V", data)[1])
#endif
#endif
#define Binary::readTriad(data) unpack("N", "\x00" . data)[1]
#define Binary::writeTriad(data) substr(pack("N", data), 1)
#define Binary::readLTriad(data) unpack("V", data . "\x00")[1]
#define Binary::writeLTriad(data) substr(pack("V", data), 0, -1)
#define Binary::readBool(data) ord(data{0}) > 0 ? true : false
#define Binary::writeBool(data) chr(data ? 1 : 0)
#define Binary::readByte(data) ord(data)
#define Binary::writeByte(data) chr(data)
#define Binary::readShort(data) unpack("n", data)[1]
#define Binary::readLShort(data) unpack("v", data)[1]
#define Binary::writeShort(data) pack("n", data)
#define Binary::writeLShort(data) pack("v", data)
#define Binary::writeInt(data) pack("N", data)
#define Binary::writeLInt(data) pack("V", data)
#define Binary::readFloat(data) (ENDIANNESS === 0 ? unpack("f", data)[1] : unpack("f", strrev(data))[1])
#define Binary::writeFloat(data) (ENDIANNESS === 0 ? pack("f", data) : strrev(pack("f", data)))
#define Binary::readLFloat(data) (ENDIANNESS === 0 ? unpack("f", strrev(data))[1] : unpack("f", data)[1])
#define Binary::writeLFloat(data) (ENDIANNESS === 0 ? strrev(pack("f", data)) : pack("f", data))
#define Binary::readDouble(data) (ENDIANNESS === 0 ? unpack("d", data)[1] : unpack("d", strrev(data))[1])
#define Binary::writeDouble(data) (ENDIANNESS === 0 ? pack("d", data) : strrev(pack("d", data)))
#define Binary::readLDouble(data) (ENDIANNESS === 0 ? unpack("d", strrev(data))[1] : unpack("d", data)[1])
#define Binary::writeLDouble(data) (ENDIANNESS === 0 ? strrev(pack("d", data)) : pack("d", data))