Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an underlying fstream instead of a FILE* for PBPReader #6537

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions Core/ELF/PBPReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,38 @@
#include "Core/ELF/PBPReader.h"

PBPReader::PBPReader(const char *filename) : header_(), isELF_(false) {
file_ = File::OpenCFile(filename, "rb");
if (!file_) {
if (!File::OpenCPPFile(file_, filename, std::ios::in | std::ios::binary | std::ios::ate)) {
ERROR_LOG(LOADER, "Failed to open PBP file %s", filename);
return;
}

fseek(file_, 0, SEEK_END);
fileSize_ = ftell(file_);
fseek(file_, 0, SEEK_SET);
fread((char *)&header_, 1, sizeof(header_), file_);
fileSize_ = file_.tellg();
file_.seekg(0, std::ios::beg);
file_.read((char*)&header_, sizeof(header_));
if (memcmp(header_.magic, "\0PBP", 4) != 0) {
if (memcmp(header_.magic, "\nFLE", 4) != 0) {
DEBUG_LOG(LOADER, "%s: File actually an ELF, not a PBP", filename);
isELF_ = true;
} else {
ERROR_LOG(LOADER, "Magic number in %s indicated no PBP: %s", filename, header_.magic);
}
fclose(file_);
file_ = 0;
return;
file_.close();
} else {
DEBUG_LOG(LOADER, "Loading PBP, version = %08x", header_.version);
}

DEBUG_LOG(LOADER, "Loading PBP, version = %08x", header_.version);
}

u8 *PBPReader::GetSubFile(PBPSubFile file, size_t *outSize) {
*outSize = GetSubFileSize(file);
u8 *buffer = new u8[*outSize];
fseek(file_, header_.offsets[(int)file], SEEK_SET);
fread(buffer, 1, *outSize, file_);
file_.seekg(header_.offsets[(int)file], std::ios::beg);
file_.read((char*)buffer, *outSize);
return buffer;
}

void PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) {
out->resize(GetSubFileSize(file));
fseek(file_, header_.offsets[(int)file], SEEK_SET);
fread((void *)out->data(), 1, out->size(), file_);
file_.seekg(header_.offsets[(int)file], std::ios::beg);
file_.read((char*)out->data(), out->size());
}

PBPReader::~PBPReader() {
if (file_)
fclose(file_);
}
11 changes: 5 additions & 6 deletions Core/ELF/PBPReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include <fstream>
#include "Common/Common.h"

enum PBPSubFile {
Expand All @@ -40,26 +41,24 @@ struct PBPHeader {
class PBPReader {
public:
PBPReader(const char *filename);
~PBPReader();

bool IsValid() const { return file_ != 0; }
bool IsELF() const { return file_ == 0 && isELF_; }
bool IsValid() const { return file_.rdbuf()->is_open(); }
bool IsELF() const { return !IsValid() && isELF_; }

// Delete the returned buffer with delete [].
u8 *GetSubFile(PBPSubFile file, size_t *outSize);
void GetSubFileAsString(PBPSubFile file, std::string *out);

size_t GetSubFileSize(PBPSubFile file) {
int num = (int)file;
if (num < 7) {
if (file < PBP_UNKNOWN_PSAR) {
return header_.offsets[file + 1] - header_.offsets[file];
} else {
return fileSize_ - header_.offsets[file];
}
}

private:
FILE *file_;
std::fstream file_;
size_t fileSize_;
const PBPHeader header_;
bool isELF_;
Expand Down