From fcd8faf2f44ba5b1a104949663dfcd8f856f3112 Mon Sep 17 00:00:00 2001 From: Vasiliy Makarov Date: Thu, 27 Jun 2019 14:57:45 +0300 Subject: [PATCH] Add encrypted files support to ConfigFile Fix #26477 Add in ConfigFile this methods: load_encrypted(path, key) load_encrypted_pass(path, password) save_encrypted(path, key) save_encrypted_pass(path, password) --- core/io/config_file.cpp | 94 ++++++++++++++++++++++++++++++++++++++++- core/io/config_file.h | 9 ++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 871e21df3ec..f7fb72c0899 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -30,7 +30,7 @@ #include "config_file.h" -#include "core/os/file_access.h" +#include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" #include "core/variant_parser.h" @@ -137,6 +137,48 @@ Error ConfigFile::save(const String &p_path) { return err; } + return _internal_save(file); +} + +Error ConfigFile::save_encrypted(const String &p_path, const Vector &p_key) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + return _internal_save(fae); +} + +Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + + return _internal_save(fae); +} + +Error ConfigFile::_internal_save(FileAccess *file) { + for (OrderedHashMap >::Element E = values.front(); E; E = E.next()) { if (E != values.front()) @@ -164,6 +206,48 @@ Error ConfigFile::load(const String &p_path) { if (!f) return ERR_CANT_OPEN; + return _internal_load(p_path, f); +} + +Error ConfigFile::load_encrypted(const String &p_path, const Vector &p_key) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + return _internal_load(p_path, fae); +} + +Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + + return _internal_load(p_path, fae); +} + +Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { + VariantParser::StreamFile stream; stream.f = f; @@ -182,7 +266,7 @@ Error ConfigFile::load(const String &p_path) { next_tag.fields.clear(); next_tag.name = String(); - err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true); + Error err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true); if (err == ERR_FILE_EOF) { memdelete(f); return OK; @@ -215,6 +299,12 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); + + ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); + ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "pass"), &ConfigFile::load_encrypted_pass); + + ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted); + ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "pass"), &ConfigFile::save_encrypted_pass); } ConfigFile::ConfigFile() { diff --git a/core/io/config_file.h b/core/io/config_file.h index 36e5c0ca7d3..3ab6fef8682 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -32,6 +32,7 @@ #define CONFIG_FILE_H #include "core/ordered_hash_map.h" +#include "core/os/file_access.h" #include "core/reference.h" class ConfigFile : public Reference { @@ -42,6 +43,8 @@ class ConfigFile : public Reference { PoolStringArray _get_sections() const; PoolStringArray _get_section_keys(const String &p_section) const; + Error _internal_load(const String &p_path, FileAccess *f); + Error _internal_save(FileAccess *file); protected: static void _bind_methods(); @@ -61,6 +64,12 @@ public: Error save(const String &p_path); Error load(const String &p_path); + Error load_encrypted(const String &p_path, const Vector &p_key); + Error load_encrypted_pass(const String &p_path, const String &p_pass); + + Error save_encrypted(const String &p_path, const Vector &p_key); + Error save_encrypted_pass(const String &p_path, const String &p_pass); + ConfigFile(); };