am: Resolve truncation and sign conversion warnings in WriteContentData() (#5397)

We can adjust the API to allow std::size_t indices, which simplifies
operating with standard container types. It also prevents truncation
warnings from occurring in these cases as well.
This commit is contained in:
LC 2020-06-20 14:09:56 -04:00 committed by GitHub
parent 353780e1c9
commit 902cc1eb49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 31 deletions

View file

@ -206,10 +206,10 @@ u64 CIAContainer::GetMetadataOffset() const {
return offset;
}
u64 CIAContainer::GetContentOffset(u16 index) const {
u64 CIAContainer::GetContentOffset(std::size_t index) const {
u64 offset =
Common::AlignUp(GetTitleMetadataOffset() + cia_header.tmd_size, CIA_SECTION_ALIGNMENT);
for (u16 i = 0; i < index; i++) {
for (std::size_t i = 0; i < index; i++) {
offset += GetContentSize(i);
}
return offset;
@ -235,10 +235,11 @@ u64 CIAContainer::GetTotalContentSize() const {
return cia_header.content_size;
}
u64 CIAContainer::GetContentSize(u16 index) const {
u64 CIAContainer::GetContentSize(std::size_t index) const {
// If the content doesn't exist in the CIA, it doesn't have a size.
if (!cia_header.isContentPresent(index))
if (!cia_header.IsContentPresent(index)) {
return 0;
}
return cia_tmd.GetContentSizeByIndex(index);
}

View file

@ -58,14 +58,14 @@ public:
u64 GetTicketOffset() const;
u64 GetTitleMetadataOffset() const;
u64 GetMetadataOffset() const;
u64 GetContentOffset(u16 index = 0) const;
u64 GetContentOffset(std::size_t index = 0) const;
u32 GetCertificateSize() const;
u32 GetTicketSize() const;
u32 GetTitleMetadataSize() const;
u32 GetMetadataSize() const;
u64 GetTotalContentSize() const;
u64 GetContentSize(u16 index = 0) const;
u64 GetContentSize(std::size_t index = 0) const;
void Print() const;
@ -81,11 +81,11 @@ private:
u64_le content_size;
std::array<u8, CIA_CONTENT_BITS_SIZE> content_present;
bool isContentPresent(u16 index) const {
bool IsContentPresent(std::size_t index) const {
// The content_present is a bit array which defines which content in the TMD
// is included in the CIA, so check the bit for this index and add if set.
// The bits in the content index are arranged w/ index 0 as the MSB, 7 as the LSB, etc.
return (content_present[index >> 3] & (0x80 >> (index & 7)));
return (content_present[index >> 3] & (0x80 >> (index & 7))) != 0;
}
};

View file

@ -165,19 +165,19 @@ u32 TitleMetadata::GetDLPContentID() const {
return tmd_chunks[TMDContentIndex::DLP].id;
}
u32 TitleMetadata::GetContentIDByIndex(u16 index) const {
u32 TitleMetadata::GetContentIDByIndex(std::size_t index) const {
return tmd_chunks[index].id;
}
u16 TitleMetadata::GetContentTypeByIndex(u16 index) const {
u16 TitleMetadata::GetContentTypeByIndex(std::size_t index) const {
return tmd_chunks[index].type;
}
u64 TitleMetadata::GetContentSizeByIndex(u16 index) const {
u64 TitleMetadata::GetContentSizeByIndex(std::size_t index) const {
return tmd_chunks[index].size;
}
std::array<u8, 16> TitleMetadata::GetContentCTRByIndex(u16 index) const {
std::array<u8, 16> TitleMetadata::GetContentCTRByIndex(std::size_t index) const {
std::array<u8, 16> ctr{};
std::memcpy(ctr.data(), &tmd_chunks[index].index, sizeof(u16));
return ctr;

View file

@ -96,10 +96,10 @@ public:
u32 GetBootContentID() const;
u32 GetManualContentID() const;
u32 GetDLPContentID() const;
u32 GetContentIDByIndex(u16 index) const;
u16 GetContentTypeByIndex(u16 index) const;
u64 GetContentSizeByIndex(u16 index) const;
std::array<u8, 16> GetContentCTRByIndex(u16 index) const;
u32 GetContentIDByIndex(std::size_t index) const;
u16 GetContentTypeByIndex(std::size_t index) const;
u64 GetContentSizeByIndex(std::size_t index) const;
std::array<u8, 16> GetContentCTRByIndex(std::size_t index) const;
void SetTitleID(u64 title_id);
void SetTitleType(u32 type);