WIP SVC_CreateStringTable data parsing
This commit is contained in:
72
external/sourcesdk/common.cpp
vendored
Normal file
72
external/sourcesdk/common.cpp
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
|
||||
#include "sourcesdk/common.h"
|
||||
#include "snappy/snappy.h"
|
||||
#include <string.h>
|
||||
|
||||
static const uint32 INVALID = static_cast<uint32>(-1);
|
||||
static const uint32 LZSS_ID = BigLong(('L' << 24) | ('Z' << 16) | ('S' << 8) | ('S'));
|
||||
static const uint32 SNAPPY_ID = BigLong(('S' << 24) | ('N' << 16) | ('A' << 8) | ('P'));
|
||||
|
||||
inline uint32_t U8ToU32(const uint8* from)
|
||||
{
|
||||
union U8ToU32
|
||||
{
|
||||
uint8 bytes[4];
|
||||
uint32 dword;
|
||||
};
|
||||
return reinterpret_cast<const U8ToU32*>(from)->dword;
|
||||
}
|
||||
|
||||
uint32 COM_BufferToBufferDecompress(uint8* dest, uint32 destLen, const uint8* src, uint32 srcLen)
|
||||
{
|
||||
const uint32 uncompressedLen = COM_GetUncompressedSize(src, srcLen);
|
||||
if (uncompressedLen == INVALID)
|
||||
{
|
||||
if (srcLen <= destLen)
|
||||
{
|
||||
memcpy(dest, src, srcLen);
|
||||
}
|
||||
}
|
||||
else if (uncompressedLen <= destLen)
|
||||
{
|
||||
const uint32 id = U8ToU32(src);
|
||||
if (id == LZSS_ID)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
else if (id == SNAPPY_ID)
|
||||
{
|
||||
snappy::RawUncompress(reinterpret_cast<const char*>(src + 4), srcLen - 4, reinterpret_cast<char*>(dest));
|
||||
}
|
||||
}
|
||||
return (uncompressedLen != INVALID) ? uncompressedLen : 0;
|
||||
}
|
||||
|
||||
uint32 COM_GetUncompressedSize(const uint8* data, uint32 numBytes)
|
||||
{
|
||||
size_t uncompressedLength = INVALID;
|
||||
if (numBytes > 4)
|
||||
{
|
||||
const uint32 magic = U8ToU32(data);
|
||||
if (numBytes >= 8 && magic == LZSS_ID)
|
||||
{
|
||||
return U8ToU32(data + 4);
|
||||
}
|
||||
|
||||
if (magic == SNAPPY_ID)
|
||||
{
|
||||
snappy::GetUncompressedLength(reinterpret_cast<const char*>(data + 4), numBytes - 4, &uncompressedLength);
|
||||
}
|
||||
}
|
||||
return static_cast<uint32>(uncompressedLength);
|
||||
}
|
||||
|
||||
uint32 COM_GetIdealDestinationCompressionBufferSize_Snappy(uint32 srcLen)
|
||||
{
|
||||
return snappy::MaxCompressedLength(srcLen) + sizeof(uint32);
|
||||
}
|
||||
|
||||
/*uint32 COM_CompressBuffer_Snappy(const uint8* src, uint32 srcLen, uint8* dest, uint32 destLen)
|
||||
{
|
||||
return COM_GetIdealDestinationCompressionBufferSize_Snappy(srcLen);
|
||||
}*/
|
8
external/sourcesdk/include/sourcesdk/common.h
vendored
Normal file
8
external/sourcesdk/include/sourcesdk/common.h
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
#include "valve_support.h"
|
||||
|
||||
uint32 COM_BufferToBufferDecompress(uint8* dest, uint32 destLen, const uint8* src, uint32 srcLen);
|
||||
uint32 COM_GetUncompressedSize(const uint8* data, uint32 numBytes);
|
||||
|
||||
uint32 COM_GetIdealDestinationCompressionBufferSize_Snappy(uint32 srcLen);
|
||||
uint32 COM_CompressBuffer_Snappy(const uint8* src, uint32 srcLen, uint8* dest, uint32 destLen);
|
@ -59,17 +59,6 @@ inline bool is_little_endian()
|
||||
#define NORMAL_DENOMINATOR ( (1<<(NORMAL_FRACTIONAL_BITS)) - 1 )
|
||||
#define NORMAL_RESOLUTION (1.0/(NORMAL_DENOMINATOR))
|
||||
|
||||
template <typename T>
|
||||
inline T DWordSwapC( T dw )
|
||||
{
|
||||
uint32 temp;
|
||||
temp = *((uint32 *)&dw) >> 24;
|
||||
temp |= ((*((uint32 *)&dw) & 0x00FF0000) >> 8);
|
||||
temp |= ((*((uint32 *)&dw) & 0x0000FF00) << 8);
|
||||
temp |= ((*((uint32 *)&dw) & 0x000000FF) << 24);
|
||||
return *((T*)&temp);
|
||||
}
|
||||
|
||||
#if defined( _MSC_VER ) && !defined( PLATFORM_WINDOWS_PC64 )
|
||||
#define DWordSwap DWordSwapAsm
|
||||
#pragma warning(push)
|
||||
@ -85,7 +74,7 @@ inline T DWordSwapC( T dw )
|
||||
}
|
||||
#pragma warning(pop)
|
||||
#else
|
||||
#define DWordSwap DWordSwapC
|
||||
#error write the 64 bit swaps
|
||||
#endif
|
||||
|
||||
inline unsigned long LoadLittleDWord(const unsigned long *base, unsigned int dwordIndex)
|
||||
@ -114,6 +103,11 @@ inline void LittleFloat(float* pOut, float* pIn)
|
||||
}
|
||||
}
|
||||
|
||||
inline long BigLong(long val)
|
||||
{
|
||||
return is_little_endian() ? DWordSwap(val) : val;
|
||||
}
|
||||
|
||||
#define BITS_PER_INT 32
|
||||
|
||||
inline int GetBitForBitnum( int bitNum )
|
||||
|
Reference in New Issue
Block a user