Skip to content
Permalink
Browse files

Merge pull request #3047 from Tilka/decompress_for_md5

DolphinWX: decompress discs to calculate MD5 hash
  • Loading branch information
Tilka committed Oct 12, 2015
2 parents 4467c76 + 00aefa5 commit 4c3dc0c1ae81e062b13cc3f1d21d367795698831
@@ -77,23 +77,19 @@ bool SectorReader::Read(u64 offset, u64 size, u8* out_ptr)
continue;
}

const u8* data = GetBlockData(block);
if (!data)
return false;

u32 toCopy = m_blocksize - positionInBlock;
if (toCopy >= remain)
{
const u8* data = GetBlockData(block);
if (!data)
return false;

// Yay, we are done!
memcpy(out_ptr, data + positionInBlock, (size_t)remain);
return true;
}
else
{
const u8* data = GetBlockData(block);
if (!data)
return false;

memcpy(out_ptr, data + positionInBlock, toCopy);
out_ptr += toCopy;
remain -= toCopy;
@@ -44,7 +44,7 @@ CISOFileReader* CISOFileReader::Create(const std::string& filename)

u64 CISOFileReader::GetDataSize() const
{
return GetRawSize();
return CISO_MAP_SIZE * m_block_size;
}

u64 CISOFileReader::GetRawSize() const
@@ -37,7 +37,11 @@ class CISOFileReader : public IBlobReader
static CISOFileReader* Create(const std::string& filename);

BlobType GetBlobType() const override { return BlobType::CISO; }

// The CISO format does not save the original file size.
// This function returns an upper bound.
u64 GetDataSize() const override;

u64 GetRawSize() const override;
bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

@@ -39,6 +39,8 @@ WbfsFileReader::WbfsFileReader(const std::string& filename)
m_wlba_table = new u16[m_blocks_per_disc];
m_files[0]->file.Seek(m_hd_sector_size + WII_DISC_HEADER_SIZE /*+ i * m_disc_info_size*/, SEEK_SET);
m_files[0]->file.ReadBytes(m_wlba_table, m_blocks_per_disc * sizeof(u16));
for (size_t i = 0; i < m_blocks_per_disc; i++)
m_wlba_table[i] = Common::swap16(m_wlba_table[i]);
}

WbfsFileReader::~WbfsFileReader()
@@ -49,6 +51,11 @@ WbfsFileReader::~WbfsFileReader()
delete[] m_wlba_table;
}

u64 WbfsFileReader::GetDataSize() const
{
return WII_SECTOR_COUNT * WII_SECTOR_SIZE;
}

