]> icculus.org git repositories - divverent/netradiant.git/blob - plugins/archivezip/archive.cpp
fix zip file warnings
[divverent/netradiant.git] / plugins / archivezip / archive.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "idatastream.h"
23 #include "cmdlib.h"
24 #include "bytestreamutils.h"
25
26 #include "modulesystem.h"
27 #include "iarchive.h"
28
29 #include <algorithm>
30 #include "stream/filestream.h"
31 #include "container/array.h"
32 #include "archivelib.h"
33 #include "zlibstream.h"
34
35 class DeflatedArchiveFile : public ArchiveFile
36 {
37   CopiedString m_name;
38   FileInputStream m_istream;
39   SubFileInputStream m_substream;
40   DeflatedInputStream m_zipstream;
41   FileInputStream::size_type m_size;
42 public:
43   typedef FileInputStream::size_type size_type;
44   typedef FileInputStream::position_type position_type;
45
46   DeflatedArchiveFile(const char* name, const char* archiveName, position_type position, size_type stream_size, size_type file_size)
47     : m_name(name), m_istream(archiveName), m_substream(m_istream, position, stream_size), m_zipstream(m_substream), m_size(file_size)
48   {
49   }
50
51   void release()
52   {
53     delete this;
54   }
55   size_type size() const
56   {
57     return m_size;
58   }
59   const char* getName() const
60   {
61     return m_name.c_str();
62   }
63   InputStream& getInputStream()
64   {
65     return m_zipstream;
66   }
67 };
68
69 class DeflatedArchiveTextFile : public ArchiveTextFile
70 {
71   CopiedString m_name;
72   FileInputStream m_istream;
73   SubFileInputStream m_substream;
74   DeflatedInputStream m_zipstream;
75   BinaryToTextInputStream<DeflatedInputStream> m_textStream;
76 public:
77   typedef FileInputStream::size_type size_type;
78   typedef FileInputStream::position_type position_type;
79
80   DeflatedArchiveTextFile(const char* name, const char* archiveName, position_type position, size_type stream_size)
81     : m_name(name), m_istream(archiveName), m_substream(m_istream, position, stream_size), m_zipstream(m_substream), m_textStream(m_zipstream)
82   {
83   }
84
85   void release()
86   {
87     delete this;
88   }
89   TextInputStream& getInputStream()
90   {
91     return m_textStream;
92   }
93 };
94
95 #include "pkzip.h"
96
97 #include <map>
98 #include "string/string.h"
99 #include "fs_filesystem.h"
100
101
102 class ZipArchive : public Archive
103 {
104   class ZipRecord
105   {
106   public:
107     enum ECompressionMode
108     {
109       eStored,
110       eDeflated,
111     };
112     ZipRecord(unsigned int position, unsigned int compressed_size, unsigned int uncompressed_size, ECompressionMode mode)
113       : m_position(position), m_stream_size(compressed_size), m_file_size(uncompressed_size), m_mode(mode)
114     {
115     }
116     unsigned int m_position;
117     unsigned int m_stream_size;
118     unsigned int m_file_size;
119     ECompressionMode m_mode;
120   };
121
122   typedef GenericFileSystem<ZipRecord> ZipFileSystem;
123   ZipFileSystem m_filesystem;
124   CopiedString m_name;
125   FileInputStream m_istream;
126
127   bool read_record()
128   {
129     zip_magic magic;
130     istream_read_zip_magic(m_istream, magic);
131     if(!(magic == zip_root_dirent_magic))
132     {
133       return false;
134     }
135     zip_version version_encoder;
136     istream_read_zip_version(m_istream, version_encoder);
137     zip_version version_extract;
138     istream_read_zip_version(m_istream, version_extract);
139     //unsigned short flags = 
140     istream_read_int16_le(m_istream);
141     unsigned short compression_mode = istream_read_int16_le(m_istream);
142     if(compression_mode != Z_DEFLATED && compression_mode != 0)
143     {
144       return false;
145     }
146     zip_dostime dostime;
147     istream_read_zip_dostime(m_istream, dostime);
148     //unsigned int crc32 = 
149     istream_read_int32_le(m_istream);
150     unsigned int compressed_size = istream_read_uint32_le(m_istream);
151     unsigned int uncompressed_size = istream_read_uint32_le(m_istream);
152     unsigned int namelength = istream_read_uint16_le(m_istream);
153     unsigned short extras = istream_read_uint16_le(m_istream);
154     unsigned short comment = istream_read_uint16_le(m_istream);
155     //unsigned short diskstart =
156     istream_read_int16_le(m_istream);
157     //unsigned short filetype = 
158     istream_read_int16_le(m_istream);
159     //unsigned int filemode =
160     istream_read_int32_le(m_istream);
161     unsigned int position = istream_read_int32_le(m_istream);
162
163     Array<char> filename(namelength+1);
164     m_istream.read(reinterpret_cast<FileInputStream::byte_type*>(filename.data()), namelength);
165     filename[namelength] = '\0';
166
167     m_istream.seek(extras + comment, FileInputStream::cur);
168
169     if(path_is_directory(filename.data()))
170     {
171       m_filesystem[filename.data()] = 0;
172     }
173     else
174     {
175       ZipFileSystem::entry_type& file = m_filesystem[filename.data()];
176       if(!file.is_directory())
177       {
178         globalOutputStream() << "Warning: zip archive " << makeQuoted(m_name.c_str()) << " contains duplicated file: " << makeQuoted(filename.data()) << "\n";
179       }
180       else
181       {
182         file = new ZipRecord(position, compressed_size, uncompressed_size, (compression_mode == Z_DEFLATED) ? ZipRecord::eDeflated : ZipRecord::eStored);
183       }
184     }
185     
186     return true;
187   }
188
189   bool read_pkzip()
190   {
191     SeekableStream::position_type pos = pkzip_find_disk_trailer(m_istream);
192     if(pos != 0)
193     {
194       zip_disk_trailer disk_trailer;
195       m_istream.seek(pos);
196       istream_read_zip_disk_trailer(m_istream, disk_trailer);
197       if(!(disk_trailer.z_magic == zip_disk_trailer_magic))
198       {
199         return false;
200       }
201
202       m_istream.seek(disk_trailer.z_rootseek);
203       for(unsigned int i = 0; i < disk_trailer.z_entries; ++i)
204       {
205         if(!read_record())
206         {
207           return false;
208         }
209       }
210       return true;
211     }
212     return false;
213   }
214 public:
215   ZipArchive(const char* name)
216     : m_name(name), m_istream(name)
217   {
218     if(!m_istream.failed())
219     {
220       if(!read_pkzip())
221       {
222         globalErrorStream() << "ERROR: invalid zip-file " << makeQuoted(name) << '\n';
223       }        
224     }
225   }
226   ~ZipArchive()
227   {
228     for(ZipFileSystem::iterator i = m_filesystem.begin(); i != m_filesystem.end(); ++i)
229     {
230       delete i->second.file();
231     }
232   }
233
234   bool failed()
235   {
236     return m_istream.failed();
237   }
238
239   void release()
240   {
241     delete this;
242   }
243   ArchiveFile* openFile(const char* name)
244   {
245     ZipFileSystem::iterator i = m_filesystem.find(name);
246     if(i != m_filesystem.end() && !i->second.is_directory())
247     {
248       ZipRecord* file = i->second.file();
249
250       m_istream.seek(file->m_position);
251       zip_file_header file_header;
252       istream_read_zip_file_header(m_istream, file_header);
253       if(file_header.z_magic != zip_file_header_magic)
254       {
255         globalErrorStream() << "error reading zip file " << makeQuoted(m_name.c_str());
256         return 0;
257       }
258
259       switch(file->m_mode)
260       {
261       case ZipRecord::eStored:
262         return StoredArchiveFile::create(name, m_name.c_str(), m_istream.tell(), file->m_stream_size, file->m_file_size);
263       case ZipRecord::eDeflated:
264         return new DeflatedArchiveFile(name, m_name.c_str(), m_istream.tell(), file->m_stream_size, file->m_file_size);
265       }
266     }
267     return 0;
268   }
269   ArchiveTextFile* openTextFile(const char* name)
270   {
271     ZipFileSystem::iterator i = m_filesystem.find(name);
272     if(i != m_filesystem.end() && !i->second.is_directory())
273     {
274       ZipRecord* file = i->second.file();
275
276       m_istream.seek(file->m_position);
277       zip_file_header file_header;
278       istream_read_zip_file_header(m_istream, file_header);
279       if(file_header.z_magic != zip_file_header_magic)
280       {
281         globalErrorStream() << "error reading zip file " << makeQuoted(m_name.c_str());
282         return 0;
283       }
284
285       switch(file->m_mode)
286       {
287       case ZipRecord::eStored:
288         return StoredArchiveTextFile::create(name, m_name.c_str(), m_istream.tell(), file->m_stream_size);
289       case ZipRecord::eDeflated:
290         return new DeflatedArchiveTextFile(name, m_name.c_str(), m_istream.tell(), file->m_stream_size);
291       }
292     }
293     return 0;
294   }
295   bool containsFile(const char* name)
296   {
297     ZipFileSystem::iterator i = m_filesystem.find(name);
298     return i != m_filesystem.end() && !i->second.is_directory();
299   }
300   void forEachFile(VisitorFunc visitor, const char* root)
301   {
302     m_filesystem.traverse(visitor, root);
303   }
304 };
305
306 Archive* OpenArchive(const char* name)
307 {
308   return new ZipArchive(name);
309 }
310
311 #if 0
312
313 class TestZip
314 {
315   class TestVisitor : public Archive::IVisitor
316   {
317   public:
318     void visit(const char* name)
319     {
320       int bleh = 0;
321     }
322   };
323 public:
324   TestZip()
325   {
326     testzip("c:/quake3/baseq3/mapmedia.pk3", "textures/radiant/notex.tga");
327   }
328
329   void testzip(const char* name, const char* filename)
330   {
331     Archive* archive = OpenArchive(name);
332     ArchiveFile* file = archive->openFile(filename);
333     if(file != 0)
334     {
335       unsigned char buffer[4096];
336       std::size_t count = file->getInputStream().read((InputStream::byte_type*)buffer, 4096);
337       file->release();
338     }
339     TestVisitor visitor;
340     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 0), "");
341     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "");
342     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFiles, 1), "");
343     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eDirectories, 1), "");
344     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures");
345     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 1), "textures/");
346     archive->forEachFile(Archive::VisitorFunc(&visitor, Archive::eFilesAndDirectories, 2), "");
347     archive->release();
348   }
349 };
350
351 TestZip g_TestZip;
352
353 #endif