ncm: update client code to better reflect latest sysupdate

This commit is contained in:
Michael Scire 2022-03-18 20:11:26 -07:00 committed by SciresM
parent ca142889c4
commit 320a946fc7
28 changed files with 1432 additions and 110 deletions

View file

@ -24,11 +24,6 @@ namespace ams::kvdb {
static_assert(N > 0, "BoundedString requires non-zero backing buffer!");
private:
char m_buffer[N];
private:
/* Utility. */
static inline void CheckLength(size_t len) {
AMS_ABORT_UNLESS(len < N);
}
public:
/* Constructors. */
constexpr BoundedString() {
@ -36,7 +31,8 @@ namespace ams::kvdb {
}
explicit constexpr BoundedString(const char *s) {
this->Set(s);
AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
util::Strlcpy(m_buffer, s, N);
}
/* Static constructors. */
@ -44,102 +40,114 @@ namespace ams::kvdb {
return BoundedString<N>(s);
}
static constexpr BoundedString<N> MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) {
static BoundedString<N> MakeFormat(const char *format, ...) __attribute__((format (printf, 1, 2))) {
BoundedString<N> string;
std::va_list args;
va_start(args, format);
CheckLength(util::VSNPrintf(string.m_buffer, N, format, args));
string.m_buffer[N - 1] = 0;
va_end(args);
std::va_list vl;
va_start(vl, format);
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(string.m_buffer, N, format, vl)) < N);
va_end(vl);
return string;
}
/* Getters. */
size_t GetLength() const {
constexpr size_t GetLength() const {
return util::Strnlen(m_buffer, N);
}
const char *Get() const {
constexpr const char *Get() const {
return m_buffer;
}
operator const char *() const {
constexpr operator const char *() const {
return m_buffer;
}
/* Setters. */
void Set(const char *s) {
/* Ensure string can fit in our buffer. */
CheckLength(util::Strnlen(s, N));
std::strncpy(m_buffer, s, N);
m_buffer[N - 1] = 0;
/* Assignment. */
constexpr BoundedString<N> &Assign(const char *s) {
AMS_ABORT_UNLESS(static_cast<size_t>(util::Strnlen(s, N)) < N);
util::Strlcpy(m_buffer, s, N);
return *this;
}
void SetFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
/* Format into the buffer, abort if too large. */
std::va_list args;
va_start(args, format);
CheckLength(util::VSNPrintf(m_buffer, N, format, args));
va_end(args);
BoundedString<N> &AssignFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
std::va_list vl;
va_start(vl, format);
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer, N, format, vl)) < N);
va_end(vl);
return *this;
}
/* Append to existing. */
void Append(const char *s) {
BoundedString<N> &Append(const char *s) {
const size_t length = GetLength();
CheckLength(length + util::Strnlen(s, N));
AMS_ABORT_UNLESS(length + static_cast<size_t>(util::Strnlen(s, N)) < N);
std::strncat(m_buffer, s, N - length - 1);
return *this;
}
void Append(char c) {
BoundedString<N> &Append(char c) {
const size_t length = GetLength();
CheckLength(length + 1);
AMS_ABORT_UNLESS(length + 1 < N);
m_buffer[length] = c;
m_buffer[length + 1] = 0;
return *this;
}
void AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
BoundedString<N> &AppendFormat(const char *format, ...) __attribute__((format (printf, 2, 3))) {
const size_t length = GetLength();
std::va_list args;
va_start(args, format);
CheckLength(util::VSNPrintf(m_buffer + length, N - length, format, args) + length);
va_end(args);
std::va_list vl;
va_start(vl, format);
AMS_ABORT_UNLESS(static_cast<size_t>(util::VSNPrintf(m_buffer + length, N - length, format, vl)) < static_cast<size_t>(N - length));
va_end(vl);
return *this;
}
/* Substring utilities. */
void GetSubstring(char *dst, size_t dst_size, size_t offset, size_t length) const {
void GetSubString(char *dst, size_t dst_size, size_t offset, size_t length) const {
/* Make sure output buffer can hold the substring. */
AMS_ABORT_UNLESS(offset + length <= GetLength());
AMS_ABORT_UNLESS(dst_size > length);
/* Copy substring to dst. */
std::strncpy(dst, m_buffer + offset, length);
dst[length] = 0;
}
BoundedString<N> GetSubstring(size_t offset, size_t length) const {
BoundedString<N> MakeSubString(size_t offset, size_t length) const {
BoundedString<N> string;
GetSubstring(string.m_buffer, N, offset, length);
GetSubString(string.m_buffer, N, offset, length);
return string;
}
/* Comparison. */
constexpr bool Equals(const char *s, size_t offset = 0) const {
if (std::is_constant_evaluated()) {
return util::Strncmp(m_buffer + offset, s, N - offset) == 0;
} else {
return std::strncmp(m_buffer + offset, s, N - offset) == 0;
}
}
constexpr bool operator==(const BoundedString<N> &rhs) const {
return std::strncmp(m_buffer, rhs.m_buffer, N) == 0;
return this->Equals(rhs.m_buffer);
}
constexpr bool operator!=(const BoundedString<N> &rhs) const {
return !(*this == rhs);
}
bool EndsWith(const char *s, size_t offset) const {
return std::strncmp(m_buffer + offset, s, N - offset) == 0;
}
bool EndsWith(const char *s) const {
constexpr bool EqualsPostfix(const char *s) const {
const size_t suffix_length = util::Strnlen(s, N);
const size_t length = GetLength();
return suffix_length <= length && EndsWith(s, length - suffix_length);
return suffix_length <= length && this->Equals(s, length - suffix_length);
}
};

View file

@ -77,7 +77,7 @@ namespace ams::kvdb {
/* Setup member variables. */
m_keys = static_cast<Key *>(buf);
m_file_path.Set(path);
m_file_path.Assign(path);
std::memset(m_keys, 0, BufferSize);
/* Open file. */

View file

@ -269,8 +269,8 @@ namespace ams::kvdb {
R_UNLESS(entry_type == fs::DirectoryEntryType_Directory, fs::ResultPathNotFound());
/* Set paths. */
m_path.SetFormat("%s%s", dir, "/imkvdb.arc");
m_temp_path.SetFormat("%s%s", dir, "/imkvdb.tmp");
m_path.AssignFormat("%s%s", dir, "/imkvdb.arc");
m_temp_path.AssignFormat("%s%s", dir, "/imkvdb.tmp");
/* Initialize our index. */
R_TRY(m_index.Initialize(capacity, mr));
@ -282,8 +282,8 @@ namespace ams::kvdb {
Result Initialize(size_t capacity, MemoryResource *mr) {
/* This initializes without an archive file. */
/* A store initialized this way cannot have its contents loaded from or flushed to disk. */
m_path.Set("");
m_temp_path.Set("");
m_path.Assign("");
m_temp_path.Assign("");
/* Initialize our index. */
R_TRY(m_index.Initialize(capacity, mr));

View file

@ -335,6 +335,10 @@ namespace ams::ncm {
size_t CalculateConvertContentMetaSize() const;
void ConvertToContentMeta(void *dst, size_t size, const ContentInfo &meta);
size_t CalculateConvertFragmentOnlyInstallContentMetaSize(s32 fragment_count) const {
return CalculateSizeImpl<InstallContentMetaHeader, InstallContentInfo>(this->GetExtendedHeaderSize(), fragment_count + 1, 0, 0, false);
}
Result CalculateConvertFragmentOnlyInstallContentMetaSize(size_t *out_size, u32 source_version) const;
Result ConvertToFragmentOnlyInstallContentMeta(void *dst, size_t size, const InstallContentInfo &content_info, u32 source_version);
@ -343,6 +347,10 @@ namespace ams::ncm {
static constexpr size_t CalculateSize(ContentMetaType type, size_t content_count, size_t content_meta_count, size_t extended_data_size) {
return ContentMetaAccessor::CalculateSize(type, content_count, content_meta_count, extended_data_size, true);
}
size_t GetExtendedDataOffset() const {
return this->GetExtendedDataAddress() - reinterpret_cast<uintptr_t>(this->GetData());
}
};
class InstallContentMetaReader : public ContentMetaAccessor<InstallContentMetaHeader, InstallContentInfo> {
@ -368,4 +376,15 @@ namespace ams::ncm {
using ContentMetaAccessor::SetStorageId;
};
class PatchMetaExtendedDataAccessor;
struct PatchDeltaHeader;
class AutoBuffer;
class MetaConverter {
public:
static Result CountContentExceptForMeta(s32 *out, PatchMetaExtendedDataAccessor *accessor, const PatchDeltaHeader &header, s32 delta_index);
static Result FindDeltaIndex(s32 *out, PatchMetaExtendedDataAccessor *accessor, u32 source_version, u32 destination_version);
static Result GetFragmentOnlyInstallContentMeta(AutoBuffer *out, const InstallContentInfo &content_info, const PackagedContentMetaReader &reader, PatchMetaExtendedDataAccessor *accessor, u32 source_version);
};
}

View file

@ -18,6 +18,7 @@
#include <stratosphere/ncm/ncm_content_meta_id.hpp>
#include <stratosphere/ncm/ncm_content_meta.hpp>
#include <stratosphere/ncm/ncm_firmware_variation.hpp>
#include <stratosphere/ncm/ncm_mapped_memory.hpp>
namespace ams::ncm {
@ -381,4 +382,476 @@ namespace ams::ncm {
constexpr SystemUpdateMetaExtendedDataReader(const void *data, size_t size) : SystemUpdateMetaExtendedDataReaderWriterBase(data, size) { /* ... */ }
};
template<typename T>
class ReadableStructPin;
class AccessorBase {
public:
template<typename T>
class PinBase {
private:
AccessorBase *m_accessor;
u64 m_pin_id;
T *m_data;
size_t m_size;
public:
PinBase() : m_accessor(nullptr), m_data(nullptr), m_size(0) {
/* ... */
}
PinBase(const PinBase &) = delete;
PinBase &operator=(const PinBase &) = delete;
PinBase(PinBase &&rhs) : m_accessor(rhs.m_accessor), m_pin_id(rhs.m_pin_id), m_data(rhs.m_data), m_size(rhs.m_size) {
rhs.m_accessor = nullptr;
}
PinBase &operator=(PinBase &&rhs) {
m_accessor = rhs.m_accessor;
m_pin_id = rhs.m_pin_id;
m_data = rhs.m_data;
m_size = rhs.m_size;
rhs.m_accessor = nullptr;
return *this;
}
virtual ~PinBase() {
this->Reset();
}
public:
void Reset() {
if (m_accessor != nullptr) {
m_accessor->ReleasePin(m_pin_id);
m_accessor = nullptr;
}
}
void Reset(AccessorBase *accessor, u64 pin_id, void *data, size_t size) {
AMS_ASSERT(data != nullptr || size == 0);
this->Reset();
m_accessor = accessor;
m_pin_id = pin_id;
m_data = reinterpret_cast<T *>(data);
m_size = size;
}
T *GetData() const {
return m_data;
}
size_t GetDataSize() const {
return m_size;
}
};
private:
IMapper *m_mapper;
public:
AccessorBase(IMapper *mapper) : m_mapper(mapper) {
/* ... */
}
template<typename T>
Result AcquireReadableStructPin(ReadableStructPin<T> *out, size_t offset) {
/* Acquire mapped memory for the pin. */
MappedMemory memory = {};
R_TRY(m_mapper->GetMappedMemory(std::addressof(memory), offset, sizeof(T)));
/* Mark the memory as in use. */
R_RETURN(m_mapper->MarkUsing(memory.id));
/* Setup the pin. */
out->Reset(this, memory.id, memory.GetBuffer(offset, sizeof(T)), sizeof(T));
R_SUCCEED();
}
Result ReleasePin(u64 id) {
R_RETURN(m_mapper->UnmarkUsing(id));
}
template<typename T>
Result ReadStruct(T *out, size_t offset) {
/* Acquire mapped memory for the pin. */
MappedMemory memory = {};
R_TRY(m_mapper->GetMappedMemory(std::addressof(memory), offset, sizeof(T)));
/* Mark the memory as in use. */
R_RETURN(m_mapper->MarkUsing(memory.id));
ON_SCOPE_EXIT { this->ReleasePin(memory.id); };
/* Copy out the struct. */
*out = *reinterpret_cast<const T *>(memory.GetBuffer(offset, sizeof(T)));
R_SUCCEED();
}
};
template<typename T>
class ReadableStructPin final : public AccessorBase::PinBase<const u8> {
public:
using PinBase::PinBase;
using PinBase::operator=;
const T *Get() const {
return reinterpret_cast<const T *>(this->GetData());
}
size_t GetSize() const {
return this->GetDataSize();
}
const T &operator*() const { return *this->Get(); }
const T *operator->() const { return this->Get(); }
};
class PatchMetaExtendedDataAccessor : public AccessorBase {
private:
struct CachedCount {
s32 index;
s32 count;
};
private:
util::optional<CachedCount> m_cached_history_content_count = util::nullopt;
util::optional<CachedCount> m_cached_delta_content_count = util::nullopt;
util::optional<CachedCount> m_cached_fragment_set_count = util::nullopt;
util::optional<CachedCount> m_cached_fragment_indicator_count = util::nullopt;
util::optional<PatchMetaExtendedDataHeader> m_header = util::nullopt;
public:
using AccessorBase::AccessorBase;
public:
Result GetHeader(ReadableStructPin<PatchMetaExtendedDataHeader> *out) { return this->AcquireReadableStructPin(out, 0); }
Result GetHeader(PatchMetaExtendedDataHeader *out) { return this->template ReadStruct<PatchMetaExtendedDataHeader>(out, 0); }
Result GetHistoryHeader(ReadableStructPin<PatchHistoryHeader> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(index) < m_header->history_count, ncm::ResultInvalidOffset());
/* Get the header. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * index;
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetPatchDeltaHistory(ReadableStructPin<PatchDeltaHistory> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(index) < m_header->delta_history_count, ncm::ResultInvalidOffset());
/* Get the history. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * index;
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetPatchDeltaHeader(ReadableStructPin<PatchDeltaHeader> *out, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= index && static_cast<u32>(index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Get the header. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * index;
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetFragmentSet(ReadableStructPin<FragmentSet> *out, s32 delta_index, s32 fragment_set_index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= delta_index && static_cast<u32>(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Get the previous fragment set count. */
s32 previous_fragment_set_count = 0;
R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index));
/* Get the set. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * (previous_fragment_set_count + fragment_set_index);
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetFragmentSetDirectly(ReadableStructPin<FragmentSet> *out, s32 fragment_set_direct_index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= fragment_set_direct_index && static_cast<u32>(fragment_set_direct_index) < m_header->fragment_set_count, ncm::ResultInvalidOffset());
/* Get the set. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * (fragment_set_direct_index);
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetPatchHistoryContentInfo(ReadableStructPin<ContentInfo> *out, s32 history_index, s32 content_index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= history_index && static_cast<u32>(history_index) < m_header->history_count, ncm::ResultInvalidOffset());
/* Determine the true history content index. */
s32 prev_history_count = 0;
R_TRY(this->CountHistoryContentInfo(std::addressof(prev_history_count), history_index));
/* Adjust and check the content index. */
content_index += prev_history_count;
R_UNLESS(0 <= content_index && static_cast<u32>(content_index) < m_header->history_content_total_count, ncm::ResultInvalidOffset());
/* Get the info. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * content_index;
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetPatchDeltaContentInfo(ReadableStructPin<PackagedContentInfo> *out, s32 delta_index, s32 content_index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= delta_index && static_cast<u32>(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Determine the true delta content index. */
s32 prev_delta_count = 0;
R_TRY(this->CountDeltaContentInfo(std::addressof(prev_delta_count), delta_index));
/* Adjust and check the content index. */
content_index += prev_delta_count;
R_UNLESS(0 <= content_index && static_cast<u32>(content_index) < m_header->delta_content_total_count, ncm::ResultInvalidOffset());
/* Get the info. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * content_index;
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result GetFragmentIndicator(ReadableStructPin<FragmentIndicator> *out, s32 delta_index, s32 fragment_set_index, s32 index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= delta_index && static_cast<u32>(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Get the previous fragment set count. */
s32 previous_fragment_set_count = 0;
R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index));
/* Get the previous fragment indicator count. */
s32 previous_fragment_count = 0;
R_TRY(this->CountFragmentIndicator(std::addressof(previous_fragment_count), previous_fragment_count + fragment_set_index));
/* Get the info. */
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * m_header->delta_content_total_count + sizeof(FragmentIndicator) * (previous_fragment_count + index);
R_RETURN(this->AcquireReadableStructPin(out, offset));
}
Result FindFragmentIndicator(ReadableStructPin<FragmentIndicator> *out, s32 delta_index, s32 fragment_set_index, s32 fragment_index) {
/* Ensure we have our header. */
R_TRY(this->EnsureHeader());
/* Check that the index is valid. */
R_UNLESS(0 <= delta_index && static_cast<u32>(delta_index) < m_header->delta_count, ncm::ResultInvalidOffset());
/* Get the fragment count. */
s32 fragment_count = 0;
{
ReadableStructPin<FragmentSet> set;
R_TRY(this->GetFragmentSet(std::addressof(set), delta_index, fragment_set_index));
fragment_count = set->fragment_count;
}
/* Get the previous fragment set count. */
s32 previous_fragment_set_count = 0;
R_TRY(this->CountFragmentSet(std::addressof(previous_fragment_set_count), delta_index));
/* Get the previous fragment indicator count. */
s32 previous_fragment_count = 0;
R_TRY(this->CountFragmentIndicator(std::addressof(previous_fragment_count), previous_fragment_count + fragment_set_index));
/* Look for a correct indicator. */
for (auto i = 0; i < fragment_count; ++i) {
/* Get the current info. */
ReadableStructPin<FragmentIndicator> indicator;
const size_t offset = sizeof(PatchHistoryHeader) + sizeof(PatchHistoryHeader) * m_header->history_count + sizeof(PatchDeltaHistory) * m_header->delta_history_count + sizeof(PatchDeltaHeader) * m_header->delta_count + sizeof(FragmentSet) * m_header->fragment_set_count + sizeof(ContentInfo) * m_header->history_content_total_count + sizeof(PackagedContentInfo) * m_header->delta_content_total_count + sizeof(FragmentIndicator) * (previous_fragment_count + i);
R_TRY(this->AcquireReadableStructPin(std::addressof(indicator), offset));
/* If it matches, return it. */
if (indicator->fragment_index == fragment_index) {
*out = std::move(indicator);
R_SUCCEED();
}
}
/* We didn't find an indicator. */
R_THROW(ncm::ResultFragmentIndicatorNotFound());
}
Result GetHistoryHeader(PatchHistoryHeader *out, s32 index) {
/* Get the pin. */
ReadableStructPin<PatchHistoryHeader> pin;
R_TRY(this->GetHistoryHeader(std::addressof(pin), index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetPatchDeltaHistory(PatchDeltaHistory *out, s32 index) {
/* Get the pin. */
ReadableStructPin<PatchDeltaHistory> pin;
R_TRY(this->GetPatchDeltaHistory(std::addressof(pin), index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetPatchDeltaHeader(PatchDeltaHeader *out, s32 index) {
/* Get the pin. */
ReadableStructPin<PatchDeltaHeader> pin;
R_TRY(this->GetPatchDeltaHeader(std::addressof(pin), index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetFragmentSet(FragmentSet *out, s32 delta_index, s32 fragment_set_index) {
/* Get the pin. */
ReadableStructPin<FragmentSet> pin;
R_TRY(this->GetFragmentSet(std::addressof(pin), delta_index, fragment_set_index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetPatchHistoryContentInfo(ContentInfo *out, s32 history_index, s32 content_index) {
/* Get the header. */
ReadableStructPin<ContentInfo> pin;
R_TRY(this->GetPatchHistoryContentInfo(std::addressof(pin), history_index, content_index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetPatchDeltaContentInfo(PackagedContentInfo *out, s32 delta_index, s32 content_index) {
/* Get the header. */
ReadableStructPin<PackagedContentInfo> pin;
R_TRY(this->GetPatchDeltaContentInfo(std::addressof(pin), delta_index, content_index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result GetFragmentIndicator(FragmentIndicator *out, s32 delta_index, s32 fragment_set_index, s32 index) {
/* Get the header. */
ReadableStructPin<FragmentIndicator> pin;
R_TRY(this->GetFragmentIndicator(std::addressof(pin), delta_index, fragment_set_index, index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result FindFragmentIndicator(FragmentIndicator *out, s32 delta_index, s32 fragment_set_index, s32 fragment_index) {
/* Get the header. */
ReadableStructPin<FragmentIndicator> pin;
R_TRY(this->FindFragmentIndicator(std::addressof(pin), delta_index, fragment_set_index, fragment_index));
/* Copy it out. */
*out = *pin;
R_SUCCEED();
}
Result CountHistoryContentInfo(s32 *out, s32 index) {
R_RETURN(this->CountImpl(out, index, m_cached_history_content_count, [&](s32 *out, s32 i) -> Result {
/* Get the history header. */
ReadableStructPin<ncm::PatchHistoryHeader> header;
R_TRY(this->GetHistoryHeader(std::addressof(header), i));
/* Set the content count. */
*out = header->content_count;
R_SUCCEED();
}));
}
Result CountDeltaContentInfo(s32 *out, s32 index) {
R_RETURN(this->CountImpl(out, index, m_cached_delta_content_count, [&](s32 *out, s32 i) -> Result {
/* Get the history header. */
ReadableStructPin<ncm::PatchDeltaHeader> header;
R_TRY(this->GetPatchDeltaHeader(std::addressof(header), i));
/* Set the content count. */
*out = header->content_count;
R_SUCCEED();
}));
}
Result CountFragmentSet(s32 *out, s32 index) {
R_RETURN(this->CountImpl(out, index, m_cached_fragment_set_count, [&](s32 *out, s32 i) -> Result {
/* Get the history header. */
ReadableStructPin<ncm::PatchDeltaHeader> header;
R_TRY(this->GetPatchDeltaHeader(std::addressof(header), i));
/* Set the fragment set count. */
*out = header->delta.fragment_set_count;
R_SUCCEED();
}));
}
Result CountFragmentIndicator(s32 *out, s32 index) {
R_RETURN(this->CountImpl(out, index, m_cached_fragment_indicator_count, [&](s32 *out, s32 i) -> Result {
/* Get the history header. */
ReadableStructPin<ncm::FragmentSet> set;
R_TRY(this->GetFragmentSetDirectly(std::addressof(set), i));
/* Set the indicator count. */
*out = set->fragment_count;
R_SUCCEED();
}));
}
private:
Result CountImpl(s32 *out, s32 index, util::optional<CachedCount> &cache, auto get_count_impl) const {
/* Ensure the value is cached. */
if (!(cache.has_value() && cache->index == index)) {
/* Determine the count. */
CachedCount calc = { .index = index, .count = 0 };
for (auto i = 0; i < index; ++i) {
s32 cur_count = 0;
R_TRY(get_count_impl(std::addressof(cur_count), i));
calc.count += cur_count;
}
/* Cache the count. */
cache = calc;
}
/* Set the output count. */
*out = cache->count;
R_SUCCEED();
}
private:
Result EnsureHeader() {
/* If we have our header, we're good. */
R_SUCCEED_IF(m_header.has_value());
/* Get our header. */
PatchMetaExtendedDataHeader header;
R_TRY(this->GetHeader(std::addressof(header)));
/* Set our header. */
m_header.emplace(header);
R_SUCCEED();
}
};
}

View file

@ -25,7 +25,14 @@ namespace ams::ncm {
using MountContentMetaFunction = Result (*)(const char *mount_name, const char *path);
Result ReadContentMetaPath(AutoBuffer *out, const char *path);
bool IsContentMetaFileName(const char *name);
Result ReadContentMetaPathAlongWithExtendedDataAndDigest(AutoBuffer *out, const char *path);
Result ReadContentMetaPathAlongWithExtendedDataAndDigestSuppressingFsAbort(AutoBuffer *out, const char *path);
Result ReadContentMetaPathWithoutExtendedDataOrDigest(AutoBuffer *out, const char *path);
Result ReadContentMetaPathWithoutExtendedDataOrDigestSuppressingFsAbort(AutoBuffer *out, const char *path);
Result ReadVariationContentMetaInfoList(s32 *out_count, std::unique_ptr<ContentMetaInfo[]> *out_meta_infos, const Path &path, FirmwareVariationId firmware_variation_id);
void SetMountContentMetaFunction(MountContentMetaFunction func);

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::ncm {
struct MappedMemory {
u64 id;
size_t offset;
u8 *buffer;
size_t buffer_size;
bool IsIncluded(size_t o, size_t sz) const {
return this->offset <= o && sz <= this->buffer_size && (o + sz) <= (this->offset + this->buffer_size);
}
u8 *GetBuffer(size_t o, size_t sz) const {
AMS_ASSERT(this->buffer != nullptr);
AMS_ASSERT(this->IsIncluded(o, sz));
return this->buffer + (o - this->offset);
}
};
static_assert(util::is_pod<MappedMemory>::value);
class IMapper {
public:
virtual ~IMapper() { /* ... */ }
public:
virtual Result GetMappedMemory(MappedMemory *out, size_t offset, size_t size) = 0;
virtual Result MarkUsing(u64 id) = 0;
virtual Result UnmarkUsing(u64 id) = 0;
virtual Result MarkDirty(u64 id) = 0;
protected:
virtual Result MapImpl(MappedMemory *out, Span<u8> data, size_t offset, size_t size) = 0;
virtual Result UnmapImpl(MappedMemory *mem) = 0;
virtual bool IsAccessibleSizeUpdatable() = 0;
};
}