mirror of
https://github.com/stenzek/duckstation.git
synced 2024-11-30 09:30:49 +00:00
CDImage: Merge bin/ecm handling into one class
Some checks are pending
Automated Builds / 💻 Windows (push) Waiting to run
Automated Builds / 🐧 Linux AppImage (push) Waiting to run
Automated Builds / 📦 Linux Flatpak (push) Waiting to run
Automated Builds / 🍎 MacOS (push) Waiting to run
Automated Builds / 📤 Create Release (push) Blocked by required conditions
Some checks are pending
Automated Builds / 💻 Windows (push) Waiting to run
Automated Builds / 🐧 Linux AppImage (push) Waiting to run
Automated Builds / 📦 Linux Flatpak (push) Waiting to run
Automated Builds / 🍎 MacOS (push) Waiting to run
Automated Builds / 📤 Create Release (push) Blocked by required conditions
Means that cuesheets with ECM tracks will now load as expected.
This commit is contained in:
parent
7e23a23536
commit
f538e6a5ff
@ -3,11 +3,9 @@ add_library(util
|
||||
audio_stream.h
|
||||
cd_image.cpp
|
||||
cd_image.h
|
||||
cd_image_bin.cpp
|
||||
cd_image_cue.cpp
|
||||
cd_image_chd.cpp
|
||||
cd_image_device.cpp
|
||||
cd_image_ecm.cpp
|
||||
cd_image_hasher.cpp
|
||||
cd_image_hasher.h
|
||||
cd_image_m3u.cpp
|
||||
|
@ -83,7 +83,7 @@ std::unique_ptr<CDImage> CDImage::Open(const char* filename, bool allow_patches,
|
||||
image = OpenCueSheetImage(filename, error);
|
||||
}
|
||||
else if (StringUtil::Strcasecmp(extension, ".bin") == 0 || StringUtil::Strcasecmp(extension, ".img") == 0 ||
|
||||
StringUtil::Strcasecmp(extension, ".iso") == 0)
|
||||
StringUtil::Strcasecmp(extension, ".iso") == 0 || StringUtil::Strcasecmp(extension, ".ecm") == 0)
|
||||
{
|
||||
image = OpenBinImage(filename, error);
|
||||
}
|
||||
@ -91,10 +91,6 @@ std::unique_ptr<CDImage> CDImage::Open(const char* filename, bool allow_patches,
|
||||
{
|
||||
image = OpenCHDImage(filename, error);
|
||||
}
|
||||
else if (StringUtil::Strcasecmp(extension, ".ecm") == 0)
|
||||
{
|
||||
image = OpenEcmImage(filename, error);
|
||||
}
|
||||
else if (StringUtil::Strcasecmp(extension, ".mds") == 0)
|
||||
{
|
||||
image = OpenMdsImage(filename, error);
|
||||
|
@ -247,10 +247,9 @@ public:
|
||||
|
||||
// Opening disc image.
|
||||
static std::unique_ptr<CDImage> Open(const char* filename, bool allow_patches, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenBinImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenCueSheetImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenBinImage(const char* path, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenCueSheetImage(const char* path, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenCHDImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenEcmImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenMdsImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenPBPImage(const char* filename, Error* error);
|
||||
static std::unique_ptr<CDImage> OpenM3uImage(const char* filename, bool apply_patches, Error* error);
|
||||
|
@ -1,135 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "cd_image.h"
|
||||
|
||||
#include "common/error.h"
|
||||
#include "common/file_system.h"
|
||||
#include "common/path.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class CDImageBin : public CDImage
|
||||
{
|
||||
public:
|
||||
CDImageBin();
|
||||
~CDImageBin() override;
|
||||
|
||||
bool Open(const char* filename, Error* error);
|
||||
|
||||
s64 GetSizeOnDisk() const override;
|
||||
|
||||
protected:
|
||||
bool ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index) override;
|
||||
|
||||
private:
|
||||
std::FILE* m_fp = nullptr;
|
||||
u64 m_file_position = 0;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
CDImageBin::CDImageBin() = default;
|
||||
|
||||
CDImageBin::~CDImageBin()
|
||||
{
|
||||
if (m_fp)
|
||||
std::fclose(m_fp);
|
||||
}
|
||||
|
||||
bool CDImageBin::Open(const char* filename, Error* error)
|
||||
{
|
||||
m_filename = filename;
|
||||
m_fp = FileSystem::OpenSharedCFile(filename, "rb", FileSystem::FileShareMode::DenyWrite, error);
|
||||
if (!m_fp)
|
||||
{
|
||||
Error::AddPrefixFmt(error, "Failed to open binfile '{}': ", Path::GetFileName(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
const u32 track_sector_size = RAW_SECTOR_SIZE;
|
||||
|
||||
// determine the length from the file
|
||||
std::fseek(m_fp, 0, SEEK_END);
|
||||
const u32 file_size = static_cast<u32>(std::ftell(m_fp));
|
||||
std::fseek(m_fp, 0, SEEK_SET);
|
||||
|
||||
m_lba_count = file_size / track_sector_size;
|
||||
|
||||
SubChannelQ::Control control = {};
|
||||
TrackMode mode = TrackMode::Mode2Raw;
|
||||
control.data = mode != TrackMode::Audio;
|
||||
|
||||
// Two seconds default pregap.
|
||||
const u32 pregap_frames = 2 * FRAMES_PER_SECOND;
|
||||
Index pregap_index = {};
|
||||
pregap_index.file_sector_size = track_sector_size;
|
||||
pregap_index.start_lba_on_disc = 0;
|
||||
pregap_index.start_lba_in_track = static_cast<LBA>(-static_cast<s32>(pregap_frames));
|
||||
pregap_index.length = pregap_frames;
|
||||
pregap_index.track_number = 1;
|
||||
pregap_index.index_number = 0;
|
||||
pregap_index.mode = mode;
|
||||
pregap_index.submode = CDImage::SubchannelMode::None;
|
||||
pregap_index.control.bits = control.bits;
|
||||
pregap_index.is_pregap = true;
|
||||
m_indices.push_back(pregap_index);
|
||||
|
||||
// Data index.
|
||||
Index data_index = {};
|
||||
data_index.file_index = 0;
|
||||
data_index.file_offset = 0;
|
||||
data_index.file_sector_size = track_sector_size;
|
||||
data_index.start_lba_on_disc = pregap_index.length;
|
||||
data_index.track_number = 1;
|
||||
data_index.index_number = 1;
|
||||
data_index.start_lba_in_track = 0;
|
||||
data_index.length = m_lba_count;
|
||||
data_index.mode = mode;
|
||||
data_index.submode = CDImage::SubchannelMode::None;
|
||||
data_index.control.bits = control.bits;
|
||||
m_indices.push_back(data_index);
|
||||
|
||||
// Assume a single track.
|
||||
m_tracks.push_back(Track{static_cast<u32>(1), data_index.start_lba_on_disc, static_cast<u32>(0), m_lba_count, mode,
|
||||
SubchannelMode::None, control});
|
||||
|
||||
AddLeadOutIndex();
|
||||
|
||||
return Seek(1, Position{0, 0, 0});
|
||||
}
|
||||
|
||||
bool CDImageBin::ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index)
|
||||
{
|
||||
const u64 file_position = index.file_offset + (static_cast<u64>(lba_in_index) * index.file_sector_size);
|
||||
if (m_file_position != file_position)
|
||||
{
|
||||
if (std::fseek(m_fp, static_cast<long>(file_position), SEEK_SET) != 0)
|
||||
return false;
|
||||
|
||||
m_file_position = file_position;
|
||||
}
|
||||
|
||||
if (std::fread(buffer, index.file_sector_size, 1, m_fp) != 1)
|
||||
{
|
||||
std::fseek(m_fp, static_cast<long>(m_file_position), SEEK_SET);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_file_position += index.file_sector_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
s64 CDImageBin::GetSizeOnDisk() const
|
||||
{
|
||||
return FileSystem::FSize64(m_fp);
|
||||
}
|
||||
|
||||
std::unique_ptr<CDImage> CDImage::OpenBinImage(const char* filename, Error* error)
|
||||
{
|
||||
std::unique_ptr<CDImageBin> image = std::make_unique<CDImageBin>();
|
||||
if (!image->Open(filename, error))
|
||||
return {};
|
||||
|
||||
return image;
|
||||
}
|
@ -11,8 +11,10 @@
|
||||
#include "common/file_system.h"
|
||||
#include "common/log.h"
|
||||
#include "common/path.h"
|
||||
#include "common/string_util.h"
|
||||
|
||||
#include "fmt/format.h"
|
||||
#include "libchdr/cdrom.h" // EDC functions
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
@ -22,13 +24,123 @@ LOG_CHANNEL(CDImage);
|
||||
|
||||
namespace {
|
||||
|
||||
class TrackFileInterface
|
||||
{
|
||||
public:
|
||||
TrackFileInterface(std::string filename);
|
||||
virtual ~TrackFileInterface();
|
||||
|
||||
ALWAYS_INLINE const std::string& GetFileName() const { return m_filename; }
|
||||
|
||||
static std::unique_ptr<TrackFileInterface> OpenBinaryFile(const std::string_view filename, const std::string& path,
|
||||
Error* error);
|
||||
|
||||
virtual u64 GetSize() = 0;
|
||||
virtual u64 GetDiskSize() = 0;
|
||||
|
||||
virtual bool Read(void* buffer, u64 offset, u32 size, Error* error) = 0;
|
||||
|
||||
protected:
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
class BinaryTrackFileInterface final : public TrackFileInterface
|
||||
{
|
||||
public:
|
||||
BinaryTrackFileInterface(std::string filename, FileSystem::ManagedCFilePtr file);
|
||||
~BinaryTrackFileInterface() override;
|
||||
|
||||
u64 GetSize() override;
|
||||
u64 GetDiskSize() override;
|
||||
|
||||
bool Read(void* buffer, u64 offset, u32 size, Error* error) override;
|
||||
|
||||
private:
|
||||
FileSystem::ManagedCFilePtr m_file;
|
||||
u64 m_file_position = 0;
|
||||
};
|
||||
|
||||
class ECMTrackFileInterface final : public TrackFileInterface
|
||||
{
|
||||
public:
|
||||
ECMTrackFileInterface(std::string filename, FileSystem::ManagedCFilePtr file);
|
||||
~ECMTrackFileInterface() override;
|
||||
|
||||
static std::unique_ptr<TrackFileInterface> Create(std::string filename, FileSystem::ManagedCFilePtr file,
|
||||
Error* error);
|
||||
|
||||
u64 GetSize() override;
|
||||
u64 GetDiskSize() override;
|
||||
|
||||
bool Read(void* buffer, u64 offset, u32 size, Error* error) override;
|
||||
|
||||
private:
|
||||
enum class SectorType : u32
|
||||
{
|
||||
Raw = 0x00,
|
||||
Mode1 = 0x01,
|
||||
Mode2Form1 = 0x02,
|
||||
Mode2Form2 = 0x03,
|
||||
Count,
|
||||
};
|
||||
|
||||
static constexpr std::array<u32, static_cast<u32>(SectorType::Count)> s_sector_sizes = {
|
||||
0x930, // raw
|
||||
0x803, // mode1
|
||||
0x804, // mode2form1
|
||||
0x918, // mode2form2
|
||||
};
|
||||
|
||||
static constexpr std::array<u32, static_cast<u32>(SectorType::Count)> s_chunk_sizes = {
|
||||
0, // raw
|
||||
2352, // mode1
|
||||
2336, // mode2form1
|
||||
2336, // mode2form2
|
||||
};
|
||||
|
||||
struct SectorEntry
|
||||
{
|
||||
u32 file_offset;
|
||||
u32 chunk_size;
|
||||
SectorType type;
|
||||
};
|
||||
|
||||
using DataMap = std::map<u32, SectorEntry>;
|
||||
|
||||
bool BuildSectorMap(Error* error);
|
||||
bool ReadChunks(u32 disc_offset, u32 size);
|
||||
|
||||
FileSystem::ManagedCFilePtr m_file;
|
||||
|
||||
DataMap m_data_map;
|
||||
std::vector<u8> m_chunk_buffer;
|
||||
u32 m_chunk_start = 0;
|
||||
u32 m_lba_count = 0;
|
||||
};
|
||||
|
||||
class WaveTrackFileInterface final : public TrackFileInterface
|
||||
{
|
||||
public:
|
||||
WaveTrackFileInterface(std::string filename, WAVReader reader);
|
||||
~WaveTrackFileInterface() override;
|
||||
|
||||
u64 GetSize() override;
|
||||
u64 GetDiskSize() override;
|
||||
|
||||
bool Read(void* buffer, u64 offset, u32 size, Error* error) override;
|
||||
|
||||
private:
|
||||
WAVReader m_reader;
|
||||
};
|
||||
|
||||
class CDImageCueSheet : public CDImage
|
||||
{
|
||||
public:
|
||||
CDImageCueSheet();
|
||||
~CDImageCueSheet() override;
|
||||
|
||||
bool OpenAndParse(const char* filename, Error* error);
|
||||
bool OpenAndParseCueSheet(const char* path, Error* error);
|
||||
bool OpenAndParseSingleFile(const char* path, Error* error);
|
||||
|
||||
s64 GetSizeOnDisk() const override;
|
||||
|
||||
@ -36,74 +148,49 @@ protected:
|
||||
bool ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index) override;
|
||||
|
||||
private:
|
||||
class TrackFileInterface
|
||||
{
|
||||
public:
|
||||
TrackFileInterface(std::string filename);
|
||||
virtual ~TrackFileInterface();
|
||||
|
||||
ALWAYS_INLINE const std::string& GetFilename() const { return m_filename; }
|
||||
|
||||
virtual u64 GetSize() = 0;
|
||||
virtual u64 GetDiskSize() = 0;
|
||||
|
||||
virtual bool Read(void* buffer, u64 offset, u32 size, Error* error) = 0;
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
struct BinaryTrackFileInterface final : public TrackFileInterface
|
||||
{
|
||||
public:
|
||||
BinaryTrackFileInterface(std::string filename, FileSystem::ManagedCFilePtr file);
|
||||
~BinaryTrackFileInterface() override;
|
||||
|
||||
u64 GetSize() override;
|
||||
u64 GetDiskSize() override;
|
||||
|
||||
bool Read(void* buffer, u64 offset, u32 size, Error* error) override;
|
||||
|
||||
private:
|
||||
FileSystem::ManagedCFilePtr m_file;
|
||||
u64 m_file_position = 0;
|
||||
};
|
||||
|
||||
struct WaveTrackFileInterface final : public TrackFileInterface
|
||||
{
|
||||
public:
|
||||
WaveTrackFileInterface(std::string filename, WAVReader reader);
|
||||
~WaveTrackFileInterface() override;
|
||||
|
||||
u64 GetSize() override;
|
||||
u64 GetDiskSize() override;
|
||||
|
||||
bool Read(void* buffer, u64 offset, u32 size, Error* error) override;
|
||||
|
||||
private:
|
||||
WAVReader m_reader;
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<TrackFileInterface>> m_files;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
CDImageCueSheet::TrackFileInterface::TrackFileInterface(std::string filename) : m_filename(std::move(filename))
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TrackFileInterface::TrackFileInterface(std::string filename) : m_filename(std::move(filename))
|
||||
{
|
||||
}
|
||||
|
||||
CDImageCueSheet::TrackFileInterface::~TrackFileInterface() = default;
|
||||
TrackFileInterface::~TrackFileInterface() = default;
|
||||
|
||||
CDImageCueSheet::BinaryTrackFileInterface::BinaryTrackFileInterface(std::string filename,
|
||||
FileSystem::ManagedCFilePtr file)
|
||||
BinaryTrackFileInterface::BinaryTrackFileInterface(std::string filename, FileSystem::ManagedCFilePtr file)
|
||||
: TrackFileInterface(std::move(filename)), m_file(std::move(file))
|
||||
{
|
||||
}
|
||||
|
||||
CDImageCueSheet::BinaryTrackFileInterface::~BinaryTrackFileInterface() = default;
|
||||
BinaryTrackFileInterface::~BinaryTrackFileInterface() = default;
|
||||
|
||||
bool CDImageCueSheet::BinaryTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* error)
|
||||
std::unique_ptr<TrackFileInterface> TrackFileInterface::OpenBinaryFile(const std::string_view filename,
|
||||
const std::string& path, Error* error)
|
||||
{
|
||||
std::unique_ptr<TrackFileInterface> fi;
|
||||
|
||||
FileSystem::ManagedCFilePtr file =
|
||||
FileSystem::OpenManagedSharedCFile(path.c_str(), "rb", FileSystem::FileShareMode::DenyWrite, error);
|
||||
if (!file)
|
||||
{
|
||||
Error::AddPrefixFmt(error, "Failed to open '{}': ", FileSystem::GetDisplayNameFromPath(path));
|
||||
return fi;
|
||||
}
|
||||
|
||||
// Check for ECM format.
|
||||
if (StringUtil::EndsWithNoCase(FileSystem::GetDisplayNameFromPath(path), ".ecm"))
|
||||
fi = ECMTrackFileInterface::Create(std::string(filename), std::move(file), error);
|
||||
else
|
||||
fi = std::make_unique<BinaryTrackFileInterface>(std::string(filename), std::move(file));
|
||||
|
||||
return fi;
|
||||
}
|
||||
|
||||
bool BinaryTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* error)
|
||||
{
|
||||
if (m_file_position != offset)
|
||||
{
|
||||
@ -122,24 +209,298 @@ bool CDImageCueSheet::BinaryTrackFileInterface::Read(void* buffer, u64 offset, u
|
||||
return true;
|
||||
}
|
||||
|
||||
u64 CDImageCueSheet::BinaryTrackFileInterface::GetSize()
|
||||
u64 BinaryTrackFileInterface::GetSize()
|
||||
{
|
||||
return static_cast<u64>(std::max<s64>(FileSystem::FSize64(m_file.get()), 0));
|
||||
}
|
||||
|
||||
u64 CDImageCueSheet::BinaryTrackFileInterface::GetDiskSize()
|
||||
u64 BinaryTrackFileInterface::GetDiskSize()
|
||||
{
|
||||
return static_cast<u64>(std::max<s64>(FileSystem::FSize64(m_file.get()), 0));
|
||||
}
|
||||
|
||||
CDImageCueSheet::WaveTrackFileInterface::WaveTrackFileInterface(std::string filename, WAVReader reader)
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ECMTrackFileInterface::ECMTrackFileInterface(std::string path, FileSystem::ManagedCFilePtr file)
|
||||
: TrackFileInterface(std::move(path)), m_file(std::move(file))
|
||||
{
|
||||
}
|
||||
|
||||
ECMTrackFileInterface::~ECMTrackFileInterface()
|
||||
{
|
||||
}
|
||||
|
||||
std::unique_ptr<TrackFileInterface> ECMTrackFileInterface::Create(std::string filename,
|
||||
FileSystem::ManagedCFilePtr file, Error* error)
|
||||
{
|
||||
std::unique_ptr<ECMTrackFileInterface> fi =
|
||||
std::make_unique<ECMTrackFileInterface>(std::move(filename), std::move(file));
|
||||
if (!fi->BuildSectorMap(error))
|
||||
fi.reset();
|
||||
|
||||
return fi;
|
||||
}
|
||||
|
||||
bool ECMTrackFileInterface::BuildSectorMap(Error* error)
|
||||
{
|
||||
const s64 file_size = FileSystem::FSize64(m_file.get(), error);
|
||||
if (file_size <= 0)
|
||||
return false;
|
||||
|
||||
char header[4];
|
||||
if (std::fread(header, sizeof(header), 1, m_file.get()) != 1 || header[0] != 'E' || header[1] != 'C' ||
|
||||
header[2] != 'M' || header[3] != 0)
|
||||
{
|
||||
ERROR_LOG("Failed to read/invalid header");
|
||||
Error::SetStringView(error, "Failed to read/invalid header");
|
||||
return false;
|
||||
}
|
||||
|
||||
// build sector map
|
||||
u32 file_offset = Truncate32(FileSystem::FTell64(m_file.get()));
|
||||
u32 disc_offset = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int bits = std::fgetc(m_file.get());
|
||||
if (bits == EOF)
|
||||
{
|
||||
ERROR_LOG("Unexpected EOF after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Unexpected EOF after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
file_offset++;
|
||||
const SectorType type = static_cast<SectorType>(static_cast<u32>(bits) & 0x03u);
|
||||
u32 count = (static_cast<u32>(bits) >> 2) & 0x1F;
|
||||
u32 shift = 5;
|
||||
while (bits & 0x80)
|
||||
{
|
||||
bits = std::fgetc(m_file.get());
|
||||
if (bits == EOF)
|
||||
{
|
||||
ERROR_LOG("Unexpected EOF after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Unexpected EOF after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
count |= (static_cast<u32>(bits) & 0x7F) << shift;
|
||||
shift += 7;
|
||||
file_offset++;
|
||||
}
|
||||
|
||||
if (count == 0xFFFFFFFFu)
|
||||
break;
|
||||
|
||||
// for this sector
|
||||
count++;
|
||||
|
||||
if (count >= 0x80000000u)
|
||||
{
|
||||
ERROR_LOG("Corrupted header after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Corrupted header after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == SectorType::Raw)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
const u32 size = std::min<u32>(count, 2352);
|
||||
m_data_map.emplace(disc_offset, SectorEntry{file_offset, size, type});
|
||||
disc_offset += size;
|
||||
file_offset += size;
|
||||
count -= size;
|
||||
|
||||
if (static_cast<s64>(file_offset) > file_size)
|
||||
{
|
||||
ERROR_LOG("Out of file bounds after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Out of file bounds after {} chunks", m_data_map.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const u32 size = s_sector_sizes[static_cast<u32>(type)];
|
||||
const u32 chunk_size = s_chunk_sizes[static_cast<u32>(type)];
|
||||
for (u32 i = 0; i < count; i++)
|
||||
{
|
||||
m_data_map.emplace(disc_offset, SectorEntry{file_offset, chunk_size, type});
|
||||
disc_offset += chunk_size;
|
||||
file_offset += size;
|
||||
|
||||
if (static_cast<s64>(file_offset) > file_size)
|
||||
{
|
||||
ERROR_LOG("Out of file bounds after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Out of file bounds after {} chunks", m_data_map.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (FileSystem::FSeek64(m_file.get(), file_offset, SEEK_SET) != 0)
|
||||
{
|
||||
ERROR_LOG("Failed to seek to offset {} after {} chunks", file_offset, m_data_map.size());
|
||||
Error::SetStringFmt(error, "Failed to seek to offset {} after {} chunks", file_offset, m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_lba_count = disc_offset / CDImage::RAW_SECTOR_SIZE;
|
||||
if ((disc_offset % CDImage::RAW_SECTOR_SIZE) != 0)
|
||||
WARNING_LOG("ECM image is misaligned with offset {}", disc_offset);
|
||||
|
||||
if (m_data_map.empty() || m_lba_count == 0)
|
||||
{
|
||||
ERROR_LOG("No data in image '{}'", m_filename);
|
||||
Error::SetStringView(error, "No sectors found");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ECMTrackFileInterface::ReadChunks(u32 disc_offset, u32 size)
|
||||
{
|
||||
DataMap::iterator next =
|
||||
m_data_map.lower_bound((disc_offset > CDImage::RAW_SECTOR_SIZE) ? (disc_offset - CDImage::RAW_SECTOR_SIZE) : 0);
|
||||
DataMap::iterator current = m_data_map.begin();
|
||||
while (next != m_data_map.end() && next->first <= disc_offset)
|
||||
current = next++;
|
||||
|
||||
// extra bytes if we need to buffer some at the start
|
||||
m_chunk_start = current->first;
|
||||
m_chunk_buffer.clear();
|
||||
if (m_chunk_start < disc_offset)
|
||||
size += (disc_offset - current->first);
|
||||
|
||||
u32 total_bytes_read = 0;
|
||||
while (total_bytes_read < size)
|
||||
{
|
||||
if (current == m_data_map.end() || FileSystem::FSeek64(m_file.get(), current->second.file_offset, SEEK_SET) != 0)
|
||||
return false;
|
||||
|
||||
const u32 chunk_size = current->second.chunk_size;
|
||||
const u32 chunk_start = static_cast<u32>(m_chunk_buffer.size());
|
||||
m_chunk_buffer.resize(chunk_start + chunk_size);
|
||||
|
||||
if (current->second.type == SectorType::Raw)
|
||||
{
|
||||
if (std::fread(&m_chunk_buffer[chunk_start], chunk_size, 1, m_file.get()) != 1)
|
||||
return false;
|
||||
|
||||
total_bytes_read += chunk_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// u8* sector = &m_chunk_buffer[chunk_start];
|
||||
u8 sector[CDImage::RAW_SECTOR_SIZE];
|
||||
|
||||
// TODO: needed?
|
||||
std::memset(sector, 0, CDImage::RAW_SECTOR_SIZE);
|
||||
std::memset(sector + 1, 0xFF, 10);
|
||||
|
||||
u32 skip;
|
||||
switch (current->second.type)
|
||||
{
|
||||
case SectorType::Mode1:
|
||||
{
|
||||
sector[0x0F] = 0x01;
|
||||
if (std::fread(sector + 0x00C, 0x003, 1, m_file.get()) != 1 ||
|
||||
std::fread(sector + 0x010, 0x800, 1, m_file.get()) != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
edc_set(§or[2064], edc_compute(sector, 2064));
|
||||
ecc_generate(sector);
|
||||
skip = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case SectorType::Mode2Form1:
|
||||
{
|
||||
sector[0x0F] = 0x02;
|
||||
if (std::fread(sector + 0x014, 0x804, 1, m_file.get()) != 1)
|
||||
return false;
|
||||
|
||||
sector[0x10] = sector[0x14];
|
||||
sector[0x11] = sector[0x15];
|
||||
sector[0x12] = sector[0x16];
|
||||
sector[0x13] = sector[0x17];
|
||||
|
||||
edc_set(§or[2072], edc_compute(§or[16], 2056));
|
||||
ecc_generate(sector);
|
||||
skip = 0x10;
|
||||
}
|
||||
break;
|
||||
|
||||
case SectorType::Mode2Form2:
|
||||
{
|
||||
sector[0x0F] = 0x02;
|
||||
if (std::fread(sector + 0x014, 0x918, 1, m_file.get()) != 1)
|
||||
return false;
|
||||
|
||||
sector[0x10] = sector[0x14];
|
||||
sector[0x11] = sector[0x15];
|
||||
sector[0x12] = sector[0x16];
|
||||
sector[0x13] = sector[0x17];
|
||||
|
||||
edc_set(§or[2348], edc_compute(§or[16], 2332));
|
||||
skip = 0x10;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
UnreachableCode();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(&m_chunk_buffer[chunk_start], sector + skip, chunk_size);
|
||||
total_bytes_read += chunk_size;
|
||||
}
|
||||
|
||||
++current;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
u64 ECMTrackFileInterface::GetSize()
|
||||
{
|
||||
return static_cast<u64>(m_lba_count) * static_cast<u64>(CDImage::RAW_SECTOR_SIZE);
|
||||
}
|
||||
|
||||
u64 ECMTrackFileInterface::GetDiskSize()
|
||||
{
|
||||
return static_cast<u64>(std::max<s64>(FileSystem::FSize64(m_file.get()), 0));
|
||||
}
|
||||
|
||||
bool ECMTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* error)
|
||||
{
|
||||
const u64 file_end = offset + size;
|
||||
if (offset < m_chunk_start || file_end > (m_chunk_start + m_chunk_buffer.size()))
|
||||
{
|
||||
if (!ReadChunks(Truncate32(offset), CDImage::RAW_SECTOR_SIZE))
|
||||
return false;
|
||||
}
|
||||
|
||||
DebugAssert(offset >= m_chunk_start && file_end <= (m_chunk_start + m_chunk_buffer.size()));
|
||||
|
||||
const size_t chunk_offset = static_cast<size_t>(offset - m_chunk_start);
|
||||
std::memcpy(buffer, &m_chunk_buffer[chunk_offset], CDImage::RAW_SECTOR_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
WaveTrackFileInterface::WaveTrackFileInterface(std::string filename, WAVReader reader)
|
||||
: TrackFileInterface(std::move(filename)), m_reader(std::move(reader))
|
||||
{
|
||||
}
|
||||
|
||||
CDImageCueSheet::WaveTrackFileInterface::~WaveTrackFileInterface() = default;
|
||||
WaveTrackFileInterface::~WaveTrackFileInterface() = default;
|
||||
|
||||
bool CDImageCueSheet::WaveTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* error)
|
||||
bool WaveTrackFileInterface::Read(void* buffer, u64 offset, u32 size, Error* error)
|
||||
{
|
||||
// Should always be a multiple of 4 (sizeof frame).
|
||||
if ((offset & 3) != 0 || (size & 3) != 0) [[unlikely]]
|
||||
@ -170,12 +531,12 @@ bool CDImageCueSheet::WaveTrackFileInterface::Read(void* buffer, u64 offset, u32
|
||||
return true;
|
||||
}
|
||||
|
||||
u64 CDImageCueSheet::WaveTrackFileInterface::GetSize()
|
||||
u64 WaveTrackFileInterface::GetSize()
|
||||
{
|
||||
return Common::AlignUp(static_cast<u64>(m_reader.GetNumFrames()) * 4, 2352);
|
||||
}
|
||||
|
||||
u64 CDImageCueSheet::WaveTrackFileInterface::GetDiskSize()
|
||||
u64 WaveTrackFileInterface::GetDiskSize()
|
||||
{
|
||||
return m_reader.GetFileSize();
|
||||
}
|
||||
@ -184,12 +545,12 @@ CDImageCueSheet::CDImageCueSheet() = default;
|
||||
|
||||
CDImageCueSheet::~CDImageCueSheet() = default;
|
||||
|
||||
bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
bool CDImageCueSheet::OpenAndParseCueSheet(const char* path, Error* error)
|
||||
{
|
||||
std::FILE* fp = FileSystem::OpenSharedCFile(filename, "rb", FileSystem::FileShareMode::DenyWrite, error);
|
||||
std::FILE* fp = FileSystem::OpenSharedCFile(path, "rb", FileSystem::FileShareMode::DenyWrite, error);
|
||||
if (!fp)
|
||||
{
|
||||
Error::AddPrefixFmt(error, "Failed to open cuesheet '{}': ", Path::GetFileName(filename));
|
||||
Error::AddPrefixFmt(error, "Failed to open cuesheet '{}': ", Path::GetFileName(path));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -202,7 +563,7 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
|
||||
std::fclose(fp);
|
||||
|
||||
m_filename = filename;
|
||||
m_filename = path;
|
||||
|
||||
u32 disc_lba = 0;
|
||||
|
||||
@ -219,41 +580,37 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
u32 track_file_index = 0;
|
||||
for (; track_file_index < m_files.size(); track_file_index++)
|
||||
{
|
||||
if (m_files[track_file_index]->GetFilename() == track_filename)
|
||||
if (m_files[track_file_index]->GetFileName() == track_filename)
|
||||
break;
|
||||
}
|
||||
if (track_file_index == m_files.size())
|
||||
{
|
||||
std::string track_full_filename =
|
||||
std::string track_full_path =
|
||||
!Path::IsAbsolute(track_filename) ? Path::BuildRelativePath(m_filename, track_filename) : track_filename;
|
||||
Error track_error;
|
||||
std::unique_ptr<TrackFileInterface> track_file;
|
||||
|
||||
if (track->file_format == CueParser::FileFormat::Binary)
|
||||
{
|
||||
FileSystem::ManagedCFilePtr track_fp =
|
||||
FileSystem::OpenManagedCFile(track_full_filename.c_str(), "rb", &track_error);
|
||||
if (!track_fp && track_file_index == 0)
|
||||
track_file = TrackFileInterface::OpenBinaryFile(track_filename, track_full_path, error);
|
||||
if (!track_file && track_file_index == 0)
|
||||
{
|
||||
// many users have bad cuesheets, or they're renamed the files without updating the cuesheet.
|
||||
// so, try searching for a bin with the same name as the cue, but only for the first referenced file.
|
||||
std::string alternative_filename = Path::ReplaceExtension(filename, "bin");
|
||||
track_fp = FileSystem::OpenManagedCFile(alternative_filename.c_str(), "rb");
|
||||
if (track_fp)
|
||||
std::string alternative_filename = Path::ReplaceExtension(path, "bin");
|
||||
track_file = TrackFileInterface::OpenBinaryFile(track_filename, alternative_filename, error);
|
||||
if (track_file)
|
||||
{
|
||||
WARNING_LOG("Your cue sheet references an invalid file '{}', but this was found at '{}' instead.",
|
||||
track_filename, alternative_filename);
|
||||
track_full_filename = std::move(alternative_filename);
|
||||
}
|
||||
}
|
||||
if (track_fp)
|
||||
track_file = std::make_unique<BinaryTrackFileInterface>(std::move(track_full_filename), std::move(track_fp));
|
||||
}
|
||||
else if (track->file_format == CueParser::FileFormat::Wave)
|
||||
{
|
||||
// Since all the frames are packed tightly in the wave file, we only need to get the start offset.
|
||||
WAVReader reader;
|
||||
if (reader.Open(track_full_filename.c_str(), &track_error))
|
||||
if (reader.Open(track_full_path.c_str(), error))
|
||||
{
|
||||
if (reader.GetNumChannels() != AUDIO_CHANNELS || reader.GetSampleRate() != AUDIO_SAMPLE_RATE)
|
||||
{
|
||||
@ -261,18 +618,16 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
return false;
|
||||
}
|
||||
|
||||
track_file = std::make_unique<WaveTrackFileInterface>(std::move(track_full_filename), std::move(reader));
|
||||
track_file = std::make_unique<WaveTrackFileInterface>(track_filename, std::move(reader));
|
||||
}
|
||||
else
|
||||
{
|
||||
Error::AddPrefixFmt(error, "Failed to open '{}': ", track_filename);
|
||||
}
|
||||
}
|
||||
|
||||
if (!track_file)
|
||||
{
|
||||
ERROR_LOG("Failed to open track filename '{}' (from '{}' and '{}'): {}", track_full_filename, track_filename,
|
||||
filename, track_error.GetDescription());
|
||||
Error::SetStringFmt(error, "Failed to open track filename '{}' (from '{}' and '{}'): {}", track_full_filename,
|
||||
track_filename, Path::GetFileName(filename), track_error.GetDescription());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_files.push_back(std::move(track_file));
|
||||
}
|
||||
@ -297,10 +652,10 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
file_size /= track_sector_size;
|
||||
if (track_start >= file_size)
|
||||
{
|
||||
ERROR_LOG("Failed to open track {} in '{}': track start is out of range ({} vs {})", track_num, filename,
|
||||
ERROR_LOG("Failed to open track {} in '{}': track start is out of range ({} vs {})", track_num, path,
|
||||
track_start, file_size);
|
||||
Error::SetStringFmt(error, "Failed to open track {} in '{}': track start is out of range ({} vs {}))",
|
||||
track_num, Path::GetFileName(filename), track_start, file_size);
|
||||
track_num, Path::GetFileName(path), track_start, file_size);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -340,12 +695,12 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
else
|
||||
{
|
||||
// Two seconds pregap for track 1 is assumed if not specified.
|
||||
// Some people have broken (older) dumps where a two second pregap was implicit but not specified in the cuesheet.
|
||||
// The problem is we can't tell between a missing implicit two second pregap and a zero second pregap. Most of
|
||||
// these seem to be a single bin file for all tracks. So if this is the case, we add the two seconds in if it's
|
||||
// not specified. If this is an audio CD (likely when track 1 is not data), we don't add these pregaps, and rely
|
||||
// on the cuesheet. If we did add them, it causes issues in some games (e.g. Dancing Stage featuring DREAMS COME
|
||||
// TRUE).
|
||||
// Some people have broken (older) dumps where a two second pregap was implicit but not specified in the
|
||||
// cuesheet. The problem is we can't tell between a missing implicit two second pregap and a zero second pregap.
|
||||
// Most of these seem to be a single bin file for all tracks. So if this is the case, we add the two seconds in
|
||||
// if it's not specified. If this is an audio CD (likely when track 1 is not data), we don't add these pregaps,
|
||||
// and rely on the cuesheet. If we did add them, it causes issues in some games (e.g. Dancing Stage featuring
|
||||
// DREAMS COME TRUE).
|
||||
const bool is_multi_track_bin = (track_num > 1 && track_file_index == m_indices[0].file_index);
|
||||
const bool likely_audio_cd = (parser.GetTrack(1)->mode == TrackMode::Audio);
|
||||
|
||||
@ -433,8 +788,8 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
|
||||
if (m_tracks.empty())
|
||||
{
|
||||
ERROR_LOG("File '{}' contains no tracks", filename);
|
||||
Error::SetStringFmt(error, "File '{}' contains no tracks", Path::GetFileName(filename));
|
||||
ERROR_LOG("File '{}' contains no tracks", path);
|
||||
Error::SetStringFmt(error, "File '{}' contains no tracks", Path::GetFileName(path));
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -444,6 +799,61 @@ bool CDImageCueSheet::OpenAndParse(const char* filename, Error* error)
|
||||
return Seek(1, Position{0, 0, 0});
|
||||
}
|
||||
|
||||
bool CDImageCueSheet::OpenAndParseSingleFile(const char* path, Error* error)
|
||||
{
|
||||
m_filename = path;
|
||||
|
||||
std::unique_ptr<TrackFileInterface> fi = TrackFileInterface::OpenBinaryFile(Path::GetFileName(path), path, error);
|
||||
if (!fi)
|
||||
return false;
|
||||
|
||||
const u32 track_sector_size = RAW_SECTOR_SIZE;
|
||||
m_lba_count = Truncate32(fi->GetSize() / track_sector_size);
|
||||
m_files.push_back(std::move(fi));
|
||||
|
||||
SubChannelQ::Control control = {};
|
||||
TrackMode mode = TrackMode::Mode2Raw;
|
||||
control.data = mode != TrackMode::Audio;
|
||||
|
||||
// Two seconds default pregap.
|
||||
const u32 pregap_frames = 2 * FRAMES_PER_SECOND;
|
||||
Index pregap_index = {};
|
||||
pregap_index.file_sector_size = track_sector_size;
|
||||
pregap_index.start_lba_on_disc = 0;
|
||||
pregap_index.start_lba_in_track = static_cast<LBA>(-static_cast<s32>(pregap_frames));
|
||||
pregap_index.length = pregap_frames;
|
||||
pregap_index.track_number = 1;
|
||||
pregap_index.index_number = 0;
|
||||
pregap_index.mode = mode;
|
||||
pregap_index.submode = CDImage::SubchannelMode::None;
|
||||
pregap_index.control.bits = control.bits;
|
||||
pregap_index.is_pregap = true;
|
||||
m_indices.push_back(pregap_index);
|
||||
|
||||
// Data index.
|
||||
Index data_index = {};
|
||||
data_index.file_index = 0;
|
||||
data_index.file_offset = 0;
|
||||
data_index.file_sector_size = track_sector_size;
|
||||
data_index.start_lba_on_disc = pregap_index.length;
|
||||
data_index.track_number = 1;
|
||||
data_index.index_number = 1;
|
||||
data_index.start_lba_in_track = 0;
|
||||
data_index.length = m_lba_count;
|
||||
data_index.mode = mode;
|
||||
data_index.submode = CDImage::SubchannelMode::None;
|
||||
data_index.control.bits = control.bits;
|
||||
m_indices.push_back(data_index);
|
||||
|
||||
// Assume a single track.
|
||||
m_tracks.push_back(Track{static_cast<u32>(1), data_index.start_lba_on_disc, static_cast<u32>(0), m_lba_count, mode,
|
||||
SubchannelMode::None, control});
|
||||
|
||||
AddLeadOutIndex();
|
||||
|
||||
return Seek(1, Position{0, 0, 0});
|
||||
}
|
||||
|
||||
bool CDImageCueSheet::ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index)
|
||||
{
|
||||
DebugAssert(index.file_index < m_files.size());
|
||||
@ -469,11 +879,20 @@ s64 CDImageCueSheet::GetSizeOnDisk() const
|
||||
return size;
|
||||
}
|
||||
|
||||
std::unique_ptr<CDImage> CDImage::OpenCueSheetImage(const char* filename, Error* error)
|
||||
std::unique_ptr<CDImage> CDImage::OpenCueSheetImage(const char* path, Error* error)
|
||||
{
|
||||
std::unique_ptr<CDImageCueSheet> image = std::make_unique<CDImageCueSheet>();
|
||||
if (!image->OpenAndParse(filename, error))
|
||||
return {};
|
||||
if (!image->OpenAndParseCueSheet(path, error))
|
||||
image.reset();
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
std::unique_ptr<CDImage> CDImage::OpenBinImage(const char* path, Error* error)
|
||||
{
|
||||
std::unique_ptr<CDImageCueSheet> image = std::make_unique<CDImageCueSheet>();
|
||||
if (!image->OpenAndParseSingleFile(path, error))
|
||||
image.reset();
|
||||
|
||||
return image;
|
||||
}
|
||||
|
@ -1,395 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
|
||||
|
||||
#include "cd_image.h"
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "common/error.h"
|
||||
#include "common/file_system.h"
|
||||
#include "common/log.h"
|
||||
#include "common/path.h"
|
||||
|
||||
#include "libchdr/cdrom.h"
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
|
||||
LOG_CHANNEL(CDImage);
|
||||
|
||||
namespace {
|
||||
|
||||
class CDImageEcm : public CDImage
|
||||
{
|
||||
public:
|
||||
CDImageEcm();
|
||||
~CDImageEcm() override;
|
||||
|
||||
bool Open(const char* filename, Error* error);
|
||||
|
||||
s64 GetSizeOnDisk() const override;
|
||||
|
||||
protected:
|
||||
bool ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index) override;
|
||||
|
||||
private:
|
||||
bool ReadChunks(u32 disc_offset, u32 size);
|
||||
|
||||
std::FILE* m_fp = nullptr;
|
||||
|
||||
enum class SectorType : u32
|
||||
{
|
||||
Raw = 0x00,
|
||||
Mode1 = 0x01,
|
||||
Mode2Form1 = 0x02,
|
||||
Mode2Form2 = 0x03,
|
||||
Count,
|
||||
};
|
||||
|
||||
static constexpr std::array<u32, static_cast<u32>(SectorType::Count)> s_sector_sizes = {
|
||||
0x930, // raw
|
||||
0x803, // mode1
|
||||
0x804, // mode2form1
|
||||
0x918, // mode2form2
|
||||
};
|
||||
|
||||
static constexpr std::array<u32, static_cast<u32>(SectorType::Count)> s_chunk_sizes = {
|
||||
0, // raw
|
||||
2352, // mode1
|
||||
2336, // mode2form1
|
||||
2336, // mode2form2
|
||||
};
|
||||
|
||||
struct SectorEntry
|
||||
{
|
||||
u32 file_offset;
|
||||
u32 chunk_size;
|
||||
SectorType type;
|
||||
};
|
||||
|
||||
using DataMap = std::map<u32, SectorEntry>;
|
||||
|
||||
DataMap m_data_map;
|
||||
std::vector<u8> m_chunk_buffer;
|
||||
u32 m_chunk_start = 0;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
CDImageEcm::CDImageEcm() = default;
|
||||
|
||||
CDImageEcm::~CDImageEcm()
|
||||
{
|
||||
if (m_fp)
|
||||
std::fclose(m_fp);
|
||||
}
|
||||
|
||||
bool CDImageEcm::Open(const char* filename, Error* error)
|
||||
{
|
||||
m_filename = filename;
|
||||
m_fp = FileSystem::OpenSharedCFile(filename, "rb", FileSystem::FileShareMode::DenyWrite, error);
|
||||
if (!m_fp)
|
||||
{
|
||||
Error::AddPrefixFmt(error, "Failed to open binfile '{}': ", Path::GetFileName(filename));
|
||||
return false;
|
||||
}
|
||||
|
||||
s64 file_size;
|
||||
if (FileSystem::FSeek64(m_fp, 0, SEEK_END) != 0 || (file_size = FileSystem::FTell64(m_fp)) <= 0 ||
|
||||
FileSystem::FSeek64(m_fp, 0, SEEK_SET) != 0)
|
||||
{
|
||||
ERROR_LOG("Get file size failed: errno {}", errno);
|
||||
if (error)
|
||||
error->SetErrno(errno);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
char header[4];
|
||||
if (std::fread(header, sizeof(header), 1, m_fp) != 1 || header[0] != 'E' || header[1] != 'C' || header[2] != 'M' ||
|
||||
header[3] != 0)
|
||||
{
|
||||
ERROR_LOG("Failed to read/invalid header");
|
||||
Error::SetStringView(error, "Failed to read/invalid header");
|
||||
return false;
|
||||
}
|
||||
|
||||
// build sector map
|
||||
u32 file_offset = static_cast<u32>(std::ftell(m_fp));
|
||||
u32 disc_offset = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int bits = std::fgetc(m_fp);
|
||||
if (bits == EOF)
|
||||
{
|
||||
ERROR_LOG("Unexpected EOF after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Unexpected EOF after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
file_offset++;
|
||||
const SectorType type = static_cast<SectorType>(static_cast<u32>(bits) & 0x03u);
|
||||
u32 count = (static_cast<u32>(bits) >> 2) & 0x1F;
|
||||
u32 shift = 5;
|
||||
while (bits & 0x80)
|
||||
{
|
||||
bits = std::fgetc(m_fp);
|
||||
if (bits == EOF)
|
||||
{
|
||||
ERROR_LOG("Unexpected EOF after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Unexpected EOF after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
count |= (static_cast<u32>(bits) & 0x7F) << shift;
|
||||
shift += 7;
|
||||
file_offset++;
|
||||
}
|
||||
|
||||
if (count == 0xFFFFFFFFu)
|
||||
break;
|
||||
|
||||
// for this sector
|
||||
count++;
|
||||
|
||||
if (count >= 0x80000000u)
|
||||
{
|
||||
ERROR_LOG("Corrupted header after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Corrupted header after {} chunks", m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (type == SectorType::Raw)
|
||||
{
|
||||
while (count > 0)
|
||||
{
|
||||
const u32 size = std::min<u32>(count, 2352);
|
||||
m_data_map.emplace(disc_offset, SectorEntry{file_offset, size, type});
|
||||
disc_offset += size;
|
||||
file_offset += size;
|
||||
count -= size;
|
||||
|
||||
if (static_cast<s64>(file_offset) > file_size)
|
||||
{
|
||||
ERROR_LOG("Out of file bounds after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Out of file bounds after {} chunks", m_data_map.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const u32 size = s_sector_sizes[static_cast<u32>(type)];
|
||||
const u32 chunk_size = s_chunk_sizes[static_cast<u32>(type)];
|
||||
for (u32 i = 0; i < count; i++)
|
||||
{
|
||||
m_data_map.emplace(disc_offset, SectorEntry{file_offset, chunk_size, type});
|
||||
disc_offset += chunk_size;
|
||||
file_offset += size;
|
||||
|
||||
if (static_cast<s64>(file_offset) > file_size)
|
||||
{
|
||||
ERROR_LOG("Out of file bounds after {} chunks", m_data_map.size());
|
||||
Error::SetStringFmt(error, "Out of file bounds after {} chunks", m_data_map.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (std::fseek(m_fp, file_offset, SEEK_SET) != 0)
|
||||
{
|
||||
ERROR_LOG("Failed to seek to offset {} after {} chunks", file_offset, m_data_map.size());
|
||||
Error::SetStringFmt(error, "Failed to seek to offset {} after {} chunks", file_offset, m_data_map.size());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_data_map.empty())
|
||||
{
|
||||
ERROR_LOG("No data in image '{}'", filename);
|
||||
Error::SetStringFmt(error, "No data in image '{}'", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lba_count = disc_offset / RAW_SECTOR_SIZE;
|
||||
if ((disc_offset % RAW_SECTOR_SIZE) != 0)
|
||||
WARNING_LOG("ECM image is misaligned with offset {}", disc_offset);
|
||||
if (m_lba_count == 0)
|
||||
return false;
|
||||
|
||||
SubChannelQ::Control control = {};
|
||||
TrackMode mode = TrackMode::Mode2Raw;
|
||||
control.data = mode != TrackMode::Audio;
|
||||
|
||||
// Two seconds default pregap.
|
||||
const u32 pregap_frames = 2 * FRAMES_PER_SECOND;
|
||||
Index pregap_index = {};
|
||||
pregap_index.file_sector_size = RAW_SECTOR_SIZE;
|
||||
pregap_index.start_lba_on_disc = 0;
|
||||
pregap_index.start_lba_in_track = static_cast<LBA>(-static_cast<s32>(pregap_frames));
|
||||
pregap_index.length = pregap_frames;
|
||||
pregap_index.track_number = 1;
|
||||
pregap_index.index_number = 0;
|
||||
pregap_index.mode = mode;
|
||||
pregap_index.submode = CDImage::SubchannelMode::None;
|
||||
pregap_index.control.bits = control.bits;
|
||||
pregap_index.is_pregap = true;
|
||||
m_indices.push_back(pregap_index);
|
||||
|
||||
// Data index.
|
||||
Index data_index = {};
|
||||
data_index.file_index = 0;
|
||||
data_index.file_offset = 0;
|
||||
data_index.file_sector_size = RAW_SECTOR_SIZE;
|
||||
data_index.start_lba_on_disc = pregap_index.length;
|
||||
data_index.track_number = 1;
|
||||
data_index.index_number = 1;
|
||||
data_index.start_lba_in_track = 0;
|
||||
data_index.length = m_lba_count;
|
||||
data_index.mode = mode;
|
||||
data_index.submode = CDImage::SubchannelMode::None;
|
||||
data_index.control.bits = control.bits;
|
||||
m_indices.push_back(data_index);
|
||||
|
||||
// Assume a single track.
|
||||
m_tracks.push_back(Track{static_cast<u32>(1), data_index.start_lba_on_disc, static_cast<u32>(0), m_lba_count, mode,
|
||||
SubchannelMode::None, control});
|
||||
|
||||
AddLeadOutIndex();
|
||||
|
||||
m_chunk_buffer.reserve(RAW_SECTOR_SIZE * 2);
|
||||
return Seek(1, Position{0, 0, 0});
|
||||
}
|
||||
|
||||
bool CDImageEcm::ReadChunks(u32 disc_offset, u32 size)
|
||||
{
|
||||
DataMap::iterator next =
|
||||
m_data_map.lower_bound((disc_offset > RAW_SECTOR_SIZE) ? (disc_offset - RAW_SECTOR_SIZE) : 0);
|
||||
DataMap::iterator current = m_data_map.begin();
|
||||
while (next != m_data_map.end() && next->first <= disc_offset)
|
||||
current = next++;
|
||||
|
||||
// extra bytes if we need to buffer some at the start
|
||||
m_chunk_start = current->first;
|
||||
m_chunk_buffer.clear();
|
||||
if (m_chunk_start < disc_offset)
|
||||
size += (disc_offset - current->first);
|
||||
|
||||
u32 total_bytes_read = 0;
|
||||
while (total_bytes_read < size)
|
||||
{
|
||||
if (current == m_data_map.end() || std::fseek(m_fp, current->second.file_offset, SEEK_SET) != 0)
|
||||
return false;
|
||||
|
||||
const u32 chunk_size = current->second.chunk_size;
|
||||
const u32 chunk_start = static_cast<u32>(m_chunk_buffer.size());
|
||||
m_chunk_buffer.resize(chunk_start + chunk_size);
|
||||
|
||||
if (current->second.type == SectorType::Raw)
|
||||
{
|
||||
if (std::fread(&m_chunk_buffer[chunk_start], chunk_size, 1, m_fp) != 1)
|
||||
return false;
|
||||
|
||||
total_bytes_read += chunk_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// u8* sector = &m_chunk_buffer[chunk_start];
|
||||
u8 sector[RAW_SECTOR_SIZE];
|
||||
|
||||
// TODO: needed?
|
||||
std::memset(sector, 0, RAW_SECTOR_SIZE);
|
||||
std::memset(sector + 1, 0xFF, 10);
|
||||
|
||||
u32 skip;
|
||||
switch (current->second.type)
|
||||
{
|
||||
case SectorType::Mode1:
|
||||
{
|
||||
sector[0x0F] = 0x01;
|
||||
if (std::fread(sector + 0x00C, 0x003, 1, m_fp) != 1 || std::fread(sector + 0x010, 0x800, 1, m_fp) != 1)
|
||||
return false;
|
||||
|
||||
edc_set(§or[2064], edc_compute(sector, 2064));
|
||||
ecc_generate(sector);
|
||||
skip = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case SectorType::Mode2Form1:
|
||||
{
|
||||
sector[0x0F] = 0x02;
|
||||
if (std::fread(sector + 0x014, 0x804, 1, m_fp) != 1)
|
||||
return false;
|
||||
|
||||
sector[0x10] = sector[0x14];
|
||||
sector[0x11] = sector[0x15];
|
||||
sector[0x12] = sector[0x16];
|
||||
sector[0x13] = sector[0x17];
|
||||
|
||||
edc_set(§or[2072], edc_compute(§or[16], 2056));
|
||||
ecc_generate(sector);
|
||||
skip = 0x10;
|
||||
}
|
||||
break;
|
||||
|
||||
case SectorType::Mode2Form2:
|
||||
{
|
||||
sector[0x0F] = 0x02;
|
||||
if (std::fread(sector + 0x014, 0x918, 1, m_fp) != 1)
|
||||
return false;
|
||||
|
||||
sector[0x10] = sector[0x14];
|
||||
sector[0x11] = sector[0x15];
|
||||
sector[0x12] = sector[0x16];
|
||||
sector[0x13] = sector[0x17];
|
||||
|
||||
edc_set(§or[2348], edc_compute(§or[16], 2332));
|
||||
skip = 0x10;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
UnreachableCode();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::memcpy(&m_chunk_buffer[chunk_start], sector + skip, chunk_size);
|
||||
total_bytes_read += chunk_size;
|
||||
}
|
||||
|
||||
++current;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CDImageEcm::ReadSectorFromIndex(void* buffer, const Index& index, LBA lba_in_index)
|
||||
{
|
||||
const u32 file_start = static_cast<u32>(index.file_offset) + (lba_in_index * index.file_sector_size);
|
||||
const u32 file_end = file_start + RAW_SECTOR_SIZE;
|
||||
|
||||
if (file_start < m_chunk_start || file_end > (m_chunk_start + m_chunk_buffer.size()))
|
||||
{
|
||||
if (!ReadChunks(file_start, RAW_SECTOR_SIZE))
|
||||
return false;
|
||||
}
|
||||
|
||||
DebugAssert(file_start >= m_chunk_start && file_end <= (m_chunk_start + m_chunk_buffer.size()));
|
||||
|
||||
const size_t chunk_offset = static_cast<size_t>(file_start - m_chunk_start);
|
||||
std::memcpy(buffer, &m_chunk_buffer[chunk_offset], RAW_SECTOR_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
s64 CDImageEcm::GetSizeOnDisk() const
|
||||
{
|
||||
return FileSystem::FSize64(m_fp);
|
||||
}
|
||||
|
||||
std::unique_ptr<CDImage> CDImage::OpenEcmImage(const char* filename, Error* error)
|
||||
{
|
||||
std::unique_ptr<CDImageEcm> image = std::make_unique<CDImageEcm>();
|
||||
if (!image->Open(filename, error))
|
||||
return {};
|
||||
|
||||
return image;
|
||||
}
|
@ -112,11 +112,9 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="audio_stream.cpp" />
|
||||
<ClCompile Include="cd_image.cpp" />
|
||||
<ClCompile Include="cd_image_bin.cpp" />
|
||||
<ClCompile Include="cd_image_chd.cpp" />
|
||||
<ClCompile Include="cd_image_cue.cpp" />
|
||||
<ClCompile Include="cd_image_device.cpp" />
|
||||
<ClCompile Include="cd_image_ecm.cpp" />
|
||||
<ClCompile Include="cd_image_hasher.cpp" />
|
||||
<ClCompile Include="cd_image_m3u.cpp" />
|
||||
<ClCompile Include="cd_image_mds.cpp" />
|
||||
|
@ -81,7 +81,6 @@
|
||||
<ClCompile Include="cd_image.cpp" />
|
||||
<ClCompile Include="audio_stream.cpp" />
|
||||
<ClCompile Include="cd_image_cue.cpp" />
|
||||
<ClCompile Include="cd_image_bin.cpp" />
|
||||
<ClCompile Include="iso_reader.cpp" />
|
||||
<ClCompile Include="cd_image_chd.cpp" />
|
||||
<ClCompile Include="wav_reader_writer.cpp" />
|
||||
@ -89,7 +88,6 @@
|
||||
<ClCompile Include="cd_image_memory.cpp" />
|
||||
<ClCompile Include="shiftjis.cpp" />
|
||||
<ClCompile Include="page_fault_handler.cpp" />
|
||||
<ClCompile Include="cd_image_ecm.cpp" />
|
||||
<ClCompile Include="cd_image_mds.cpp" />
|
||||
<ClCompile Include="cd_image_pbp.cpp" />
|
||||
<ClCompile Include="cd_image_m3u.cpp" />
|
||||
|
Loading…
Reference in New Issue
Block a user