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/FileSystems/BlockDevices.h
Views: 1401
// Copyright (c) 2012- 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#pragma once1819// Abstractions around read-only blockdevices, such as PSP UMD discs.20// CISOFileBlockDevice implements compressed iso images, CISO format.21//22// The ISOFileSystemReader reads from a BlockDevice, so it automatically works23// with CISO images.2425#include <mutex>2627#include "Common/CommonTypes.h"28#include "Core/ELF/PBPReader.h"2930class FileLoader;3132class BlockDevice {33public:34BlockDevice(FileLoader *fileLoader) : fileLoader_(fileLoader) {}35virtual ~BlockDevice() {}36virtual bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) = 0;37virtual bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) {38for (int b = 0; b < count; ++b) {39if (!ReadBlock(minBlock + b, outPtr)) {40return false;41}42outPtr += GetBlockSize();43}44return true;45}46int GetBlockSize() const { return 2048;} // forced, it cannot be changed by subclasses47virtual u32 GetNumBlocks() const = 0;48virtual u64 GetUncompressedSize() const {49return (u64)GetNumBlocks() * (u64)GetBlockSize();50}51virtual bool IsDisc() const = 0;5253void NotifyReadError();5455protected:56FileLoader *fileLoader_;57bool reportedError_ = false;58};5960class CISOFileBlockDevice : public BlockDevice {61public:62CISOFileBlockDevice(FileLoader *fileLoader);63~CISOFileBlockDevice();64bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;65bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;66u32 GetNumBlocks() const override { return numBlocks; }67bool IsDisc() const override { return true; }6869private:70u32 *index = nullptr;71u8 *readBuffer = nullptr;72u8 *zlibBuffer = nullptr;73u32 zlibBufferFrame = 0;74u8 indexShift = 0;75u8 blockShift = 0;76u32 frameSize = 0;77u32 numBlocks = 0;78u32 numFrames = 0;79int ver_ = 0;80};818283class FileBlockDevice : public BlockDevice {84public:85FileBlockDevice(FileLoader *fileLoader);86~FileBlockDevice();87bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;88bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;89u32 GetNumBlocks() const override {return (u32)(filesize_ / GetBlockSize());}90bool IsDisc() const override { return true; }91u64 GetUncompressedSize() const override {92return filesize_;93}94private:95u64 filesize_;96};979899// For encrypted ISOs in PBP files.100101struct table_info {102u8 mac[16];103u32 offset;104int size;105int flag;106int unk_1c;107};108109class NPDRMDemoBlockDevice : public BlockDevice {110public:111NPDRMDemoBlockDevice(FileLoader *fileLoader);112~NPDRMDemoBlockDevice();113114bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;115u32 GetNumBlocks() const override {return (u32)lbaSize;}116bool IsDisc() const override { return false; }117118private:119static std::mutex mutex_;120u32 lbaSize;121122u32 psarOffset;123int blockSize;124int blockLBAs;125u32 numBlocks;126127u8 vkey[16];128u8 hkey[16];129struct table_info *table;130131int currentBlock;132u8 *blockBuf;133u8 *tempBuf;134};135136struct CHDImpl;137138struct ExtendedCoreFile;139140class CHDFileBlockDevice : public BlockDevice {141public:142CHDFileBlockDevice(FileLoader *fileLoader);143~CHDFileBlockDevice();144bool ReadBlock(int blockNumber, u8 *outPtr, bool uncached = false) override;145bool ReadBlocks(u32 minBlock, int count, u8 *outPtr) override;146u32 GetNumBlocks() const override { return numBlocks; }147bool IsDisc() const override { return true; }148private:149struct ExtendedCoreFile *core_file_ = nullptr;150std::unique_ptr<CHDImpl> impl_;151u8 *readBuffer = nullptr;152u32 currentHunk = 0;153u32 blocksPerHunk = 0;154u32 numBlocks = 0;155};156157BlockDevice *constructBlockDevice(FileLoader *fileLoader);158159160