diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 120cb0000b2..95cafdb36d4 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 &p_arr) { + + int len = p_arr.size(); + DVector::Read r = p_arr.read(); + + int b64len = len / 3 * 4 + 4 + 1; + DVector b64buff; + b64buff.resize(b64len); + DVector::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 _Marshalls::base64_to_raw(const String &p_str) { + + int strlen = p_str.length(); + CharString cstr = p_str.ascii(); + + int arr_len; + DVector buf; + { + buf.resize(strlen / 4 * 3 + 1); + DVector::Write w = buf.write(); + + arr_len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen); + }; + buf.resize(arr_len); + + // conversion from DVector 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 b64buff; + b64buff.resize(b64len); + DVector::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 buf; + buf.resize(strlen / 4 * 3 + 1 + 1); + DVector::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 74f29c23e86..83f9e533eab 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& p_arr); + DVector base64_to_raw(const String& p_str); + + String utf8_to_base64(const String& p_str); + String base64_to_utf8(const String& p_str); + _Marshalls() {}; };