| 9 | #include "mbedtls/base64.h" |
| 10 | |
| 11 | static 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 | |
| 31 | JSValue native_buffer_from(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { |
| 32 | if (argc < 1 || !JS_IsString(ctx, argv[0])) { |
no outgoing calls
no test coverage detected