/
ZipArchive.cpp
259 lines (202 loc) · 6.35 KB
/
ZipArchive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "ZipArchive.h"
#include <stdexcept>
#include "itextstream.h"
#include "iarchive.h"
#include "gamelib.h"
#include <zlib.h>
#include "os/fs.h"
#include "os/path.h"
#include "ZipStreamUtils.h"
#include "DeflatedArchiveFile.h"
#include "DeflatedArchiveTextFile.h"
#include "StoreArchiveFile.h"
#include "StoredArchiveTextFile.h"
namespace archive
{
// Thrown by the zip reader methods below
class ZipFailureException :
public std::runtime_error
{
public:
ZipFailureException(const char* msg) :
std::runtime_error(msg)
{}
};
ZipArchive::ZipArchive(const std::string& fullPath) :
_fullPath(fullPath),
_containingFolder(os::standardPathWithSlash(fs::path(_fullPath).remove_filename())),
_istream(_fullPath)
{
if (_istream.failed())
{
rError() << "Cannot open Zip file stream: " << _fullPath << std::endl;
return;
}
try
{
// Try loading the zip file, this will throw exceptoions on any problem
loadZipFile();
}
catch (ZipFailureException& ex)
{
rError() << "Cannot read Zip file " << _fullPath << ": " << ex.what() << std::endl;
}
}
ZipArchive::~ZipArchive()
{
_filesystem.clear();
}
ArchiveFilePtr ZipArchive::openFile(const std::string& name)
{
ZipFileSystem::iterator i = _filesystem.find(name);
if (i != _filesystem.end() && !i->second.isDirectory())
{
const std::shared_ptr<ZipRecord>& file = i->second.getRecord();
FileInputStream::size_type position = 0;
{
// Guard against concurrent access
std::lock_guard<std::mutex> lock(_streamLock);
_istream.seek(file->position);
ZipFileHeader header;
stream::readZipFileHeader(_istream, header);
position = _istream.tell();
if (header.magic != ZIP_MAGIC_FILE_HEADER)
{
rError() << "Error reading zip file " << _fullPath << std::endl;
return ArchiveFilePtr();
}
}
switch (file->mode)
{
case ZipRecord::eStored:
return std::make_shared<StoredArchiveFile>(name, _fullPath, position, file->stream_size, file->file_size);
case ZipRecord::eDeflated:
return std::make_shared<DeflatedArchiveFile>(name, _fullPath, position, file->stream_size, file->file_size);
}
}
return ArchiveFilePtr();
}
ArchiveTextFilePtr ZipArchive::openTextFile(const std::string& name)
{
ZipFileSystem::iterator i = _filesystem.find(name);
if (i != _filesystem.end() && !i->second.isDirectory())
{
const std::shared_ptr<ZipRecord>& file = i->second.getRecord();
{
// Guard against concurrent access
std::lock_guard<std::mutex> lock(_streamLock);
_istream.seek(file->position);
ZipFileHeader header;
stream::readZipFileHeader(_istream, header);
if (header.magic != ZIP_MAGIC_FILE_HEADER)
{
rError() << "Error reading zip file " << _fullPath << std::endl;
return ArchiveTextFilePtr();
}
}
std::string modDir = game::current::getModPath(_containingFolder);
switch (file->mode)
{
case ZipRecord::eStored:
return std::make_shared<StoredArchiveTextFile>(name, _fullPath, modDir, _istream.tell(), file->stream_size);
case ZipRecord::eDeflated:
return std::make_shared<DeflatedArchiveTextFile>(name, _fullPath, modDir, _istream.tell(), file->stream_size);
}
}
return ArchiveTextFilePtr();
}
bool ZipArchive::containsFile(const std::string& name)
{
ZipFileSystem::iterator i = _filesystem.find(name);
return i != _filesystem.end() && !i->second.isDirectory();
}
void ZipArchive::forEachFile(VisitorFunc visitor, const std::string& root)
{
_filesystem.traverse(visitor, root);
}
void ZipArchive::readZipRecord()
{
ZipMagic magic;
stream::readZipMagic(_istream, magic);
if (magic != ZIP_MAGIC_ROOT_DIR_ENTRY)
{
throw ZipFailureException("Invalid Zip directory entry magic");
}
ZipVersion version_encoder;
stream::readZipVersion(_istream, version_encoder);
ZipVersion version_extract;
stream::readZipVersion(_istream, version_extract);
//unsigned short flags =
stream::readLittleEndian<int16_t>(_istream);
uint16_t compression_mode = stream::readLittleEndian<uint16_t>(_istream);
if (compression_mode != Z_DEFLATED && compression_mode != 0)
{
throw ZipFailureException("Unsupported compression mode");
}
ZipDosTime dostime;
stream::readZipDosTime(_istream, dostime);
//unsigned int crc32 =
stream::readLittleEndian<uint32_t>(_istream);
uint32_t compressed_size = stream::readLittleEndian<uint32_t>(_istream);
uint32_t uncompressed_size = stream::readLittleEndian<uint32_t>(_istream);
uint16_t namelength = stream::readLittleEndian<uint16_t>(_istream);
uint16_t extras = stream::readLittleEndian<uint16_t>(_istream);
uint16_t comment = stream::readLittleEndian<uint16_t>(_istream);
//unsigned short diskstart =
stream::readLittleEndian<uint16_t>(_istream);
//unsigned short filetype =
stream::readLittleEndian<uint16_t>(_istream);
//unsigned int filemode =
stream::readLittleEndian<uint32_t>(_istream);
uint32_t position = stream::readLittleEndian<uint32_t>(_istream);
// greebo: Read the filename directly into a newly constructed std::string.
// I'm not entirely happy about this brute-force casting, but I wanted to
// avoid reading the filename into a temporary char[] array
// only to let its contents end up being copied by the std::string anyway.
// Alternative: use a static boost::shared_array here, resized to fit?
std::string path(namelength, '\0');
_istream.read(
reinterpret_cast<FileInputStream::byte_type*>(const_cast<char*>(path.data())),
namelength);
_istream.seek(extras + comment, FileInputStream::cur);
if (os::isDirectory(path))
{
_filesystem[path].getRecord().reset();
}
else
{
ZipFileSystem::entry_type& entry = _filesystem[path];
if (!entry.isDirectory())
{
rWarning() << "Zip archive " << _fullPath << " contains duplicated file: " << path << std::endl;
}
else
{
entry.getRecord().reset(new ZipRecord(position,
compressed_size,
uncompressed_size,
(compression_mode == Z_DEFLATED) ? ZipRecord::eDeflated : ZipRecord::eStored));
}
}
}
void ZipArchive::loadZipFile()
{
SeekableStream::position_type pos = findZipDiskTrailerPosition(_istream);
if (pos == 0)
{
throw ZipFailureException("Unable to locate Zip disk trailer");
}
_istream.seek(pos);
ZipDiskTrailer trailer;
stream::readZipDiskTrailer(_istream, trailer);
if (trailer.magic != ZIP_MAGIC_DISK_TRAILER)
{
throw ZipFailureException("Invalid Zip Magic, maybe this is not a zip file?");
}
_istream.seek(trailer.rootseek);
for (unsigned short i = 0; i < trailer.entries; ++i)
{
readZipRecord();
}
}
}