Large blob code cleanup. Compressed ISO ("gcz") support reactivated. Beginnings of raw drive reading code. Deprecate file mapping in an ugly way.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@566 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard
2008-09-17 23:25:35 +00:00
parent 2cd7acfaee
commit cb90e61368
31 changed files with 1209 additions and 844 deletions
+170 -162
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Version="8.00"
Name="Common"
ProjectGUID="{C573CAF7-EE6A-458E-8049-16C0BF34C2E9}"
RootNamespace="Common"
@@ -88,151 +88,6 @@
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
CommandLine="&quot;$(ProjectDir)SubWCRev.exe&quot; &quot;$(SolutionDir)\.&quot; &quot;$(ProjectDir)\src\svnrev_template.h&quot; &quot;$(ProjectDir)\src\svnrev.h&quot;"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
FavorSizeOrSpeed="2"
AdditionalIncludeDirectories="../../PluginSpecs"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB;_SECURE_SCL=0;_CRT_SECURE_NO_WARNINGS"
RuntimeLibrary="0"
BufferSecurityCheck="false"
EnableEnhancedInstructionSet="2"
FloatingPointModel="0"
RuntimeTypeInfo="false"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="stdafx.h"
AssemblerListingLocation="$(IntDir)\"
WarningLevel="3"
WarnAsError="false"
DebugInformationFormat="3"
ForcedIncludeFiles="stdafx.h"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
AdditionalDependencies="wsock32.lib"
OutputFile="$(OutDir)/Common.lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="DebugFast|Win32"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
CommandLine="&quot;$(ProjectDir)SubWCRev.exe&quot; &quot;$(SolutionDir)\.&quot; &quot;$(ProjectDir)\src\svnrev_template.h&quot; &quot;$(ProjectDir)\src\svnrev.h&quot;"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="3"
FavorSizeOrSpeed="1"
AdditionalIncludeDirectories="../../PluginSpecs"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB;_SECURE_SCL=0;LOGGING"
RuntimeLibrary="0"
BufferSecurityCheck="false"
EnableEnhancedInstructionSet="2"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="stdafx.h"
AssemblerListingLocation="$(IntDir)\"
WarningLevel="3"
WarnAsError="false"
DebugInformationFormat="3"
ForcedIncludeFiles="stdafx.h"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
AdditionalDependencies="wsock32.lib"
OutputFile="$(OutDir)/Common.lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Debug|x64"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
@@ -307,6 +162,80 @@
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
CommandLine="&quot;$(ProjectDir)SubWCRev.exe&quot; &quot;$(SolutionDir)\.&quot; &quot;$(ProjectDir)\src\svnrev_template.h&quot; &quot;$(ProjectDir)\src\svnrev.h&quot;"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
FavorSizeOrSpeed="2"
AdditionalIncludeDirectories="../../PluginSpecs"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB;_SECURE_SCL=0;_CRT_SECURE_NO_WARNINGS"
RuntimeLibrary="0"
BufferSecurityCheck="false"
EnableEnhancedInstructionSet="2"
FloatingPointModel="0"
RuntimeTypeInfo="false"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="stdafx.h"
AssemblerListingLocation="$(IntDir)\"
WarningLevel="3"
WarnAsError="false"
DebugInformationFormat="3"
ForcedIncludeFiles="stdafx.h"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
AdditionalDependencies="wsock32.lib"
OutputFile="$(OutDir)/Common.lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|x64"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
@@ -382,6 +311,77 @@
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="DebugFast|Win32"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
ConfigurationType="4"
InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
CommandLine="&quot;$(ProjectDir)SubWCRev.exe&quot; &quot;$(SolutionDir)\.&quot; &quot;$(ProjectDir)\src\svnrev_template.h&quot; &quot;$(ProjectDir)\src\svnrev.h&quot;"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="3"
FavorSizeOrSpeed="1"
AdditionalIncludeDirectories="../../PluginSpecs"
PreprocessorDefinitions="WIN32;NDEBUG;_LIB;_SECURE_SCL=0;LOGGING"
RuntimeLibrary="0"
BufferSecurityCheck="false"
EnableEnhancedInstructionSet="2"
UsePrecompiledHeader="2"
PrecompiledHeaderThrough="stdafx.h"
AssemblerListingLocation="$(IntDir)\"
WarningLevel="3"
WarnAsError="false"
DebugInformationFormat="3"
ForcedIncludeFiles="stdafx.h"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLibrarianTool"
AdditionalDependencies="wsock32.lib"
OutputFile="$(OutDir)/Common.lib"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="DebugFast|x64"
OutputDirectory="$(PlatformName)\$(ConfigurationName)"
@@ -489,6 +489,14 @@
RelativePath=".\Src\CPUDetect.h"
>
</File>
<File
RelativePath=".\Src\DriveUtil.cpp"
>
</File>
<File
RelativePath=".\Src\DriveUtil.h"
>
</File>
<File
RelativePath=".\Src\DynamicLibrary.cpp"
>
@@ -596,22 +604,6 @@
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="DebugFast|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Debug|x64"
>
@@ -620,6 +612,14 @@
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|x64"
>
@@ -628,6 +628,14 @@
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="DebugFast|Win32"
>
<Tool
Name="VCCLCompilerTool"
UsePrecompiledHeader="1"
/>
</FileConfiguration>
<FileConfiguration
Name="DebugFast|x64"
>
+7
View File
@@ -18,7 +18,14 @@
#ifndef _COMMON_H
#define _COMMON_H
#define _CRTDBG_MAP_ALLOC
#define _CRTDBG_MAP_ALLOC_NEW
#ifdef _WIN32
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#include "../../../PluginSpecs/CommonTypes.h"
#else
#include "CommonTypes.h"
+57
View File
@@ -0,0 +1,57 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "Common.h"
#ifdef _WIN32
#include <windows.h>
#include <winioctl.h>
#endif
void GetAllRemovableDrives(std::vector<std::string> *drives) {
drives->clear();
#ifdef _WIN32
HANDLE hDisk;
DISK_GEOMETRY diskGeometry;
for (int i = 'A'; i < 'Z'; i++)
{
char path[MAX_PATH];
sprintf(path, "\\\\.\\%c:", i);
hDisk = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDisk != INVALID_HANDLE_VALUE)
{
DWORD dwBytes;
DeviceIoControl(hDisk, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &diskGeometry, sizeof(DISK_GEOMETRY), &dwBytes, NULL);
// Only proceed if disk is a removable media
if (diskGeometry.MediaType == RemovableMedia)
{
if (diskGeometry.BytesPerSector == 2048) {
// Probably CD/DVD drive.
// "Remove" the "\\.\" part of the path and return it.
drives->push_back(path + 4);
}
}
}
CloseHandle(hDisk);
}
#else
// TODO
// stat("/media/cdrom") or whatever etc etc
#endif
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef _DRIVEUTIL_H
#define _DRIVEUTIL_H
#include <string>
#include <vector>
// Tools to enumerate drives (HDD, DVD, CD) in a platform-independent manner.
void GetAllRemovableDrives(std::vector<std::string> *drives);
#endif
+1 -1
View File
@@ -28,7 +28,7 @@ public:
static void Explore(const std::string &path);
static bool IsDirectory(const std::string &filename);
static bool CreateDir(const std::string &filename);
static std::string GetUserDirectory();
static std::string GetUserDirectory();
};
#endif
+1 -1
View File
@@ -19,7 +19,7 @@
#include "Common.h"
u32 HashFletcher(const u8* data_u8, size_t length); // FAST
u32 HashFletcher(const u8* data_u8, size_t length); // FAST. Length & 1 == 0.
u32 HashAdler32(const u8* data, size_t len); // Fairly accurate, slightly slower
u32 HashFNV(const u8* ptr, int length); // Another fast and decent hash
u32 HashEctor(const u8* ptr, int length); // JUNK. DO NOT USE FOR NEW THINGS
+1 -1
View File
@@ -226,7 +226,7 @@ void CMappedFile::Unlock(u8* ptr)
}
IMappedFile* IMappedFile::CreateMappedFile(void)
IMappedFile* IMappedFile::CreateMappedFileDEPRECATED(void)
{
return(new CMappedFile);
}
+1 -1
View File
@@ -43,7 +43,7 @@ class IMappedFile
virtual u8* Lock(u64 _offset, u64 _size) = 0;
virtual void Unlock(u8* ptr) = 0;
static IMappedFile* CreateMappedFile();
static IMappedFile* CreateMappedFileDEPRECATED();
};
} // end of namespace DiscIO
+2 -2
View File
@@ -49,7 +49,7 @@
bool CBoot::Boot_BIN(const std::string& _rFilename)
{
Common::IMappedFile* pFile = Common::IMappedFile::CreateMappedFile();
Common::IMappedFile* pFile = Common::IMappedFile::CreateMappedFileDEPRECATED();
if (pFile->Open(_rFilename.c_str()))
{
@@ -443,7 +443,7 @@ bool CBoot::LoadMapFromFilename(const std::string &_rFilename, const char *_game
bool CBoot::Load_BIOS(const std::string& _rBiosFilename)
{
bool bResult = false;
Common::IMappedFile* pFile = Common::IMappedFile::CreateMappedFile();
Common::IMappedFile* pFile = Common::IMappedFile::CreateMappedFileDEPRECATED();
if (pFile->Open(_rBiosFilename.c_str()))
{
if (pFile->GetSize() >= 1024*1024*2)
+2 -2
View File
@@ -7,7 +7,7 @@
bool CBoot::IsElfWii(const char *filename)
{
Common::IMappedFile *mapfile = Common::IMappedFile::CreateMappedFile();
Common::IMappedFile *mapfile = Common::IMappedFile::CreateMappedFileDEPRECATED();
bool ok = mapfile->Open(filename);
if (!ok)
return false;
@@ -29,7 +29,7 @@ bool CBoot::IsElfWii(const char *filename)
bool CBoot::Boot_ELF(const char *filename)
{
Common::IMappedFile *mapfile = Common::IMappedFile::CreateMappedFile();
Common::IMappedFile *mapfile = Common::IMappedFile::CreateMappedFileDEPRECATED();
mapfile->Open(filename);
u8 *ptr = mapfile->Lock(0, mapfile->GetSize());
u8 *mem = new u8[(size_t)mapfile->GetSize()];
+1 -1
View File
@@ -60,7 +60,7 @@ static const SPatch OSPatches[] =
{ "OSPanic", HLE_OS::HLE_OSPanic },
{ "vprintf", HLE_OS::HLE_vprintf },
{ "printf", HLE_OS::HLE_printf },
{ "puts", HLE_OS::HLE_printf }, //gcc-optimized printf?
{ "puts", HLE_OS::HLE_printf }, //gcc-optimized printf?
// wii only
{ "SCCheckStatus", HLE_Misc::UnimplementedFunctionFalse },
@@ -60,6 +60,7 @@ void BackPatchError(const std::string &text, u8 *codePtr, u32 emAddress) {
// that many of them in a typical program/game.
void BackPatch(u8 *codePtr, int accessType, u32 emAddress)
{
#ifdef _M_X64
if (!IsInJitCode(codePtr))
return; // this will become a regular crash real soon after this
@@ -135,6 +136,7 @@ void BackPatch(u8 *codePtr, int accessType, u32 emAddress)
CALL(trampoline);
NOP((int)info.instructionSize + bswapNopCount - 5);
SetCodePtr(oldCodePtr);
#endif
}
} // namespace
+25 -17
View File
@@ -28,6 +28,14 @@
SymbolDB g_symbolDB;
SymbolDB::SymbolDB()
{
}
SymbolDB::~SymbolDB()
{
}
void SymbolDB::List()
{
for (XFuncMap::iterator iter = functions.begin(); iter != functions.end(); iter++)
@@ -232,27 +240,27 @@ bool SymbolDB::LoadMap(const char *filename)
bool started = false;
while (!feof(f))
{
char line[512],temp[256];
char line[512], temp[256];
fgets(line, 511, f);
if (strlen(line) < 4)
continue;
sscanf(line,"%s",temp);
if (strcmp(temp,"UNUSED")==0) continue;
if (strcmp(temp,".text")==0) {started = true; continue;};
if (strcmp(temp,".init")==0) {started = true; continue;};
if (strcmp(temp,"Starting")==0) continue;
if (strcmp(temp,"extab")==0) continue;
if (strcmp(temp,".ctors")==0) break; //uh?
if (strcmp(temp,".dtors")==0) break;
if (strcmp(temp,".rodata")==0) continue;
if (strcmp(temp,".data")==0) continue;
if (strcmp(temp,".sbss")==0) continue;
if (strcmp(temp,".sdata")==0) continue;
if (strcmp(temp,".sdata2")==0) continue;
if (strcmp(temp,"address")==0) continue;
if (strcmp(temp,"-----------------------")==0) continue;
if (strcmp(temp,".sbss2")==0) break;
sscanf(line, "%s", temp);
if (strcmp(temp, "UNUSED")==0) continue;
if (strcmp(temp, ".text")==0) {started = true; continue;};
if (strcmp(temp, ".init")==0) {started = true; continue;};
if (strcmp(temp, "Starting")==0) continue;
if (strcmp(temp, "extab")==0) continue;
if (strcmp(temp, ".ctors")==0) break; //uh?
if (strcmp(temp, ".dtors")==0) break;
if (strcmp(temp, ".rodata")==0) continue;
if (strcmp(temp, ".data")==0) continue;
if (strcmp(temp, ".sbss")==0) continue;
if (strcmp(temp, ".sdata")==0) continue;
if (strcmp(temp, ".sdata2")==0) continue;
if (strcmp(temp, "address")==0) continue;
if (strcmp(temp, "-----------------------")==0) continue;
if (strcmp(temp, ".sbss2")==0) break;
if (temp[1] == ']') continue;
if (!started) continue;
+2 -1
View File
@@ -93,7 +93,8 @@ private:
public:
typedef void (*functionGetterCallback)(Symbol *f);
SymbolDB() {}
SymbolDB();
~SymbolDB();
Symbol *AddFunction(u32 startAddr);
void AddKnownSymbol(u32 startAddr, u32 size, const char *name, int type = Symbol::SYMBOL_FUNCTION);
+28
View File
@@ -768,6 +768,30 @@
RelativePath=".\Src\Blob.h"
>
</File>
<File
RelativePath=".\Src\CompressedBlob.cpp"
>
</File>
<File
RelativePath=".\Src\CompressedBlob.h"
>
</File>
<File
RelativePath=".\Src\DriveBlob.cpp"
>
</File>
<File
RelativePath=".\Src\DriveBlob.h"
>
</File>
<File
RelativePath=".\Src\FileBlob.cpp"
>
</File>
<File
RelativePath=".\Src\FileBlob.h"
>
</File>
</Filter>
</Filter>
<Filter
@@ -1114,6 +1138,10 @@
>
</File>
</Filter>
<File
RelativePath=".\Src\SConscript"
>
</File>
<File
RelativePath=".\Src\stdafx.cpp"
>
File diff suppressed because it is too large Load Diff
+54 -34
View File
@@ -1,54 +1,73 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef _BLOB_H
#define _BLOB_H
/*
Code not big-endian safe.
// BLOB
BLOB
// Blobs in Dolphin are read only Binary Large OBjects. For example, a typical DVD image.
// Often, you may want to store these things in a highly compressed format, but still
// allow random access. Or you may store them on an odd device, like raw on a DVD.
Blobs are Binary Large OBjects. For example, a typical DVD image.
Often, you may want to store these things in a highly compressed format, but still
allow random access.
Always read your BLOBs using an interface returned by CreateBlobReader(). It will
detect whether the file is a compressed blob, or just a big hunk of data, and
automatically do the right thing.
To create new BLOBs, use CompressFileToBlob.
Right now caching of decompressed chunks doesn't work, so it's a bit slow.
*/
// Always read your BLOBs using an interface returned by CreateBlobReader(). It will
// detect whether the file is a compressed blob, or just a big hunk of data, and
// automatically do the right thing.
#include "Common.h"
namespace DiscIO
{
class IBlobReader
{
public:
public:
virtual ~IBlobReader() {}
virtual ~IBlobReader() {}
virtual u64 GetRawSize() const = 0;
virtual u64 GetDataSize() const = 0;
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
virtual u64 GetRawSize() const = 0;
virtual u64 GetDataSize() const = 0;
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
protected:
IBlobReader() {}
private:
IBlobReader(const IBlobReader& /*other*/) {}
protected:
IBlobReader() {}
};
IBlobReader* CreateBlobReader(const char* filename);
bool IsCompressedBlob(const char* filename);
// Provides caching and split-operation-to-block-operations facilities.
// Used for compressed blob reading and direct drive reading.
class SectorReader : public IBlobReader
{
private:
virtual void GetBlock(u64 block_num, u8 *out) = 0;
enum { CACHE_SIZE = 32 };
int m_blocksize;
u8* cache[CACHE_SIZE];
u64 cache_tags[CACHE_SIZE];
int cache_age[CACHE_SIZE];
protected:
void SetSectorSize(int blocksize);
public:
~SectorReader();
const u8 *GetBlockData(u64 block_num);
bool Read(u64 offset, u64 size, u8* out_ptr);
};
// Factory function - examines the path to choose the right type of IBlobReader, and returns one.
IBlobReader* CreateBlobReader(const char* filename);
typedef void (*CompressCB)(const char* text, float percent, void* arg);
@@ -56,7 +75,8 @@ bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type =
CompressCB callback = 0, void* arg = 0);
bool DecompressBlobToFile(const char* infile, const char* outfile,
CompressCB callback = 0, void* arg = 0);
}
} // namespace
#endif
+339
View File
@@ -0,0 +1,339 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "stdafx.h"
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "Common.h"
#include "CompressedBlob.h"
#include "Hash.h"
#ifdef _WIN32
#include "../../../../Externals/zlib/zlib.h"
#else
// TODO: Include generic zlib.h
#include "../../../../Externals/zlib/zlib.h"
#endif
#ifdef _WIN32
#define fseek _fseeki64
#endif
namespace DiscIO
{
CompressedBlobReader::CompressedBlobReader(const char *filename)
{
file = fopen(filename, "rb");
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
fread(&header, sizeof(CompressedBlobHeader), 1, file);
SetSectorSize(header.block_size);
// cache block pointers and hashes
block_pointers = new u64[header.num_blocks];
fread(block_pointers, sizeof(u64), header.num_blocks, file);
hashes = new u32[header.num_blocks];
fread(hashes, sizeof(u32), header.num_blocks, file);
data_offset = (sizeof(CompressedBlobHeader))
+ (sizeof(u64)) * header.num_blocks // skip block pointers
+ (sizeof(u32)) * header.num_blocks; // skip hashes
// A compressed block is never ever longer than a decompressed block, so just header.block_size should be fine.
// I still add some safety margin.
zlib_buffer_size = header.block_size + 64;
zlib_buffer = new u8[zlib_buffer_size];
memset(zlib_buffer, 0, zlib_buffer_size);
}
CompressedBlobReader* CompressedBlobReader::Create(const char* filename)
{
if (IsCompressedBlob(filename))
return new CompressedBlobReader(filename);
else
return 0;
}
CompressedBlobReader::~CompressedBlobReader()
{
delete [] zlib_buffer;
delete [] block_pointers;
delete [] hashes;
fclose(file);
file = 0;
}
// IMPORTANT: Calling this function invalidates all earlier pointers gotten from this function.
u64 CompressedBlobReader::GetBlockCompressedSize(u64 block_num) const
{
u64 start = block_pointers[block_num];
if (block_num < header.num_blocks - 1)
return block_pointers[block_num + 1] - start;
else if (block_num == header.num_blocks - 1)
return header.compressed_data_size - start;
else
PanicAlert("GetBlockCompressedSize - illegal block number %i", (int)block_num);
return 0;
}
void CompressedBlobReader::GetBlock(u64 block_num, u8 *out_ptr)
{
bool uncompressed = false;
u32 comp_block_size = (u32)GetBlockCompressedSize(block_num);
u64 offset = block_pointers[block_num] + data_offset;
if (offset & (1ULL << 63))
{
if (comp_block_size != header.block_size)
PanicAlert("Uncompressed block with wrong size");
uncompressed = true;
offset &= ~(1ULL << 63);
}
// clear unused part of zlib buffer. maybe this can be deleted when it works fully.
memset(zlib_buffer + comp_block_size, 0, zlib_buffer_size - comp_block_size);
fseek(file, offset, SEEK_SET);
fread(zlib_buffer, 1, comp_block_size, file);
u8* source = zlib_buffer;
u8* dest = out_ptr;
// First, check hash.
u32 block_hash = HashAdler32(source, comp_block_size);
if (block_hash != hashes[block_num])
PanicAlert("Hash of block %i is %08x instead of %08x. Your ISO is corrupt.",
block_num, block_hash, hashes[block_num]);
if (uncompressed)
{
memcpy(dest, source, comp_block_size);
}
else
{
z_stream z;
memset(&z, 0, sizeof(z));
z.next_in = source;
z.avail_in = comp_block_size;
if (z.avail_in > header.block_size)
{
PanicAlert("We have a problem");
}
z.next_out = dest;
z.avail_out = header.block_size;
inflateInit(&z);
int status = inflate(&z, Z_FULL_FLUSH);
u32 uncomp_size = header.block_size - z.avail_out;
if (status != Z_STREAM_END)
{
// this seem to fire wrongly from time to time
// to be sure, don't use compressed isos :P
PanicAlert("Failure reading block %i - out of data and not at end.", block_num);
}
inflateEnd(&z);
if (uncomp_size != header.block_size)
PanicAlert("Wrong block size");
}
}
bool CompressFileToBlob(const char* infile, const char* outfile, u32 sub_type,
int block_size, CompressCB callback, void* arg)
{
if (IsCompressedBlob(infile))
{
PanicAlert("%s is already compressed! Cannot compress it further.", infile);
return false;
}
FILE* inf = fopen(infile, "rb");
if (!inf)
return false;
FILE* f = fopen(outfile, "wb");
if (!f)
return false;
callback("Files opened, ready to compress.", 0, arg);
fseek(inf, 0, SEEK_END);
int insize = ftell(inf);
fseek(inf, 0, SEEK_SET);
CompressedBlobHeader header;
header.magic_cookie = kBlobCookie;
header.sub_type = sub_type;
header.block_size = block_size;
header.data_size = insize;
// round upwards!
header.num_blocks = (u32)((header.data_size + (block_size - 1)) / block_size);
u64* offsets = new u64[header.num_blocks];
u32* hashes = new u32[header.num_blocks];
u8* out_buf = new u8[block_size];
u8* in_buf = new u8[block_size];
// seek past the header (we will write it at the end)
fseek(f, sizeof(CompressedBlobHeader), SEEK_CUR);
// seek past the offset and hash tables (we will write them at the end)
fseek(f, (sizeof(u64) + sizeof(u32)) * header.num_blocks, SEEK_CUR);
// Now we are ready to write compressed data!
u64 position = 0;
int num_compressed = 0;
int num_stored = 0;
for (u32 i = 0; i < header.num_blocks; i++)
{
if (i % (header.num_blocks / 1000) == 0)
{
u64 inpos = ftell(inf);
int ratio = 0;
if (inpos != 0)
ratio = (int)(100 * position / inpos);
char temp[512];
sprintf(temp, "%i of %i blocks. compression ratio %i%%", i, header.num_blocks, ratio);
callback(temp, (float)i / (float)header.num_blocks, arg);
}
offsets[i] = position;
// u64 start = i * header.block_size;
// u64 size = header.block_size;
memset(in_buf, 0, header.block_size);
fread(in_buf, header.block_size, 1, inf);
z_stream z;
memset(&z, 0, sizeof(z));
z.zalloc = Z_NULL;
z.zfree = Z_NULL;
z.opaque = Z_NULL;
z.next_in = in_buf;
z.avail_in = header.block_size;
z.next_out = out_buf;
z.avail_out = block_size;
int retval = deflateInit(&z, 9);
if (retval != Z_OK)
{
PanicAlert("Deflate failed");
goto cleanup;
}
int status = deflate(&z, Z_FINISH);
int comp_size = block_size - z.avail_out;
if ((status != Z_STREAM_END) || (z.avail_out < 10))
{
//PanicAlert("%i %i Store %i", i*block_size, position, comp_size);
// let's store uncompressed
offsets[i] |= 0x8000000000000000ULL;
fwrite(in_buf, block_size, 1, f);
hashes[i] = HashAdler32(in_buf, block_size);
position += block_size;
num_stored++;
}
else
{
// let's store compressed
//PanicAlert("Comp %i to %i", block_size, comp_size);
fwrite(out_buf, comp_size, 1, f);
hashes[i] = HashAdler32(out_buf, comp_size);
position += comp_size;
num_compressed++;
}
deflateEnd(&z);
}
header.compressed_data_size = position;
// Okay, go back and fill in headers
fseek(f, 0, SEEK_SET);
fwrite(&header, sizeof(header), 1, f);
fwrite(offsets, sizeof(u64), header.num_blocks, f);
fwrite(hashes, sizeof(u32), header.num_blocks, f);
cleanup:
// Cleanup
delete[] in_buf;
delete[] out_buf;
delete[] offsets;
fclose(f);
fclose(inf);
callback("Done compressing disc image.", 1.0f, arg);
return true;
}
bool DecompressBlobToFile(const char* infile, const char* outfile, CompressCB callback, void* arg)
{
if (!IsCompressedBlob(infile))
{
PanicAlert("File not compressed");
return false;
}
CompressedBlobReader* reader = CompressedBlobReader::Create(infile);
if (!reader) return false;
FILE* f = fopen(outfile, "wb");
const CompressedBlobHeader &header = reader->GetHeader();
u8* buffer = new u8[header.block_size];
for (u64 i = 0; i < header.num_blocks; i++)
{
if (i % (header.num_blocks / 100) == 0)
{
callback("Unpacking", (float)i / (float)header.num_blocks, arg);
}
reader->Read(i * header.block_size, header.block_size, buffer);
fwrite(buffer, header.block_size, 1, f);
}
delete reader;
delete[] buffer;
#ifdef _WIN32
// ector: _chsize sucks, not 64-bit safe
// F|RES: changed to _chsize_s. i think it is 64-bit safe
_chsize_s(_fileno(f), (long)header.data_size);
#else
ftruncate(fileno(f), header.data_size);
#endif
fclose(f);
return true;
}
bool IsCompressedBlob(const char* filename)
{
FILE* f = fopen(filename, "rb");
if (!f)
return false;
CompressedBlobHeader header;
fread(&header, sizeof(header), 1, f);
fclose(f);
return header.magic_cookie == kBlobCookie;
}
} // namespace
+83
View File
@@ -0,0 +1,83 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
// WARNING Code not big-endian safe.
// To create new compressed BLOBs, use CompressFileToBlob.
// File format
// * Header
// * [Block Pointers interleaved with block hashes (hash of decompressed data)]
// * [Data]
#ifndef COMPRESSED_BLOB_H_
#define COMPRESSED_BLOB_H_
#include <stdio.h>
#include "Blob.h"
namespace DiscIO
{
bool IsCompressedBlob(const char* filename);
const u32 kBlobCookie = 0xB10BC001;
// A blob file structure:
// BlobHeader
// u64 offsetsToBlocks[n], top bit specifies whether the block is compressed, or not.
// compressed data
// Blocks that won't compress to less than 97% of the original size are stored as-is.
struct CompressedBlobHeader // 32 bytes
{
u32 magic_cookie; //0xB10BB10B
u32 sub_type; // gc image, whatever
u64 compressed_data_size;
u64 data_size;
u32 block_size;
u32 num_blocks;
};
class CompressedBlobReader : public SectorReader
{
private:
CompressedBlobHeader header;
u64 *block_pointers;
u32 *hashes;
int data_offset;
FILE *file;
u64 file_size;
u8 *zlib_buffer;
int zlib_buffer_size;
CompressedBlobReader(const char *filename);
public:
static CompressedBlobReader* Create(const char *filename);
~CompressedBlobReader();
const CompressedBlobHeader &GetHeader() const { return header; }
u64 GetDataSize() const { return header.data_size; }
u64 GetRawSize() const { return file_size; }
u64 GetBlockCompressedSize(u64 block_num) const;
void GetBlock(u64 block_num, u8 *out_ptr);
};
} // namespace
#endif // COMPRESSED_BLOB_H_
+25
View File
@@ -0,0 +1,25 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "stdafx.h"
#include "DriveBlob.h"
namespace DiscIO
{
} // namespace

Some files were not shown because too many files have changed in this diff Show More