diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-07-04 15:55:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-04 15:55:33 +0200 |
commit | 550f436f8fbea86984a845c821270fba78189143 (patch) | |
tree | 93985664212af0602cea06547273015ee580acc2 /core | |
parent | 7b569e91c0c6b84965cad416b8e86dcfdacbcfc4 (diff) | |
parent | 3380dc963895d1f97d4f06c3a71fe15d1c04d9fe (diff) |
Merge pull request #30263 from Faless/ws/wslay_pr
Use wslay as a WebSocket library
Diffstat (limited to 'core')
-rw-r--r-- | core/bind/core_bind.cpp | 42 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.cpp | 5 | ||||
-rw-r--r-- | core/io/tcp_server.cpp | 7 | ||||
-rw-r--r-- | core/io/tcp_server.h | 1 | ||||
-rw-r--r-- | core/math/crypto_core.cpp | 11 | ||||
-rw-r--r-- | core/math/crypto_core.h | 1 | ||||
-rw-r--r-- | core/ustring.cpp | 21 | ||||
-rw-r--r-- | core/ustring.h | 2 | ||||
-rw-r--r-- | core/variant_call.cpp | 4 |
9 files changed, 58 insertions, 36 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 382ab31f6d..1bf3425d50 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -2432,16 +2432,8 @@ String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) err = encode_variant(p_var, &w[0], len, p_full_objects); ERR_FAIL_COND_V(err != OK, ""); - int b64len = len / 3 * 4 + 4 + 1; - PoolVector<uint8_t> b64buff; - b64buff.resize(b64len); - PoolVector<uint8_t>::Write w64 = b64buff.write(); - - size_t strlen = 0; - ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &w[0], len) != OK, String()); - //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen); - w64[strlen] = 0; - String ret = (char *)&w64[0]; + String ret = CryptoCore::b64_encode_str(&w[0], len); + ERR_FAIL_COND_V(ret == "", ret); return ret; }; @@ -2467,19 +2459,8 @@ Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) { - int len = p_arr.size(); - PoolVector<uint8_t>::Read r = p_arr.read(); - - int b64len = len / 3 * 4 + 4 + 1; - PoolVector<uint8_t> b64buff; - b64buff.resize(b64len); - PoolVector<uint8_t>::Write w64 = b64buff.write(); - - size_t strlen = 0; - ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &r[0], len) != OK, String()); - w64[strlen] = 0; - String ret = (char *)&w64[0]; - + String ret = CryptoCore::b64_encode_str(p_arr.read().ptr(), p_arr.size()); + ERR_FAIL_COND_V(ret == "", ret); return ret; }; @@ -2504,19 +2485,8 @@ PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) { 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; - PoolVector<uint8_t> b64buff; - b64buff.resize(b64len); - PoolVector<uint8_t>::Write w64 = b64buff.write(); - - size_t strlen = 0; - ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, (unsigned char *)cstr.get_data(), len) != OK, String()); - - w64[strlen] = 0; - String ret = (char *)&w64[0]; - + String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length()); + ERR_FAIL_COND_V(ret == "", ret); return ret; }; diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index bcdae343b8..a8dd263484 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -217,6 +217,11 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool to_read -= read; total_read += read; + + if (!p_block) { + r_received = total_read; + return OK; + } } } diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index be87f47d50..a2756164bc 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -34,6 +34,7 @@ void TCP_Server::_bind_methods() { ClassDB::bind_method(D_METHOD("listen", "port", "bind_address"), &TCP_Server::listen, DEFVAL("*")); ClassDB::bind_method(D_METHOD("is_connection_available"), &TCP_Server::is_connection_available); + ClassDB::bind_method(D_METHOD("is_listening"), &TCP_Server::is_listening); ClassDB::bind_method(D_METHOD("take_connection"), &TCP_Server::take_connection); ClassDB::bind_method(D_METHOD("stop"), &TCP_Server::stop); } @@ -75,6 +76,12 @@ Error TCP_Server::listen(uint16_t p_port, const IP_Address &p_bind_address) { return OK; } +bool TCP_Server::is_listening() const { + ERR_FAIL_COND_V(!_sock.is_valid(), false); + + return _sock->is_open(); +} + bool TCP_Server::is_connection_available() const { ERR_FAIL_COND_V(!_sock.is_valid(), false); diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h index 538db175ad..ef64044599 100644 --- a/core/io/tcp_server.h +++ b/core/io/tcp_server.h @@ -50,6 +50,7 @@ protected: public: Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*")); + bool is_listening() const; bool is_connection_available() const; Ref<StreamPeerTCP> take_connection(); diff --git a/core/math/crypto_core.cpp b/core/math/crypto_core.cpp index 6449d94db8..d7ba54e469 100644 --- a/core/math/crypto_core.cpp +++ b/core/math/crypto_core.cpp @@ -120,6 +120,17 @@ Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst } // CryptoCore +String CryptoCore::b64_encode_str(const uint8_t *p_src, int p_src_len) { + int b64len = p_src_len / 3 * 4 + 4 + 1; + PoolVector<uint8_t> b64buff; + b64buff.resize(b64len); + PoolVector<uint8_t>::Write w64 = b64buff.write(); + size_t strlen = 0; + int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len); + w64[strlen] = 0; + return ret ? String() : (const char *)&w64[0]; +} + Error CryptoCore::b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len) { int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len); return ret ? FAILED : OK; diff --git a/core/math/crypto_core.h b/core/math/crypto_core.h index 1cb3c86e18..e28cb5a792 100644 --- a/core/math/crypto_core.h +++ b/core/math/crypto_core.h @@ -79,6 +79,7 @@ public: Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]); }; + static String b64_encode_str(const uint8_t *p_src, int p_src_len); static Error b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len); static Error b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len); diff --git a/core/ustring.cpp b/core/ustring.cpp index 2b312191e2..21ac304a1b 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2257,6 +2257,13 @@ String String::md5_text() const { return String::hex_encode_buffer(hash, 16); } +String String::sha1_text() const { + CharString cs = utf8(); + unsigned char hash[20]; + CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash); + return String::hex_encode_buffer(hash, 20); +} + String String::sha256_text() const { CharString cs = utf8(); unsigned char hash[32]; @@ -2278,6 +2285,20 @@ Vector<uint8_t> String::md5_buffer() const { return ret; }; +Vector<uint8_t> String::sha1_buffer() const { + CharString cs = utf8(); + unsigned char hash[20]; + CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash); + + Vector<uint8_t> ret; + ret.resize(20); + for (int i = 0; i < 20; i++) { + ret.write[i] = hash[i]; + } + + return ret; +} + Vector<uint8_t> String::sha256_buffer() const { CharString cs = utf8(); unsigned char hash[32]; diff --git a/core/ustring.h b/core/ustring.h index a32daabb91..8a52c53238 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -305,8 +305,10 @@ public: uint32_t hash() const; /* hash the string */ uint64_t hash64() const; /* hash the string */ String md5_text() const; + String sha1_text() const; String sha256_text() const; Vector<uint8_t> md5_buffer() const; + Vector<uint8_t> sha1_buffer() const; Vector<uint8_t> sha256_buffer() const; _FORCE_INLINE_ bool empty() const { return length() == 0; } diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 811008e7c8..4c3cbfa484 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -275,8 +275,10 @@ struct _VariantCall { VCALL_LOCALMEM2(String, erase); VCALL_LOCALMEM0R(String, hash); VCALL_LOCALMEM0R(String, md5_text); + VCALL_LOCALMEM0R(String, sha1_text); VCALL_LOCALMEM0R(String, sha256_text); VCALL_LOCALMEM0R(String, md5_buffer); + VCALL_LOCALMEM0R(String, sha1_buffer); VCALL_LOCALMEM0R(String, sha256_buffer); VCALL_LOCALMEM0R(String, empty); VCALL_LOCALMEM0R(String, is_abs_path); @@ -1539,8 +1541,10 @@ void register_variant_methods() { ADDFUNC2(STRING, NIL, String, erase, INT, "position", INT, "chars", varray()); ADDFUNC0R(STRING, INT, String, hash, varray()); ADDFUNC0R(STRING, STRING, String, md5_text, varray()); + ADDFUNC0R(STRING, STRING, String, sha1_text, varray()); ADDFUNC0R(STRING, STRING, String, sha256_text, varray()); ADDFUNC0R(STRING, POOL_BYTE_ARRAY, String, md5_buffer, varray()); + ADDFUNC0R(STRING, POOL_BYTE_ARRAY, String, sha1_buffer, varray()); ADDFUNC0R(STRING, POOL_BYTE_ARRAY, String, sha256_buffer, varray()); ADDFUNC0R(STRING, BOOL, String, empty, varray()); ADDFUNC0R(STRING, BOOL, String, is_abs_path, varray()); |