diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-11-16 00:10:51 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-11-16 00:10:51 (GMT) |
commit | b92c2f64953e8aabbf3644dc03524de048764c6a (patch) | |
tree | fe0ad7b96fe45e8ec8c8f0d55652907751494e3b /src/uscxml/util/Base64.hpp | |
parent | c60cc4c17191bc432e89af914dde700724633b38 (diff) | |
download | uscxml-b92c2f64953e8aabbf3644dc03524de048764c6a.zip uscxml-b92c2f64953e8aabbf3644dc03524de048764c6a.tar.gz uscxml-b92c2f64953e8aabbf3644dc03524de048764c6a.tar.bz2 |
Have crypto available from C with C++ bindings via header only
Diffstat (limited to 'src/uscxml/util/Base64.hpp')
-rw-r--r-- | src/uscxml/util/Base64.hpp | 46 |
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 */ |