* Refactor compression and utility

This commit is contained in:
Phillip Stephens 2014-09-09 03:19:19 -07:00
parent 814eda3d4c
commit 5a621215de
8 changed files with 194 additions and 28 deletions

View File

@ -25,15 +25,15 @@ namespace io
namespace Compression
{
// Zlib compression
atInt32 decompressZlib(atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen);
atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen);
atInt32 compressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen);
// lzo compression
atInt32 decompressLZO(atUint8* source, atInt32 sourceSize, atUint8* dest, atInt32& dstSize);
atInt32 decompressLZO(const atUint8* source, atInt32 sourceSize, atUint8* dest, atInt32& dstSize);
// Yaz0 encoding
atUint32 yaz0Decode(atUint8* src, atUint8* dst, atUint32 uncompressedSize);
atUint32 yaz0Encode(atUint8* src, atUint32 srcSize, atUint8* data);
atUint32 yaz0Decode(const atUint8* src, atUint8* dst, atUint32 uncompressedSize);
atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data);
}
}

View File

@ -26,9 +26,9 @@
# elif defined(__FUNCTION__)
# define __PRETTY_FUNCTION__ __FUNCTION__
# elif defined(__FUNC__)
# define __PRETTY_FUNCTION__ __FUNCTION__
# define __PRETTY_FUNCTION__ __FUNC__
# elif defined(__func__)
# define __PRETTY_FUNCTION__ __FUNCTION__
# define __PRETTY_FUNCTION__ __func__
# else
# define __PRETTY_FUNCTION__ "<unkown>"
# endif

View File

@ -34,9 +34,25 @@ atUint32 swapU32(atUint32 val);
atInt32 swap32 (atInt32 val );
atUint64 swapU64(atUint64 val);
atInt64 swap64 (atInt64 val);
float swapFloat(float val);
double swapDouble(double val);
atInt16 LittleInt16(atInt16& val);
atUint16 LittleUint16(atUint16& val);
atInt16 BigInt16(atInt16& val);
atUint16 BigUint16(atUint16& val);
atInt32 LittleInt32(atInt32& val);
atUint32 LittleUint32(atUint32& val);
atInt32 BigInt32(atInt32& val);
atUint32 BigUint32(atUint32& val);
atInt64 LittleInt64(atInt64& val);
atUint64 LittleUint64(atUint64& val);
atInt64 BigInt64(atInt64& val);
atUint16 BigUint64(atUint16& val);
float LittleFloat(float& val);
float BigFloat(float& val);
double LittleDouble(double& val);
double BigDouble(double& val);
bool isSystemBigEndian();
void fillRandom(atUint8 * rndArea, atUint64 count);

View File

@ -36,7 +36,7 @@ typedef unsigned char uint8_t;
extern "C" {
#endif
int lzo1x_decode(atUint8 *out, atInt32 *outlen, atUint8 *in, atInt32 *inlen);
int lzo1x_decode(atUint8 *out, atInt32 *outlen, const atUint8* in, atInt32 *inlen);
#ifdef __cplusplus
}

View File

@ -267,8 +267,11 @@ atInt16 BinaryReader::readInt16()
atInt16 ret = *(atInt16*)(m_data + m_position);
m_position += sizeof(atInt16);
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swap16(ret);
if (isBigEndian())
utility::BigInt16(ret);
else
utility::LittleInt16(ret);
return ret;
}
@ -293,8 +296,11 @@ atInt32 BinaryReader::readInt32()
atInt32 ret = *(atInt32*)(m_data + m_position);
m_position += 4;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swap32(ret);
if (isBigEndian())
utility::BigInt32(ret);
else
utility::LittleInt32(ret);
return ret;
}
@ -319,8 +325,11 @@ atInt64 BinaryReader::readInt64()
atInt64 ret = *(atInt64*)(m_data + m_position);
m_position += 8;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swap64(ret);
if (isBigEndian())
utility::BigInt64(ret);
else
utility::LittleInt64(ret);
return ret;
}
@ -345,8 +354,11 @@ float BinaryReader::readFloat()
float ret = *(float*)(m_data + m_position);
m_position += 4;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swapFloat(ret);
if (isBigEndian())
utility::BigFloat(ret);
else
utility::LittleFloat(ret);
return ret;
}
@ -366,9 +378,10 @@ double BinaryReader::readDouble()
double ret = *(double*)(m_data + m_position);
m_position += 8;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swapDouble(ret);
if (isBigEndian())
utility::BigDouble(ret);
else
utility::LittleDouble(ret);
return ret;
}

View File

