summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/Blob.cpp
blob: 760669bc1a5eb7a2c68cfca7984a243d8f754037 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <cstddef>
#include <cstring>
#include <limits>
#include <memory>
#include <string>

#include "Common/CDUtils.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"

#include "DiscIO/Blob.h"
#include "DiscIO/CISOBlob.h"
#include "DiscIO/CompressedBlob.h"
#include "DiscIO/DriveBlob.h"
#include "DiscIO/FileBlob.h"
#include "DiscIO/WbfsBlob.h"

namespace DiscIO
{

// Provides caching and split-operation-to-block-operations facilities.
// Used for compressed blob reading and direct drive reading.

void SectorReader::SetSectorSize(int blocksize)
{
	for (auto& cache_entry : m_cache)
		cache_entry.resize(blocksize);

	m_cache_tags.fill(std::numeric_limits<u64>::max());
	m_blocksize = blocksize;
}

SectorReader::~SectorReader()
{
}

const std::vector<u8>& SectorReader::GetBlockData(u64 block_num)
{
	// TODO : Expand usage of the cache to more than one block :P
	if (m_cache_tags[0] == block_num)
		return m_cache[0];

	GetBlock(block_num, m_cache[0].data());
	m_cache_tags[0] = block_num;
	return m_cache[0];
}

bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
{
	u64 startingBlock = offset / m_blocksize;
	u64 remain = size;

	int positionInBlock = (int)(offset % m_blocksize);
	u64 block = startingBlock;

	while (remain > 0)
	{
		// Check if we are ready to do a large block read. > instead of >= so we don't bother if remain is only one block.
		if (positionInBlock == 0 && remain > (u64)m_blocksize)
		{
			u64 num_blocks = remain / m_blocksize;
			ReadMultipleAlignedBlocks(block, num_blocks, out_ptr);
			block += num_blocks;
			out_ptr += num_blocks * m_blocksize;
			remain -= num_blocks * m_blocksize;
			continue;
		}

		const std::vector<u8>& data = GetBlockData(block);

		u32 to_copy = m_blocksize - positionInBlock;
		if (to_copy >= remain)
		{
			// Yay, we are done!
			std::copy(data.begin() + positionInBlock, data.begin() + positionInBlock + remain, out_ptr);
			return true;
		}
		else
		{
			std::copy(data.begin() + positionInBlock, data.begin() + positionInBlock + to_copy, out_ptr);
			out_ptr += to_copy;
			remain -= to_copy;
			positionInBlock = 0;
			block++;
		}
	}

	return true;
}

bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr)
{
	for (u64 i = 0; i < num_blocks; i++)
	{
		const std::vector<u8>& data = GetBlockData(block_num + i);
		const u64 offset = i * m_blocksize;

		std::copy(data.begin(), data.end(), out_ptr + offset);
	}

	return true;
}

std::unique_ptr<IBlobReader> CreateBlobReader(const std::string& filename)
{
	if (cdio_is_cdrom(filename))
		return DriveReader::Create(filename);

	if (!File::Exists(filename))
		return nullptr;

	if (IsWbfsBlob(filename))
		return WbfsFileReader::Create(filename);

	if (IsGCZBlob(filename))
		return CompressedBlobReader::Create(filename);

	if (IsCISOBlob(filename))
		return CISOFileReader::Create(filename);

	// Still here? Assume plain file - since we know it exists due to the File::Exists check above.
	return PlainFileReader::Create(filename);
}

}  // namespace