godot/core/io/file_access_memory.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
5 KiB
C++
Raw Normal View History

2014-02-09 22:10:30 -03:00
/**************************************************************************/
/* file_access_memory.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2014-02-09 22:10:30 -03:00
#include "file_access_memory.h"
#include "core/config/project_settings.h"
2014-02-09 22:10:30 -03:00
static HashMap<String, Vector<uint8_t>> *files = nullptr;
2014-02-09 22:10:30 -03:00
void FileAccessMemory::register_file(const String &p_name, const Vector<uint8_t> &p_data) {
2014-02-09 22:10:30 -03:00
if (!files) {
files = memnew((HashMap<String, Vector<uint8_t>>));
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
String name;
if (ProjectSettings::get_singleton()) {
name = ProjectSettings::get_singleton()->globalize_path(p_name);
} else {
2014-02-09 22:10:30 -03:00
name = p_name;
}
//name = DirAccess::normalize_path(name);
2014-02-09 22:10:30 -03:00
(*files)[name] = p_data;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
void FileAccessMemory::cleanup() {
if (!files) {
2014-02-09 22:10:30 -03:00
return;
}
2014-02-09 22:10:30 -03:00
memdelete(files);
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
Ref<FileAccess> FileAccessMemory::create() {
2014-02-09 22:10:30 -03:00
return memnew(FileAccessMemory);
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
bool FileAccessMemory::file_exists(const String &p_name) {
String name = fix_path(p_name);
//name = DirAccess::normalize_path(name);
2014-02-09 22:10:30 -03:00
2020-04-02 01:20:12 +02:00
return files && (files->find(name) != nullptr);
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) {
2015-05-31 01:59:42 -03:00
data = (uint8_t *)p_data;
length = p_len;
pos = 0;
return OK;
}
Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) {
ERR_FAIL_NULL_V(files, ERR_FILE_NOT_FOUND);
2014-02-09 22:10:30 -03:00
String name = fix_path(p_path);
//name = DirAccess::normalize_path(name);
2014-02-09 22:10:30 -03:00
HashMap<String, Vector<uint8_t>>::Iterator E = files->find(name);
ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s'.", p_path));
2014-02-09 22:10:30 -03:00
data = E->value.ptrw();
length = E->value.size();
2014-02-09 22:10:30 -03:00
pos = 0;
return OK;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
bool FileAccessMemory::is_open() const {
2020-04-02 01:20:12 +02:00
return data != nullptr;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
void FileAccessMemory::seek(uint64_t p_position) {
ERR_FAIL_NULL(data);
2014-02-09 22:10:30 -03:00
pos = p_position;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
void FileAccessMemory::seek_end(int64_t p_position) {
ERR_FAIL_NULL(data);
2014-02-09 22:10:30 -03:00
pos = length + p_position;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
uint64_t FileAccessMemory::get_position() const {
ERR_FAIL_NULL_V(data, 0);
2014-02-09 22:10:30 -03:00
return pos;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
2021-05-25 07:58:49 +01:00
uint64_t FileAccessMemory::get_length() const {
ERR_FAIL_NULL_V(data, 0);
2014-02-09 22:10:30 -03:00
return length;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
bool FileAccessMemory::eof_reached() const {
return pos >= length;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
Core ubsan fixes This fixes UBSAN errors reported by running our testsuite, importing the TPS demo, and running the TPS demo. I have tried, wherever possible, to fix issues related to reported issues but not directly reported by UBSAN because thse code paths just happened to not have been exercised in these cases. These fixes apply only to errors reported, and caused by, core/ The following things have been changed: * Make sure there are no implicit sign changing casts in core. * Explicitly type enums that are part of a public API such that users of the API cannot pass in wrongly-sized values leading to potential stack corruption. * Ensure that memcpy is never called with invalid or null pointers as this is undefined behavior, and when the engine is built with optimizations turned on leads to memory corruption and hard to debug crashes. * Replace enum values only used as static values with constexpr static const values instead. This has no runtime overhead. This makes it so that the size of the enums is explicit. * Make sure that nan and inf is handled consistently in String. * Implement a _to_int template to ensure that all of the paths use the same algorhithm, and correct the negative integer case. * Changed the way the json serializer precision work, and added tests to verify the new behavior. The behavior doesn't quite match master in particulary for negative doubles as the original code tried to cast -inf to an int. This then led to negative doubles losing all but one of their decimal points when serializing. Behavior in GDScript remains unchanged.
2024-12-14 02:17:09 +01:00
if (!p_length) {
return 0;
}
ERR_FAIL_NULL_V(p_dst, -1);
ERR_FAIL_NULL_V(data, -1);
2014-02-09 22:10:30 -03:00
uint64_t left = length - pos;
uint64_t read = MIN(p_length, left);
2014-02-09 22:10:30 -03:00
if (read < p_length) {
WARN_PRINT("Reading less data than requested");
}
2014-02-09 22:10:30 -03:00
memcpy(p_dst, &data[pos], read);
pos += read;
2014-02-09 22:10:30 -03:00
return read;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
Error FileAccessMemory::get_error() const {
return pos >= length ? ERR_FILE_EOF : OK;
2015-05-03 01:48:20 +02:00
}
2014-02-09 22:10:30 -03:00
void FileAccessMemory::flush() {
ERR_FAIL_NULL(data);
}
bool FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) {
Core ubsan fixes This fixes UBSAN errors reported by running our testsuite, importing the TPS demo, and running the TPS demo. I have tried, wherever possible, to fix issues related to reported issues but not directly reported by UBSAN because thse code paths just happened to not have been exercised in these cases. These fixes apply only to errors reported, and caused by, core/ The following things have been changed: * Make sure there are no implicit sign changing casts in core. * Explicitly type enums that are part of a public API such that users of the API cannot pass in wrongly-sized values leading to potential stack corruption. * Ensure that memcpy is never called with invalid or null pointers as this is undefined behavior, and when the engine is built with optimizations turned on leads to memory corruption and hard to debug crashes. * Replace enum values only used as static values with constexpr static const values instead. This has no runtime overhead. This makes it so that the size of the enums is explicit. * Make sure that nan and inf is handled consistently in String. * Implement a _to_int template to ensure that all of the paths use the same algorhithm, and correct the negative integer case. * Changed the way the json serializer precision work, and added tests to verify the new behavior. The behavior doesn't quite match master in particulary for negative doubles as the original code tried to cast -inf to an int. This then led to negative doubles losing all but one of their decimal points when serializing. Behavior in GDScript remains unchanged.
2024-12-14 02:17:09 +01:00
if (!p_length) {
return true;
}
ERR_FAIL_NULL_V(p_src, false);
2024-05-20 18:07:27 +02:00
uint64_t left = length - pos;
uint64_t write = MIN(p_length, left);
2024-05-20 18:07:27 +02:00
memcpy(&data[pos], p_src, write);
pos += write;
ERR_FAIL_COND_V_MSG(write < p_length, false, "Writing less data than requested.");
return true;
2015-05-03 01:48:20 +02:00
}