mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 08:47:17 +01:00
Add checksum generation.
This commit is contained in:
parent
fdc1d9d993
commit
b04a71dd24
@ -10,6 +10,9 @@
|
||||
#include "Base/Vector2i.hpp"
|
||||
#include "Base/Vector4.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <Poco/Checksum.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
@ -390,6 +393,33 @@ Vector4 SqBuffer::ReadVector4()
|
||||
return Vector4(value);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger SqBuffer::GetCRC32(SQInteger n)
|
||||
{
|
||||
// Validate the managed buffer reference
|
||||
ValidateDeeper();
|
||||
// Create the checksum computer
|
||||
Poco::Checksum c(Poco::Checksum::TYPE_CRC32);
|
||||
// Give it the data to process
|
||||
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
|
||||
// return the result
|
||||
return static_cast< SQInteger >(c.checksum());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger SqBuffer::GetADLER32(SQInteger n)
|
||||
{
|
||||
// Validate the managed buffer reference
|
||||
ValidateDeeper();
|
||||
// Create the checksum computer
|
||||
Poco::Checksum c(Poco::Checksum::TYPE_ADLER32);
|
||||
// Give it the data to process
|
||||
c.update(&m_Buffer->Cursor< char >(), n >= 0 ? static_cast< uint32_t >(n) : m_Buffer->Remaining());
|
||||
// return the result
|
||||
return static_cast< SQInteger >(c.checksum());
|
||||
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_Buffer(HSQUIRRELVM vm)
|
||||
{
|
||||
@ -474,6 +504,8 @@ void Register_Buffer(HSQUIRRELVM vm)
|
||||
.Func(_SC("ReadVector2i"), &SqBuffer::ReadVector2i)
|
||||
.Func(_SC("ReadVector3"), &SqBuffer::ReadVector3)
|
||||
.Func(_SC("ReadVector4"), &SqBuffer::ReadVector4)
|
||||
.Func(_SC("CRC32"), &SqBuffer::GetCRC32)
|
||||
.Func(_SC("ADLER32"), &SqBuffer::GetADLER32)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -824,6 +824,16 @@ public:
|
||||
* Read a Vector4 from the buffer.
|
||||
*/
|
||||
Vector4 ReadVector4();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Compute the CRC-32 checksums on the data in the buffer.
|
||||
*/
|
||||
SQInteger GetCRC32(SQInteger n);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Compute the Adler-32 checksums on the data in the buffer.
|
||||
*/
|
||||
SQInteger GetADLER32(SQInteger n);
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -10,6 +10,8 @@
|
||||
#include <Poco/Base32Decoder.h>
|
||||
#include <Poco/Base64Encoder.h>
|
||||
#include <Poco/Base64Decoder.h>
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include <Poco/Checksum.h>
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
@ -157,6 +159,7 @@ static SQInteger SqDecodeBase64(HSQUIRRELVM vm)
|
||||
{
|
||||
return val.mRes; // Propagate the error!
|
||||
}
|
||||
// Prevent any exceptions from reach the VM
|
||||
try
|
||||
{
|
||||
// Create a string receiver
|
||||
@ -180,6 +183,62 @@ static SQInteger SqDecodeBase64(HSQUIRRELVM vm)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static SQInteger SqGetCRC32(HSQUIRRELVM vm)
|
||||
{
|
||||
// Attempt to retrieve the value from the stack as a string
|
||||
StackStrF val(vm, 2);
|
||||
// Have we failed to retrieve the string?
|
||||
if (SQ_FAILED(val.Proc(true)))
|
||||
{
|
||||
return val.mRes; // Propagate the error!
|
||||
}
|
||||
// Prevent any exceptions from reach the VM
|
||||
try
|
||||
{
|
||||
// Create the checksum computer
|
||||
Poco::Checksum c(Poco::Checksum::TYPE_CRC32);
|
||||
// Give it the data to process
|
||||
c.update(val.mPtr, static_cast< uint32_t >(val.mLen));
|
||||
// Push the result on the stack
|
||||
sq_pushinteger(vm, static_cast< SQInteger >(c.checksum()));
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
return sq_throwerrorf(vm, _SC("Failed to compute checksum: %s"), e.what());
|
||||
}
|
||||
// At this point we have a valid string on the stack
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static SQInteger SqGetADLER32(HSQUIRRELVM vm)
|
||||
{
|
||||
// Attempt to retrieve the value from the stack as a string
|
||||
StackStrF val(vm, 2);
|
||||
// Have we failed to retrieve the string?
|
||||
if (SQ_FAILED(val.Proc(true)))
|
||||
{
|
||||
return val.mRes; // Propagate the error!
|
||||
}
|
||||
// Prevent any exceptions from reach the VM
|
||||
try
|
||||
{
|
||||
// Create the checksum computer
|
||||
Poco::Checksum c(Poco::Checksum::TYPE_ADLER32);
|
||||
// Give it the data to process
|
||||
c.update(val.mPtr, static_cast< uint32_t >(val.mLen));
|
||||
// Push the result on the stack
|
||||
sq_pushinteger(vm, static_cast< SQInteger >(c.checksum()));
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
return sq_throwerrorf(vm, _SC("Failed to compute checksum: %s"), e.what());
|
||||
}
|
||||
// At this point we have a valid string on the stack
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_POCO_Crypto(HSQUIRRELVM vm)
|
||||
{
|
||||
@ -190,6 +249,8 @@ void Register_POCO_Crypto(HSQUIRRELVM vm)
|
||||
ns.SquirrelFunc(_SC("DecodeBase32"), &SqDecodeBase32);
|
||||
ns.SquirrelFunc(_SC("EncodeBase64"), &SqEncodeBase64);
|
||||
ns.SquirrelFunc(_SC("DecodeBase64"), &SqDecodeBase64);
|
||||
ns.SquirrelFunc(_SC("CRC32"), &SqGetCRC32);
|
||||
ns.SquirrelFunc(_SC("ADLER32"), &SqGetADLER32);
|
||||
|
||||
RootTable(vm).Bind(_SC("SqCrypto"), ns);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user