Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/ELF/PBPReader.cpp
3186 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <string>
19
#include <cstring>
20
21
#include "Common/Log.h"
22
#include "Core/Loaders.h"
23
#include "Core/ELF/PBPReader.h"
24
25
PBPReader::PBPReader(FileLoader *fileLoader) {
26
if (!fileLoader->Exists()) {
27
ERROR_LOG(Log::Loader, "Failed to open PBP file %s", fileLoader->GetPath().c_str());
28
return;
29
}
30
31
fileSize_ = (size_t)fileLoader->FileSize();
32
if (fileLoader->ReadAt(0, sizeof(header_), (u8 *)&header_) != sizeof(header_)) {
33
ERROR_LOG(Log::Loader, "PBP is too small to be valid: %s", fileLoader->GetPath().c_str());
34
return;
35
}
36
if (memcmp(header_.magic, "\0PBP", 4) != 0) {
37
if (memcmp(header_.magic, "\nFLE", 4) != 0) {
38
VERBOSE_LOG(Log::Loader, "%s: File actually an ELF, not a PBP", fileLoader->GetPath().c_str());
39
isELF_ = true;
40
} else {
41
ERROR_LOG(Log::Loader, "Magic number in %s indicated no PBP: %s", fileLoader->GetPath().c_str(), header_.magic);
42
}
43
return;
44
}
45
46
VERBOSE_LOG(Log::Loader, "Loading PBP, version = %08x", header_.version);
47
file_ = fileLoader;
48
}
49
50
bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) const {
51
if (!file_) {
52
return false;
53
}
54
55
const size_t expected = GetSubFileSize(file);
56
57
// This is only used to get the PARAM.SFO, so let's have a strict 256MB file size limit for sanity.
58
if (expected > 256 * 1024 * 1024) {
59
ERROR_LOG(Log::Loader, "Bad subfile size: %d", (int)expected);
60
return false;
61
}
62
63
const u32 off = header_.offsets[(int)file];
64
65
out->resize(expected);
66
size_t bytes = file_->ReadAt(off, expected, &(*out)[0]);
67
if (bytes != expected) {
68
ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);
69
if (bytes < expected) {
70
out->resize(bytes);
71
}
72
}
73
return true;
74
}
75
76
bool PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) const {
77
if (!file_) {
78
out->clear();
79
return false;
80
}
81
82
const size_t expected = GetSubFileSize(file);
83
84
// This is only used to get the PNG, AT3 etc, so let's have a strict 256MB file size limit for sanity.
85
if (expected > 256 * 1024 * 1024) {
86
ERROR_LOG(Log::Loader, "Bad subfile size: %d", (int)expected);
87
return false;
88
}
89
const u32 off = header_.offsets[(int)file];
90
91
out->resize(expected);
92
size_t bytes = file_->ReadAt(off, expected, (void *)out->data());
93
if (bytes != expected) {
94
ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);
95
if (bytes < expected) {
96
out->resize(bytes);
97
// should we still return true here?
98
}
99
}
100
return true;
101
}
102
103
PBPReader::~PBPReader() {
104
// Does not take ownership.
105
file_ = nullptr;
106
}
107
108