1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Add base 32/64 encode/decode.

This commit is contained in:
Sandu Liviu Catalin 2021-01-31 22:21:42 +02:00
parent fe27504972
commit fdc1d9d993

View File

@ -2,9 +2,17 @@
#include "PocoLib/Crypto.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
#include <iostream>
#include <sstream>
// ------------------------------------------------------------------------------------------------
#include <Poco/Base32Encoder.h>
#include <Poco/Base32Decoder.h>
#include <Poco/Base64Encoder.h>
#include <Poco/Base64Decoder.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
static SQInteger SqGetHash(HSQUIRRELVM vm)
@ -43,12 +51,145 @@ static SQInteger SqGetHash(HSQUIRRELVM vm)
return 1;
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqEncodeBase32(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 a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base32Encoder enc(out);
// Encode the string
enc.write(val.mPtr, static_cast< size_t >(val.mLen));
// Close the encoder
enc.close();
// Push the resulted string on the stack
Var< String >::push(vm, out.str());
}
catch (const std::exception & e)
{
return sq_throwerrorf(vm, _SC("Failed to encode: %s"), e.what());
}
// At this point we have a valid string on the stack
return 1;
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqDecodeBase32(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 a string receiver
std::stringstream io;
// Put the encoded that in the stream
io.write(val.mPtr, static_cast< size_t >(val.mLen));
// Create the encoder
Poco::Base32Decoder dec(io);
// Create a string to receive the decoded data
String out;
// Decode the string
dec >> out;
// Push the resulted string on the stack
Var< String >::push(vm, out);
}
catch (const std::exception & e)
{
return sq_throwerrorf(vm, _SC("Failed to decode: %s"), e.what());
}
// At this point we have a valid string on the stack
return 1;
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqEncodeBase64(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 a string receiver
std::ostringstream out;
// Create the encoder
Poco::Base64Encoder enc(out);
// Encode the string
enc.write(val.mPtr, static_cast< size_t >(val.mLen));
// Close the encoder
enc.close();
// Push the resulted string on the stack
Var< String >::push(vm, out.str());
}
catch (const std::exception & e)
{
return sq_throwerrorf(vm, _SC("Failed to encode: %s"), e.what());
}
// At this point we have a valid string on the stack
return 1;
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqDecodeBase64(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!
}
try
{
// Create a string receiver
std::stringstream io;
// Put the encoded that in the stream
io.write(val.mPtr, static_cast< size_t >(val.mLen));
// Create the encoder
Poco::Base64Decoder dec(io);
// Create a string to receive the decoded data
String out;
// Decode the string
dec >> out;
// Push the resulted string on the stack
Var< String >::push(vm, out);
}
catch (const std::exception & e)
{
return sq_throwerrorf(vm, _SC("Failed to decode: %s"), e.what());
}
// At this point we have a valid string on the stack
return 1;
}
// ================================================================================================
void Register_POCO_Crypto(HSQUIRRELVM vm)
{
Table ns(vm);
ns.SquirrelFunc(_SC("Hash"), &SqGetHash);
ns.SquirrelFunc(_SC("EncodeBase32"), &SqEncodeBase32);
ns.SquirrelFunc(_SC("DecodeBase32"), &SqDecodeBase32);
ns.SquirrelFunc(_SC("EncodeBase64"), &SqEncodeBase64);
ns.SquirrelFunc(_SC("DecodeBase64"), &SqDecodeBase64);
RootTable(vm).Bind(_SC("SqCrypto"), ns);
}