Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/VFS/SevenZipFileReader.h
10523 views
1
#pragma once
2
3
#include <cstdint>
4
#include <mutex>
5
#include <set>
6
#include <string>
7
#include <string_view>
8
#include <vector>
9
10
#include "Common/CommonWindows.h"
11
12
#include "ext/lzma-sdk/7z.h"
13
#include "ext/lzma-sdk/7zFile.h"
14
15
#include "Common/File/VFS/VFS.h"
16
#include "Common/File/FileUtil.h"
17
#include "Common/File/Path.h"
18
19
class SevenZipFileReader : public VFSBackend {
20
public:
21
static SevenZipFileReader *Create(const Path &archivePath, std::string_view inArchivePath, bool logErrors = true);
22
~SevenZipFileReader();
23
24
bool IsValid() const { return valid_; }
25
26
// Use delete[] on the returned value.
27
uint8_t *ReadFile(std::string_view path, size_t *size) override;
28
29
VFSFileReference *GetFile(std::string_view path) override;
30
bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) override;
31
void ReleaseFile(VFSFileReference *vfsReference) override;
32
33
VFSOpenFile *OpenFileForRead(VFSFileReference *vfsReference, size_t *size) override;
34
void Rewind(VFSOpenFile *vfsOpenFile) override;
35
size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) override;
36
void CloseFile(VFSOpenFile *vfsOpenFile) override;
37
38
bool GetFileListing(std::string_view path, std::vector<File::FileInfo> *listing, const char *filter) override;
39
bool GetFileInfo(std::string_view path, File::FileInfo *info) override;
40
std::string toString() const override {
41
std::string retval = archivePath_.ToVisualString();
42
if (!inArchivePath_.empty()) {
43
retval += ": ";
44
retval += inArchivePath_;
45
}
46
return retval;
47
}
48
49
private:
50
struct SevenZipEntry {
51
std::string path;
52
bool isDirectory = false;
53
uint64_t size = 0;
54
};
55
56
SevenZipFileReader(const Path &archivePath, const std::string &inArchivePath);
57
bool OpenArchive(bool logErrors);
58
void CloseArchive();
59
bool BuildEntryCache();
60
bool FindEntry(std::string_view path, UInt32 *index, bool *isDirectory = nullptr) const;
61
std::string ResolvePath(std::string_view path) const;
62
std::string ReadEntryPath(UInt32 index) const;
63
uint8_t *ExtractFile(UInt32 fileIndex, size_t *size);
64
65
static void *Alloc(ISzAllocPtr p, size_t size);
66
static void Free(ISzAllocPtr p, void *address);
67
68
Path archivePath_;
69
std::string inArchivePath_;
70
71
mutable std::mutex lock_;
72
CFileInStream archiveStream_;
73
CLookToRead2 lookStream_;
74
ISzAlloc allocImp_;
75
ISzAlloc allocTempImp_;
76
CSzArEx db_;
77
Byte *lookStreamBuf_ = nullptr;
78
Byte *cachedBlock_ = nullptr;
79
size_t cachedBlockSize_ = 0;
80
UInt32 blockIndex_ = 0xFFFFFFFF;
81
bool valid_ = false;
82
83
std::vector<SevenZipEntry> entries_;
84
};
85
86