| 234 | } |
| 235 | |
| 236 | GANDIVA_EXPORT |
| 237 | const char* gdv_fn_base64_encode_binary(int64_t context, const char* in, int32_t in_len, |
| 238 | int32_t* out_len) { |
| 239 | if (in_len < 0) { |
| 240 | char err_msg[96]; |
| 241 | snprintf(err_msg, sizeof(err_msg), |
| 242 | "BASE64: input length must be non-negative, got %d", in_len); |
| 243 | gdv_fn_context_set_error_msg(context, err_msg); |
| 244 | *out_len = 0; |
| 245 | return ""; |
| 246 | } |
| 247 | if (in_len == 0) { |
| 248 | *out_len = 0; |
| 249 | return ""; |
| 250 | } |
| 251 | // use arrow method to encode base64 string |
| 252 | std::string encoded_str = arrow::util::base64_encode(std::string_view(in, in_len)); |
| 253 | *out_len = static_cast<int32_t>(encoded_str.length()); |
| 254 | // allocate memory for response |
| 255 | char* ret = reinterpret_cast<char*>( |
| 256 | gdv_fn_context_arena_malloc(context, static_cast<int32_t>(*out_len))); |
| 257 | if (ret == nullptr) { |
| 258 | gdv_fn_context_set_error_msg(context, "Could not allocate memory"); |
| 259 | *out_len = 0; |
| 260 | return ""; |
| 261 | } |
| 262 | memcpy(ret, encoded_str.data(), *out_len); |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | GANDIVA_EXPORT |
| 267 | const char* gdv_fn_base64_decode_utf8(int64_t context, const char* in, int32_t in_len, |