Debian Codegraph @

Source/Core/DiscIO/CISOBlob.cpp

raw symbols SCIP
1 // Copyright 2010 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DiscIO/CISOBlob.h"
5
6 #include <algorithm>
7 #include <memory>
8 #include <utility>
9
10 #include "Common/BitUtils.h"
11 #include "Common/CommonTypes.h"
12
13 namespace DiscIO
14 {
15 CISOFileReader::CISOFileReader(File::DirectIOFile file) : m_file(std::move(file))
16 {
17 m_size = m_file.GetSize();
18
19 CISOHeader header;
20 m_file.OffsetRead(0, Common::AsWritableU8Span(header));
21
22 m_block_size = header.block_size;
23
24 MapType count = 0;
25 for (u32 idx = 0; idx < CISO_MAP_SIZE; ++idx)
26 m_ciso_map[idx] = (1 == header.map[idx]) ? count++ : UNUSED_BLOCK_ID;
27 }
28
29 std::unique_ptr<CISOFileReader> CISOFileReader::Create(File::DirectIOFile file)
30 {
31 CISOHeader header;
32 if (file.OffsetRead(0, Common::AsWritableU8Span(header)) && header.magic == CISO_MAGIC)
33 {
34 return std::unique_ptr<CISOFileReader>(new CISOFileReader(std::move(file)));
35 }
36
37 return nullptr;
38 }
39
40 std::unique_ptr<BlobReader> CISOFileReader::CopyReader() const
41 {
42 return Create(m_file);
43 }
44
45 u64 CISOFileReader::GetDataSize() const
46 {
47 return static_cast<u64>(CISO_MAP_SIZE) * m_block_size;
48 }
49
50 u64 CISOFileReader::GetRawSize() const
51 {
52 return m_size;
53 }
54
55 bool CISOFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
56 {
57 if (offset + nbytes > GetDataSize())
58 return false;
59
60 while (nbytes != 0)
61 {
62 u64 const block = offset / m_block_size;
63 u64 const data_offset = offset % m_block_size;
64 u64 const bytes_to_read = std::min(m_block_size - data_offset, nbytes);
65
66 if (block < CISO_MAP_SIZE && UNUSED_BLOCK_ID != m_ciso_map[block])
67 {
68 // calculate the base address
69 u64 const file_off = CISO_HEADER_SIZE + m_ciso_map[block] * (u64)m_block_size + data_offset;
70
71 if (!m_file.OffsetRead(file_off, out_ptr, bytes_to_read))
72 return false;
73 }
74 else
75 {
76 std::fill_n(out_ptr, bytes_to_read, 0);
77 }
78
79 out_ptr += bytes_to_read;
80 offset += bytes_to_read;
81 nbytes -= bytes_to_read;
82 }
83
84 return true;
85 }
86
87 } // namespace DiscIO