summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util/Base64.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/uscxml/util/Base64.hpp')
-rw-r--r--src/uscxml/util/Base64.hpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/uscxml/util/Base64.hpp b/src/uscxml/util/Base64.hpp
new file mode 100644
index 0000000..a106a12
--- /dev/null
+++ b/src/uscxml/util/Base64.hpp
@@ -0,0 +1,46 @@
+// taken from http://www.adp-gmbh.ch/cpp/common/base64.html
+#ifndef BASE64_H_5FKG12HF
+#define BASE64_H_5FKG12HF
+
+extern "C" {
+#include "Base64.h"
+}
+
+#include <stdlib.h>
+#include <string>
+#include "uscxml/Common.h"
+
+namespace uscxml {
+
+USCXML_API inline std::string base64Encode(const char* data, unsigned int len) {
+ base64_encodestate* ctx = (base64_encodestate*)malloc(sizeof(base64_encodestate));
+ base64_init_encodestate(ctx);
+
+ /**
+ * Wikipedia: Very roughly, the final size of Base64-encoded binary data is equal to 1.37
+ * times the original data size + 814 bytes (for headers). The size of the decoded data can
+ * be approximated with this formula:
+ */
+
+ char* out = (char*)malloc(len * 1.4 + 814);
+ base64_encode_block(data, len, out, ctx);
+ free(ctx);
+ std::string result(out);
+ free(out);
+ return result;
+}
+
+USCXML_API inline std::string base64Decode(const std::string& data) {
+ base64_decodestate* ctx = (base64_decodestate*)malloc(sizeof(base64_decodestate));
+ base64_init_decodestate(ctx);
+
+ char* out = (char*)malloc(data.size());
+ base64_decode_block(data.data(), data.size(), out, ctx);
+ free(ctx);
+ std::string result(out);
+ free(out);
+ return result;
+}
+
+}
+#endif /* end of include guard: BASE64_H_5FKG12HF */