|
4 | 4 |
|
5 | 5 | #include "bindings/global_callbacks.h" |
6 | 6 |
|
| 7 | +#include <openssl/bio.h> |
| 8 | +#include <openssl/evp.h> |
| 9 | + |
7 | 10 | #include "base/file_path.h" |
8 | 11 | #include "base/file_search.h" |
9 | 12 | #include "base/logging.h" |
@@ -50,6 +53,81 @@ void AddEventListenerCallback(const v8::FunctionCallbackInfo<v8::Value>& argumen |
50 | 53 | global->AddEventListener(toString(arguments[0]), v8::Local<v8::Function>::Cast(arguments[1])); |
51 | 54 | } |
52 | 55 |
|
| 56 | +static std::string Base64Transform(const std::string& input, bool encode) { |
| 57 | + static std::vector<char> buffer; |
| 58 | + |
| 59 | + BIO* base64 = BIO_new(BIO_f_base64()); |
| 60 | + BIO* bio = BIO_new(BIO_s_mem()); |
| 61 | + |
| 62 | + BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); |
| 63 | + BIO_push(base64, bio); |
| 64 | + |
| 65 | + int length = 0; |
| 66 | + |
| 67 | + if (encode) { |
| 68 | + buffer.resize(4 + (size_t)(input.size() * 1.5)); // +50% for encoding overhead |
| 69 | + |
| 70 | + int ret = BIO_write(base64, input.data(), input.length()); |
| 71 | + BIO_flush(base64); |
| 72 | + |
| 73 | + if (ret > 0) |
| 74 | + length = BIO_read(bio, buffer.data(), buffer.size()); |
| 75 | + |
| 76 | + } else { |
| 77 | + buffer.resize((size_t)(input.size() * 0.8)); // data will shrink by ~33%. |
| 78 | + |
| 79 | + int ret = BIO_write(bio, input.data(), input.length()); |
| 80 | + BIO_flush(bio); |
| 81 | + |
| 82 | + if (ret > 0) |
| 83 | + length = BIO_read(base64, buffer.data(), buffer.size()); |
| 84 | + } |
| 85 | + |
| 86 | + BIO_free(base64); |
| 87 | + BIO_free_all(bio); |
| 88 | + |
| 89 | + if (length <= 0) |
| 90 | + return std::string(); |
| 91 | + |
| 92 | + return std::string(buffer.data(), buffer.data() + length); |
| 93 | +} |
| 94 | + |
| 95 | +// string atob(string data); |
| 96 | +void Base64DecodeCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) { |
| 97 | + if (arguments.Length() < 1) { |
| 98 | + ThrowException("unable to execute atob(): 1 argument required, none provided."); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (!arguments[0]->IsString()) { |
| 103 | + ThrowException("unable to execute atob(): expected a string for argument 1."); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const std::string encoded = toString(arguments[0]); |
| 108 | + const std::string plaintext = Base64Transform(encoded, /* encode= */ false); |
| 109 | + |
| 110 | + arguments.GetReturnValue().Set(v8String(plaintext)); |
| 111 | +} |
| 112 | + |
| 113 | +// string btoa(string data); |
| 114 | +void Base64EncodeCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) { |
| 115 | + if (arguments.Length() < 1) { |
| 116 | + ThrowException("unable to execute atob(): 1 argument required, none provided."); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (!arguments[0]->IsString()) { |
| 121 | + ThrowException("unable to execute atob(): expected a string for argument 1."); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const std::string plaintext = toString(arguments[0]); |
| 126 | + const std::string encoded = Base64Transform(plaintext, /* encode= */ true); |
| 127 | + |
| 128 | + arguments.GetReturnValue().Set(v8String(encoded)); |
| 129 | +} |
| 130 | + |
53 | 131 | // void clearModuleCache(string prefix); |
54 | 132 | void ClearModuleCacheCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) { |
55 | 133 | auto runtime = Runtime::FromIsolate(arguments.GetIsolate()); |
|
0 commit comments