Gets a binary object and returns its hexadecimal representation. That representation maps each byte in the input to a 2-length string containing a hexadecimal number. - Examples: - foo -> 666F6F = 66[f] 6F[o] 6F[o] - bar -> 626172 = 62[b] 61[a] 72[r]
| 2937 | // - foo -> 666F6F = 66[f] 6F[o] 6F[o] |
| 2938 | // - bar -> 626172 = 62[b] 61[a] 72[r] |
| 2939 | FORCE_INLINE |
| 2940 | const char* to_hex_binary(int64_t context, const char* text, int32_t text_len, |
| 2941 | int32_t* out_len) { |
| 2942 | if (text_len == 0) { |
| 2943 | *out_len = 0; |
| 2944 | return ""; |
| 2945 | } |
| 2946 | |
| 2947 | if (ARROW_PREDICT_FALSE(text_len < 0)) { |
| 2948 | gdv_fn_context_set_error_msg(context, "Text length invalid (negative)."); |
| 2949 | *out_len = 0; |
| 2950 | return ""; |
| 2951 | } |
| 2952 | |
| 2953 | int32_t double_len = 0; |
| 2954 | // Check multiply overflow for text_len |
| 2955 | if (ARROW_PREDICT_FALSE( |
| 2956 | arrow::internal::MultiplyWithOverflow(2, text_len, &double_len))) { |
| 2957 | gdv_fn_context_set_error_msg(context, "Memory allocation size too large."); |
| 2958 | *out_len = 0; |
| 2959 | return ""; |
| 2960 | } |
| 2961 | |
| 2962 | int32_t alloc_length = 0; |
| 2963 | // Check add overflow for text_len |
| 2964 | if (ARROW_PREDICT_FALSE( |
| 2965 | arrow::internal::AddWithOverflow(1, double_len, &alloc_length))) { |
| 2966 | gdv_fn_context_set_error_msg(context, "Memory allocation size too large."); |
| 2967 | *out_len = 0; |
| 2968 | return ""; |
| 2969 | } |
| 2970 | |
| 2971 | auto ret = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, alloc_length)); |
| 2972 | |
| 2973 | if (ret == nullptr) { |
| 2974 | gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string"); |
| 2975 | *out_len = 0; |
| 2976 | return ""; |
| 2977 | } |
| 2978 | |
| 2979 | uint32_t ret_index = 0; |
| 2980 | uint32_t max_len = static_cast<uint32_t>(text_len) * 2; |
| 2981 | uint32_t max_char_to_write = 4; |
| 2982 | |
| 2983 | for (gdv_int32 i = 0; i < text_len; i++) { |
| 2984 | DCHECK(ret_index >= 0 && ret_index < max_len); |
| 2985 | |
| 2986 | int32_t ch = static_cast<int32_t>(text[i]) & 0xFF; |
| 2987 | |
| 2988 | ret_index += snprintf(ret + ret_index, max_char_to_write, "%02X", ch); |
| 2989 | } |
| 2990 | |
| 2991 | *out_len = static_cast<int32_t>(ret_index); |
| 2992 | return ret; |
| 2993 | } |
| 2994 | |
| 2995 | FORCE_INLINE |
| 2996 | const char* to_hex_int64(int64_t context, int64_t data, int32_t* out_len) { |