2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Always read your BLOBs using an interface returned by CreateBlobReader(). It will
|
2009-02-22 16:48:54 +00:00
|
|
|
// detect whether the file is a compressed blob, or just a big hunk of data, or a drive, and
|
2008-12-08 04:46:09 +00:00
|
|
|
// automatically do the right thing.
|
|
|
|
|
2015-12-08 00:53:42 +00:00
|
|
|
#include <array>
|
2015-12-07 04:15:51 +00:00
|
|
|
#include <memory>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2015-10-03 11:42:26 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
|
|
|
|
2015-09-27 12:01:12 +00:00
|
|
|
// Increment CACHE_REVISION if the enum below is modified (ISOFile.cpp & GameFile.cpp)
|
|
|
|
enum class BlobType
|
|
|
|
{
|
|
|
|
PLAIN,
|
|
|
|
DRIVE,
|
|
|
|
DIRECTORY,
|
|
|
|
GCZ,
|
|
|
|
CISO,
|
|
|
|
WBFS
|
|
|
|
};
|
|
|
|
|
2008-12-08 04:46:09 +00:00
|
|
|
class IBlobReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~IBlobReader() {}
|
|
|
|
|
2015-09-27 12:01:12 +00:00
|
|
|
virtual BlobType GetBlobType() const = 0;
|
2015-09-26 13:24:29 +00:00
|
|
|
virtual u64 GetRawSize() const = 0;
|
2008-12-08 04:46:09 +00:00
|
|
|
virtual u64 GetDataSize() const = 0;
|
2010-01-11 23:27:02 +00:00
|
|
|
// NOT thread-safe - can't call this from multiple threads.
|
2008-12-08 04:46:09 +00:00
|
|
|
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
IBlobReader() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Provides caching and split-operation-to-block-operations facilities.
|
|
|
|
// Used for compressed blob reading and direct drive reading.
|
2009-02-22 16:48:54 +00:00
|
|
|
// Currently only uses a single entry cache.
|
|
|
|
// Multi-block reads are not cached.
|
2008-12-08 04:46:09 +00:00
|
|
|
class SectorReader : public IBlobReader
|
|
|
|
{
|
|
|
|
public:
|
2009-02-22 16:48:54 +00:00
|
|
|
virtual ~SectorReader();
|
|
|
|
|
2015-07-30 10:47:02 +00:00
|
|
|
bool Read(u64 offset, u64 size, u8 *out_ptr) override;
|
2009-02-22 07:52:02 +00:00
|
|
|
friend class DriveReader;
|
2014-09-01 19:48:02 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetSectorSize(int blocksize);
|
|
|
|
virtual void GetBlock(u64 block_num, u8 *out) = 0;
|
|
|
|
// This one is uncached. The default implementation is to simply call GetBlockData multiple times and memcpy.
|
|
|
|
virtual bool ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8 *out_ptr);
|
|
|
|
|
|
|
|
private:
|
2015-12-08 00:53:42 +00:00
|
|
|
// A reference returned by GetBlockData is invalidated as soon as GetBlockData, Read, or ReadMultipleAlignedBlocks is called again.
|
|
|
|
const std::vector<u8>& GetBlockData(u64 block_num);
|
|
|
|
|
2014-09-01 19:48:02 +00:00
|
|
|
enum { CACHE_SIZE = 32 };
|
|
|
|
int m_blocksize;
|
2015-12-08 00:53:42 +00:00
|
|
|
std::array<std::vector<u8>, CACHE_SIZE> m_cache;
|
|
|
|
std::array<u64, CACHE_SIZE> m_cache_tags;
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|
|
|
|
|
2015-10-03 11:42:26 +00:00
|
|
|
class CBlobBigEndianReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CBlobBigEndianReader(IBlobReader& reader) : m_reader(reader) {}
|
|
|
|
|
2015-10-03 19:25:43 +00:00
|
|
|
template <typename T>
|
|
|
|
bool ReadSwapped(u64 offset, T* buffer) const
|
2015-10-03 11:42:26 +00:00
|
|
|
{
|
2015-10-03 19:25:43 +00:00
|
|
|
T temp;
|
|
|
|
if (!m_reader.Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp)))
|
|
|
|
return false;
|
|
|
|
*buffer = Common::FromBigEndian(temp);
|
|
|
|
return true;
|
2015-10-03 11:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IBlobReader& m_reader;
|
|
|
|
};
|
|
|
|
|
2008-12-08 04:46:09 +00:00
|
|
|
// Factory function - examines the path to choose the right type of IBlobReader, and returns one.
|
2015-12-07 04:15:51 +00:00
|
|
|
std::unique_ptr<IBlobReader> CreateBlobReader(const std::string& filename);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-11-27 15:53:28 +00:00
|
|
|
typedef bool (*CompressCB)(const std::string& text, float percent, void* arg);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u32 sub_type = 0, int sector_size = 16384,
|
2014-03-09 20:14:26 +00:00
|
|
|
CompressCB callback = nullptr, void *arg = nullptr);
|
2014-03-12 19:33:41 +00:00
|
|
|
bool DecompressBlobToFile(const std::string& infile, const std::string& outfile,
|
2014-03-09 20:14:26 +00:00
|
|
|
CompressCB callback = nullptr, void *arg = nullptr);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
} // namespace
|