MCPcopy Create free account
hub / github.com/BruceDevices/firmware / buffer_decode_base64

Function buffer_decode_base64

src/modules/bjs_interpreter/buffer_js.cpp:11–29  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9#include "mbedtls/base64.h"
10
11static JSValue buffer_decode_base64(JSContext *ctx, const uint8_t *input, size_t input_len, size_t *out_len) {
12 if (!input) { return JS_ThrowTypeError(ctx, "Buffer.from: invalid input"); }
13
14 size_t out_max = (input_len / 4) * 3 + 3;
15 uint8_t *out = (uint8_t *)malloc(out_max);
16
17 if (!out) { return JS_ThrowOutOfMemory(ctx); }
18
19 int rc = mbedtls_base64_decode(out, out_max, out_len, input, input_len);
20
21 if (rc != 0) {
22 free(out);
23 return JS_ThrowTypeError(ctx, "Buffer.from: invalid base64");
24 }
25
26 JSValue bytes = JS_NewUint8ArrayCopy(ctx, out, *out_len);
27 free(out);
28 return bytes;
29}
30
31JSValue native_buffer_from(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) {
32 if (argc < 1 || !JS_IsString(ctx, argv[0])) {

Callers 1

native_buffer_fromFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected