From f01d4c5aa84d80bac1355f8b709f4e1244fec3dc Mon Sep 17 00:00:00 2001 From: Tomas Jakobsson Date: Sun, 30 Dec 2012 19:33:59 +0100 Subject: COMMON: Add PackBitsReadStream to iff_container --- common/iff_container.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'common/iff_container.cpp') diff --git a/common/iff_container.cpp b/common/iff_container.cpp index 7bcbf86e0f..ffaa5c6d06 100644 --- a/common/iff_container.cpp +++ b/common/iff_container.cpp @@ -22,6 +22,7 @@ #include "common/iff_container.h" #include "common/substream.h" +#include "common/util.h" namespace Common { @@ -75,4 +76,50 @@ void IFFParser::parse(IFFCallback &callback) { } while (!stop); } + +PackBitsReadStream::PackBitsReadStream(Common::ReadStream &input) : _input(&input) { +} + +PackBitsReadStream::~PackBitsReadStream() { +} + +bool PackBitsReadStream::eos() const { + return _input->eos(); +} + +uint32 PackBitsReadStream::read(void *dataPtr, uint32 dataSize) { + byte *out = (byte *)dataPtr; + uint32 left = dataSize; + + uint32 lenR = 0, lenW = 0; + while (left > 0 && !_input->eos()) { + lenR = _input->readByte(); + + if (lenR == 128) { + // no-op + lenW = 0; + } else if (lenR <= 127) { + // literal run + lenR++; + lenW = MIN(lenR, left); + for (uint32 j = 0; j < lenW; j++) { + *out++ = _input->readByte(); + } + for (; lenR > lenW; lenR--) { + _input->readByte(); + } + } else { // len > 128 + // expand run + lenW = MIN((256 - lenR) + 1, left); + byte val = _input->readByte(); + memset(out, val, lenW); + out += lenW; + } + + left -= lenW; + } + + return dataSize - left; +} + } // End of namespace Common -- cgit v1.2.3 From b4d0a8ba66e2c99949d1fa14d801c7de77db76ba Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 26 Jan 2013 19:33:27 +0100 Subject: JANITORIAL: Enforce "} // End of namespace" with a single space after }. --- common/iff_container.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/iff_container.cpp') diff --git a/common/iff_container.cpp b/common/iff_container.cpp index ffaa5c6d06..9c6e5f124a 100644 --- a/common/iff_container.cpp +++ b/common/iff_container.cpp @@ -122,4 +122,4 @@ uint32 PackBitsReadStream::read(void *dataPtr, uint32 dataSize) { return dataSize - left; } -} // End of namespace Common +} // End of namespace Common -- cgit v1.2.3