Skip to content

Commit

Permalink
hv::Base64Encode hv::Base64Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Dec 24, 2021
1 parent 70430b8 commit 2b4d898
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions util/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,31 @@ HV_EXPORT int hv_base64_decode(const char *in, unsigned int inlen, unsigned char

END_EXTERN_C

#ifdef __cplusplus

#include <string.h>
#include <string>

namespace hv {

HV_INLINE std::string Base64Encode(const unsigned char* data, unsigned int len) {
int encoded_size = BASE64_ENCODE_OUT_SIZE(len);
std::string encoded_str(encoded_size + 1, 0);
encoded_size = hv_base64_encode(data, len, (char*)encoded_str.data());
encoded_str.resize(encoded_size);
return encoded_str;
}

HV_INLINE std::string Base64Decode(const char* str, unsigned int len = 0) {
if (len == 0) len = strlen(str);
int decoded_size = BASE64_DECODE_OUT_SIZE(len);
std::string decoded_buf(decoded_size + 1, 0);
decoded_size = hv_base64_decode(str, len, (unsigned char*)decoded_buf.data());
decoded_buf.resize(decoded_size);
return decoded_buf;
}

}
#endif

#endif // HV_BASE64_H_

0 comments on commit 2b4d898

Please sign in to comment.