bool WbfsFileReader::OpenFiles(const std::string& filename)
{
m_total_files = 0;
@@ -81,37 +88,26 @@ bool WbfsFileReader::OpenFiles(const std::string& filename)

bool WbfsFileReader::ReadHeader()
{
m_files[0]->file.Seek(4, SEEK_SET);

// Read hd size info
m_files[0]->file.ReadBytes(&m_hd_sector_count, 4);
m_hd_sector_count = Common::swap32(m_hd_sector_count);
m_files[0]->file.ReadBytes(&m_header, sizeof(WbfsHeader));
m_header.hd_sector_count = Common::swap32(m_header.hd_sector_count);

m_files[0]->file.ReadBytes(&m_hd_sector_shift, 1);
m_hd_sector_size = 1ull << m_hd_sector_shift;
m_hd_sector_size = 1ull << m_header.hd_sector_shift;

if (m_size != (m_hd_sector_count * m_hd_sector_size))
if (m_size != (m_header.hd_sector_count * m_hd_sector_size))
return false;

// Read wbfs cluster info
m_files[0]->file.ReadBytes(&m_wbfs_sector_shift, 1);
m_wbfs_sector_size = 1ull << m_wbfs_sector_shift;
m_wbfs_sector_size = 1ull << m_header.wbfs_sector_shift;
m_wbfs_sector_count = m_size / m_wbfs_sector_size;

if (m_wbfs_sector_size < WII_SECTOR_SIZE)
return false;

m_blocks_per_disc = (WII_SECTOR_COUNT * WII_SECTOR_SIZE) / m_wbfs_sector_size;
m_disc_info_size = align(WII_DISC_HEADER_SIZE + m_blocks_per_disc * 2, m_hd_sector_size);
m_blocks_per_disc = (WII_SECTOR_COUNT * WII_SECTOR_SIZE + m_wbfs_sector_size - 1) / m_wbfs_sector_size;
m_disc_info_size = align(WII_DISC_HEADER_SIZE + m_blocks_per_disc * sizeof(u16), m_hd_sector_size);

// Read disc table
m_files[0]->file.Seek(2, SEEK_CUR);
m_files[0]->file.ReadBytes(m_disc_table, 500);

if (m_disc_table[0] == 0)
return false;

return true;
return m_header.disc_table[0] != 0;
}

bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
@@ -122,7 +118,7 @@ bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
File::IOFile& data_file = SeekToCluster(offset, &read_size);
if (read_size == 0)
return false;
read_size = (read_size > nbytes) ? nbytes : read_size;
read_size = std::min(read_size, nbytes);

if (!data_file.ReadBytes(out_ptr, read_size))
{
@@ -140,10 +136,10 @@ bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)

File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available)
{
u64 base_cluster = (offset >> m_wbfs_sector_shift);
u64 base_cluster = (offset >> m_header.wbfs_sector_shift);
if (base_cluster < m_blocks_per_disc)
{
u64 cluster_address = m_wbfs_sector_size * Common::swap16(m_wlba_table[base_cluster]);
u64 cluster_address = m_wbfs_sector_size * m_wlba_table[base_cluster];
u64 cluster_offset = offset & (m_wbfs_sector_size - 1);
u64 final_address = cluster_address + cluster_offset;

@@ -20,7 +20,12 @@ class WbfsFileReader : public IBlobReader
static WbfsFileReader* Create(const std::string& filename);

BlobType GetBlobType() const override { return BlobType::WBFS; }
u64 GetDataSize() const override { return m_size; }

// The WBFS format does not save the original file size.
// This function returns a constant upper bound
// (the size of a double-layer Wii disc).
u64 GetDataSize() const override;

u64 GetRawSize() const override { return m_size; }
bool Read(u64 offset, u64 nbytes, u8* out_ptr) override;

@@ -48,15 +53,21 @@ class WbfsFileReader : public IBlobReader
u64 m_size;

u64 m_hd_sector_size;
u8 m_hd_sector_shift;
u32 m_hd_sector_count;

u64 m_wbfs_sector_size;
u8 m_wbfs_sector_shift;
u64 m_wbfs_sector_count;
u64 m_disc_info_size;

u8 m_disc_table[500];
#pragma pack(1)
struct WbfsHeader
{
char magic[4];
u32 hd_sector_count;
u8 hd_sector_shift;
u8 wbfs_sector_shift;
u8 padding[2];
u8 disc_table[500];
} m_header;
#pragma pack()

u16* m_wlba_table;
u64 m_blocks_per_disc;
@@ -57,6 +57,7 @@
#include "Core/GeckoCodeConfig.h"
#include "Core/PatchEngine.h"
#include "Core/Boot/Boot.h"
#include "DiscIO/Blob.h"
#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"
#include "DiscIO/VolumeCreator.h"
@@ -1237,8 +1238,8 @@ void CISOProperties::OnComputeMD5Sum(wxCommandEvent& WXUNUSED (event))
u64 read_offset = 0;
mbedtls_md5_context ctx;

File::IOFile file(OpenGameListItem.GetFileName(), "rb");
u64 game_size = file.GetSize();
std::unique_ptr<DiscIO::IBlobReader> file(DiscIO::CreateBlobReader(OpenGameListItem.GetFileName()));
u64 game_size = file->GetDataSize();

wxProgressDialog progressDialog(
_("Computing MD5 checksum"),
@@ -1254,13 +1255,14 @@ void CISOProperties::OnComputeMD5Sum(wxCommandEvent& WXUNUSED (event))

while(read_offset < game_size)
{
if (!progressDialog.Update((int)((double)read_offset / (double)game_size * 1000), _("Computing MD5 checksum")))
if (!progressDialog.Update((int)((double)read_offset / (double)game_size * 1000)))
return;

size_t read_size;
file.ReadArray(&data[0], data.size(), &read_size);
mbedtls_md5_update(&ctx, &data[0], read_size);
size_t read_size = std::min((u64)data.size(), game_size - read_offset);
if (!file->Read(read_offset, read_size, data.data()))
return;

mbedtls_md5_update(&ctx, data.data(), read_size);
read_offset += read_size;
}

0 comments on commit 4c3dc0c

Please sign in to comment.
You can’t perform that action at this time.