diff options
author | punto- <ariel@okamstudio.com> | 2015-07-28 12:50:52 +0200 |
---|---|---|
committer | punto- <ariel@okamstudio.com> | 2015-07-28 12:50:52 +0200 |
commit | 922356b903061cda7591090bf19e8346c3a78cf5 (patch) | |
tree | b62520532f5c712767984205ea276214f4c7fb8c /core | |
parent | ed85339bcb42c11789b8d595a4338654c5cf5e89 (diff) |
adding some base64 marshalls
Diffstat (limited to 'core')
-rw-r--r-- | core/bind/core_bind.cpp | 77 | ||||
-rw-r--r-- | core/bind/core_bind.h | 6 |
2 files changed, 83 insertions, 0 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 120cb0000b..95cafdb36d 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -1678,12 +1678,89 @@ Variant _Marshalls::base64_to_variant(const String& p_str) { return v; }; +String _Marshalls::raw_to_base64(const DVector<uint8_t> &p_arr) { + + int len = p_arr.size(); + DVector<uint8_t>::Read r = p_arr.read(); + + int b64len = len / 3 * 4 + 4 + 1; + DVector<uint8_t> b64buff; + b64buff.resize(b64len); + DVector<uint8_t>::Write w64 = b64buff.write(); + + int strlen = base64_encode((char*)(&w64[0]), (char*)(&r[0]), len); + w64[strlen] = 0; + String ret = (char*)&w64[0]; + + return ret; +}; + +DVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) { + + int strlen = p_str.length(); + CharString cstr = p_str.ascii(); + + int arr_len; + DVector<uint8_t> buf; + { + buf.resize(strlen / 4 * 3 + 1); + DVector<uint8_t>::Write w = buf.write(); + + arr_len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); + }; + buf.resize(arr_len); + + // conversion from DVector<uint8_t> to raw array? + return buf; +}; + +String _Marshalls::utf8_to_base64(const String& p_str) { + + CharString cstr = p_str.utf8(); + int len = cstr.length(); + + int b64len = len / 3 * 4 + 4 + 1; + DVector<uint8_t> b64buff; + b64buff.resize(b64len); + DVector<uint8_t>::Write w64 = b64buff.write(); + + int strlen = base64_encode((char*)(&w64[0]), (char*)cstr.get_data(), len); + + w64[strlen] = 0; + String ret = (char*)&w64[0]; + + return ret; +}; + +String _Marshalls::base64_to_utf8(const String& p_str) { + + int strlen = p_str.length(); + CharString cstr = p_str.ascii(); + + DVector<uint8_t> buf; + buf.resize(strlen / 4 * 3 + 1 + 1); + DVector<uint8_t>::Write w = buf.write(); + + int len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); + + w[len] = 0; + String ret = String::utf8((char*)&w[0]); + + return ret; +}; + void _Marshalls::_bind_methods() { ObjectTypeDB::bind_method(_MD("variant_to_base64:String","variant"),&_Marshalls::variant_to_base64); ObjectTypeDB::bind_method(_MD("base64_to_variant:Variant","base64_str"),&_Marshalls::base64_to_variant); + ObjectTypeDB::bind_method(_MD("raw_to_base64:String","array"),&_Marshalls::raw_to_base64); + ObjectTypeDB::bind_method(_MD("base64_to_raw:RawArray","base64_str"),&_Marshalls::base64_to_raw); + + ObjectTypeDB::bind_method(_MD("utf8_to_base64:String","utf8_str"),&_Marshalls::utf8_to_base64); + ObjectTypeDB::bind_method(_MD("base64_to_utf8:String","base64_str"),&_Marshalls::base64_to_utf8); + }; diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 74f29c23e8..83f9e533ea 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -436,6 +436,12 @@ public: String variant_to_base64(const Variant& p_var); Variant base64_to_variant(const String& p_str); + String raw_to_base64(const DVector<uint8_t>& p_arr); + DVector<uint8_t> base64_to_raw(const String& p_str); + + String utf8_to_base64(const String& p_str); + String base64_to_utf8(const String& p_str); + _Marshalls() {}; }; |