summaryrefslogtreecommitdiff
path: root/src/input/gc/BlockReader.h
blob: e4011d3e927eb9e67a27b0e32fef34bd8480bd42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <memory>
#include <mutex>
#include <vector>

namespace GC {

class IBlockReader {
public:
    virtual ~IBlockReader() = default;
    virtual void Read(uint64_t offset, void* dst, size_t size) = 0;
    virtual uint64_t Size() const = 0;

    std::vector<uint8_t> Read(uint64_t offset, size_t size) {
        std::vector<uint8_t> out(size);
        Read(offset, out.data(), size);
        return out;
    }
};

class RawImageReader : public IBlockReader {
public:
    explicit RawImageReader(const std::filesystem::path& path);
    void Read(uint64_t offset, void* dst, size_t size) override;
    uint64_t Size() const override { return mSize; }

private:
    std::ifstream mFile;
    uint64_t mSize;
    std::mutex mMutex;
};

class MemoryBlockReader : public IBlockReader {
public:
    explicit MemoryBlockReader(std::vector<uint8_t> data) : mData(std::move(data)) {}
    void Read(uint64_t offset, void* dst, size_t size) override;
    uint64_t Size() const override { return mData.size(); }

private:
    std::vector<uint8_t> mData;
};

class CisoReader : public IBlockReader {
public:
    explicit CisoReader(std::shared_ptr<IBlockReader> base);
    void Read(uint64_t offset, void* dst, size_t size) override;
    uint64_t Size() const override { return mSize; }

    static bool Probe(IBlockReader& base);

private:
    std::shared_ptr<IBlockReader> mBase;
    uint32_t mBlockSize;
    uint64_t mSize;
    std::vector<int64_t> mBlockOffsets;
};

class GczReader : public IBlockReader {
public:
    explicit GczReader(std::shared_ptr<IBlockReader> base);
    void Read(uint64_t offset, void* dst, size_t size) override;
    uint64_t Size() const override { return mDataSize; }

    static bool Probe(IBlockReader& base);

private:
    void LoadBlock(uint32_t index);

    std::shared_ptr<IBlockReader> mBase;
    uint64_t mDataSize;
    uint32_t mBlockSize;
    uint64_t mDataStart;
    uint64_t mCompressedSize;
    std::vector<uint64_t> mBlockPointers;
    std::vector<uint8_t> mCache;
    int64_t mCachedBlock = -1;
    std::mutex mMutex;
};

std::shared_ptr<IBlockReader> OpenDiscImage(const std::filesystem::path& path);
std::shared_ptr<IBlockReader> WrapDiscImage(std::shared_ptr<IBlockReader> base);

uint32_t ReadBE32(const uint8_t* p);
uint16_t ReadBE16(const uint8_t* p);
void WriteBE32(uint8_t* p, uint32_t v);
void WriteBE16(uint8_t* p, uint16_t v);

} // namespace GC