From c49058ffb738c87271e58a76a0ecf3814db1c4ac Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Thu, 13 Mar 2014 00:03:56 +0100 Subject: Reading is no longer little endian by default. It's now having explicit little endian accessors, and a mode flag that can be toggled. --- includes/Handle.h | 33 +++++++++++++++++++----- src/Handle.cc | 76 +++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 83 insertions(+), 26 deletions(-) diff --git a/includes/Handle.h b/includes/Handle.h index ba710b2..412c6c9 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -64,6 +64,14 @@ class Handle { virtual time_t getMTime(); virtual bool isPendingComplete() { return true; } + enum Endianness { + LITTLE_ENDIAN; + BIG_ENDIAN; + }; + + void setEndianness(Endianness endianness) { m_bigEndianMode = endianness == BIG_ENDIAN; } + Endianness getEndianness() { return m_bigEndianMode ? BIG_ENDIAN : LITTLE_ENDIAN; } + // helpers off64_t tell() { return rtell(); } void seek(off64_t offset, int whence = SEEK_SET) { rseek(offset, whence); } @@ -76,13 +84,22 @@ class Handle { Future readI16(); Future readI32(); Future readI64(); - Future readBEU16(); - Future readBEU32(); - Future readBEU64(); - Future readBEI16(); - Future readBEI32(); - Future readBEI64(); - Future writeU8(uint8_t); + + Future readBEU16(); + Future readBEU32(); + Future readBEU64(); + Future readBEI16(); + Future readBEI32(); + Future readBEI64(); + + Future readLEU16(); + Future readLEU32(); + Future readLEU64(); + Future readLEI16(); + Future readLEI32(); + Future readLEI64(); + + Future writeU8(uint8_t); Future writeU16(uint16_t); Future writeU32(uint32_t); Future writeU64(uint64_t); @@ -118,6 +135,8 @@ class Handle { std::atomic m_refCount; + bool m_bigEndianMode = false; + Handle(const Handle &) = delete; Handle & operator=(const Handle &) = delete; }; diff --git a/src/Handle.cc b/src/Handle.cc index 0d1fb15..e75edbb 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -115,28 +115,66 @@ Balau::Future genericRead(Balau::IO t) { template Balau::Future genericReadBE(Balau::IO t) { - std::shared_ptr b(new T); - int c = 0; - *b.get() = 0; - return Balau::Future([t, b, c]() mutable { - do { - uint8_t v = t->readU8().get(); - *b.get() <<= 8; - *b.get() += v; - c++; - } while (c < sizeof(T)); - return *b; - }); + std::shared_ptr b(new T); + int c = 0; + *b.get() = 0; + return Balau::Future([t, b, c]() mutable { + do { + uint8_t v = t->readU8().get(); + *b.get() <<= 8; + *b.get() += v; + c++; + } while (c < sizeof(T)); + return *b; + }); } Balau::Future Balau::Handle::readU8() { return genericRead (this); } -Balau::Future Balau::Handle::readU16() { return genericRead(this); } -Balau::Future Balau::Handle::readU32() { return genericRead(this); } -Balau::Future Balau::Handle::readU64() { return genericRead(this); } -Balau::Future Balau::Handle::readI8 () { return genericRead (this); } -Balau::Future Balau::Handle::readI16() { return genericRead (this); } -Balau::Future Balau::Handle::readI32() { return genericRead (this); } -Balau::Future Balau::Handle::readI64() { return genericRead (this); } +Balau::Future Balau::Handle::readI8() { return genericRead (this); } + +Balau::Future Balau::Handle::readU16() { + if (m_bigEndianMode) + return readBEU16(); + else + return readLEU16(); +} +Balau::Future Balau::Handle::readU32() { + if (m_bigEndianMode) + return readBEU32(); + else + return readLEU32(); +} +Balau::Future Balau::Handle::readU64() { + if (m_bigEndianMode) + return readBEU64(); + else + return readLEU64(); +} +Balau::Future Balau::Handle::readI16() { + if (m_bigEndianMode) + return readBEI16(); + else + return readLEI16(); +} +Balau::Future Balau::Handle::readI32() { + if (m_bigEndianMode) + return readBEI32(); + else + return readLEI32(); +} +Balau::Future Balau::Handle::readI64() { + if (m_bigEndianMode) + return readBEI64(); + else + return readLEI64(); +} + +Balau::Future Balau::Handle::readLEU16() { return genericRead(this); } +Balau::Future Balau::Handle::readLEU32() { return genericRead(this); } +Balau::Future Balau::Handle::readLEU64() { return genericRead(this); } +Balau::Future Balau::Handle::readLEI16() { return genericRead (this); } +Balau::Future Balau::Handle::readLEI32() { return genericRead (this); } +Balau::Future Balau::Handle::readLEI64() { return genericRead (this); } Balau::Future Balau::Handle::readBEU16() { return genericReadBE(this); } Balau::Future Balau::Handle::readBEU32() { return genericReadBE(this); } -- cgit v1.2.3