@ -26,7 +26,7 @@ namespace io
namespace Compression
{
atInt32 decompressZlib(atUint8 *src, atUint32 srcLen, atUint8* dst, atUint32 dstLen)
atInt32 decompressZlib(const atUint8* src, atUint32 srcLen, atUint8* dst, atUint32 dstLen)
{
z_stream strm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
strm.total_in = strm.avail_in = srcLen;
@ -103,7 +103,7 @@ atInt32 compressZlib(const atUint8 *src, atUint32 srcLen, atUint8 *dst, atUint32
return ret;
}
atInt32 decompressLZO(atUint8* source, atInt32 sourceSize, atUint8* dest, atInt32& dstSize)
atInt32 decompressLZO(const atUint8* source, atInt32 sourceSize, atUint8* dest, atInt32& dstSize)
{
int size = dstSize;
int result = lzo1x_decode(dest, &size, source, &sourceSize);
@ -114,7 +114,7 @@ atInt32 decompressLZO(atUint8* source, atInt32 sourceSize, atUint8* dest, atInt3
//src points to the yaz0 source data (to the "real" source data, not at the header!)
//dst points to a buffer uncompressedSize bytes large (you get uncompressedSize from
//the second 4 bytes in the Yaz0 header).
atUint32 yaz0Decode(atUint8* src, atUint8* dst, atUint32 uncompressedSize)
atUint32 yaz0Decode(const atUint8* src, atUint8* dst, atUint32 uncompressedSize)
{
atUint32 srcPlace = 0, dstPlace = 0; //current read/write positions
@ -179,10 +179,10 @@ typedef struct
atUint32 srcPos, dstPos;
} yaz0_Ret;
atUint32 simpleEnc(atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos);
atUint32 nintendoEnc(atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos);
atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos);
atUint32 nintendoEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos);
atUint32 yaz0Encode(atUint8* src, atUint32 srcSize, atUint8* data)
atUint32 yaz0Encode(const atUint8* src, atUint32 srcSize, atUint8* data)
{
yaz0_Ret r = { 0, 0 };
atInt32 pos = 0;
@ -265,7 +265,7 @@ atUint32 yaz0Encode(atUint8* src, atUint32 srcSize, atUint8* data)
}
// a lookahead encoding scheme for ngc Yaz0
atUint32 nintendoEnc(atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos)
atUint32 nintendoEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos)
{
atUint32 numBytes = 1;
static atUint32 numBytes1;
@ -297,7 +297,7 @@ atUint32 nintendoEnc(atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPo
}
// simple and straight encoding scheme for Yaz0
atUint32 simpleEnc(atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos)
atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32 *pMatchPos)
{
int startPos = pos - 0x1000, j, i;
atUint32 numBytes = 1;

View File

@ -77,6 +77,143 @@ bool isSystemBigEndian()
return (*(atUint16*)test == 0xFEFF);
}
atInt16 LittleInt16(atInt16& val)
{
if (Athena::utility::isSystemBigEndian())
val = Athena::utility::swap16(val);
return val;
}
atUint16 LittleUint16(atUint16& val)
{
atInt16 ret = val;
LittleInt16(ret);
val = ret;
return val;
}
atInt16 BigInt16(atInt16& val)
{
if (!Athena::utility::isSystemBigEndian())
val = Athena::utility::swap16(val);
return val;
}
atUint16 BigUint16(atUint16& val)
{
atInt16 ret = val;
BigInt16(ret);
val = ret;
return val;
}
atInt32 LittleInt32(atInt32& val)
{
if (Athena::utility::isSystemBigEndian())
val = Athena::utility::swap32(val);
return val;
}
atUint32 LittleUint32(atUint32& val)
{
atInt32 ret = val;
LittleInt32(ret);
val = ret;
return val;
}
atInt32 BigInt32(atInt32& val)
{
if (!Athena::utility::isSystemBigEndian())
val = Athena::utility::swap32(val);
return val;
}
atUint32 BigUint32(atUint32& val)
{
atInt32 ret = val;
BigInt32(ret);
val = ret;
return val;
}
atInt64 LittleInt64(atInt64& val)
{
if (Athena::utility::isSystemBigEndian())
val = Athena::utility::swap64(val);
return val;
}
atUint64 LittleUint64(atUint64& val)
{
atInt64 ret = val;
LittleInt64(ret);
val = ret;
return val;
}
atInt64 BigInt64(atInt64& val)
{
if (!Athena::utility::isSystemBigEndian())
val = Athena::utility::swap64(val);
return val;
}
atUint64 BigUint64(atUint64& val)
{
atInt64 ret = val;
BigInt64(ret);
val = ret;
return val;
}
float LittleFloat(float& val)
{
if (Athena::utility::isSystemBigEndian())
val = Athena::utility::swapFloat(val);
return val;
}
float BigFloat(float& val)
{
if (!Athena::utility::isSystemBigEndian())
val = Athena::utility::swapFloat(val);
return val;
}
double LittleDouble(double& val)
{
if (Athena::utility::isSystemBigEndian())
val = Athena::utility::swapDouble(val);
return val;
}
double BigDouble(double& val)
{
if (!Athena::utility::isSystemBigEndian())
val = Athena::utility::swapDouble(val);
return val;
}
void fillRandom(atUint8 * rndArea, atUint64 count)
{
for(atUint64 i = 0; i < count; i++)

View File

@ -166,7 +166,7 @@ static void copy_backptr(LZOContext *c, int back, int cnt) {
* make sure all buffers are appropriately padded, in must provide
* LZO_INPUT_PADDING, out must provide LZO_OUTPUT_PADDING additional bytes
*/
int lzo1x_decode(atUint8 *out, atInt32 *outlen, atUint8 *in, atInt32 *inlen) {
int lzo1x_decode(atUint8 *out, atInt32 *outlen, const atUint8 *in, atInt32 *inlen) {
enum {COPY, BACKPTR} state = COPY;
atInt32 x;
LZOContext c;