Skip to content

Commit

Permalink
PBPReader sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 1, 2024
1 parent dd8f9b6 commit eb5769c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
21 changes: 17 additions & 4 deletions Core/ELF/PBPReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "Core/Loaders.h"
#include "Core/ELF/PBPReader.h"

PBPReader::PBPReader(FileLoader *fileLoader) : file_(nullptr), header_(), isELF_(false) {
PBPReader::PBPReader(FileLoader *fileLoader) {
if (!fileLoader->Exists()) {
ERROR_LOG(Log::Loader, "Failed to open PBP file %s", fileLoader->GetPath().c_str());
return;
Expand All @@ -48,12 +48,19 @@ PBPReader::PBPReader(FileLoader *fileLoader) : file_(nullptr), header_(), isELF_
file_ = fileLoader;
}

bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) {
bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) const {
if (!file_) {
return false;
}

const size_t expected = GetSubFileSize(file);

// This is only used to get the PARAM.SFO, so let's have a strict 256MB file size limit for sanity.
if (expected > 256 * 1024 * 1024) {
ERROR_LOG(Log::Loader, "Bad subfile size: %d", (int)expected);
return false;
}

const u32 off = header_.offsets[(int)file];

out->resize(expected);
Expand All @@ -67,13 +74,19 @@ bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) {
return true;
}

void PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) {
bool PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) const {
if (!file_) {
out->clear();
return;
return false;
}

const size_t expected = GetSubFileSize(file);

// This is only used to get the PNG, AT3 etc, so let's have a strict 256MB file size limit for sanity.
if (expected > 256 * 1024 * 1024) {
ERROR_LOG(Log::Loader, "Bad subfile size: %d", (int)expected);
return false;
}
const u32 off = header_.offsets[(int)file];

out->resize(expected);
Expand Down
14 changes: 7 additions & 7 deletions Core/ELF/PBPReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class PBPReader {
bool IsValid() const { return file_ != nullptr; }
bool IsELF() const { return file_ == nullptr && isELF_; }

bool GetSubFile(PBPSubFile file, std::vector<u8> *out);
void GetSubFileAsString(PBPSubFile file, std::string *out);
bool GetSubFile(PBPSubFile file, std::vector<u8> *out) const;
bool GetSubFileAsString(PBPSubFile file, std::string *out) const;

size_t GetSubFileSize(PBPSubFile file) {
size_t GetSubFileSize(PBPSubFile file) const {
int num = (int)file;
if (num < 7) {
return header_.offsets[file + 1] - header_.offsets[file];
Expand All @@ -63,8 +63,8 @@ class PBPReader {
}

private:
FileLoader *file_;
size_t fileSize_;
const PBPHeader header_;
bool isELF_;
FileLoader *file_ = nullptr;
size_t fileSize_ = 0;
const PBPHeader header_{};
bool isELF_ = false;
};

0 comments on commit eb5769c

Please sign in to comment.