diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/marshalls.cpp | 11 | ||||
-rw-r--r-- | core/io/marshalls.h | 2 | ||||
-rw-r--r-- | core/io/resource_uid.cpp | 23 | ||||
-rw-r--r-- | core/io/resource_uid.h | 5 |
4 files changed, 19 insertions, 22 deletions
diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index a363cc3694..5c39b2fa1b 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -94,7 +94,8 @@ static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r return OK; } -Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects) { +Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects, int p_depth) { + ERR_FAIL_COND_V_MSG(p_depth > Variant::MAX_RECURSION_DEPTH, ERR_OUT_OF_MEMORY, "Variant is too deep. Bailing."); const uint8_t *buf = p_buffer; int len = p_len; @@ -585,7 +586,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Variant value; int used; - err = decode_variant(value, buf, len, &used, p_allow_objects); + err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1); if (err) { return err; } @@ -635,7 +636,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Variant key, value; int used; - Error err = decode_variant(key, buf, len, &used, p_allow_objects); + Error err = decode_variant(key, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; @@ -644,7 +645,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int (*r_len) += used; } - err = decode_variant(value, buf, len, &used, p_allow_objects); + err = decode_variant(value, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; @@ -677,7 +678,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int for (int i = 0; i < count; i++) { int used = 0; Variant v; - Error err = decode_variant(v, buf, len, &used, p_allow_objects); + Error err = decode_variant(v, buf, len, &used, p_allow_objects, p_depth + 1); ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; len -= used; diff --git a/core/io/marshalls.h b/core/io/marshalls.h index 4d7b98b749..fef3a1c2c1 100644 --- a/core/io/marshalls.h +++ b/core/io/marshalls.h @@ -212,7 +212,7 @@ public: EncodedObjectAsID() {} }; -Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false); +Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false, int p_depth = 0); Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects = false, int p_depth = 0); #endif // MARSHALLS_H diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp index 776756e64e..d0335bed3a 100644 --- a/core/io/resource_uid.cpp +++ b/core/io/resource_uid.cpp @@ -31,7 +31,7 @@ #include "resource_uid.h" #include "core/config/project_settings.h" -#include "core/crypto/crypto.h" +#include "core/crypto/crypto_core.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" @@ -82,20 +82,14 @@ ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const { return ID(uid & 0x7FFFFFFFFFFFFFFF); } -ResourceUID::ID ResourceUID::create_id() const { - mutex.lock(); - if (crypto.is_null()) { - crypto = Ref<Crypto>(Crypto::create()); - } - mutex.unlock(); +ResourceUID::ID ResourceUID::create_id() { while (true) { - PackedByteArray bytes = crypto->generate_random_bytes(8); - ERR_FAIL_COND_V(bytes.size() != 8, INVALID_ID); - const uint64_t *ptr64 = (const uint64_t *)bytes.ptr(); - ID id = int64_t((*ptr64) & 0x7FFFFFFFFFFFFFFF); - mutex.lock(); + ID id = INVALID_ID; + MutexLock lock(mutex); + Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id)); + ERR_FAIL_COND_V(err != OK, INVALID_ID); + id &= 0x7FFFFFFFFFFFFFFF; bool exists = unique_ids.has(id); - mutex.unlock(); if (!exists) { return id; } @@ -261,6 +255,9 @@ ResourceUID *ResourceUID::singleton = nullptr; ResourceUID::ResourceUID() { ERR_FAIL_COND(singleton != nullptr); singleton = this; + crypto = memnew(CryptoCore::RandomGenerator); + ((CryptoCore::RandomGenerator *)crypto)->init(); } ResourceUID::~ResourceUID() { + memdelete((CryptoCore::RandomGenerator *)crypto); } diff --git a/core/io/resource_uid.h b/core/io/resource_uid.h index 9f2ab5245b..1ea44b9d06 100644 --- a/core/io/resource_uid.h +++ b/core/io/resource_uid.h @@ -35,7 +35,6 @@ #include "core/string/string_name.h" #include "core/templates/ordered_hash_map.h" -class Crypto; class ResourceUID : public Object { GDCLASS(ResourceUID, Object) public: @@ -47,7 +46,7 @@ public: static String get_cache_file(); private: - mutable Ref<Crypto> crypto; + void *crypto; // CryptoCore::RandomGenerator (avoid including crypto_core.h) Mutex mutex; struct Cache { CharString cs; @@ -67,7 +66,7 @@ public: String id_to_text(ID p_id) const; ID text_to_id(const String &p_text) const; - ID create_id() const; + ID create_id(); bool has_id(ID p_id) const; void add_id(ID p_id, const String &p_path); void set_id(ID p_id, const String &p_path); |