CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ELF/PBPReader.cpp
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <string>18#include <cstring>1920#include "Common/Log.h"21#include "Common/File/FileUtil.h"22#include "Core/Loaders.h"23#include "Core/ELF/PBPReader.h"2425PBPReader::PBPReader(FileLoader *fileLoader) : file_(nullptr), header_(), isELF_(false) {26if (!fileLoader->Exists()) {27ERROR_LOG(Log::Loader, "Failed to open PBP file %s", fileLoader->GetPath().c_str());28return;29}3031fileSize_ = (size_t)fileLoader->FileSize();32if (fileLoader->ReadAt(0, sizeof(header_), (u8 *)&header_) != sizeof(header_)) {33ERROR_LOG(Log::Loader, "PBP is too small to be valid: %s", fileLoader->GetPath().c_str());34return;35}36if (memcmp(header_.magic, "\0PBP", 4) != 0) {37if (memcmp(header_.magic, "\nFLE", 4) != 0) {38VERBOSE_LOG(Log::Loader, "%s: File actually an ELF, not a PBP", fileLoader->GetPath().c_str());39isELF_ = true;40} else {41ERROR_LOG(Log::Loader, "Magic number in %s indicated no PBP: %s", fileLoader->GetPath().c_str(), header_.magic);42}43return;44}4546VERBOSE_LOG(Log::Loader, "Loading PBP, version = %08x", header_.version);47file_ = fileLoader;48}4950bool PBPReader::GetSubFile(PBPSubFile file, std::vector<u8> *out) {51if (!file_) {52return false;53}5455const size_t expected = GetSubFileSize(file);56const u32 off = header_.offsets[(int)file];5758out->resize(expected);59size_t bytes = file_->ReadAt(off, expected, &(*out)[0]);60if (bytes != expected) {61ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);62if (bytes < expected) {63out->resize(bytes);64}65}66return true;67}6869void PBPReader::GetSubFileAsString(PBPSubFile file, std::string *out) {70if (!file_) {71out->clear();72return;73}7475const size_t expected = GetSubFileSize(file);76const u32 off = header_.offsets[(int)file];7778out->resize(expected);79size_t bytes = file_->ReadAt(off, expected, (void *)out->data());80if (bytes != expected) {81ERROR_LOG(Log::Loader, "PBP file read truncated: %d -> %d", (int)expected, (int)bytes);82if (bytes < expected) {83out->resize(bytes);84}85}86}8788PBPReader::~PBPReader() {89// Does not take ownership.90file_ = nullptr;91}929394