2015-08-23 23:58:07 +00:00
|
|
|
#include "IOStreams.hpp"
|
|
|
|
#include "CMemory.hpp"
|
|
|
|
|
2016-03-04 23:04:53 +00:00
|
|
|
namespace urde
|
2015-08-23 23:58:07 +00:00
|
|
|
{
|
2016-03-19 19:19:43 +00:00
|
|
|
/*!
|
|
|
|
* \brief CBitStreamReader::ReadBit
|
|
|
|
* Reads and decodes an encoded value from a bitstream.
|
|
|
|
* \param bitCount How many bits to read
|
|
|
|
* \return s32 The encoded value
|
|
|
|
*/
|
|
|
|
s32 CBitStreamReader::ReadEncoded(u32 bitCount)
|
2016-03-15 23:44:59 +00:00
|
|
|
{
|
2016-03-19 19:19:43 +00:00
|
|
|
s32 ret = 0;
|
|
|
|
if (bitCount < x20_bitOffset)
|
2016-03-15 23:44:59 +00:00
|
|
|
{
|
2016-03-19 19:19:43 +00:00
|
|
|
u32 diff = 0x20 - bitCount;
|
|
|
|
u32 baseVal = -1;
|
|
|
|
if (x20_bitOffset != 0x20)
|
|
|
|
baseVal = (1 << bitCount) - 1;
|
|
|
|
x20_bitOffset -= bitCount;
|
|
|
|
ret = baseVal & (x1c_val >> diff);
|
|
|
|
x1c_val <<= bitCount;
|
2016-03-15 23:44:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-19 19:19:43 +00:00
|
|
|
u32 diff = bitCount - x20_bitOffset;
|
|
|
|
u32 baseVal = -1;
|
|
|
|
if (x20_bitOffset != 32)
|
|
|
|
baseVal = (1 << x20_bitOffset) - 1;
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2016-03-20 04:10:29 +00:00
|
|
|
baseVal = (baseVal & (x1c_val >> (32 - x20_bitOffset))) << diff;
|
2016-03-19 19:19:43 +00:00
|
|
|
x20_bitOffset = 0;
|
2016-03-20 04:10:29 +00:00
|
|
|
readUBytesToBuf(&x1c_val, 4);
|
2016-03-19 19:19:43 +00:00
|
|
|
/* The game uses Big Endian, which doesn't work for us */
|
|
|
|
/* Little Endian sucks */
|
|
|
|
athena::utility::BigUint32(x1c_val);
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2016-03-19 19:19:43 +00:00
|
|
|
u32 baseVal2 = -1;
|
|
|
|
if (diff != 32)
|
|
|
|
baseVal2 = (1 << diff) - 1;
|
2016-03-15 23:44:59 +00:00
|
|
|
|
2016-03-19 19:19:43 +00:00
|
|
|
ret = baseVal | (baseVal2 & (x1c_val >> (32 - diff))) << x20_bitOffset;
|
2016-03-20 04:10:29 +00:00
|
|
|
x20_bitOffset = (4 << 3) - diff;
|
2016-03-19 19:19:43 +00:00
|
|
|
x1c_val <<= diff;
|
2016-03-15 23:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:10:29 +00:00
|
|
|
|
|
|
|
void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount)
|
|
|
|
{
|
|
|
|
if (x18_bitOffset >= bitCount)
|
|
|
|
{
|
|
|
|
int baseVal = -1;
|
|
|
|
if (bitCount != 32)
|
|
|
|
baseVal = (1 << bitCount) - 1;
|
|
|
|
x14_val |= (val & baseVal) << (x18_bitOffset - bitCount);
|
|
|
|
x18_bitOffset -= bitCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u32 diff = bitCount - x18_bitOffset;
|
|
|
|
u32 baseVal = -1;
|
|
|
|
if (x18_bitOffset != 32)
|
|
|
|
baseVal = (1 << x18_bitOffset) - 1;
|
|
|
|
|
|
|
|
x14_val |= (val >> diff) & baseVal;
|
|
|
|
x18_bitOffset = 0;
|
|
|
|
u32 tmp = x14_val;
|
|
|
|
athena::utility::BigUint32(tmp);
|
|
|
|
writeBytes(&tmp, 4);
|
|
|
|
|
|
|
|
u32 rem = 32 - diff;
|
|
|
|
baseVal = -1;
|
|
|
|
if (diff != 32)
|
|
|
|
baseVal = (1 << diff) - 1;
|
|
|
|
|
|
|
|
x14_val = (val & baseVal) << rem;
|
|
|
|
x18_bitOffset -= diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:58:07 +00:00
|
|
|
class CZipSupport
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void* Alloc(void*, u32 c, u32 n)
|
|
|
|
{
|
2015-08-26 06:38:45 +00:00
|
|
|
return new u8[c*n];
|
2015-08-23 23:58:07 +00:00
|
|
|
}
|
|
|
|
static void Free(void*, void* buf)
|
|
|
|
{
|
|
|
|
delete[] static_cast<u8*>(buf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CZipInputStream::CZipInputStream(std::unique_ptr<CInputStream>&& strm)
|
2015-08-26 06:38:45 +00:00
|
|
|
: x24_compBuf(new u8[4096]), x28_strm(std::move(strm))
|
2015-08-23 23:58:07 +00:00
|
|
|
{
|
|
|
|
x30_zstrm.next_in = x24_compBuf.get();
|
|
|
|
x30_zstrm.avail_in = 0;
|
|
|
|
x30_zstrm.zalloc = CZipSupport::Alloc;
|
|
|
|
x30_zstrm.zfree = CZipSupport::Free;
|
|
|
|
inflateInit(&x30_zstrm);
|
|
|
|
}
|
|
|
|
|
|
|
|
CZipInputStream::~CZipInputStream()
|
|
|
|
{
|
|
|
|
inflateEnd(&x30_zstrm);
|
|
|
|
}
|
|
|
|
|
|
|
|
atUint64 CZipInputStream::readUBytesToBuf(void *buf, atUint64 len)
|
|
|
|
{
|
|
|
|
x30_zstrm.next_out = (Bytef*)buf;
|
|
|
|
x30_zstrm.avail_out = len;
|
|
|
|
if (!x30_zstrm.avail_in)
|
|
|
|
{
|
|
|
|
atUint64 readSz = x28_strm->readUBytesToBuf(x24_compBuf.get(), 4096);
|
|
|
|
x30_zstrm.avail_in = readSz;
|
|
|
|
x30_zstrm.next_in = x24_compBuf.get();
|
|
|
|
}
|
|
|
|
inflate(&x30_zstrm, Z_NO_FLUSH);
|
|
|
|
return x30_zstrm.total_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|