]> git.saurik.com Git - wxWidgets.git/blob - include/wx/zipstrm.h
added wxMBConv::Clone() to be able to copy conversion objects polymorphically
[wxWidgets.git] / include / wx / zipstrm.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: zipstrm.h
3 // Purpose: Streams for Zip files
4 // Author: Mike Wetherell
5 // RCS-ID: $Id$
6 // Copyright: (c) Mike Wetherell
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_WXZIPSTREAM_H__
11 #define _WX_WXZIPSTREAM_H__
12
13 #include "wx/defs.h"
14
15 #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
16
17 #include "wx/archive.h"
18 #include "wx/hashmap.h"
19 #include "wx/filename.h"
20
21 // some methods from wxZipInputStream and wxZipOutputStream stream do not get
22 // exported/imported when compiled with Mingw versions before 3.4.2. So they
23 // are imported/exported individually as a workaround
24 #if (defined(__GNUWIN32__) || defined(__MINGW32__)) \
25 && (!defined __GNUC__ \
26 || !defined __GNUC_MINOR__ \
27 || !defined __GNUC_PATCHLEVEL__ \
28 || __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 30402)
29 #define WXZIPFIX WXDLLIMPEXP_BASE
30 #else
31 #define WXZIPFIX
32 #endif
33
34 /////////////////////////////////////////////////////////////////////////////
35 // constants
36
37 // Compression Method, only 0 (store) and 8 (deflate) are supported here
38 //
39 enum wxZipMethod
40 {
41 wxZIP_METHOD_STORE,
42 wxZIP_METHOD_SHRINK,
43 wxZIP_METHOD_REDUCE1,
44 wxZIP_METHOD_REDUCE2,
45 wxZIP_METHOD_REDUCE3,
46 wxZIP_METHOD_REDUCE4,
47 wxZIP_METHOD_IMPLODE,
48 wxZIP_METHOD_TOKENIZE,
49 wxZIP_METHOD_DEFLATE,
50 wxZIP_METHOD_DEFLATE64,
51 wxZIP_METHOD_BZIP2 = 12,
52 wxZIP_METHOD_DEFAULT = 0xffff
53 };
54
55 // Originating File-System.
56 //
57 // These are Pkware's values. Note that Info-zip disagree on some of them,
58 // most notably NTFS.
59 //
60 enum wxZipSystem
61 {
62 wxZIP_SYSTEM_MSDOS,
63 wxZIP_SYSTEM_AMIGA,
64 wxZIP_SYSTEM_OPENVMS,
65 wxZIP_SYSTEM_UNIX,
66 wxZIP_SYSTEM_VM_CMS,
67 wxZIP_SYSTEM_ATARI_ST,
68 wxZIP_SYSTEM_OS2_HPFS,
69 wxZIP_SYSTEM_MACINTOSH,
70 wxZIP_SYSTEM_Z_SYSTEM,
71 wxZIP_SYSTEM_CPM,
72 wxZIP_SYSTEM_WINDOWS_NTFS,
73 wxZIP_SYSTEM_MVS,
74 wxZIP_SYSTEM_VSE,
75 wxZIP_SYSTEM_ACORN_RISC,
76 wxZIP_SYSTEM_VFAT,
77 wxZIP_SYSTEM_ALTERNATE_MVS,
78 wxZIP_SYSTEM_BEOS,
79 wxZIP_SYSTEM_TANDEM,
80 wxZIP_SYSTEM_OS_400
81 };
82
83 // Dos/Win file attributes
84 //
85 enum wxZipAttributes
86 {
87 wxZIP_A_RDONLY = 0x01,
88 wxZIP_A_HIDDEN = 0x02,
89 wxZIP_A_SYSTEM = 0x04,
90 wxZIP_A_SUBDIR = 0x10,
91 wxZIP_A_ARCH = 0x20,
92
93 wxZIP_A_MASK = 0x37
94 };
95
96 // Values for the flags field in the zip headers
97 //
98 enum wxZipFlags
99 {
100 wxZIP_ENCRYPTED = 0x0001,
101 wxZIP_DEFLATE_NORMAL = 0x0000, // normal compression
102 wxZIP_DEFLATE_EXTRA = 0x0002, // extra compression
103 wxZIP_DEFLATE_FAST = 0x0004, // fast compression
104 wxZIP_DEFLATE_SUPERFAST = 0x0006, // superfast compression
105 wxZIP_DEFLATE_MASK = 0x0006,
106 wxZIP_SUMS_FOLLOW = 0x0008, // crc and sizes come after the data
107 wxZIP_ENHANCED = 0x0010,
108 wxZIP_PATCH = 0x0020,
109 wxZIP_STRONG_ENC = 0x0040,
110 wxZIP_UNUSED = 0x0F80,
111 wxZIP_RESERVED = 0xF000
112 };
113
114 // Forward decls
115 //
116 class WXDLLIMPEXP_BASE wxZipEntry;
117 class WXDLLIMPEXP_BASE wxZipInputStream;
118
119
120 /////////////////////////////////////////////////////////////////////////////
121 // wxZipNotifier
122
123 class WXDLLIMPEXP_BASE wxZipNotifier
124 {
125 public:
126 virtual ~wxZipNotifier() { }
127
128 virtual void OnEntryUpdated(wxZipEntry& entry) = 0;
129 };
130
131
132 /////////////////////////////////////////////////////////////////////////////
133 // Zip Entry - holds the meta data for a file in the zip
134
135 class WXDLLIMPEXP_BASE wxZipEntry : public wxArchiveEntry
136 {
137 public:
138 wxZipEntry(const wxString& name = wxEmptyString,
139 const wxDateTime& dt = wxDateTime::Now(),
140 wxFileOffset size = wxInvalidOffset);
141 virtual ~wxZipEntry();
142
143 wxZipEntry(const wxZipEntry& entry);
144 wxZipEntry& operator=(const wxZipEntry& entry);
145
146 // Get accessors
147 wxDateTime GetDateTime() const { return m_DateTime; }
148 wxFileOffset GetSize() const { return m_Size; }
149 wxFileOffset GetOffset() const { return m_Offset; }
150 wxString GetInternalName() const { return m_Name; }
151 int GetMethod() const { return m_Method; }
152 int GetFlags() const { return m_Flags; }
153 wxUint32 GetCrc() const { return m_Crc; }
154 wxFileOffset GetCompressedSize() const { return m_CompressedSize; }
155 int GetSystemMadeBy() const { return m_SystemMadeBy; }
156 wxString GetComment() const { return m_Comment; }
157 wxUint32 GetExternalAttributes() const { return m_ExternalAttributes; }
158 wxPathFormat GetInternalFormat() const { return wxPATH_UNIX; }
159 int GetMode() const;
160 const char *GetLocalExtra() const;
161 size_t GetLocalExtraLen() const;
162 const char *GetExtra() const;
163 size_t GetExtraLen() const;
164 wxString GetName(wxPathFormat format = wxPATH_NATIVE) const;
165
166 // is accessors
167 inline bool IsDir() const;
168 inline bool IsText() const;
169 inline bool IsReadOnly() const;
170 inline bool IsMadeByUnix() const;
171
172 // set accessors
173 void SetDateTime(const wxDateTime& dt) { m_DateTime = dt; }
174 void SetSize(wxFileOffset size) { m_Size = size; }
175 void SetMethod(int method) { m_Method = (wxUint16)method; }
176 void SetComment(const wxString& comment) { m_Comment = comment; }
177 void SetExternalAttributes(wxUint32 attr ) { m_ExternalAttributes = attr; }
178 void SetSystemMadeBy(int system);
179 void SetMode(int mode);
180 void SetExtra(const char *extra, size_t len);
181 void SetLocalExtra(const char *extra, size_t len);
182
183 inline void SetName(const wxString& name,
184 wxPathFormat format = wxPATH_NATIVE);
185
186 static wxString GetInternalName(const wxString& name,
187 wxPathFormat format = wxPATH_NATIVE,
188 bool *pIsDir = NULL);
189
190 // set is accessors
191 void SetIsDir(bool isDir = true);
192 inline void SetIsReadOnly(bool isReadOnly = true);
193 inline void SetIsText(bool isText = true);
194
195 wxZipEntry *Clone() const { return ZipClone(); }
196
197 void SetNotifier(wxZipNotifier& notifier);
198 void UnsetNotifier();
199
200 protected:
201 // Internal attributes
202 enum { TEXT_ATTR = 1 };
203
204 // protected Get accessors
205 int GetVersionNeeded() const { return m_VersionNeeded; }
206 wxFileOffset GetKey() const { return m_Key; }
207 int GetVersionMadeBy() const { return m_VersionMadeBy; }
208 int GetDiskStart() const { return m_DiskStart; }
209 int GetInternalAttributes() const { return m_InternalAttributes; }
210
211 void SetVersionNeeded(int version) { m_VersionNeeded = (wxUint16)version; }
212 void SetOffset(wxFileOffset offset) { m_Offset = offset; }
213 void SetFlags(int flags) { m_Flags = (wxUint16)flags; }
214 void SetVersionMadeBy(int version) { m_VersionMadeBy = (wxUint8)version; }
215 void SetCrc(wxUint32 crc) { m_Crc = crc; }
216 void SetCompressedSize(wxFileOffset size) { m_CompressedSize = size; }
217 void SetKey(wxFileOffset offset) { m_Key = offset; }
218 void SetDiskStart(int start) { m_DiskStart = (wxUint16)start; }
219 void SetInternalAttributes(int attr) { m_InternalAttributes = (wxUint16)attr; }
220
221 virtual wxZipEntry *ZipClone() const { return new wxZipEntry(*this); }
222
223 void Notify();
224
225 wxArchiveEntry* DoClone() const { return ZipClone(); }
226
227 private:
228 size_t ReadLocal(wxInputStream& stream, wxMBConv& conv);
229 size_t WriteLocal(wxOutputStream& stream, wxMBConv& conv) const;
230
231 size_t ReadCentral(wxInputStream& stream, wxMBConv& conv);
232 size_t WriteCentral(wxOutputStream& stream, wxMBConv& conv) const;
233
234 size_t ReadDescriptor(wxInputStream& stream);
235 size_t WriteDescriptor(wxOutputStream& stream, wxUint32 crc,
236 wxFileOffset compressedSize, wxFileOffset size);
237
238 wxUint8 m_SystemMadeBy; // one of enum wxZipSystem
239 wxUint8 m_VersionMadeBy; // major * 10 + minor
240
241 wxUint16 m_VersionNeeded; // ver needed to extract (20 i.e. v2.0)
242 wxUint16 m_Flags;
243 wxUint16 m_Method; // compression method (one of wxZipMethod)
244 wxDateTime m_DateTime;
245 wxUint32 m_Crc;
246 wxFileOffset m_CompressedSize;
247 wxFileOffset m_Size;
248 wxString m_Name; // in internal format
249 wxFileOffset m_Key; // the original offset for copied entries
250 wxFileOffset m_Offset; // file offset of the entry
251
252 wxString m_Comment;
253 wxUint16 m_DiskStart; // for multidisk archives, not unsupported
254 wxUint16 m_InternalAttributes; // bit 0 set for text files
255 wxUint32 m_ExternalAttributes; // system specific depends on SystemMadeBy
256
257 class wxZipMemory *m_Extra;
258 class wxZipMemory *m_LocalExtra;
259
260 wxZipNotifier *m_zipnotifier;
261 class wxZipWeakLinks *m_backlink;
262
263 friend class wxZipInputStream;
264 friend class wxZipOutputStream;
265
266 DECLARE_DYNAMIC_CLASS(wxZipEntry)
267 };
268
269
270 /////////////////////////////////////////////////////////////////////////////
271 // wxZipOutputStream
272
273 WX_DECLARE_LIST_WITH_DECL(wxZipEntry, wx__ZipEntryList, class WXDLLIMPEXP_BASE);
274
275 class WXDLLIMPEXP_BASE wxZipOutputStream : public wxArchiveOutputStream
276 {
277 public:
278 wxZipOutputStream(wxOutputStream& stream,
279 int level = -1,
280 wxMBConv& conv = wxConvLocal);
281 virtual WXZIPFIX ~wxZipOutputStream();
282
283 bool PutNextEntry(wxZipEntry *entry) { return DoCreate(entry); }
284
285 bool WXZIPFIX PutNextEntry(const wxString& name,
286 const wxDateTime& dt = wxDateTime::Now(),
287 wxFileOffset size = wxInvalidOffset);
288
289 bool WXZIPFIX PutNextDirEntry(const wxString& name,
290 const wxDateTime& dt = wxDateTime::Now());
291
292 bool WXZIPFIX CopyEntry(wxZipEntry *entry, wxZipInputStream& inputStream);
293 bool WXZIPFIX CopyArchiveMetaData(wxZipInputStream& inputStream);
294
295 void WXZIPFIX Sync();
296 bool WXZIPFIX CloseEntry();
297 bool WXZIPFIX Close();
298
299 void SetComment(const wxString& comment) { m_Comment = comment; }
300
301 int GetLevel() const { return m_level; }
302 void WXZIPFIX SetLevel(int level);
303
304 bool WXZIPFIX PutNextEntry(wxArchiveEntry *entry);
305 bool WXZIPFIX CopyEntry(wxArchiveEntry *entry, wxArchiveInputStream& stream);
306 bool WXZIPFIX CopyArchiveMetaData(wxArchiveInputStream& stream);
307
308 protected:
309 virtual size_t WXZIPFIX OnSysWrite(const void *buffer, size_t size);
310 virtual wxFileOffset OnSysTell() const { return m_entrySize; }
311
312 // this protected interface isn't yet finalised
313 struct Buffer { const char *m_data; size_t m_size; };
314 virtual wxOutputStream* WXZIPFIX OpenCompressor(wxOutputStream& stream,
315 wxZipEntry& entry,
316 const Buffer bufs[]);
317 virtual bool WXZIPFIX CloseCompressor(wxOutputStream *comp);
318
319 bool IsParentSeekable() const
320 { return m_offsetAdjustment != wxInvalidOffset; }
321
322 private:
323 bool IsOpened() const { return m_comp || m_pending; }
324
325 bool DoCreate(wxZipEntry *entry, bool raw = false);
326 void CreatePendingEntry(const void *buffer, size_t size);
327 void CreatePendingEntry();
328
329 class wxStoredOutputStream *m_store;
330 class wxZlibOutputStream2 *m_deflate;
331 class wxZipStreamLink *m_backlink;
332 wx__ZipEntryList m_entries;
333 char *m_initialData;
334 size_t m_initialSize;
335 wxZipEntry *m_pending;
336 bool m_raw;
337 wxFileOffset m_headerOffset;
338 size_t m_headerSize;
339 wxFileOffset m_entrySize;
340 wxUint32 m_crcAccumulator;
341 wxOutputStream *m_comp;
342 int m_level;
343 wxFileOffset m_offsetAdjustment;
344 wxString m_Comment;
345
346 DECLARE_NO_COPY_CLASS(wxZipOutputStream)
347 };
348
349
350 /////////////////////////////////////////////////////////////////////////////
351 // wxZipInputStream
352
353 class WXDLLIMPEXP_BASE wxZipInputStream : public wxArchiveInputStream
354 {
355 public:
356 typedef wxZipEntry entry_type;
357
358 wxZipInputStream(wxInputStream& stream, wxMBConv& conv = wxConvLocal);
359
360 #if 1 //WXWIN_COMPATIBILITY_2_6
361 wxZipInputStream(const wxString& archive, const wxString& file)
362 : wxArchiveInputStream(OpenFile(archive), wxConvLocal) { Init(file); }
363 #endif
364
365 virtual WXZIPFIX ~wxZipInputStream();
366
367 bool WXZIPFIX OpenEntry(wxArchiveEntry& entry);
368 bool OpenEntry(wxZipEntry& entry) { return DoOpen(&entry); }
369 bool WXZIPFIX CloseEntry();
370
371 wxZipEntry *GetNextEntry();
372
373 wxString WXZIPFIX GetComment();
374 int WXZIPFIX GetTotalEntries();
375
376 virtual wxFileOffset GetLength() const { return m_entry.GetSize(); }
377
378 protected:
379 size_t WXZIPFIX OnSysRead(void *buffer, size_t size);
380 wxFileOffset OnSysTell() const { return m_decomp ? m_decomp->TellI() : 0; }
381
382 #if 1 //WXWIN_COMPATIBILITY_2_6
383 wxFileOffset WXZIPFIX OnSysSeek(wxFileOffset seek, wxSeekMode mode);
384 #endif
385
386 // this protected interface isn't yet finalised
387 virtual wxInputStream* WXZIPFIX OpenDecompressor(wxInputStream& stream);
388 virtual bool WXZIPFIX CloseDecompressor(wxInputStream *decomp);
389
390 wxArchiveEntry *DoGetNextEntry() { return GetNextEntry(); }
391
392 private:
393 void Init();
394 void Init(const wxString& file);
395 wxInputStream& OpenFile(const wxString& archive);
396
397 wxStreamError ReadLocal(bool readEndRec = false);
398 wxStreamError ReadCentral();
399
400 wxUint32 ReadSignature();
401 bool FindEndRecord();
402 bool LoadEndRecord();
403
404 bool AtHeader() const { return m_headerSize == 0; }
405 bool AfterHeader() const { return m_headerSize > 0 && !m_decomp; }
406 bool IsOpened() const { return m_decomp != NULL; }
407
408 wxZipStreamLink *MakeLink(wxZipOutputStream *out);
409
410 bool DoOpen(wxZipEntry *entry = NULL, bool raw = false);
411 bool OpenDecompressor(bool raw = false);
412
413 class wxStoredInputStream *m_store;
414 class wxZlibInputStream2 *m_inflate;
415 class wxRawInputStream *m_rawin;
416 class wxFFileInputStream *m_ffile;
417 wxZipEntry m_entry;
418 bool m_raw;
419 size_t m_headerSize;
420 wxUint32 m_crcAccumulator;
421 wxInputStream *m_decomp;
422 bool m_parentSeekable;
423 class wxZipWeakLinks *m_weaklinks;
424 class wxZipStreamLink *m_streamlink;
425 wxFileOffset m_offsetAdjustment;
426 wxFileOffset m_position;
427 wxUint32 m_signature;
428 size_t m_TotalEntries;
429 wxString m_Comment;
430
431 friend bool wxZipOutputStream::CopyEntry(
432 wxZipEntry *entry, wxZipInputStream& inputStream);
433 friend bool wxZipOutputStream::CopyArchiveMetaData(
434 wxZipInputStream& inputStream);
435
436 #if 1 //WXWIN_COMPATIBILITY_2_6
437 bool m_allowSeeking;
438 friend class wxZipFSInputStream;
439 #endif
440
441 DECLARE_NO_COPY_CLASS(wxZipInputStream)
442 };
443
444
445 /////////////////////////////////////////////////////////////////////////////
446 // Iterators
447
448 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
449 typedef wxArchiveIterator<wxZipInputStream> wxZipIter;
450 typedef wxArchiveIterator<wxZipInputStream,
451 std::pair<wxString, wxZipEntry*> > wxZipPairIter;
452 #endif
453
454
455 /////////////////////////////////////////////////////////////////////////////
456 // wxZipClassFactory
457
458 class WXDLLIMPEXP_BASE wxZipClassFactory : public wxArchiveClassFactory
459 {
460 public:
461 typedef wxZipEntry entry_type;
462 typedef wxZipInputStream instream_type;
463 typedef wxZipOutputStream outstream_type;
464 typedef wxZipNotifier notifier_type;
465 #if wxUSE_STL || defined WX_TEST_ARCHIVE_ITERATOR
466 typedef wxZipIter iter_type;
467 typedef wxZipPairIter pairiter_type;
468 #endif
469
470 wxZipEntry *NewEntry() const
471 { return new wxZipEntry; }
472 wxZipInputStream *NewStream(wxInputStream& stream) const
473 { return new wxZipInputStream(stream, GetConv()); }
474 wxZipOutputStream *NewStream(wxOutputStream& stream) const
475 { return new wxZipOutputStream(stream, -1, GetConv()); }
476
477 wxString GetInternalName(const wxString& name,
478 wxPathFormat format = wxPATH_NATIVE) const
479 { return wxZipEntry::GetInternalName(name, format); }
480
481 protected:
482 wxArchiveEntry *DoNewEntry() const
483 { return NewEntry(); }
484 wxArchiveInputStream *DoNewStream(wxInputStream& stream) const
485 { return NewStream(stream); }
486 wxArchiveOutputStream *DoNewStream(wxOutputStream& stream) const
487 { return NewStream(stream); }
488
489 private:
490 DECLARE_DYNAMIC_CLASS(wxZipClassFactory)
491 };
492
493
494 /////////////////////////////////////////////////////////////////////////////
495 // wxZipEntry inlines
496
497 inline bool wxZipEntry::IsText() const
498 {
499 return (m_InternalAttributes & TEXT_ATTR) != 0;
500 }
501
502 inline bool wxZipEntry::IsDir() const
503 {
504 return (m_ExternalAttributes & wxZIP_A_SUBDIR) != 0;
505 }
506
507 inline bool wxZipEntry::IsReadOnly() const
508 {
509 return (m_ExternalAttributes & wxZIP_A_RDONLY) != 0;
510 }
511
512 inline bool wxZipEntry::IsMadeByUnix() const
513 {
514 const int pattern =
515 (1 << wxZIP_SYSTEM_OPENVMS) |
516 (1 << wxZIP_SYSTEM_UNIX) |
517 (1 << wxZIP_SYSTEM_ATARI_ST) |
518 (1 << wxZIP_SYSTEM_ACORN_RISC) |
519 (1 << wxZIP_SYSTEM_BEOS) | (1 << wxZIP_SYSTEM_TANDEM);
520
521 // note: some unix zippers put madeby = dos
522 return (m_SystemMadeBy == wxZIP_SYSTEM_MSDOS
523 && (m_ExternalAttributes & ~0xFFFF))
524 || ((pattern >> m_SystemMadeBy) & 1);
525 }
526
527 inline void wxZipEntry::SetIsText(bool isText)
528 {
529 if (isText)
530 m_InternalAttributes |= TEXT_ATTR;
531 else
532 m_InternalAttributes &= ~TEXT_ATTR;
533 }
534
535 inline void wxZipEntry::SetIsReadOnly(bool isReadOnly)
536 {
537 if (isReadOnly)
538 SetMode(GetMode() & ~0222);
539 else
540 SetMode(GetMode() | 0200);
541 }
542
543 inline void wxZipEntry::SetName(const wxString& name,
544 wxPathFormat format /*=wxPATH_NATIVE*/)
545 {
546 bool isDir;
547 m_Name = GetInternalName(name, format, &isDir);
548 SetIsDir(isDir);
549 }
550
551
552 #endif // wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
553
554 #endif // _WX_WXZIPSTREAM_H__