summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/file_access_encrypted.cpp36
-rw-r--r--core/io/file_access_pack.cpp2
-rw-r--r--core/io/marshalls.cpp6
-rw-r--r--core/io/packet_peer.cpp5
-rw-r--r--core/io/resource_format_binary.cpp22
-rw-r--r--core/io/resource_saver.cpp1
-rw-r--r--core/io/stream_peer.cpp8
-rw-r--r--core/io/stream_peer_ssl.cpp4
-rw-r--r--core/io/stream_peer_tcp.cpp17
-rw-r--r--core/io/stream_peer_tcp.h1
-rw-r--r--core/io/tcp_server.cpp7
-rw-r--r--core/io/tcp_server.h1
12 files changed, 62 insertions, 48 deletions
diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp
index 7dea749a43..ccee6aeb15 100644
--- a/core/io/file_access_encrypted.cpp
+++ b/core/io/file_access_encrypted.cpp
@@ -30,13 +30,11 @@
#include "file_access_encrypted.h"
+#include "core/math/crypto_core.h"
#include "core/os/copymem.h"
#include "core/print_string.h"
#include "core/variant.h"
-#include "thirdparty/misc/aes256.h"
-#include "thirdparty/misc/md5.h"
-
#include <stdio.h>
#define COMP_MAGIC 0x43454447
@@ -83,25 +81,21 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8
uint32_t blen = p_base->get_buffer(data.ptrw(), ds);
ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
- aes256_context ctx;
- aes256_init(&ctx, key.ptrw());
+ CryptoCore::AESContext ctx;
+ ctx.set_decode_key(key.ptrw(), 256);
for (size_t i = 0; i < ds; i += 16) {
- aes256_decrypt_ecb(&ctx, &data.write[i]);
+ ctx.decrypt_ecb(&data.write[i], &data.write[i]);
}
- aes256_done(&ctx);
-
data.resize(length);
- MD5_CTX md5;
- MD5Init(&md5);
- MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
- MD5Final(&md5);
+ unsigned char hash[16];
+ ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG);
ERR_EXPLAIN("The MD5 sum of the decrypted file does not match the expected value. It could be that the file is corrupt, or that the provided decryption key is invalid.");
- ERR_FAIL_COND_V(String::md5(md5.digest) != String::md5(md5d), ERR_FILE_CORRUPT);
+ ERR_FAIL_COND_V(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT);
file = p_base;
}
@@ -140,10 +134,8 @@ void FileAccessEncrypted::close() {
len += 16 - (len % 16);
}
- MD5_CTX md5;
- MD5Init(&md5);
- MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
- MD5Final(&md5);
+ unsigned char hash[16];
+ ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
compressed.resize(len);
zeromem(compressed.ptrw(), len);
@@ -151,20 +143,18 @@ void FileAccessEncrypted::close() {
compressed.write[i] = data[i];
}
- aes256_context ctx;
- aes256_init(&ctx, key.ptrw());
+ CryptoCore::AESContext ctx;
+ ctx.set_encode_key(key.ptrw(), 256);
for (size_t i = 0; i < len; i += 16) {
- aes256_encrypt_ecb(&ctx, &compressed.write[i]);
+ ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]);
}
- aes256_done(&ctx);
-
file->store_32(COMP_MAGIC);
file->store_32(mode);
- file->store_buffer(md5.digest, 16);
+ file->store_buffer(hash, 16);
file->store_64(data.size());
file->store_buffer(compressed.ptr(), compressed.size());
diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp
index d38d09c6bb..ca66b34e17 100644
--- a/core/io/file_access_pack.cpp
+++ b/core/io/file_access_pack.cpp
@@ -144,7 +144,7 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) {
uint32_t magic = f->get_32();
if (magic != 0x43504447) {
- //maybe at he end.... self contained exe
+ //maybe at the end.... self contained exe
f->seek_end();
f->seek(f->get_position() - 4);
magic = f->get_32();
diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp
index 17a3f52a65..dc5581ea01 100644
--- a/core/io/marshalls.cpp
+++ b/core/io/marshalls.cpp
@@ -558,8 +558,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
w[i] = buf[i];
}
-
- w = PoolVector<uint8_t>::Write();
}
r_variant = data;
@@ -590,8 +588,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
w[i] = decode_uint32(&buf[i * 4]);
}
-
- w = PoolVector<int>::Write();
}
r_variant = Variant(data);
if (r_len) {
@@ -618,8 +614,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
w[i] = decode_float(&buf[i * 4]);
}
-
- w = PoolVector<float>::Write();
}
r_variant = data;
diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp
index c77c81f9e2..1e4ea715b3 100644
--- a/core/io/packet_peer.cpp
+++ b/core/io/packet_peer.cpp
@@ -110,10 +110,11 @@ Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) {
Variant PacketPeer::_bnd_get_var(bool p_allow_objects) {
Variant var;
- get_var(var, p_allow_objects);
+ Error err = get_var(var, p_allow_objects);
+ ERR_FAIL_COND_V(err != OK, Variant());
return var;
-};
+}
Error PacketPeer::_put_packet(const PoolVector<uint8_t> &p_buffer) {
return put_packet_buffer(p_buffer);
diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp
index 688dfc21e5..146480e5a2 100644
--- a/core/io/resource_format_binary.cpp
+++ b/core/io/resource_format_binary.cpp
@@ -410,7 +410,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
PoolVector<uint8_t>::Write w = array.write();
f->get_buffer(w.ptr(), len);
_advance_padding(len);
- w = PoolVector<uint8_t>::Write();
+ w.release();
r_v = array;
} break;
@@ -432,7 +432,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
}
#endif
- w = PoolVector<int>::Write();
+ w.release();
r_v = array;
} break;
case VARIANT_REAL_ARRAY: {
@@ -454,7 +454,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
#endif
- w = PoolVector<real_t>::Write();
+ w.release();
r_v = array;
} break;
case VARIANT_STRING_ARRAY: {
@@ -465,7 +465,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
PoolVector<String>::Write w = array.write();
for (uint32_t i = 0; i < len; i++)
w[i] = get_unicode_string();
- w = PoolVector<String>::Write();
+ w.release();
r_v = array;
} break;
@@ -493,7 +493,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
ERR_EXPLAIN("Vector2 size is NOT 8!");
ERR_FAIL_V(ERR_UNAVAILABLE);
}
- w = PoolVector<Vector2>::Write();
+ w.release();
r_v = array;
} break;
@@ -521,7 +521,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
ERR_EXPLAIN("Vector3 size is NOT 12!");
ERR_FAIL_V(ERR_UNAVAILABLE);
}
- w = PoolVector<Vector3>::Write();
+ w.release();
r_v = array;
} break;
@@ -549,7 +549,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
ERR_EXPLAIN("Color size is NOT 16!");
ERR_FAIL_V(ERR_UNAVAILABLE);
}
- w = PoolVector<Color>::Write();
+ w.release();
r_v = array;
} break;
#ifndef DISABLE_DEPRECATED
@@ -584,7 +584,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
PoolVector<uint8_t>::Write w = imgdata.write();
f->get_buffer(w.ptr(), datalen);
_advance_padding(datalen);
- w = PoolVector<uint8_t>::Write();
+ w.release();
Ref<Image> image;
image.instance();
@@ -597,7 +597,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) {
data.resize(f->get_32());
PoolVector<uint8_t>::Write w = data.write();
f->get_buffer(w.ptr(), data.size());
- w = PoolVector<uint8_t>::Write();
+ w.release();
Ref<Image> image;
@@ -712,14 +712,14 @@ Error ResourceInteractiveLoaderBinary::poll() {
if (!obj) {
error = ERR_FILE_CORRUPT;
ERR_EXPLAIN(local_path + ":Resource of unrecognized type in file: " + t);
+ ERR_FAIL_V(ERR_FILE_CORRUPT);
}
- ERR_FAIL_COND_V(!obj, ERR_FILE_CORRUPT);
Resource *r = Object::cast_to<Resource>(obj);
if (!r) {
error = ERR_FILE_CORRUPT;
- memdelete(obj); //bye
ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_class());
+ memdelete(obj); //bye
ERR_FAIL_V(ERR_FILE_CORRUPT);
}
diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp
index 0cecca904d..e2c1c3402a 100644
--- a/core/io/resource_saver.cpp
+++ b/core/io/resource_saver.cpp
@@ -137,7 +137,6 @@ Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t
save_callback(p_resource, p_path);
return OK;
- } else {
}
}
diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp
index 6ad24a5f3a..84b8554d54 100644
--- a/core/io/stream_peer.cpp
+++ b/core/io/stream_peer.cpp
@@ -79,7 +79,7 @@ Array StreamPeer::_get_data(int p_bytes) {
PoolVector<uint8_t>::Write w = data.write();
Error err = get_data(&w[0], p_bytes);
- w = PoolVector<uint8_t>::Write();
+ w.release();
ret.push_back(err);
ret.push_back(data);
return ret;
@@ -101,7 +101,7 @@ Array StreamPeer::_get_partial_data(int p_bytes) {
PoolVector<uint8_t>::Write w = data.write();
int received;
Error err = get_partial_data(&w[0], p_bytes, received);
- w = PoolVector<uint8_t>::Write();
+ w.release();
if (err != OK) {
data.resize(0);
@@ -369,7 +369,9 @@ Variant StreamPeer::get_var(bool p_allow_objects) {
ERR_FAIL_COND_V(err != OK, Variant());
Variant ret;
- decode_variant(ret, var.ptr(), len, NULL, p_allow_objects);
+ err = decode_variant(ret, var.ptr(), len, NULL, p_allow_objects);
+ ERR_FAIL_COND_V(err != OK, Variant());
+
return ret;
}
diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_ssl.cpp
index 254ae84bf5..ccce48ccd7 100644
--- a/core/io/stream_peer_ssl.cpp
+++ b/core/io/stream_peer_ssl.cpp
@@ -39,7 +39,9 @@ StreamPeerSSL *(*StreamPeerSSL::_create)() = NULL;
StreamPeerSSL *StreamPeerSSL::create() {
- return _create();
+ if (_create)
+ return _create();
+ return NULL;
}
StreamPeerSSL::LoadCertsFromMemory StreamPeerSSL::load_certs_func = NULL;
diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp
index bcdae343b8..310bb12bc0 100644
--- a/core/io/stream_peer_tcp.cpp
+++ b/core/io/stream_peer_tcp.cpp
@@ -30,6 +30,8 @@
#include "stream_peer_tcp.h"
+#include "core/project_settings.h"
+
Error StreamPeerTCP::_poll_connection() {
ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED);
@@ -40,6 +42,12 @@ Error StreamPeerTCP::_poll_connection() {
status = STATUS_CONNECTED;
return OK;
} else if (err == ERR_BUSY) {
+ // Check for connect timeout
+ if (OS::get_singleton()->get_ticks_msec() > timeout) {
+ disconnect_from_host();
+ status = STATUS_ERROR;
+ return ERR_CONNECTION_ERROR;
+ }
// Still trying to connect
return OK;
}
@@ -54,6 +62,7 @@ void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint
_sock = p_sock;
_sock->set_blocking_enabled(false);
+ timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
status = STATUS_CONNECTING;
peer_host = p_host;
@@ -74,6 +83,7 @@ Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, uint16_t p_port)
_sock->set_blocking_enabled(false);
+ timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
err = _sock->connect_to_host(p_host, p_port);
if (err == OK) {
@@ -217,6 +227,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;
+ }
}
}
@@ -276,6 +291,7 @@ void StreamPeerTCP::disconnect_from_host() {
if (_sock.is_valid() && _sock->is_open())
_sock->close();
+ timeout = 0;
status = STATUS_NONE;
peer_host = IP_Address();
peer_port = 0;
@@ -351,6 +367,7 @@ void StreamPeerTCP::_bind_methods() {
StreamPeerTCP::StreamPeerTCP() :
_sock(Ref<NetSocket>(NetSocket::create())),
+ timeout(0),
status(STATUS_NONE),
peer_port(0) {
}
diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h
index 1ca39375aa..321fb3a6c8 100644
--- a/core/io/stream_peer_tcp.h
+++ b/core/io/stream_peer_tcp.h
@@ -52,6 +52,7 @@ public:
protected:
Ref<NetSocket> _sock;
+ uint64_t timeout;
Status status;
IP_Address peer_host;
uint16_t peer_port;
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();