/src/ogre/OgreMain/src/OgreZip.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | |
30 | | #if OGRE_NO_ZIP_ARCHIVE == 0 |
31 | | #include <zip.h> |
32 | | |
33 | | namespace Ogre { |
34 | | namespace { |
35 | | class ZipArchive : public Archive |
36 | | { |
37 | | protected: |
38 | | /// Handle to root zip file |
39 | | zip_t* mZipFile; |
40 | | MemoryDataStreamPtr mBuffer; |
41 | | /// File list (since zziplib seems to only allow scanning of dir tree once) |
42 | | FileInfoList mFileList; |
43 | | OGRE_AUTO_MUTEX; |
44 | | public: |
45 | | ZipArchive(const String& name, const String& archType, const uint8* externBuf = 0, size_t externBufSz = 0); |
46 | | ~ZipArchive(); |
47 | | /// @copydoc Archive::isCaseSensitive |
48 | 0 | bool isCaseSensitive(void) const override { return OGRE_RESOURCEMANAGER_STRICT != 0; } |
49 | | |
50 | | /// @copydoc Archive::load |
51 | | void load() override; |
52 | | /// @copydoc Archive::unload |
53 | | void unload() final override; |
54 | | |
55 | | /// @copydoc Archive::open |
56 | | DataStreamPtr open(const String& filename, bool readOnly = true) const override; |
57 | | |
58 | | /// @copydoc Archive::create |
59 | | DataStreamPtr create(const String& filename) override; |
60 | | |
61 | | /// @copydoc Archive::remove |
62 | | void remove(const String& filename) override; |
63 | | |
64 | | /// @copydoc Archive::list |
65 | | StringVectorPtr list(bool recursive = true, bool dirs = false) const override; |
66 | | |
67 | | /// @copydoc Archive::listFileInfo |
68 | | FileInfoListPtr listFileInfo(bool recursive = true, bool dirs = false) const override; |
69 | | |
70 | | /// @copydoc Archive::find |
71 | | StringVectorPtr find(const String& pattern, bool recursive = true, |
72 | | bool dirs = false) const override; |
73 | | |
74 | | /// @copydoc Archive::findFileInfo |
75 | | FileInfoListPtr findFileInfo(const String& pattern, bool recursive = true, |
76 | | bool dirs = false) const override; |
77 | | |
78 | | /// @copydoc Archive::exists |
79 | | bool exists(const String& filename) const override; |
80 | | |
81 | | /// @copydoc Archive::getModifiedTime |
82 | | time_t getModifiedTime(const String& filename) const override; |
83 | | }; |
84 | | } |
85 | | //----------------------------------------------------------------------- |
86 | | ZipArchive::ZipArchive(const String& name, const String& archType, const uint8* externBuf, size_t externBufSz) |
87 | 0 | : Archive(name, archType), mZipFile(0) |
88 | 0 | { |
89 | 0 | if(externBuf) |
90 | 0 | mBuffer.reset(new MemoryDataStream(const_cast<uint8*>(externBuf), externBufSz)); |
91 | 0 | } |
92 | | //----------------------------------------------------------------------- |
93 | | ZipArchive::~ZipArchive() |
94 | 0 | { |
95 | 0 | unload(); |
96 | 0 | } |
97 | | //----------------------------------------------------------------------- |
98 | | void ZipArchive::load() |
99 | 0 | { |
100 | 0 | OGRE_LOCK_AUTO_MUTEX; |
101 | 0 | if (!mZipFile) |
102 | 0 | { |
103 | 0 | if(!mBuffer) |
104 | 0 | mBuffer.reset(new MemoryDataStream(_openFileStream(mName, std::ios::binary))); |
105 | |
|
106 | 0 | mZipFile = zip_stream_open((const char*)mBuffer->getPtr(), mBuffer->size(), 0, 'r'); |
107 | | |
108 | | // Cache names |
109 | 0 | int n = zip_entries_total(mZipFile); |
110 | 0 | for (int i = 0; i < n; ++i) { |
111 | 0 | FileInfo info; |
112 | 0 | info.archive = this; |
113 | |
|
114 | 0 | zip_entry_openbyindex(mZipFile, i); |
115 | |
|
116 | 0 | info.filename = zip_entry_name(mZipFile); |
117 | | // Get basename / path |
118 | 0 | StringUtil::splitFilename(info.filename, info.basename, info.path); |
119 | | |
120 | | // Get sizes |
121 | 0 | info.uncompressedSize = zip_entry_size(mZipFile); |
122 | 0 | info.compressedSize = zip_entry_comp_size(mZipFile); |
123 | |
|
124 | 0 | if (zip_entry_isdir(mZipFile)) |
125 | 0 | { |
126 | 0 | info.filename = info.filename.substr(0, info.filename.length() - 1); |
127 | 0 | StringUtil::splitFilename(info.filename, info.basename, info.path); |
128 | | // Set compressed size to -1 for folders; anyway nobody will check |
129 | | // the compressed size of a folder, and if he does, its useless anyway |
130 | 0 | info.compressedSize = size_t(-1); |
131 | 0 | } |
132 | | #if !OGRE_RESOURCEMANAGER_STRICT |
133 | | else |
134 | | { |
135 | | info.filename = info.basename; |
136 | | } |
137 | | #endif |
138 | 0 | zip_entry_close(mZipFile); |
139 | 0 | mFileList.push_back(info); |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | | //----------------------------------------------------------------------- |
144 | | void ZipArchive::unload() |
145 | 0 | { |
146 | 0 | OGRE_LOCK_AUTO_MUTEX; |
147 | 0 | if (mZipFile) |
148 | 0 | { |
149 | 0 | zip_close(mZipFile); |
150 | 0 | mZipFile = 0; |
151 | 0 | mFileList.clear(); |
152 | 0 | mBuffer.reset(); |
153 | 0 | } |
154 | | |
155 | 0 | } |
156 | | //----------------------------------------------------------------------- |
157 | | DataStreamPtr ZipArchive::open(const String& filename, bool readOnly) const |
158 | 0 | { |
159 | | // zip is not threadsafe |
160 | 0 | OGRE_LOCK_AUTO_MUTEX; |
161 | 0 | String lookUpFileName = filename; |
162 | |
|
163 | 0 | bool open = zip_entry_open(mZipFile, lookUpFileName.c_str(), OGRE_RESOURCEMANAGER_STRICT) == 0; |
164 | | #if !OGRE_RESOURCEMANAGER_STRICT |
165 | | if (!open) // Try if we find the file |
166 | | { |
167 | | String basename, path; |
168 | | StringUtil::splitFilename(lookUpFileName, basename, path); |
169 | | const FileInfoListPtr fileNfo = findFileInfo(basename, true); |
170 | | if (fileNfo->size() == 1) // If there are more files with the same do not open anyone |
171 | | { |
172 | | Ogre::FileInfo info = fileNfo->at(0); |
173 | | lookUpFileName = info.path + info.basename; |
174 | | open = zip_entry_open(mZipFile, lookUpFileName.c_str(), OGRE_RESOURCEMANAGER_STRICT) == 0; |
175 | | } |
176 | | } |
177 | | #endif |
178 | |
|
179 | 0 | if (!open) |
180 | 0 | { |
181 | 0 | OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "could not open "+lookUpFileName); |
182 | 0 | } |
183 | | |
184 | | // Construct & return stream |
185 | 0 | auto ret = std::make_shared<MemoryDataStream>(lookUpFileName, zip_entry_size(mZipFile), true, true); |
186 | |
|
187 | 0 | if(zip_entry_noallocread(mZipFile, ret->getPtr(), ret->size()) < 0) |
188 | 0 | OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "could not read "+lookUpFileName); |
189 | 0 | zip_entry_close(mZipFile); |
190 | |
|
191 | 0 | return ret; |
192 | 0 | } |
193 | | //--------------------------------------------------------------------- |
194 | | DataStreamPtr ZipArchive::create(const String& filename) |
195 | 0 | { |
196 | 0 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Modification of zipped archives is not implemented"); |
197 | 0 | } |
198 | | //--------------------------------------------------------------------- |
199 | | void ZipArchive::remove(const String& filename) |
200 | 0 | { |
201 | 0 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Modification of zipped archives is not implemented"); |
202 | 0 | } |
203 | | //----------------------------------------------------------------------- |
204 | | StringVectorPtr ZipArchive::list(bool recursive, bool dirs) const |
205 | 0 | { |
206 | 0 | OGRE_LOCK_AUTO_MUTEX; |
207 | 0 | auto ret = std::make_shared<StringVector>(); |
208 | |
|
209 | 0 | for (auto& f : mFileList) |
210 | 0 | if ((dirs == (f.compressedSize == size_t (-1))) && |
211 | 0 | (recursive || f.path.empty())) |
212 | 0 | ret->push_back(f.filename); |
213 | |
|
214 | 0 | return ret; |
215 | 0 | } |
216 | | //----------------------------------------------------------------------- |
217 | | FileInfoListPtr ZipArchive::listFileInfo(bool recursive, bool dirs) const |
218 | 0 | { |
219 | 0 | OGRE_LOCK_AUTO_MUTEX; |
220 | 0 | auto ret = std::make_shared<FileInfoList>(); |
221 | 0 | for (auto& f : mFileList) |
222 | 0 | if ((dirs == (f.compressedSize == size_t (-1))) && |
223 | 0 | (recursive || f.path.empty())) |
224 | 0 | ret->push_back(f); |
225 | |
|
226 | 0 | return ret; |
227 | 0 | } |
228 | | //----------------------------------------------------------------------- |
229 | | StringVectorPtr ZipArchive::find(const String& pattern, bool recursive, bool dirs) const |
230 | 0 | { |
231 | 0 | OGRE_LOCK_AUTO_MUTEX; |
232 | 0 | auto ret = std::make_shared<StringVector>(); |
233 | | // If pattern contains a directory name, do a full match |
234 | 0 | bool full_match = (pattern.find ('/') != String::npos) || |
235 | 0 | (pattern.find ('\\') != String::npos); |
236 | 0 | bool wildCard = pattern.find('*') != String::npos; |
237 | | |
238 | 0 | for (auto& f : mFileList) |
239 | 0 | if ((dirs == (f.compressedSize == size_t (-1))) && |
240 | 0 | (recursive || full_match || wildCard)) |
241 | | // Check basename matches pattern (zip is case insensitive) |
242 | 0 | if (StringUtil::match(full_match ? f.filename : f.basename, pattern, false)) |
243 | 0 | ret->push_back(f.filename); |
244 | |
|
245 | 0 | return ret; |
246 | 0 | } |
247 | | //----------------------------------------------------------------------- |
248 | | FileInfoListPtr ZipArchive::findFileInfo(const String& pattern, |
249 | | bool recursive, bool dirs) const |
250 | 0 | { |
251 | 0 | OGRE_LOCK_AUTO_MUTEX; |
252 | 0 | auto ret = std::make_shared<FileInfoList>(); |
253 | | // If pattern contains a directory name, do a full match |
254 | 0 | bool full_match = (pattern.find ('/') != String::npos) || |
255 | 0 | (pattern.find ('\\') != String::npos); |
256 | 0 | bool wildCard = pattern.find('*') != String::npos; |
257 | |
|
258 | 0 | for (auto& f : mFileList) |
259 | 0 | if ((dirs == (f.compressedSize == size_t (-1))) && |
260 | 0 | (recursive || full_match || wildCard)) |
261 | | // Check name matches pattern (zip is case insensitive) |
262 | 0 | if (StringUtil::match(full_match ? f.filename : f.basename, pattern, false)) |
263 | 0 | ret->push_back(f); |
264 | |
|
265 | 0 | return ret; |
266 | 0 | } |
267 | | //----------------------------------------------------------------------- |
268 | | bool ZipArchive::exists(const String& filename) const |
269 | 0 | { |
270 | 0 | OGRE_LOCK_AUTO_MUTEX; |
271 | 0 | String cleanName = filename; |
272 | | #if !OGRE_RESOURCEMANAGER_STRICT |
273 | | if(filename.rfind('/') != String::npos) |
274 | | { |
275 | | StringVector tokens = StringUtil::split(filename, "/"); |
276 | | cleanName = tokens[tokens.size() - 1]; |
277 | | } |
278 | | #endif |
279 | |
|
280 | 0 | return std::find_if(mFileList.begin(), mFileList.end(), [&cleanName](const Ogre::FileInfo& fi) { |
281 | 0 | return fi.filename == cleanName; |
282 | 0 | }) != mFileList.end(); |
283 | 0 | } |
284 | | //--------------------------------------------------------------------- |
285 | | time_t ZipArchive::getModifiedTime(const String& filename) const |
286 | 0 | { |
287 | | // Zziplib doesn't yet support getting the modification time of individual files |
288 | | // so just check the mod time of the zip itself |
289 | 0 | struct stat tagStat; |
290 | 0 | bool ret = (stat(mName.c_str(), &tagStat) == 0); |
291 | |
|
292 | 0 | if (ret) |
293 | 0 | { |
294 | 0 | return tagStat.st_mtime; |
295 | 0 | } |
296 | 0 | else |
297 | 0 | { |
298 | 0 | return 0; |
299 | 0 | } |
300 | |
|
301 | 0 | } |
302 | | //----------------------------------------------------------------------- |
303 | | // ZipArchiveFactory |
304 | | //----------------------------------------------------------------------- |
305 | | Archive *ZipArchiveFactory::createInstance( const String& name, bool readOnly ) |
306 | 0 | { |
307 | 0 | if(!readOnly) |
308 | 0 | return NULL; |
309 | | |
310 | 0 | return OGRE_NEW ZipArchive(name, getType()); |
311 | 0 | } |
312 | | //----------------------------------------------------------------------- |
313 | | const String& ZipArchiveFactory::getType(void) const |
314 | 0 | { |
315 | 0 | static String name = "Zip"; |
316 | 0 | return name; |
317 | 0 | } |
318 | | //----------------------------------------------------------------------- |
319 | | //----------------------------------------------------------------------- |
320 | | // EmbeddedZipArchiveFactory |
321 | | //----------------------------------------------------------------------- |
322 | | //----------------------------------------------------------------------- |
323 | | /// a struct to hold embedded file data |
324 | | struct EmbeddedFileData |
325 | | { |
326 | | const uint8 * fileData; |
327 | | size_t fileSize; |
328 | | size_t curPos; |
329 | | bool isFileOpened; |
330 | | EmbeddedZipArchiveFactory::DecryptEmbeddedZipFileFunc decryptFunc; |
331 | | }; |
332 | | //----------------------------------------------------------------------- |
333 | | /// A type for a map between the file names to file index |
334 | | typedef std::map<String, EmbeddedFileData> EmbbedFileDataList; |
335 | | |
336 | | namespace { |
337 | | /// A static list to store the embedded files data |
338 | | EmbbedFileDataList *gEmbeddedFileDataList; |
339 | | } // namespace { |
340 | | //----------------------------------------------------------------------- |
341 | 0 | EmbeddedZipArchiveFactory::EmbeddedZipArchiveFactory() {} |
342 | 0 | EmbeddedZipArchiveFactory::~EmbeddedZipArchiveFactory() {} |
343 | | //----------------------------------------------------------------------- |
344 | | Archive *EmbeddedZipArchiveFactory::createInstance( const String& name, bool readOnly ) |
345 | 0 | { |
346 | 0 | auto it = gEmbeddedFileDataList->find(name); |
347 | 0 | if(it == gEmbeddedFileDataList->end()) |
348 | 0 | return NULL; |
349 | | |
350 | | // TODO: decryptFunc |
351 | | |
352 | 0 | return new ZipArchive(name, getType(), it->second.fileData, it->second.fileSize); |
353 | 0 | } |
354 | | void EmbeddedZipArchiveFactory::destroyInstance(Archive* ptr) |
355 | 0 | { |
356 | 0 | removeEmbbeddedFile(ptr->getName()); |
357 | 0 | ZipArchiveFactory::destroyInstance(ptr); |
358 | 0 | } |
359 | | //----------------------------------------------------------------------- |
360 | | const String& EmbeddedZipArchiveFactory::getType(void) const |
361 | 0 | { |
362 | 0 | static String name = "EmbeddedZip"; |
363 | 0 | return name; |
364 | 0 | } |
365 | | //----------------------------------------------------------------------- |
366 | | void EmbeddedZipArchiveFactory::addEmbbeddedFile(const String& name, const uint8 * fileData, |
367 | | size_t fileSize, DecryptEmbeddedZipFileFunc decryptFunc) |
368 | 0 | { |
369 | 0 | static bool needToInit = true; |
370 | 0 | if(needToInit) |
371 | 0 | { |
372 | 0 | needToInit = false; |
373 | | |
374 | | // we can't be sure when global variables get initialized |
375 | | // meaning it is possible our list has not been init when this |
376 | | // function is being called. The solution is to use local |
377 | | // static members in this function an init the pointers for the |
378 | | // global here. We know for use that the local static variables |
379 | | // are create in this stage. |
380 | 0 | static EmbbedFileDataList sEmbbedFileDataList; |
381 | 0 | gEmbeddedFileDataList = &sEmbbedFileDataList; |
382 | 0 | } |
383 | |
|
384 | 0 | EmbeddedFileData newEmbeddedFileData; |
385 | 0 | newEmbeddedFileData.curPos = 0; |
386 | 0 | newEmbeddedFileData.isFileOpened = false; |
387 | 0 | newEmbeddedFileData.fileData = fileData; |
388 | 0 | newEmbeddedFileData.fileSize = fileSize; |
389 | 0 | newEmbeddedFileData.decryptFunc = decryptFunc; |
390 | 0 | gEmbeddedFileDataList->emplace(name, newEmbeddedFileData); |
391 | 0 | } |
392 | | //----------------------------------------------------------------------- |
393 | | void EmbeddedZipArchiveFactory::removeEmbbeddedFile( const String& name ) |
394 | 0 | { |
395 | 0 | gEmbeddedFileDataList->erase(name); |
396 | 0 | } |
397 | | } |
398 | | |
399 | | #endif |