diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/stream.cpp | 48 | ||||
-rw-r--r-- | common/stream.h | 24 |
2 files changed, 71 insertions, 1 deletions
diff --git a/common/stream.cpp b/common/stream.cpp index e151c058ce..11cce2a4cd 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -34,12 +34,24 @@ byte ReadStream::readByte() { return b; } +int8 ReadStream::readSByte() { + int8 b = 0; + read(&b, 1); + return b; +} + uint16 ReadStream::readUint16LE() { uint16 a = readByte(); uint16 b = readByte(); return a | (b << 8); } +uint32 ReadStream::readUint24LE() { + uint32 a = readUint16LE(); + uint32 b = readByte(); + return (b << 16) | a; +} + uint32 ReadStream::readUint32LE() { uint32 a = readUint16LE(); uint32 b = readUint16LE(); @@ -52,6 +64,12 @@ uint16 ReadStream::readUint16BE() { return a | (b << 8); } +uint32 ReadStream::readUint24BE() { + uint32 b = readByte(); + uint32 a = readUint16BE(); + return (b << 16) | a; +} + uint32 ReadStream::readUint32BE() { uint32 b = readUint16BE(); uint32 a = readUint16BE(); @@ -63,6 +81,10 @@ int16 ReadStream::readSint16LE() { return (int16)readUint16LE(); } +int32 ReadStream::readSint24LE() { + return (int32)readUint24LE(); +} + int32 ReadStream::readSint32LE() { return (int32)readUint32LE(); } @@ -71,6 +93,10 @@ int16 ReadStream::readSint16BE() { return (int16)readUint16BE(); } +int32 ReadStream::readSint24BE() { + return (int32)readUint24BE(); +} + int32 ReadStream::readSint32BE() { return (int32)readUint32BE(); } @@ -81,11 +107,20 @@ void WriteStream::writeByte(byte value) { write(&value, 1); } +void WriteStream::writeSByte(int8 value) { + write(&value, 1); +} + void WriteStream::writeUint16LE(uint16 value) { writeByte((byte)(value & 0xff)); writeByte((byte)(value >> 8)); } +void WriteStream::writeUint24LE(uint32 value) { + writeUint16LE((uint16)(value & 0xffff)); + writeByte((byte)(value >> 16)); +} + void WriteStream::writeUint32LE(uint32 value) { writeUint16LE((uint16)(value & 0xffff)); writeUint16LE((uint16)(value >> 16)); @@ -96,6 +131,11 @@ void WriteStream::writeUint16BE(uint16 value) { writeByte((byte)(value & 0xff)); } +void WriteStream::writeUint24BE(uint32 value) { + writeByte((byte)(value >> 16)); + writeUint16BE((uint16)(value & 0xffff)); +} + void WriteStream::writeUint32BE(uint32 value) { writeUint16BE((uint16)(value >> 16)); writeUint16BE((uint16)(value & 0xffff)); @@ -106,6 +146,10 @@ void WriteStream::writeSint16LE(int16 value) { writeUint16LE((uint16)value); } +void WriteStream::writeSint24LE(int32 value) { + writeUint24LE((uint32)value); +} + void WriteStream::writeSint32LE(int32 value) { writeUint32LE((uint32)value); } @@ -114,6 +158,10 @@ void WriteStream::writeSint16BE(int16 value) { writeUint16BE((uint16)value); } +void WriteStream::writeSint24BE(int32 value) { + writeUint24BE((uint32)value); +} + void WriteStream::writeSint32BE(int32 value) { writeUint32BE((uint32)value); } diff --git a/common/stream.h b/common/stream.h index 2ab5d8b893..d30fa63a6d 100644 --- a/common/stream.h +++ b/common/stream.h @@ -36,17 +36,22 @@ public: // The remaining methods all have default implementations void writeByte(byte value); + void writeSByte(int8 value); void writeUint16LE(uint16 value); + void writeUint24LE(uint32 value); void writeUint32LE(uint32 value); void writeUint16BE(uint16 value); + void writeUint24BE(uint32 value); void writeUint32BE(uint32 value); void writeSint16LE(int16 value); + void writeSint24LE(int32 value); void writeSint32LE(int32 value); void writeSint16BE(int16 value); + void writeSint24BE(int32 value); void writeSint32BE(int32 value); }; @@ -58,17 +63,22 @@ public: // The remaining methods all have default implementations byte readByte(); + int8 readSByte(); uint16 readUint16LE(); + uint32 readUint24LE(); uint32 readUint32LE(); uint16 readUint16BE(); + uint32 readUint24BE(); uint32 readUint32BE(); int16 readSint16LE(); + int32 readSint24LE(); int32 readSint32LE(); int16 readSint16BE(); + int32 readSint24BE(); int32 readSint32BE(); }; @@ -109,9 +119,12 @@ public: class MemoryReadStream : public ReadStream { private: const byte *_ptr; + const byte *_ptrOrig; uint32 _size; + uint32 _sizeOrig; + uint32 _pos; public: - MemoryReadStream(const byte *ptr, uint32 size) : _ptr(ptr), _size(size) {} + MemoryReadStream(const byte *ptr, uint32 size) : _ptr(ptr), _ptrOrig(ptr), _size(size), _sizeOrig(size), _pos(0) {} uint32 read(void *ptr, uint32 size) { if (size > _size) @@ -119,8 +132,17 @@ public: memcpy(ptr, _ptr, size); _size -= size; _ptr += size; + _pos += size; return size; } + + uint32 tell() { return _pos; } + + void rewind() { + _ptr = _ptrOrig; + _size = _sizeOrig; + _pos = 0; + } }; } // End of namespace Common |