feat: updated engine version to 4.4-rc1
This commit is contained in:
parent
ee00efde1f
commit
21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions
|
|
@ -31,13 +31,10 @@
|
|||
#include "file_access_encrypted.h"
|
||||
|
||||
#include "core/crypto/crypto_core.h"
|
||||
#include "core/string/print_string.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic) {
|
||||
ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open.");
|
||||
Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic, const Vector<uint8_t> &p_iv) {
|
||||
ERR_FAIL_COND_V_MSG(file.is_valid(), ERR_ALREADY_IN_USE, vformat("Can't open file while another file from path '%s' is open.", file->get_path_absolute()));
|
||||
ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
|
||||
|
||||
pos = 0;
|
||||
|
|
@ -49,6 +46,16 @@ Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<u
|
|||
writing = true;
|
||||
file = p_base;
|
||||
key = p_key;
|
||||
if (p_iv.is_empty()) {
|
||||
iv.resize(16);
|
||||
CryptoCore::RandomGenerator rng;
|
||||
ERR_FAIL_COND_V_MSG(rng.init(), FAILED, "Failed to initialize random number generator.");
|
||||
Error err = rng.get_random_bytes(iv.ptrw(), 16);
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
} else {
|
||||
ERR_FAIL_COND_V(p_iv.size() != 16, ERR_INVALID_PARAMETER);
|
||||
iv = p_iv;
|
||||
}
|
||||
|
||||
} else if (p_mode == MODE_READ) {
|
||||
writing = false;
|
||||
|
|
@ -63,10 +70,8 @@ Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<u
|
|||
p_base->get_buffer(md5d, 16);
|
||||
length = p_base->get_64();
|
||||
|
||||
unsigned char iv[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
iv[i] = p_base->get_8();
|
||||
}
|
||||
iv.resize(16);
|
||||
p_base->get_buffer(iv.ptrw(), 16);
|
||||
|
||||
base = p_base->get_position();
|
||||
ERR_FAIL_COND_V(p_base->get_length() < base + length, ERR_FILE_CORRUPT);
|
||||
|
|
@ -83,7 +88,7 @@ Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<u
|
|||
CryptoCore::AESContext ctx;
|
||||
|
||||
ctx.set_encode_key(key.ptrw(), 256); // Due to the nature of CFB, same key schedule is used for both encryption and decryption!
|
||||
ctx.decrypt_cfb(ds, iv, data.ptrw(), data.ptrw());
|
||||
ctx.decrypt_cfb(ds, iv.ptrw(), data.ptrw(), data.ptrw());
|
||||
}
|
||||
|
||||
data.resize(length);
|
||||
|
|
@ -145,14 +150,9 @@ void FileAccessEncrypted::_close() {
|
|||
|
||||
file->store_buffer(hash, 16);
|
||||
file->store_64(data.size());
|
||||
file->store_buffer(iv.ptr(), 16);
|
||||
|
||||
unsigned char iv[16];
|
||||
for (int i = 0; i < 16; i++) {
|
||||
iv[i] = Math::rand() % 256;
|
||||
file->store_8(iv[i]);
|
||||
}
|
||||
|
||||
ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw());
|
||||
ctx.encrypt_cfb(len, iv.ptrw(), compressed.ptrw(), compressed.ptrw());
|
||||
|
||||
file->store_buffer(compressed.ptr(), compressed.size());
|
||||
data.clear();
|
||||
|
|
@ -162,7 +162,7 @@ void FileAccessEncrypted::_close() {
|
|||
}
|
||||
|
||||
bool FileAccessEncrypted::is_open() const {
|
||||
return file != nullptr;
|
||||
return file.is_valid();
|
||||
}
|
||||
|
||||
String FileAccessEncrypted::get_path() const {
|
||||
|
|
@ -206,26 +206,19 @@ bool FileAccessEncrypted::eof_reached() const {
|
|||
return eofed;
|
||||
}
|
||||
|
||||
uint8_t FileAccessEncrypted::get_8() const {
|
||||
ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
|
||||
if (pos >= get_length()) {
|
||||
eofed = true;
|
||||
uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
|
||||
ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
|
||||
|
||||
if (!p_length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t b = data[pos];
|
||||
pos++;
|
||||
return b;
|
||||
}
|
||||
|
||||
uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
|
||||
ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
|
||||
ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
|
||||
ERR_FAIL_NULL_V(p_dst, -1);
|
||||
|
||||
uint64_t to_copy = MIN(p_length, get_length() - pos);
|
||||
for (uint64_t i = 0; i < to_copy; i++) {
|
||||
p_dst[i] = data[pos++];
|
||||
}
|
||||
|
||||
memcpy(p_dst, data.ptr() + pos, to_copy);
|
||||
pos += to_copy;
|
||||
|
||||
if (to_copy < p_length) {
|
||||
eofed = true;
|
||||
|
|
@ -238,21 +231,23 @@ Error FileAccessEncrypted::get_error() const {
|
|||
return eofed ? ERR_FILE_EOF : OK;
|
||||
}
|
||||
|
||||
void FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) {
|
||||
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
|
||||
ERR_FAIL_COND(!p_src && p_length > 0);
|
||||
bool FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) {
|
||||
ERR_FAIL_COND_V_MSG(!writing, false, "File has not been opened in write mode.");
|
||||
|
||||
if (pos < get_length()) {
|
||||
for (uint64_t i = 0; i < p_length; i++) {
|
||||
store_8(p_src[i]);
|
||||
}
|
||||
} else if (pos == get_length()) {
|
||||
data.resize(pos + p_length);
|
||||
for (uint64_t i = 0; i < p_length; i++) {
|
||||
data.write[pos + i] = p_src[i];
|
||||
}
|
||||
pos += p_length;
|
||||
if (!p_length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ERR_FAIL_NULL_V(p_src, false);
|
||||
|
||||
if (pos + p_length >= get_length()) {
|
||||
ERR_FAIL_COND_V(data.resize(pos + p_length) != OK, false);
|
||||
}
|
||||
|
||||
memcpy(data.ptrw() + pos, p_src, p_length);
|
||||
pos += p_length;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileAccessEncrypted::flush() {
|
||||
|
|
@ -261,18 +256,6 @@ void FileAccessEncrypted::flush() {
|
|||
// encrypted files keep data in memory till close()
|
||||
}
|
||||
|
||||
void FileAccessEncrypted::store_8(uint8_t p_dest) {
|
||||
ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
|
||||
|
||||
if (pos < get_length()) {
|
||||
data.write[pos] = p_dest;
|
||||
pos++;
|
||||
} else if (pos == get_length()) {
|
||||
data.push_back(p_dest);
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileAccessEncrypted::file_exists(const String &p_name) {
|
||||
Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ);
|
||||
if (fa.is_null()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue