File: zipsource.cpp

package info (click to toggle)
fife 0.4.2-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,204 kB
  • sloc: cpp: 42,642; xml: 18,881; python: 13,521; makefile: 23
file content (226 lines) | stat: -rw-r--r-- 7,267 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2005-2019 by the FIFE team                              *
 *   http://www.fifengine.net                                              *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library 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     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes
#include <algorithm>
#include <list>
#include <memory>

// 3rd party library includes
#include "zlib.h"

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "vfs/raw/rawdata.h"
#include "vfs/fife_boost_filesystem.h"
#include "util/base/exception.h"
#include "util/log/logger.h"

#include "zipsource.h"
#include "zipfilesource.h"
#include "zipnode.h"

namespace FIFE {

	static const uint32_t LF_HEADER = 0x04034b50;
	static const uint32_t DE_HEADER = 0x08064b50;
	static const uint32_t CF_HEADER = 0x02014b50;

	static Logger _log(LM_LOADERS);

	ZipSource::ZipSource(VFS* vfs, const std::string& zip_file) : VFSSource(vfs), m_zipfile(vfs->open(zip_file)) {
		readIndex();
	}

	ZipSource::~ZipSource() {
		delete m_zipfile;
	}

	bool ZipSource::fileExists(const std::string& file) const {
		bfs::path path(file);
		return (m_zipTree.getNode(path.string()) != 0);
	}

	RawData* ZipSource::open(const std::string& path) const {
		bfs::path filePath(path);
		ZipNode* node = m_zipTree.getNode(filePath.string());

		assert(node != 0);

		if (node) {
			const ZipEntryData& entryData = node->getZipEntryData();

			m_zipfile->setIndex(entryData.offset);
			uint8_t* data = new uint8_t[entryData.size_real]; // beware of me - one day i WILL cause memory leaks
			if (entryData.comp == 8) { // compressed using deflate
				FL_DBG(_log, LMsg("trying to uncompress file ") <<  path << " (compressed with method " << entryData.comp << ")");
				std::unique_ptr<uint8_t[]> compdata(new uint8_t[entryData.size_comp]);
				m_zipfile->readInto(compdata.get(), entryData.size_comp);

				z_stream zstream;
				zstream.next_in = compdata.get();
				zstream.avail_in = entryData.size_comp;
				zstream.zalloc = Z_NULL;
				zstream.zfree = Z_NULL;
				zstream.opaque = Z_NULL;
				zstream.next_out = data;
				zstream.avail_out = entryData.size_real;

				if (inflateInit2(&zstream, -15) != Z_OK) {
					FL_ERR(_log, LMsg("inflateInit2 failed"));
					delete[] data;
					return 0;
				}

				int32_t err = inflate(&zstream, Z_FINISH);
				if (err != Z_STREAM_END) {
					if (zstream.msg) {
						FL_ERR(_log, LMsg("inflate failed: ") << zstream.msg);
					} else {
						FL_ERR(_log, LMsg("inflate failed without msg, err: ") << err);
					}

					inflateEnd(&zstream);
					delete[] data;
					return 0;
				}

				inflateEnd(&zstream);
			} else if (entryData.comp == 0) { // uncompressed
				m_zipfile->readInto(data, entryData.size_real);
			} else {
				FL_ERR(_log, LMsg("unsupported compression"));
				delete[] data;
				return 0;
			}

			return new RawData(new ZipFileSource(data, entryData.size_real));
		}

		return 0;
	}

	void ZipSource::readIndex() {
		m_zipfile->setIndex(0);

		while (!readFileToIndex()) {}
	}

	bool ZipSource::readFileToIndex() {
		uint32_t header   = m_zipfile->read32Little();
		if (header == DE_HEADER || header == CF_HEADER) { // decryption header or central directory header - we are finished
			return true;
		}

		uint16_t vneeded  = m_zipfile->read16Little();
		uint16_t gflags   = m_zipfile->read16Little();
		uint16_t comp     = m_zipfile->read16Little();
		uint16_t lmodtime = m_zipfile->read16Little();
		uint16_t lmoddate = m_zipfile->read16Little();
		uint32_t crc      = m_zipfile->read32Little();
		uint32_t compsize = m_zipfile->read32Little();
		uint32_t realsize = m_zipfile->read32Little();
		uint16_t fnamelen = m_zipfile->read16Little();
		uint16_t extralen = m_zipfile->read16Little();

		if (header != LF_HEADER) {
			FL_ERR(_log, LMsg("invalid local file header: ") << header);
			return true;
		}

		if (vneeded > 20) {
			FL_ERR(_log, LMsg("only zip version 2 is supported, required: ") << vneeded);
			return true;
		}

		bfs::path filePath = bfs::path(m_zipfile->readString(fnamelen));

		m_zipfile->moveIndex(extralen);
		uint32_t offset = m_zipfile->getCurrentIndex();
		FL_DBG(_log, LMsg("found file: ") << filePath.string() << " (" << compsize << "/" << realsize << ") on offset " << offset);

		m_zipfile->moveIndex(compsize);
		if (gflags & (0x01 << 3)) {
			crc = m_zipfile->read32Little();
			compsize = m_zipfile->read32Little();
			realsize = m_zipfile->read32Little();
		}

		if (lmodtime || lmoddate) {} // shut up the compiler (warnings of unused variables)
		
		ZipEntryData data;
		data.comp = comp;
		data.size_real = realsize;
		data.size_comp = compsize;
		data.offset = offset;
		data.crc32 = crc;

		std::string filename = filePath.string();
		ZipNode* node = m_zipTree.addNode(filename);

		if (node) {
			// store the zip entry information in the node
			node->setZipEntryData(data);
		}

		return false;
	}


	std::set<std::string> ZipSource::listFiles(const std::string& path) const {
		std::set<std::string> result;

		bfs::path fixedPath(path);

		ZipNode* node = m_zipTree.getNode(fixedPath.string());
		
		if (node) {
			ZipNodeContainer files = node->getChildren(ZipContentType::File);
			ZipNodeContainer::iterator iter;
			for (iter = files.begin(); iter != files.end(); ++iter) {
				result.insert((*iter)->getFullName());
			}
		}

		return result;
	}

	// FIXME: quick&very dirty..
	std::set<std::string> ZipSource::listDirectories(const std::string& path) const {
		std::set<std::string> result;

		bfs::path fixedPath(path);

		ZipNode* node = m_zipTree.getNode(fixedPath.string());

		if (node) {
			ZipNodeContainer files = node->getChildren(ZipContentType::Directory);
			ZipNodeContainer::iterator iter;
			for (iter = files.begin(); iter != files.end(); ++iter) {
				result.insert((*iter)->getFullName());
			}
		}

		return result;
	}
}