Remove dependency on base64c. Use libsodium's. Also consolidate the calls, as the usage is quite special.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4175 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-02-14 03:15:24 +00:00
parent fa87462405
commit 1f77df7a90
26 changed files with 43 additions and 771 deletions

View File

@ -4,11 +4,11 @@
#include "task.h"
#include "trace.h"
#include <base64c.h>
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <picohttpparser.h>
#include <quickjs-libc.h>
#include <sodium/utils.h>
#include <uv.h>
#include <string.h>
@ -70,7 +70,7 @@ static JSValue _util_base64_encode(JSContext* context, JSValueConst this_val, in
if (array)
{
char* encoded = tf_malloc(length * 4);
int r = base64c_encode(array, length, (uint8_t*)encoded, length * 4);
int r = tf_base64_encode(array, length, encoded, length * 4);
if (r >= 0)
{
result = JS_NewStringLen(context, encoded, r);
@ -81,7 +81,7 @@ static JSValue _util_base64_encode(JSContext* context, JSValueConst this_val, in
{
const char* value = JS_ToCStringLen(context, &length, argv[0]);
char* encoded = tf_malloc(length * 4);
int r = base64c_encode((const uint8_t*)value, length, (uint8_t*)encoded, length * 4);
int r = tf_base64_encode((const uint8_t*)value, length, encoded, length * 4);
if (r >= 0)
{
result = JS_NewStringLen(context, encoded, r);
@ -97,12 +97,12 @@ static JSValue _util_base64_decode(JSContext* context, JSValueConst this_val, in
JSValue result = JS_UNDEFINED;
size_t length = 0;
const char* value = JS_ToCStringLen(context, &length, argv[0]);
char* encoded = tf_malloc(length);
uint8_t* encoded = tf_malloc(length);
int r = base64c_decode((const uint8_t*)value, length, (uint8_t*)encoded, length);
int r = tf_base64_decode(value, length, encoded, length);
if (r >= 0)
{
result = JS_NewStringLen(context, encoded, r);
result = JS_NewStringLen(context, (const char*)encoded, r);
}
tf_free(encoded);
@ -406,3 +406,14 @@ int tf_util_insert_index(const void* key, const void* base, size_t count, size_t
return lower;
}
size_t tf_base64_encode(const uint8_t* source, size_t source_length, char* out, size_t out_length)
{
sodium_bin2base64(out, out_length, source, source_length, sodium_base64_VARIANT_ORIGINAL);
return sodium_base64_ENCODED_LEN(source_length, sodium_base64_VARIANT_ORIGINAL) - 1;
}
size_t tf_base64_decode(const char* source, size_t source_length, uint8_t* out, size_t out_length)
{
size_t actual_length = 0;
return sodium_base642bin(out, out_length, source, source_length, NULL, &actual_length, NULL, sodium_base64_VARIANT_ORIGINAL) == 0 ? actual_length : 0;
}