From 5a621215deca6439ce5ccaabdf4cbea6c4079c71 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Tue, 9 Sep 2014 03:19:19 -0700 Subject: [PATCH] * Refactor compression and utility --- include/Athena/Compression.hpp | 8 +- include/Athena/Global.hpp | 4 +- include/Athena/Utility.hpp | 18 ++++- include/lzo.h | 2 +- src/Athena/BinaryReader.cpp | 35 ++++++--- src/Athena/Compression.cpp | 16 ++-- src/Athena/Utility.cpp | 137 +++++++++++++++++++++++++++++++++ src/lzo.c | 2 +- 8 files changed, 194 insertions(+), 28 deletions(-) diff --git a/include/Athena/Compression.hpp b/include/Athena/Compression.hpp index e92e843..02394c5 100644 --- a/include/Athena/Compression.hpp +++ b/include/Athena/Compression.hpp @@ -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); } } diff --git a/include/Athena/Global.hpp b/include/Athena/Global.hpp index da3d5e8..cb67236 100644 --- a/include/Athena/Global.hpp +++ b/include/Athena/Global.hpp @@ -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__ "" # endif diff --git a/include/Athena/Utility.hpp b/include/Athena/Utility.hpp index 42a53fb..3219dfc 100644 --- a/include/Athena/Utility.hpp +++ b/include/Athena/Utility.hpp @@ -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); diff --git a/include/lzo.h b/include/lzo.h index d702f9a..8d917dd 100644 --- a/include/lzo.h +++ b/include/lzo.h @@ -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 } diff --git a/src/Athena/BinaryReader.cpp b/src/Athena/BinaryReader.cpp index 598448f..7c0f04e 100644 --- a/src/Athena/BinaryReader.cpp +++ b/src/Athena/BinaryReader.cpp @@ -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; } diff --git a/src/Athena/Compression.cpp b/src/Athena/Compression.cpp index 4f5eb3f..79aef76 100644 --- a/src/Athena/Compression.cpp +++ b/src/Athena/Compression.cpp @@ -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; diff --git a/src/Athena/Utility.cpp b/src/Athena/Utility.cpp index b6ed91b..8c3013b 100644 --- a/src/Athena/Utility.cpp +++ b/src/Athena/Utility.cpp @@ -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++) diff --git a/src/lzo.c b/src/lzo.c index e1d85e0..0a90196 100644 --- a/src/lzo.c +++ b/src/lzo.c @@ -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;