forked from stenzek/duckstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd_subchannel_replacement.cpp
114 lines (94 loc) · 3.26 KB
/
cd_subchannel_replacement.cpp
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
#include "cd_subchannel_replacement.h"
#include "file_system.h"
#include "log.h"
#include <algorithm>
#include <memory>
Log_SetChannel(CDSubChannelReplacement);
#pragma pack(push, 1)
struct SBIFileEntry
{
u8 minute_bcd;
u8 second_bcd;
u8 frame_bcd;
u8 type;
u8 data[10];
};
#pragma pack(pop)
CDSubChannelReplacement::CDSubChannelReplacement() = default;
CDSubChannelReplacement::~CDSubChannelReplacement() = default;
static constexpr u32 MSFToLBA(u8 minute_bcd, u8 second_bcd, u8 frame_bcd)
{
const u8 minute = PackedBCDToBinary(minute_bcd);
const u8 second = PackedBCDToBinary(second_bcd);
const u8 frame = PackedBCDToBinary(frame_bcd);
return (ZeroExtend32(minute) * 60 * 75) + (ZeroExtend32(second) * 75) + ZeroExtend32(frame);
}
bool CDSubChannelReplacement::LoadSBI(const char* path)
{
auto fp = FileSystem::OpenManagedCFile(path, "rb");
if (!fp)
return false;
char header[4];
if (std::fread(header, sizeof(header), 1, fp.get()) != 1)
{
Log_ErrorPrintf("Failed to read header for '%s'", path);
return true;
}
static constexpr char expected_header[] = {'S', 'B', 'I', '\0'};
if (std::memcmp(header, expected_header, sizeof(header)) != 0)
{
Log_ErrorPrintf("Invalid header in '%s'", path);
return true;
}
SBIFileEntry entry;
while (std::fread(&entry, sizeof(entry), 1, fp.get()) == 1)
{
if (!IsValidPackedBCD(entry.minute_bcd) || !IsValidPackedBCD(entry.second_bcd) ||
!IsValidPackedBCD(entry.frame_bcd))
{
Log_ErrorPrintf("Invalid position [%02x:%02x:%02x] in '%s'", entry.minute_bcd, entry.second_bcd, entry.frame_bcd,
path);
return false;
}
if (entry.type != 1)
{
Log_ErrorPrintf("Invalid type 0x%02X in '%s'", entry.type, path);
return false;
}
const u32 lba = MSFToLBA(entry.minute_bcd, entry.second_bcd, entry.frame_bcd);
CDImage::SubChannelQ subq;
std::copy_n(entry.data, countof(entry.data), subq.data.data());
// generate an invalid crc by flipping all bits from the valid crc (will never collide)
const u16 crc = subq.ComputeCRC(subq.data) ^ 0xFFFF;
subq.data[10] = Truncate8(crc);
subq.data[11] = Truncate8(crc >> 8);
m_replacement_subq.emplace(lba, subq);
}
Log_InfoPrintf("Loaded %zu replacement sectors from '%s'", m_replacement_subq.size(), path);
return true;
}
bool CDSubChannelReplacement::LoadSBIFromImagePath(const char* image_path)
{
return LoadSBI(FileSystem::ReplaceExtension(image_path, "sbi").c_str());
}
void CDSubChannelReplacement::AddReplacementSubChannelQ(u32 lba, const CDImage::SubChannelQ& subq)
{
auto iter = m_replacement_subq.find(lba);
if (iter != m_replacement_subq.end())
iter->second.data = subq.data;
else
m_replacement_subq.emplace(lba, subq);
}
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u8 minute_bcd, u8 second_bcd, u8 frame_bcd,
CDImage::SubChannelQ* subq) const
{
return GetReplacementSubChannelQ(MSFToLBA(minute_bcd, second_bcd, frame_bcd), subq);
}
bool CDSubChannelReplacement::GetReplacementSubChannelQ(u32 lba, CDImage::SubChannelQ* subq) const
{
const auto iter = m_replacement_subq.find(lba);
if (iter == m_replacement_subq.cend())
return false;
*subq = iter->second;
return true;
}