瀏覽代碼

hv::Base64Encode hv::Base64Decode

ithewei 4 年之前
父節點
當前提交
2b4d8985cd
共有 1 個文件被更改,包括 27 次插入0 次删除
  1. 27 0
      util/base64.h

+ 27 - 0
util/base64.h

@@ -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_