FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
zipsource.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <algorithm>
24 #include <list>
25 
26 // 3rd party library includes
27 #include "zlib.h"
28 #include <boost/scoped_array.hpp>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "util/base/exception.h"
35 #include "vfs/raw/rawdata.h"
36 #include "util/log/logger.h"
37 
38 #include "zipsource.h"
39 #include "zipfilesource.h"
40 
41 namespace FIFE {
42 
43  static const uint32_t LF_HEADER = 0x04034b50;
44  static const uint32_t DE_HEADER = 0x08064b50;
45  static const uint32_t CF_HEADER = 0x02014b50;
46 
47  static Logger _log(LM_LOADERS);
48 
49  ZipSource::ZipSource(VFS* vfs, const std::string& zip_file) : VFSSource(vfs), m_zipfile(vfs->open(zip_file)) {
50  readIndex();
51  }
52 
53  ZipSource::~ZipSource() {
54  delete m_zipfile;
55  }
56 
57  bool ZipSource::fileExists(const std::string& file) const {
58  return m_files.find(file) != m_files.end();
59  }
60 
61  RawData* ZipSource::open(const std::string& path) const {
62  type_files::const_iterator i = m_files.find(path);
63  assert(i != m_files.end());
64  const s_data& info = i->second;
65 
66  m_zipfile->setIndex(info.offset);
67  uint8_t* data = new uint8_t[info.size_real]; // beware of me - one day i WILL cause memory leaks
68  if (info.comp == 8) { // compressed using deflate
69  FL_DBG(_log, LMsg("trying to uncompress file ") << path << " (compressed with method " << info.comp << ")");
70  boost::scoped_array<uint8_t> compdata(new uint8_t[info.size_comp]);
71  m_zipfile->readInto(compdata.get(), info.size_comp);
72 
73  z_stream zstream;
74  zstream.next_in = compdata.get();
75  zstream.avail_in = info.size_comp;
76  zstream.zalloc = Z_NULL;
77  zstream.zfree = Z_NULL;
78  zstream.opaque = Z_NULL;
79  zstream.next_out = data;
80  zstream.avail_out = info.size_real;
81 
82  if (inflateInit2(&zstream, -15) != Z_OK) {
83  FL_ERR(_log, LMsg("inflateInit2 failed"));
84  delete[] data;
85  return 0;
86  }
87 
88  int err = inflate(&zstream, Z_FINISH);
89  if (err != Z_STREAM_END) {
90  if (zstream.msg) {
91  FL_ERR(_log, LMsg("inflate failed: ") << zstream.msg);
92  } else {
93  FL_ERR(_log, LMsg("inflate failed without msg, err: ") << err);
94  }
95 
96  inflateEnd(&zstream);
97  delete[] data;
98  return 0;
99  }
100 
101  inflateEnd(&zstream);
102  } else if (info.comp == 0) { // uncompressed
103  m_zipfile->readInto(data, info.size_real);
104  } else {
105  FL_ERR(_log, LMsg("unsupported compression"));
106  return 0;
107  }
108 
109  return new RawData(new ZipFileSource(data, info.size_real));
110  }
111 
112  void ZipSource::readIndex() {
113  m_zipfile->setIndex(0);
114  m_files.clear();
115 
116  while (!readFileToIndex()) {}
117  }
118 
119  bool ZipSource::readFileToIndex() {
120  uint32_t header = m_zipfile->read32Little();
121  if (header == DE_HEADER || header == CF_HEADER) { // decryption header or central directory header - we are finished
122  return true;
123  }
124 
125  uint16_t vneeded = m_zipfile->read16Little();
126  uint16_t gflags = m_zipfile->read16Little();
127  uint16_t comp = m_zipfile->read16Little();
128  uint16_t lmodtime = m_zipfile->read16Little();
129  uint16_t lmoddate = m_zipfile->read16Little();
130  uint32_t crc = m_zipfile->read32Little();
131  uint32_t compsize = m_zipfile->read32Little();
132  uint32_t realsize = m_zipfile->read32Little();
133  uint16_t fnamelen = m_zipfile->read16Little();
134  uint16_t extralen = m_zipfile->read16Little();
135 
136  if (header != LF_HEADER) {
137  FL_ERR(_log, LMsg("invalid local file header: ") << header);
138  return true;
139  }
140 
141  if (vneeded > 20) {
142  FL_ERR(_log, LMsg("only zip version 2 is supported, required: ") << vneeded);
143  return true;
144  }
145 
146  std::string filename = m_zipfile->readString(fnamelen);
147  m_zipfile->moveIndex(extralen);
148  unsigned int offset = m_zipfile->getCurrentIndex();
149  FL_DBG(_log, LMsg("found file: ") << filename << " (" << compsize << "/" << realsize << ") on offset " << offset);
150 
151  m_zipfile->moveIndex(compsize);
152  if (gflags & (0x01 << 3)) {
153  crc = m_zipfile->read32Little();
154  compsize = m_zipfile->read32Little();
155  realsize = m_zipfile->read32Little();
156  }
157 
158  if (lmodtime || lmoddate) {} // shut up the compiler (warnings of unused variables)
159 
160  s_data data;
161  data.comp = comp;
162  data.size_real = realsize;
163  data.size_comp = compsize;
164  data.offset = offset;
165  data.path = filename;
166  data.crc32 = crc;
167 
168  m_files[filename] = data;
169  return false;
170  }
171 
172 
173  // FIXME: quick&very dirty..
174  std::set<std::string> ZipSource::listFiles(const std::string& path) const {
175  std::set<std::string> result;
176 
177  std::string fixedPath = fixPath(path);
178  int path_len = path.length();
179  if (fixedPath[path_len - 1] != '/') {
180  fixedPath += '/';
181  path_len++;
182  }
183 
184  type_files::const_iterator end = m_files.end();
185  for (type_files::const_iterator i = m_files.begin(); i != end; ++i) {
186  std::string name = i->first;
187  int len = name.length();
188  if (len && name.find(fixedPath) == 0 && name[len - 1] != '/') {
189  name = name.substr(path_len);
190  size_t pos = name.find("/");
191  if (pos != std::string::npos)
192  continue;
193 
194  result.insert(name);
195  }
196  }
197 
198  return result;
199  }
200 
201  // FIXME: quick&very dirty..
202  std::set<std::string> ZipSource::listDirectories(const std::string& path) const {
203  std::set<std::string> result;
204 
205  std::string fixedPath = fixPath(path);
206  int path_len = path.length();
207  if (fixedPath[path_len - 1] != '/') {
208  fixedPath += '/';
209  path_len++;
210  }
211 
212  type_files::const_iterator end = m_files.end();
213  for (type_files::const_iterator i = m_files.begin(); i != end; ++i) {
214  std::string name = i->first;
215  int len = name.length();
216  if (len && name.find(fixedPath) == 0 && name[len - 1] == '/' && len > path_len) {
217  name = name.substr(path_len);
218  size_t pos = name.find("/");
219  if (pos != std::string::npos) {
220  name = name.substr(0, pos);
221  }
222  result.insert(name);
223  }
224  }
225 
226  return result;
227  }
228 }