CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/ELF/PBPReader.cpp
Views: 1401
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 "Common/File/FileUtil.h"
23
#include "Core/Loaders.h"
24
#include "Core/ELF/PBPReader.h"
25
26
PBPReader::PBPReader(FileLoader *fileLoader) : file_(nullptr), header_(), isELF_(false) {
27
if (!fileLoader->Exists()) {
28
ERROR_LOG(Log::Loader, "Failed to open PBP file %s", fileLoader->GetPath().c_str());
29
return;
30
}
31
32
fileSize_ = (size_t)fileLoader->FileSize();
33
if (fileLoader->ReadAt(0, sizeof(header_), (u8 *)&header_) != sizeof(header_)) {
34
ERROR_LOG(Log::Loader, "PBP is too small to be valid: %s", fileLoader->GetPath().c_str());
35
return;
36
}
37
if (memcmp(header_.magic, "\0PBP", 4) != 0) {
38
if (memcmp(header_.magic, "\nFLE", 4) != 0) {
39
VERBOSE_LOG(Log::Loader, "%s: File actually an ELF, not a PBP", fileLoader->GetPath().c_str());
40
isELF_ = true;
41
} else {
42
ERROR_LOG(Log::Loader, "Magic number in %s indicated no PBP: %s", fileLoader->GetPath().c_str(), header_.magic);
43
}
44
return;
45
}
46
47
VERBOSE_LOG(Log::Loader, "Loading PBP, version = %08x", header_.version);
48
file_ = fileLoader;
49
}
50
51
bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) {
52
if (!file_) {
53
return false;
54
}
55
56
const size_t expected = GetSubFileSize(file);
57
const u32 off = header_.offsets[(int)file];
58
59
out->resize(expected);
60
size_t bytes = file_->ReadAt(off, expected, &(*out)[0]);
61
if (bytes != expected) {
62
ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);
63
if (bytes < expected) {
64
out->resize(bytes);
65
}
66
}
67
return true;
68
}
69
70
void PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) {
71
if (!file_) {
72
out->clear();
73
return;
74
}
75
76
const size_t expected = GetSubFileSize(file);
77
const u32 off = header_.offsets[(int)file];
78
79
out->resize(expected);
80
size_t bytes = file_->ReadAt(off, expected, (void *)out->data());
81
if (bytes != expected) {
82
ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);
83
if (bytes < expected) {
84
out->resize(bytes);
85
}
86
}
87
}
88
89
PBPReader::~PBPReader() {
90
// Does not take ownership.
91
file_ = nullptr;
92
}
93
94