From c58f7146102be3aac2cef5d69bdceb109da3f09f Mon Sep 17 00:00:00 2001 From: Adrian Astley Date: Tue, 16 Sep 2014 13:49:27 -0500 Subject: COMMON: Add support for endian-safe reading/writing of int64 --- common/stream.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'common/stream.h') diff --git a/common/stream.h b/common/stream.h index 2702068cf3..5655f0fcb4 100644 --- a/common/stream.h +++ b/common/stream.h @@ -125,6 +125,11 @@ public: write(&value, 4); } + void writeUint64LE(uint64 value) { + value = TO_LE_64(value); + write(&value, 8); + } + void writeUint16BE(uint16 value) { value = TO_BE_16(value); write(&value, 2); @@ -135,6 +140,11 @@ public: write(&value, 4); } + void writeUint64BE(uint64 value) { + value = TO_BE_64(value); + write(&value, 8); + } + FORCEINLINE void writeSint16LE(int16 value) { writeUint16LE((uint16)value); } @@ -143,6 +153,10 @@ public: writeUint32LE((uint32)value); } + FORCEINLINE void writeSint64LE(int64 value) { + writeUint64LE((uint64)value); + } + FORCEINLINE void writeSint16BE(int16 value) { writeUint16BE((uint16)value); } @@ -151,6 +165,10 @@ public: writeUint32BE((uint32)value); } + FORCEINLINE void writeSint64BE(int64 value) { + writeUint64BE((uint64)value); + } + /** * Write the given string to the stream. * This writes str.size() characters, but no terminating zero byte. @@ -241,6 +259,19 @@ public: return FROM_LE_32(val); } + /** + * Read an unsigned 64-bit word stored in little endian (LSB first) order + * from the stream and return it. + * Performs no error checking. The return value is undefined + * if a read error occurred (for which client code can check by + * calling err() and eos() ). + */ + uint32 readUint64LE() { + uint64 val; + read(&val, 8); + return FROM_LE_64(val); + } + /** * Read an unsigned 16-bit word stored in big endian (MSB first) order * from the stream and return it. @@ -267,6 +298,19 @@ public: return FROM_BE_32(val); } + /** + * Read an unsigned 64-bit word stored in big endian (MSB first) order + * from the stream and return it. + * Performs no error checking. The return value is undefined + * if a read error occurred (for which client code can check by + * calling err() and eos() ). + */ + uint32 readUint64BE() { + uint64 val; + read(&val, 8); + return FROM_BE_64(val); + } + /** * Read a signed 16-bit word stored in little endian (LSB first) order * from the stream and return it. @@ -289,6 +333,17 @@ public: return (int32)readUint32LE(); } + /** + * Read a signed 64-bit word stored in little endian (LSB first) order + * from the stream and return it. + * Performs no error checking. The return value is undefined + * if a read error occurred (for which client code can check by + * calling err() and eos() ). + */ + FORCEINLINE int64 readSint64LE() { + return (int64)readUint64LE(); + } + /** * Read a signed 16-bit word stored in big endian (MSB first) order * from the stream and return it. @@ -311,6 +366,17 @@ public: return (int32)readUint32BE(); } + /** + * Read a signed 64-bit word stored in big endian (MSB first) order + * from the stream and return it. + * Performs no error checking. The return value is undefined + * if a read error occurred (for which client code can check by + * calling err() and eos() ). + */ + FORCEINLINE int64 readSint64BE() { + return (int64)readUint64BE(); + } + /** * Read the specified amount of data into a malloc'ed buffer * which then is wrapped into a MemoryReadStream. -- cgit v1.2.3 From d497b45b1c8e13a67946522927dcbbcbc795af9a Mon Sep 17 00:00:00 2001 From: Adrian Astley Date: Sun, 21 Dec 2014 03:58:19 -0600 Subject: COMMON: Fix typo that caused uint64 reads to return a uint32 --- common/stream.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common/stream.h') diff --git a/common/stream.h b/common/stream.h index 5655f0fcb4..251995001c 100644 --- a/common/stream.h +++ b/common/stream.h @@ -266,7 +266,7 @@ public: * if a read error occurred (for which client code can check by * calling err() and eos() ). */ - uint32 readUint64LE() { + uint64 readUint64LE() { uint64 val; read(&val, 8); return FROM_LE_64(val); @@ -305,7 +305,7 @@ public: * if a read error occurred (for which client code can check by * calling err() and eos() ). */ - uint32 readUint64BE() { + uint64 readUint64BE() { uint64 val; read(&val, 8); return FROM_BE_64(val); -- cgit v1.2.3 From 7865c7821140a2b83a1473c66e90644cbf39a2c9 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Sun, 4 Jan 2015 20:47:12 +0100 Subject: COMMON: Put more 64-bit stuff under HAVE_INT64 --- common/stream.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'common/stream.h') diff --git a/common/stream.h b/common/stream.h index 251995001c..3021df304c 100644 --- a/common/stream.h +++ b/common/stream.h @@ -125,10 +125,12 @@ public: write(&value, 4); } +#ifdef HAVE_INT64 void writeUint64LE(uint64 value) { value = TO_LE_64(value); write(&value, 8); } +#endif void writeUint16BE(uint16 value) { value = TO_BE_16(value); @@ -140,10 +142,12 @@ public: write(&value, 4); } +#ifdef HAVE_INT64 void writeUint64BE(uint64 value) { value = TO_BE_64(value); write(&value, 8); } +#endif FORCEINLINE void writeSint16LE(int16 value) { writeUint16LE((uint16)value); @@ -153,9 +157,11 @@ public: writeUint32LE((uint32)value); } +#ifdef HAVE_INT64 FORCEINLINE void writeSint64LE(int64 value) { writeUint64LE((uint64)value); } +#endif FORCEINLINE void writeSint16BE(int16 value) { writeUint16BE((uint16)value); @@ -165,9 +171,11 @@ public: writeUint32BE((uint32)value); } +#ifdef HAVE_INT64 FORCEINLINE void writeSint64BE(int64 value) { writeUint64BE((uint64)value); } +#endif /** * Write the given string to the stream. @@ -259,6 +267,7 @@ public: return FROM_LE_32(val); } +#ifdef HAVE_INT64 /** * Read an unsigned 64-bit word stored in little endian (LSB first) order * from the stream and return it. @@ -271,6 +280,7 @@ public: read(&val, 8); return FROM_LE_64(val); } +#endif /** * Read an unsigned 16-bit word stored in big endian (MSB first) order @@ -298,6 +308,7 @@ public: return FROM_BE_32(val); } +#ifdef HAVE_INT64 /** * Read an unsigned 64-bit word stored in big endian (MSB first) order * from the stream and return it. @@ -310,6 +321,7 @@ public: read(&val, 8); return FROM_BE_64(val); } +#endif /** * Read a signed 16-bit word stored in little endian (LSB first) order @@ -333,6 +345,7 @@ public: return (int32)readUint32LE(); } +#ifdef HAVE_INT64 /** * Read a signed 64-bit word stored in little endian (LSB first) order * from the stream and return it. @@ -343,6 +356,7 @@ public: FORCEINLINE int64 readSint64LE() { return (int64)readUint64LE(); } +#endif /** * Read a signed 16-bit word stored in big endian (MSB first) order @@ -366,6 +380,7 @@ public: return (int32)readUint32BE(); } +#ifdef HAVE_INT64 /** * Read a signed 64-bit word stored in big endian (MSB first) order * from the stream and return it. @@ -376,6 +391,7 @@ public: FORCEINLINE int64 readSint64BE() { return (int64)readUint64BE(); } +#endif /** * Read the specified amount of data into a malloc'ed buffer -- cgit v1.2.3 From eb4d1a69255b06f93c497aabd9cf950bfe0756ec Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 4 Jan 2015 21:07:36 +0100 Subject: COMMON: Add missing readUint64/readSint64 to ReadStreamEndian. --- common/stream.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'common/stream.h') diff --git a/common/stream.h b/common/stream.h index 3021df304c..abe5192b70 100644 --- a/common/stream.h +++ b/common/stream.h @@ -517,6 +517,14 @@ public: return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val); } +#ifdef HAVE_INT64 + uint64 readUint64() { + uint64 val; + read(&val, 8); + return (_bigEndian) ? TO_BE_64(val) : TO_LE_64(val); + } +#endif + FORCEINLINE int16 readSint16() { return (int16)readUint16(); } @@ -524,6 +532,12 @@ public: FORCEINLINE int32 readSint32() { return (int32)readUint32(); } + +#ifdef HAVE_INT64 + FORCEINLINE int64 readSint64() { + return (int64)readUint64(); + } +#endif }; /** -- cgit v1.2.3