20 # pragma warning(push)
21 # pragma warning(disable: 4804)
46 constexpr
const char encoder_table[] =
48 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
50 "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu"
53 static_assert(
sizeof(encoder_table) == 85 + 1,
"encoder table has more than 85 entries");
55 constexpr
const char decoder_table[] = {
58 '\xff',
'\x44',
'\xff',
'\x54',
'\x53',
'\x52',
'\x48',
'\xff',
59 '\x4b',
'\x4c',
'\x46',
'\x41',
'\xff',
'\x3f',
'\x3e',
'\x45',
60 '\x00',
'\x01',
'\x02',
'\x03',
'\x04',
'\x05',
'\x06',
'\x07',
61 '\x08',
'\x09',
'\x40',
'\xff',
'\x49',
'\x42',
'\x4a',
'\x47',
62 '\x51',
'\x24',
'\x25',
'\x26',
'\x27',
'\x28',
'\x29',
'\x2a',
63 '\x2b',
'\x2c',
'\x2d',
'\x2e',
'\x2f',
'\x30',
'\x31',
'\x32',
64 '\x33',
'\x34',
'\x35',
'\x36',
'\x37',
'\x38',
'\x39',
'\x3a',
65 '\x3b',
'\x3c',
'\x3d',
'\x4d',
'\xff',
'\x4e',
'\x43',
'\xff',
66 '\xff',
'\x0a',
'\x0b',
'\x0c',
'\x0d',
'\x0e',
'\x0f',
'\x10',
67 '\x11',
'\x12',
'\x13',
'\x14',
'\x15',
'\x16',
'\x17',
'\x18',
68 '\x19',
'\x1a',
'\x1b',
'\x1c',
'\x1d',
'\x1e',
'\x1f',
'\x20',
69 '\x21',
'\x22',
'\x23',
'\x4f',
'\xff',
'\x50',
'\xff',
'\xff'
71 '\xff',
'\x00',
'\x01',
'\x02',
'\x03',
'\x04',
'\x05',
'\x06',
72 '\x07',
'\x08',
'\x09',
'\x0a',
'\x0b',
'\x0c',
'\x0d',
'\x0e',
73 '\x0f',
'\x10',
'\x11',
'\x12',
'\x13',
'\x14',
'\x15',
'\x16',
74 '\x17',
'\x18',
'\x19',
'\x1a',
'\x1b',
'\x1c',
'\x1d',
'\x1e',
75 '\x1f',
'\x20',
'\x21',
'\x22',
'\x23',
'\x24',
'\x25',
'\x26',
76 '\x27',
'\x28',
'\x29',
'\x2a',
'\x2b',
'\x2c',
'\x2d',
'\x2e',
77 '\x2f',
'\x30',
'\x31',
'\x32',
'\x33',
'\x34',
'\x35',
'\x36',
78 '\x37',
'\x38',
'\x39',
'\x3a',
'\x3b',
'\x3c',
'\x3d',
'\x3e',
79 '\x3f',
'\x40',
'\x41',
'\x42',
'\x43',
'\x44',
'\x45',
'\x46',
80 '\x47',
'\x48',
'\x49',
'\x4a',
'\x4b',
'\x4c',
'\x4d',
'\x4e',
81 '\x4f',
'\x50',
'\x51',
'\x52',
'\x53',
'\x54',
'\xff',
'\xff',
82 '\xff',
'\xff',
'\xff',
'\xff',
'\xff',
'\xff',
'\xff',
'\xff',
86 std::string b85_encode(
const unsigned char *input,
size_t length)
90 size_t padding = (4 - length % 4) % 4;
93 size_t output_length = (length + padding) * 5 / 4;
94 std::string encoded(output_length,
'\0');
96 int encode_cursor = 0;
97 for (
size_t i = 0; i < length; i += 4)
100 for (
int j = 0; j < 4; ++j)
105 value += input[i + j];
108 for (
int j = 4; j >= 0; --j)
110 encoded[encode_cursor + j] = encoder_table[value % 85];
116 encoded.resize(output_length - padding);
120 std::string b85_decode(
const std::string &input)
123 size_t length = input.size();
124 size_t padding = (5 - length % 5) % 5;
126 size_t output_length = (length + padding) * 4 / 5;
127 std::string decoded(output_length,
'\0');
129 int decode_cursor = 0;
130 for (
size_t i = 0; i < length; i += 5)
133 for (
int j = 0; j < 5; ++j)
139 unsigned char byte = input[i + j];
140 if (
byte >= 32 &&
byte <= 127)
141 value += decoder_table[
byte - 32];
149 for (
int j = 3; j >= 0; --j)
151 decoded[decode_cursor + j] =
static_cast<char>(value & 0xFF);
156 decoded.resize(output_length - padding);
163 static constexpr
const char *LIBRARY_NAME =
"_Crypto";
164 static constexpr
const char *FUNC_COMPUTE_HASH =
"_ComputeHash";
169 constexpr
auto MAX_EXPANDED_OUTPUT = BLAKE2B_OUTBYTES * 5 / 4;
170 if (output_length < 1 || output_length > MAX_EXPANDED_OUTPUT)
180 int32_t raw_output_length = output_length * 4 / 5 + (output_length % 5 != 0);
182 const unsigned char *data = (
const unsigned char*) input->
GetCStr();
185 auto hash_output = std::make_unique<unsigned char[]>(raw_output_length);
186 if (blake2b(hash_output.get(), raw_output_length, data, data_length,
nullptr, 0) != 0)
191 std::string encoded = b85_encode(hash_output.get(), raw_output_length);
194 encoded.resize(output_length);
201 AddFunc(
this, FUNC_COMPUTE_HASH, &Hash_Dispatch);
214 for (
auto library : libraries)
216 library->RegisterWithEngine(engine);
void AddFunc(C4PropListStatic *Parent, const char *Name, RType(*pFunc)(ThisType *, ParTypes...), bool Public=true)
const char * GetC4VName(const C4V_Type Type)
C4Value C4VPropList(C4PropList *p)
C4Value C4VString(C4String *pStr)
std::string strprintf(const char *format,...)
void RegisterGlobalConstant(const char *szName, const C4Value &rValue)
C4RefCntPointer< C4String > ParentKeyName
void CreateFunctions() override
static void InstantiateAllLibraries(C4AulScriptEngine *engine)
virtual void CreateFunctions()=0
C4ScriptLibrary(const char *name)
StdStrBuf GetData() const
const char * GetCStr() const