2019-07-18 04:04:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include <stratosphere/kvdb/kvdb_archive.hpp>
|
|
|
|
|
|
|
|
namespace sts::kvdb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Convenience definitions. */
|
|
|
|
constexpr u8 ArchiveHeaderMagic[4] = {'I', 'M', 'K', 'V'};
|
|
|
|
constexpr u8 ArchiveEntryMagic[4] = {'I', 'M', 'E', 'N'};
|
|
|
|
|
|
|
|
/* Archive types. */
|
|
|
|
struct ArchiveHeader {
|
|
|
|
u8 magic[sizeof(ArchiveHeaderMagic)];
|
|
|
|
u32 pad;
|
|
|
|
u32 entry_count;
|
|
|
|
|
|
|
|
Result Validate() const {
|
|
|
|
if (std::memcmp(this->magic, ArchiveHeaderMagic, sizeof(ArchiveHeaderMagic)) != 0) {
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultInvalidKeyValue();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ArchiveHeader Make(size_t entry_count) {
|
|
|
|
ArchiveHeader header = {};
|
|
|
|
std::memcpy(header.magic, ArchiveHeaderMagic, sizeof(ArchiveHeaderMagic));
|
|
|
|
header.entry_count = static_cast<u32>(entry_count);
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ArchiveHeader) == 0xC && std::is_pod<ArchiveHeader>::value, "ArchiveHeader definition!");
|
|
|
|
|
|
|
|
struct ArchiveEntryHeader {
|
|
|
|
u8 magic[sizeof(ArchiveEntryMagic)];
|
|
|
|
u32 key_size;
|
|
|
|
u32 value_size;
|
|
|
|
|
|
|
|
Result Validate() const {
|
|
|
|
if (std::memcmp(this->magic, ArchiveEntryMagic, sizeof(ArchiveEntryMagic)) != 0) {
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultInvalidKeyValue();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ArchiveEntryHeader Make(size_t ksz, size_t vsz) {
|
|
|
|
ArchiveEntryHeader header = {};
|
|
|
|
std::memcpy(header.magic, ArchiveEntryMagic, sizeof(ArchiveEntryMagic));
|
|
|
|
header.key_size = ksz;
|
|
|
|
header.value_size = vsz;
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ArchiveEntryHeader) == 0xC && std::is_pod<ArchiveEntryHeader>::value, "ArchiveEntryHeader definition!");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reader functionality. */
|
|
|
|
Result ArchiveReader::Peek(void *dst, size_t size) {
|
|
|
|
/* Bounds check. */
|
|
|
|
if (this->offset + size > this->buffer.GetSize() || this->offset + size <= this->offset) {
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultInvalidKeyValue();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(dst, this->buffer.Get() + this->offset, size);
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ArchiveReader::Read(void *dst, size_t size) {
|
|
|
|
R_TRY(this->Peek(dst, size));
|
|
|
|
this->offset += size;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ArchiveReader::ReadEntryCount(size_t *out) {
|
|
|
|
/* This should only be called at the start of reading stream. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(this->offset == 0);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* Read and validate header. */
|
|
|
|
ArchiveHeader header;
|
|
|
|
R_TRY(this->Read(&header, sizeof(header)));
|
|
|
|
R_TRY(header.Validate());
|
|
|
|
|
|
|
|
*out = header.entry_count;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ArchiveReader::GetEntrySize(size_t *out_key_size, size_t *out_value_size) {
|
|
|
|
/* This should only be called after ReadEntryCount. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(this->offset != 0);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* Peek the next entry header. */
|
|
|
|
ArchiveEntryHeader header;
|
|
|
|
R_TRY(this->Peek(&header, sizeof(header)));
|
|
|
|
R_TRY(header.Validate());
|
|
|
|
|
|
|
|
*out_key_size = header.key_size;
|
|
|
|
*out_value_size = header.value_size;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ArchiveReader::ReadEntry(void *out_key, size_t key_size, void *out_value, size_t value_size) {
|
|
|
|
/* This should only be called after ReadEntryCount. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(this->offset != 0);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
/* Read the next entry header. */
|
|
|
|
ArchiveEntryHeader header;
|
|
|
|
R_TRY(this->Read(&header, sizeof(header)));
|
|
|
|
R_TRY(header.Validate());
|
|
|
|
|
|
|
|
/* Key size and Value size must be correct. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(key_size == header.key_size);
|
|
|
|
STS_ASSERT(value_size == header.value_size);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
R_ASSERT(this->Read(out_key, key_size));
|
|
|
|
R_ASSERT(this->Read(out_value, value_size));
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Writer functionality. */
|
|
|
|
Result ArchiveWriter::Write(const void *src, size_t size) {
|
|
|
|
/* Bounds check. */
|
|
|
|
if (this->offset + size > this->buffer.GetSize() || this->offset + size <= this->offset) {
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultInvalidKeyValue();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(this->buffer.Get() + this->offset, src, size);
|
|
|
|
this->offset += size;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-18 04:04:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveWriter::WriteHeader(size_t entry_count) {
|
|
|
|
/* This should only be called at start of write. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(this->offset == 0);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
ArchiveHeader header = ArchiveHeader::Make(entry_count);
|
|
|
|
R_ASSERT(this->Write(&header, sizeof(header)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveWriter::WriteEntry(const void *key, size_t key_size, const void *value, size_t value_size) {
|
|
|
|
/* This should only be called after writing header. */
|
2019-09-28 02:04:58 +01:00
|
|
|
STS_ASSERT(this->offset != 0);
|
2019-07-18 04:04:00 +01:00
|
|
|
|
|
|
|
ArchiveEntryHeader header = ArchiveEntryHeader::Make(key_size, value_size);
|
|
|
|
R_ASSERT(this->Write(&header, sizeof(header)));
|
|
|
|
R_ASSERT(this->Write(key, key_size));
|
|
|
|
R_ASSERT(this->Write(value, value_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Size helper functionality. */
|
|
|
|
ArchiveSizeHelper::ArchiveSizeHelper() : size(sizeof(ArchiveHeader)) {
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveSizeHelper::AddEntry(size_t key_size, size_t value_size) {
|
|
|
|
this->size += sizeof(ArchiveEntryHeader) + key_size + value_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|