aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/compression.cpp
diff options
context:
space:
mode:
authorModMaker101 <119018978+ModMaker101@users.noreply.github.com>2026-03-08 19:08:36 -0400
committerGitHub <noreply@github.com>2026-03-08 18:08:36 -0500
commit28614b922fb77149a54da1a87bebfbc98736f296 (patch)
tree7f828ba86a4ee18d0a80d29de64f6199a5412512 /Minecraft.World/compression.cpp
parent88798b501d0cf6287b6f87acb2592676e3cec58d (diff)
Modernize project codebase (#906)
* Fixed boats falling and a TP glitch #266 * Replaced every C-style cast with C++ ones * Replaced every C-style cast with C++ ones * Fixed boats falling and a TP glitch #266 * Updated NULL to nullptr and fixing some type issues * Modernized and fixed a few bugs - Replaced most instances of `NULL` with `nullptr`. - Replaced most `shared_ptr(new ...)` with `make_shared`. - Removed the `nullptr` macro as it was interfering with the actual nullptr keyword in some instances. * Fixing more conflicts * Replace int loops with size_t and start work on overrides * Add safety checks and fix a issue with vector going OOR
Diffstat (limited to 'Minecraft.World/compression.cpp')
-rw-r--r--Minecraft.World/compression.cpp104
1 files changed, 52 insertions, 52 deletions
diff --git a/Minecraft.World/compression.cpp b/Minecraft.World/compression.cpp
index b9845f01..8c2e51c0 100644
--- a/Minecraft.World/compression.cpp
+++ b/Minecraft.World/compression.cpp
@@ -12,7 +12,7 @@
DWORD Compression::tlsIdx = 0;
-Compression::ThreadStorage *Compression::tlsDefault = NULL;
+Compression::ThreadStorage *Compression::tlsDefault = nullptr;
Compression::ThreadStorage::ThreadStorage()
{
@@ -27,7 +27,7 @@ Compression::ThreadStorage::~ThreadStorage()
void Compression::CreateNewThreadStorage()
{
ThreadStorage *tls = new ThreadStorage();
- if(tlsDefault == NULL )
+ if(tlsDefault == nullptr )
{
tlsIdx = TlsAlloc();
tlsDefault = tls;
@@ -42,7 +42,7 @@ void Compression::UseDefaultThreadStorage()
void Compression::ReleaseThreadStorage()
{
- ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx);
+ const ThreadStorage *tls = static_cast<ThreadStorage *>(TlsGetValue(tlsIdx));
if( tls == tlsDefault ) return;
delete tls;
@@ -50,7 +50,7 @@ void Compression::ReleaseThreadStorage()
Compression *Compression::getCompression()
{
- ThreadStorage *tls = (ThreadStorage *)TlsGetValue(tlsIdx);
+ const ThreadStorage *tls = static_cast<ThreadStorage *>(TlsGetValue(tlsIdx));
return tls->compression;
}
@@ -63,7 +63,7 @@ HRESULT Compression::CompressLZXRLE(void *pDestination, unsigned int *pDestSize,
// Use the static buffer when it fits, dynamically allocate otherwise.
static const unsigned int staticCompressSize = 1024*100;
unsigned int rleBufSize = SrcSize * 2;
- unsigned char *dynamicRleBuf = NULL;
+ unsigned char *dynamicRleBuf = nullptr;
unsigned char *rleBuf;
if(rleBufSize <= staticCompressSize)
@@ -77,8 +77,8 @@ HRESULT Compression::CompressLZXRLE(void *pDestination, unsigned int *pDestSize,
rleBuf = dynamicRleBuf;
}
- unsigned char *pucIn = (unsigned char *)pSource;
- unsigned char *pucEnd = pucIn + SrcSize;
+ const unsigned char *pucIn = static_cast<unsigned char*>(pSource);
+ const unsigned char *pucEnd = pucIn + SrcSize;
unsigned char *pucOut = rleBuf;
// Compress with RLE first:
@@ -88,7 +88,7 @@ HRESULT Compression::CompressLZXRLE(void *pDestination, unsigned int *pDestSize,
PIXBeginNamedEvent(0,"RLE compression");
do
{
- unsigned char thisOne = *pucIn++;
+ const unsigned char thisOne = *pucIn++;
unsigned int count = 1;
while( ( pucIn != pucEnd ) && ( *pucIn == thisOne ) && ( count < 256 ) )
@@ -119,14 +119,14 @@ HRESULT Compression::CompressLZXRLE(void *pDestination, unsigned int *pDestSize,
*pucOut++ = thisOne;
}
} while (pucIn != pucEnd);
- unsigned int rleSize = (unsigned int)(pucOut - rleBuf);
+ const unsigned int rleSize = static_cast<unsigned int>(pucOut - rleBuf);
PIXEndNamedEvent();
PIXBeginNamedEvent(0,"Secondary compression");
Compress(pDestination, pDestSize, rleBuf, rleSize);
PIXEndNamedEvent();
- if(dynamicRleBuf != NULL) delete [] dynamicRleBuf;
+ if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
LeaveCriticalSection(&rleCompressLock);
// printf("Compressed from %d to %d to %d\n",SrcSize,rleSize,*pDestSize);
@@ -142,7 +142,7 @@ HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, vo
// RLE can expand data (each 0xFF byte becomes 2 bytes), so worst case is 2*SrcSize.
static const unsigned int staticCompressSize = 1024*100;
unsigned int rleBufSize = SrcSize * 2;
- unsigned char *dynamicRleBuf = NULL;
+ unsigned char *dynamicRleBuf = nullptr;
unsigned char *rleBuf;
if(rleBufSize <= staticCompressSize)
@@ -156,8 +156,8 @@ HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, vo
rleBuf = dynamicRleBuf;
}
- unsigned char *pucIn = (unsigned char *)pSource;
- unsigned char *pucEnd = pucIn + SrcSize;
+ const unsigned char *pucIn = static_cast<unsigned char*>(pSource);
+ const unsigned char *pucEnd = pucIn + SrcSize;
unsigned char *pucOut = rleBuf;
// Compress with RLE first:
@@ -167,7 +167,7 @@ HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, vo
PIXBeginNamedEvent(0,"RLE compression");
do
{
- unsigned char thisOne = *pucIn++;
+ const unsigned char thisOne = *pucIn++;
unsigned int count = 1;
while( ( pucIn != pucEnd ) && ( *pucIn == thisOne ) && ( count < 256 ) )
@@ -198,7 +198,7 @@ HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, vo
*pucOut++ = thisOne;
}
} while (pucIn != pucEnd);
- unsigned int rleSize = (unsigned int)(pucOut - rleBuf);
+ const unsigned int rleSize = static_cast<unsigned int>(pucOut - rleBuf);
PIXEndNamedEvent();
// Return
@@ -214,7 +214,7 @@ HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, vo
#endif
}
- if(dynamicRleBuf != NULL) delete [] dynamicRleBuf;
+ if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
LeaveCriticalSection(&rleCompressLock);
return S_OK;
@@ -229,12 +229,12 @@ HRESULT Compression::DecompressLZXRLE(void *pDestination, unsigned int *pDestSiz
// only use 5% of this buffer
// 4J Stu - Changed this again to dynamically allocate a buffer if it's going to be too big
- unsigned char *pucIn = NULL;
+ unsigned char *pucIn = nullptr;
//const unsigned int staticRleSize = 1024*200;
//static unsigned char rleBuf[staticRleSize];
unsigned int rleSize = staticRleSize;
- unsigned char *dynamicRleBuf = NULL;
+ unsigned char *dynamicRleBuf = nullptr;
HRESULT decompressResult;
if(*pDestSize > rleSize)
@@ -247,27 +247,27 @@ HRESULT Compression::DecompressLZXRLE(void *pDestination, unsigned int *pDestSiz
else
{
decompressResult = Decompress(rleDecompressBuf, &rleSize, pSource, SrcSize);
- pucIn = (unsigned char *)rleDecompressBuf;
+ pucIn = static_cast<unsigned char*>(rleDecompressBuf);
}
if(decompressResult != S_OK)
{
app.DebugPrintf("*** DecompressLZXRLE: zlib Decompress FAILED hr=0x%08X srcSize=%u expectedDest=%u rleSize=%u\n",
decompressResult, SrcSize, *pDestSize, rleSize);
- if(dynamicRleBuf != NULL) delete [] dynamicRleBuf;
+ if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
*pDestSize = 0;
LeaveCriticalSection(&rleDecompressLock);
return decompressResult;
}
//unsigned char *pucIn = (unsigned char *)rleDecompressBuf;
- unsigned char *pucEnd = pucIn + rleSize;
- unsigned char *pucOut = (unsigned char *)pDestination;
- unsigned char *pucOutEnd = pucOut + *pDestSize;
+ const unsigned char *pucEnd = pucIn + rleSize;
+ unsigned char *pucOut = static_cast<unsigned char*>(pDestination);
+ const unsigned char *pucOutEnd = pucOut + *pDestSize;
while( pucIn != pucEnd )
{
- unsigned char thisOne = *pucIn++;
+ const unsigned char thisOne = *pucIn++;
if( thisOne == 255 )
{
if( pucIn >= pucEnd ) break;
@@ -285,7 +285,7 @@ HRESULT Compression::DecompressLZXRLE(void *pDestination, unsigned int *pDestSiz
{
count++;
if( pucIn >= pucEnd ) break;
- unsigned char data = *pucIn++;
+ const unsigned char data = *pucIn++;
if( pucOut + count > pucOutEnd ) break;
for( unsigned int i = 0; i < count; i++ )
{
@@ -299,11 +299,11 @@ HRESULT Compression::DecompressLZXRLE(void *pDestination, unsigned int *pDestSiz
*pucOut++ = thisOne;
}
}
- *pDestSize = (unsigned int)(pucOut - (unsigned char *)pDestination);
+ *pDestSize = static_cast<unsigned int>(pucOut - static_cast<unsigned char*>(pDestination));
// printf("Decompressed from %d to %d to %d\n",SrcSize,rleSize,*pDestSize);
- if(dynamicRleBuf != NULL) delete [] dynamicRleBuf;
+ if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
LeaveCriticalSection(&rleDecompressLock);
return S_OK;
@@ -314,14 +314,14 @@ HRESULT Compression::DecompressRLE(void *pDestination, unsigned int *pDestSize,
EnterCriticalSection(&rleDecompressLock);
//unsigned char *pucIn = (unsigned char *)rleDecompressBuf;
- unsigned char *pucIn = (unsigned char *)pSource;
- unsigned char *pucEnd = pucIn + SrcSize;
- unsigned char *pucOut = (unsigned char *)pDestination;
- unsigned char *pucOutEnd = pucOut + *pDestSize;
+ unsigned char *pucIn = static_cast<unsigned char *>(pSource);
+ const unsigned char *pucEnd = pucIn + SrcSize;
+ unsigned char *pucOut = static_cast<unsigned char*>(pDestination);
+ const unsigned char *pucOutEnd = pucOut + *pDestSize;
while( pucIn != pucEnd )
{
- unsigned char thisOne = *pucIn++;
+ const unsigned char thisOne = *pucIn++;
if( thisOne == 255 )
{
if( pucIn >= pucEnd ) break;
@@ -339,7 +339,7 @@ HRESULT Compression::DecompressRLE(void *pDestination, unsigned int *pDestSize,
{
count++;
if( pucIn >= pucEnd ) break;
- unsigned char data = *pucIn++;
+ const unsigned char data = *pucIn++;
if( pucOut + count > pucOutEnd ) break;
for( unsigned int i = 0; i < count; i++ )
{
@@ -353,7 +353,7 @@ HRESULT Compression::DecompressRLE(void *pDestination, unsigned int *pDestSize,
*pucOut++ = thisOne;
}
}
- *pDestSize = (unsigned int)(pucOut - (unsigned char *)pDestination);
+ *pDestSize = static_cast<unsigned int>(pucOut - static_cast<unsigned char*>(pDestination));
LeaveCriticalSection(&rleDecompressLock);
return S_OK;
@@ -365,8 +365,8 @@ HRESULT Compression::Compress(void *pDestination, unsigned int *pDestSize, void
// Using zlib for x64 compression - 360 is using native 360 compression and PS3 a stubbed non-compressing version of this
#if defined __ORBIS__ || defined _DURANGO || defined _WIN64 || defined __PSVITA__
SIZE_T destSize = (SIZE_T)(*pDestSize);
- int res = ::compress((Bytef *)pDestination, (uLongf *)&destSize, (Bytef *)pSource, SrcSize);
- *pDestSize = (unsigned int)destSize;
+ const int res = ::compress(static_cast<Bytef *>(pDestination), (uLongf *)&destSize, static_cast<Bytef *>(pSource), SrcSize);
+ *pDestSize = static_cast<unsigned int>(destSize);
return ( ( res == Z_OK ) ? S_OK : -1 );
#elif defined __PS3__
uint32_t destSize = (uint32_t)(*pDestSize);
@@ -393,8 +393,8 @@ HRESULT Compression::Decompress(void *pDestination, unsigned int *pDestSize, voi
// Using zlib for x64 compression - 360 is using native 360 compression and PS3 a stubbed non-compressing version of this
#if defined __ORBIS__ || defined _DURANGO || defined _WIN64 || defined __PSVITA__
SIZE_T destSize = (SIZE_T)(*pDestSize);
- int res = ::uncompress((Bytef *)pDestination, (uLongf *)&destSize, (Bytef *)pSource, SrcSize);
- *pDestSize = (unsigned int)destSize;
+ const int res = ::uncompress(static_cast<Bytef *>(pDestination), (uLongf *)&destSize, static_cast<Bytef *>(pSource), SrcSize);
+ *pDestSize = static_cast<unsigned int>(destSize);
return ( ( res == Z_OK ) ? S_OK : -1 );
#elif defined __PS3__
uint32_t destSize = (uint32_t)(*pDestSize);
@@ -413,11 +413,11 @@ HRESULT Compression::Decompress(void *pDestination, unsigned int *pDestSize, voi
#ifndef _XBOX
VOID Compression::VitaVirtualDecompress(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize) // (LPVOID buf, SIZE_T dwSize, LPVOID dst)
{
- uint8_t *pSrc = (uint8_t *)pSource;
+ const uint8_t *pSrc = static_cast<uint8_t *>(pSource);
int Offset = 0;
int Page = 0;
int Index = 0;
- uint8_t* Data = (uint8_t*)pDestination;
+ uint8_t* Data = static_cast<uint8_t *>(pDestination);
while( Index != SrcSize )
{
// is this a normal value
@@ -431,7 +431,7 @@ VOID Compression::VitaVirtualDecompress(void *pDestination, unsigned int *pDestS
{
// how many zeros do we have
Index += 1;
- int Count = pSrc[Index];
+ const int Count = pSrc[Index];
// to do : this should really be a sequence of memsets
for( int i = 0;i < Count;i += 1 )
{
@@ -459,8 +459,8 @@ HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestS
{
#if (defined _XBOX || defined _DURANGO || defined _WIN64)
SIZE_T destSize = (SIZE_T)(*pDestSize);
- HRESULT res = XMemDecompress(decompressionContext, pDestination, (SIZE_T *)&destSize, pSource, SrcSize);
- *pDestSize = (unsigned int)destSize;
+ const HRESULT res = XMemDecompress(decompressionContext, pDestination, (SIZE_T *)&destSize, pSource, SrcSize);
+ *pDestSize = static_cast<unsigned int>(destSize);
return res;
#else
assert(0);
@@ -469,9 +469,9 @@ HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestS
break;
case eCompressionType_ZLIBRLE:
#if (defined __ORBIS__ || defined __PS3__ || defined _DURANGO || defined _WIN64)
- if (pDestination != NULL)
- return ::uncompress((PBYTE)pDestination, (unsigned long *) pDestSize, (PBYTE) pSource, SrcSize); // Decompress
- else break; // Cannot decompress when destination is NULL
+ if (pDestination != nullptr)
+ return ::uncompress(static_cast<PBYTE>(pDestination), (unsigned long *) pDestSize, static_cast<PBYTE>(pSource), SrcSize); // Decompress
+ else break; // Cannot decompress when destination is nullptr
#else
assert(0);
break;
@@ -480,16 +480,16 @@ HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestS
#if (defined __ORBIS__ || defined __PSVITA__ || defined _DURANGO || defined _WIN64)
// Note that we're missing the normal zlib header and footer so we'll use inflate to
// decompress the payload and skip all the CRC checking, etc
- if (pDestination != NULL)
+ if (pDestination != nullptr)
{
// Read big-endian srcize from array
- PBYTE pbDestSize = (PBYTE) pDestSize;
- PBYTE pbSource = (PBYTE) pSource;
+ const PBYTE pbDestSize = (PBYTE) pDestSize;
+ const PBYTE pbSource = static_cast<PBYTE>(pSource);
for (int i = 3; i >= 0; i--) {
pbDestSize[3-i] = pbSource[i];
}
- byteArray uncompr = byteArray(*pDestSize);
+ const byteArray uncompr = byteArray(*pDestSize);
// Build decompression stream
z_stream strm;
@@ -499,7 +499,7 @@ HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestS
strm.next_out = uncompr.data;
strm.avail_out = uncompr.length;
// Skip those first 4 bytes
- strm.next_in = (PBYTE) pSource + 4;
+ strm.next_in = static_cast<PBYTE>(pSource) + 4;
strm.avail_in = SrcSize - 4;
int hr = inflateInit2(&strm, -15);
@@ -529,7 +529,7 @@ HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestS
delete uncompr.data;
return S_OK;
}
- else break; // Cannot decompress when destination is NULL
+ else break; // Cannot decompress when destination is nullptr
#else
assert(0);
#endif