1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/zipstrm.cpp
3 // Purpose: Streams for Zip files
4 // Author: Mike Wetherell
6 // Copyright: (c) Mike Wetherell
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM
25 #include "wx/zipstrm.h"
26 #include "wx/datstrm.h"
27 #include "wx/zstream.h"
28 #include "wx/mstream.h"
29 #include "wx/buffer.h"
30 #include "wx/ptr_scpd.h"
31 #include "wx/wfstream.h"
35 // value for the 'version needed to extract' field (20 means 2.0)
37 VERSION_NEEDED_TO_EXTRACT
= 20
40 // signatures for the various records (PKxx)
42 CENTRAL_MAGIC
= 0x02014b50, // central directory record
43 LOCAL_MAGIC
= 0x04034b50, // local header
44 END_MAGIC
= 0x06054b50, // end of central directory record
45 SUMS_MAGIC
= 0x08074b50 // data descriptor (info-zip)
48 // unix file attributes. zip stores them in the high 16 bits of the
49 // 'external attributes' field, hence the extra zeros.
51 wxZIP_S_IFMT
= 0xF0000000,
52 wxZIP_S_IFDIR
= 0x40000000,
53 wxZIP_S_IFREG
= 0x80000000
56 // minimum sizes for the various records
64 // The number of bytes that must be written to an wxZipOutputStream before
65 // a zip entry is created. The purpose of this latency is so that
66 // OpenCompressor() can see a little data before deciding which compressor
72 // Some offsets into the local header
77 IMPLEMENT_DYNAMIC_CLASS(wxZipEntry
, wxArchiveEntry
)
78 IMPLEMENT_DYNAMIC_CLASS(wxZipClassFactory
, wxArchiveClassFactory
)
80 wxFORCE_LINK_THIS_MODULE(zipstrm
)
83 /////////////////////////////////////////////////////////////////////////////
86 // read a string of a given length
88 static wxString
ReadString(wxInputStream
& stream
, wxUint16 len
, wxMBConv
& conv
)
91 wxCharBuffer
buf(len
);
92 stream
.Read(buf
.data(), len
);
93 wxString
str(buf
, conv
);
98 wxStringBuffer
buf(str
, len
);
99 stream
.Read(buf
, len
);
106 // Decode a little endian wxUint32 number from a character array
108 static inline wxUint32
CrackUint32(const char *m
)
110 const unsigned char *n
= (const unsigned char*)m
;
111 return (n
[3] << 24) | (n
[2] << 16) | (n
[1] << 8) | n
[0];
114 // Temporarily lower the logging level in debug mode to avoid a warning
115 // from SeekI about seeking on a stream with data written back to it.
117 static wxFileOffset
QuietSeek(wxInputStream
& stream
, wxFileOffset pos
)
119 #if defined(__WXDEBUG__) && wxUSE_LOG
120 wxLogLevel level
= wxLog::GetLogLevel();
121 wxLog::SetLogLevel(wxLOG_Debug
- 1);
122 wxFileOffset result
= stream
.SeekI(pos
);
123 wxLog::SetLogLevel(level
);
126 return stream
.SeekI(pos
);
131 /////////////////////////////////////////////////////////////////////////////
137 wxZipHeader(wxInputStream
& stream
, size_t size
);
139 inline wxUint8
Read8();
140 inline wxUint16
Read16();
141 inline wxUint32
Read32();
143 const char *GetData() const { return m_data
; }
144 size_t GetSize() const { return m_size
; }
145 operator bool() const { return m_ok
; }
147 size_t Seek(size_t pos
) { m_pos
= pos
; return m_pos
; }
148 size_t Skip(size_t size
) { m_pos
+= size
; return m_pos
; }
150 wxZipHeader
& operator>>(wxUint8
& n
) { n
= Read8(); return *this; }
151 wxZipHeader
& operator>>(wxUint16
& n
) { n
= Read16(); return *this; }
152 wxZipHeader
& operator>>(wxUint32
& n
) { n
= Read32(); return *this; }
161 wxZipHeader::wxZipHeader(wxInputStream
& stream
, size_t size
)
166 wxCHECK_RET(size
<= sizeof(m_data
), _T("buffer too small"));
167 m_size
= stream
.Read(m_data
, size
).LastRead();
168 m_ok
= m_size
== size
;
171 wxUint8
wxZipHeader::Read8()
173 wxASSERT(m_pos
< m_size
);
174 return *wx_reinterpret_cast(wxUint8
*, m_data
+ m_pos
++);
177 wxUint16
wxZipHeader::Read16()
179 wxASSERT(m_pos
+ 2 <= m_size
);
180 wxUint16 n
= *wx_reinterpret_cast(wxUint16
*, m_data
+ m_pos
);
182 return wxUINT16_SWAP_ON_BE(n
);
185 wxUint32
wxZipHeader::Read32()
187 wxASSERT(m_pos
+ 4 <= m_size
);
188 wxUint32 n
= *wx_reinterpret_cast(wxUint32
*, m_data
+ m_pos
);
190 return wxUINT32_SWAP_ON_BE(n
);
194 /////////////////////////////////////////////////////////////////////////////
195 // Stored input stream
196 // Trival decompressor for files which are 'stored' in the zip file.
198 class wxStoredInputStream
: public wxFilterInputStream
201 wxStoredInputStream(wxInputStream
& stream
);
203 void Open(wxFileOffset len
) { Close(); m_len
= len
; }
204 void Close() { m_pos
= 0; m_lasterror
= wxSTREAM_NO_ERROR
; }
206 virtual char Peek() { return wxInputStream::Peek(); }
207 virtual wxFileOffset
GetLength() const { return m_len
; }
210 virtual size_t OnSysRead(void *buffer
, size_t size
);
211 virtual wxFileOffset
OnSysTell() const { return m_pos
; }
217 DECLARE_NO_COPY_CLASS(wxStoredInputStream
)
220 wxStoredInputStream::wxStoredInputStream(wxInputStream
& stream
)
221 : wxFilterInputStream(stream
),
227 size_t wxStoredInputStream::OnSysRead(void *buffer
, size_t size
)
229 size_t count
= wx_truncate_cast(size_t,
230 wxMin(size
+ wxFileOffset(0), m_len
- m_pos
+ size_t(0)));
231 count
= m_parent_i_stream
->Read(buffer
, count
).LastRead();
235 m_lasterror
= m_pos
== m_len
? wxSTREAM_EOF
: wxSTREAM_READ_ERROR
;
241 /////////////////////////////////////////////////////////////////////////////
242 // Stored output stream
243 // Trival compressor for files which are 'stored' in the zip file.
245 class wxStoredOutputStream
: public wxFilterOutputStream
248 wxStoredOutputStream(wxOutputStream
& stream
) :
249 wxFilterOutputStream(stream
), m_pos(0) { }
253 m_lasterror
= wxSTREAM_NO_ERROR
;
258 virtual size_t OnSysWrite(const void *buffer
, size_t size
);
259 virtual wxFileOffset
OnSysTell() const { return m_pos
; }
263 DECLARE_NO_COPY_CLASS(wxStoredOutputStream
)
266 size_t wxStoredOutputStream::OnSysWrite(const void *buffer
, size_t size
)
268 if (!IsOk() || !size
)
270 size_t count
= m_parent_o_stream
->Write(buffer
, size
).LastWrite();
272 m_lasterror
= wxSTREAM_WRITE_ERROR
;
278 /////////////////////////////////////////////////////////////////////////////
281 // Used to handle the unusal case of raw copying an entry of unknown
282 // length. This can only happen when the zip being copied from is being
283 // read from a non-seekable stream, and also was original written to a
284 // non-seekable stream.
286 // In this case there's no option but to decompress the stream to find
287 // it's length, but we can still write the raw compressed data to avoid the
288 // compression overhead (which is the greater one).
290 // Usage is like this:
291 // m_rawin = new wxRawInputStream(*m_parent_i_stream);
292 // m_decomp = m_rawin->Open(OpenDecompressor(m_rawin->GetTee()));
294 // The wxRawInputStream owns a wxTeeInputStream object, the role of which
295 // is something like the unix 'tee' command; it is a transparent filter, but
296 // allows the data read to be read a second time via an extra method 'GetData'.
298 // The wxRawInputStream then draws data through the tee using a decompressor
299 // then instead of returning the decompressed data, retuns the raw data
300 // from wxTeeInputStream::GetData().
302 class wxTeeInputStream
: public wxFilterInputStream
305 wxTeeInputStream(wxInputStream
& stream
);
307 size_t GetCount() const { return m_end
- m_start
; }
308 size_t GetData(char *buffer
, size_t size
);
313 wxInputStream
& Read(void *buffer
, size_t size
);
316 virtual size_t OnSysRead(void *buffer
, size_t size
);
317 virtual wxFileOffset
OnSysTell() const { return m_pos
; }
321 wxMemoryBuffer m_buf
;
325 DECLARE_NO_COPY_CLASS(wxTeeInputStream
)
328 wxTeeInputStream::wxTeeInputStream(wxInputStream
& stream
)
329 : wxFilterInputStream(stream
),
330 m_pos(0), m_buf(8192), m_start(0), m_end(0)
334 void wxTeeInputStream::Open()
336 m_pos
= m_start
= m_end
= 0;
337 m_lasterror
= wxSTREAM_NO_ERROR
;
340 bool wxTeeInputStream::Final()
342 bool final
= m_end
== m_buf
.GetDataLen();
343 m_end
= m_buf
.GetDataLen();
347 wxInputStream
& wxTeeInputStream::Read(void *buffer
, size_t size
)
349 size_t count
= wxInputStream::Read(buffer
, size
).LastRead();
350 m_end
= m_buf
.GetDataLen();
351 m_buf
.AppendData(buffer
, count
);
355 size_t wxTeeInputStream::OnSysRead(void *buffer
, size_t size
)
357 size_t count
= m_parent_i_stream
->Read(buffer
, size
).LastRead();
359 m_lasterror
= m_parent_i_stream
->GetLastError();
363 size_t wxTeeInputStream::GetData(char *buffer
, size_t size
)
366 size_t len
= m_buf
.GetDataLen();
367 len
= len
> m_wbacksize
? len
- m_wbacksize
: 0;
368 m_buf
.SetDataLen(len
);
370 wxFAIL
; // we've already returned data that's now being ungot
373 m_parent_i_stream
->Reset();
374 m_parent_i_stream
->Ungetch(m_wback
, m_wbacksize
);
381 if (size
> GetCount())
384 memcpy(buffer
, m_buf
+ m_start
, size
);
386 wxASSERT(m_start
<= m_end
);
389 if (m_start
== m_end
&& m_start
> 0 && m_buf
.GetDataLen() > 0) {
390 size_t len
= m_buf
.GetDataLen();
391 char *buf
= (char*)m_buf
.GetWriteBuf(len
);
393 memmove(buf
, buf
+ m_end
, len
);
394 m_buf
.UngetWriteBuf(len
);
401 class wxRawInputStream
: public wxFilterInputStream
404 wxRawInputStream(wxInputStream
& stream
);
405 virtual ~wxRawInputStream() { delete m_tee
; }
407 wxInputStream
* Open(wxInputStream
*decomp
);
408 wxInputStream
& GetTee() const { return *m_tee
; }
411 virtual size_t OnSysRead(void *buffer
, size_t size
);
412 virtual wxFileOffset
OnSysTell() const { return m_pos
; }
416 wxTeeInputStream
*m_tee
;
418 enum { BUFSIZE
= 8192 };
419 wxCharBuffer m_dummy
;
421 DECLARE_NO_COPY_CLASS(wxRawInputStream
)
424 wxRawInputStream::wxRawInputStream(wxInputStream
& stream
)
425 : wxFilterInputStream(stream
),
427 m_tee(new wxTeeInputStream(stream
)),
432 wxInputStream
*wxRawInputStream::Open(wxInputStream
*decomp
)
435 m_parent_i_stream
= decomp
;
437 m_lasterror
= wxSTREAM_NO_ERROR
;
445 size_t wxRawInputStream::OnSysRead(void *buffer
, size_t size
)
447 char *buf
= (char*)buffer
;
450 while (count
< size
&& IsOk())
452 while (m_parent_i_stream
->IsOk() && m_tee
->GetCount() == 0)
453 m_parent_i_stream
->Read(m_dummy
.data(), BUFSIZE
);
455 size_t n
= m_tee
->GetData(buf
+ count
, size
- count
);
458 if (n
== 0 && m_tee
->Final())
459 m_lasterror
= m_parent_i_stream
->GetLastError();
467 /////////////////////////////////////////////////////////////////////////////
468 // Zlib streams than can be reused without recreating.
470 class wxZlibOutputStream2
: public wxZlibOutputStream
473 wxZlibOutputStream2(wxOutputStream
& stream
, int level
) :
474 wxZlibOutputStream(stream
, level
, wxZLIB_NO_HEADER
) { }
476 bool Open(wxOutputStream
& stream
);
477 bool Close() { DoFlush(true); m_pos
= wxInvalidOffset
; return IsOk(); }
480 bool wxZlibOutputStream2::Open(wxOutputStream
& stream
)
482 wxCHECK(m_pos
== wxInvalidOffset
, false);
484 m_deflate
->next_out
= m_z_buffer
;
485 m_deflate
->avail_out
= m_z_size
;
487 m_lasterror
= wxSTREAM_NO_ERROR
;
488 m_parent_o_stream
= &stream
;
490 if (deflateReset(m_deflate
) != Z_OK
) {
491 wxLogError(_("can't re-initialize zlib deflate stream"));
492 m_lasterror
= wxSTREAM_WRITE_ERROR
;
499 class wxZlibInputStream2
: public wxZlibInputStream
502 wxZlibInputStream2(wxInputStream
& stream
) :
503 wxZlibInputStream(stream
, wxZLIB_NO_HEADER
) { }
505 bool Open(wxInputStream
& stream
);
508 bool wxZlibInputStream2::Open(wxInputStream
& stream
)
510 m_inflate
->avail_in
= 0;
512 m_lasterror
= wxSTREAM_NO_ERROR
;
513 m_parent_i_stream
= &stream
;
515 if (inflateReset(m_inflate
) != Z_OK
) {
516 wxLogError(_("can't re-initialize zlib inflate stream"));
517 m_lasterror
= wxSTREAM_READ_ERROR
;
525 /////////////////////////////////////////////////////////////////////////////
526 // Class to hold wxZipEntry's Extra and LocalExtra fields
531 wxZipMemory() : m_data(NULL
), m_size(0), m_capacity(0), m_ref(1) { }
533 wxZipMemory
*AddRef() { m_ref
++; return this; }
534 void Release() { if (--m_ref
== 0) delete this; }
536 char *GetData() const { return m_data
; }
537 size_t GetSize() const { return m_size
; }
538 size_t GetCapacity() const { return m_capacity
; }
540 wxZipMemory
*Unique(size_t size
);
543 ~wxZipMemory() { delete [] m_data
; }
550 wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipMemory
)
553 wxZipMemory
*wxZipMemory::Unique(size_t size
)
559 zm
= new wxZipMemory
;
564 if (zm
->m_capacity
< size
) {
565 delete [] zm
->m_data
;
566 zm
->m_data
= new char[size
];
567 zm
->m_capacity
= size
;
574 static inline wxZipMemory
*AddRef(wxZipMemory
*zm
)
581 static inline void Release(wxZipMemory
*zm
)
587 static void Copy(wxZipMemory
*& dest
, wxZipMemory
*src
)
593 static void Unique(wxZipMemory
*& zm
, size_t size
)
596 zm
= new wxZipMemory
;
598 zm
= zm
->Unique(size
);
602 /////////////////////////////////////////////////////////////////////////////
603 // Collection of weak references to entries
605 WX_DECLARE_HASH_MAP(long, wxZipEntry
*, wxIntegerHash
,
606 wxIntegerEqual
, wx__OffsetZipEntryMap
);
611 wxZipWeakLinks() : m_ref(1) { }
613 void Release(const wxZipInputStream
* WXUNUSED(x
))
614 { if (--m_ref
== 0) delete this; }
615 void Release(wxFileOffset key
)
616 { RemoveEntry(key
); if (--m_ref
== 0) delete this; }
618 wxZipWeakLinks
*AddEntry(wxZipEntry
*entry
, wxFileOffset key
);
619 void RemoveEntry(wxFileOffset key
)
620 { m_entries
.erase(wx_truncate_cast(key_type
, key
)); }
621 wxZipEntry
*GetEntry(wxFileOffset key
) const;
622 bool IsEmpty() const { return m_entries
.empty(); }
625 ~wxZipWeakLinks() { wxASSERT(IsEmpty()); }
627 typedef wx__OffsetZipEntryMap::key_type key_type
;
630 wx__OffsetZipEntryMap m_entries
;
632 wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipWeakLinks
)
635 wxZipWeakLinks
*wxZipWeakLinks::AddEntry(wxZipEntry
*entry
, wxFileOffset key
)
637 m_entries
[wx_truncate_cast(key_type
, key
)] = entry
;
642 wxZipEntry
*wxZipWeakLinks::GetEntry(wxFileOffset key
) const
644 wx__OffsetZipEntryMap::const_iterator it
=
645 m_entries
.find(wx_truncate_cast(key_type
, key
));
646 return it
!= m_entries
.end() ? it
->second
: NULL
;
650 /////////////////////////////////////////////////////////////////////////////
653 wxZipEntry::wxZipEntry(
654 const wxString
& name
/*=wxEmptyString*/,
655 const wxDateTime
& dt
/*=wxDateTime::Now()*/,
656 wxFileOffset size
/*=wxInvalidOffset*/)
658 m_SystemMadeBy(wxZIP_SYSTEM_MSDOS
),
659 m_VersionMadeBy(wxMAJOR_VERSION
* 10 + wxMINOR_VERSION
),
660 m_VersionNeeded(VERSION_NEEDED_TO_EXTRACT
),
662 m_Method(wxZIP_METHOD_DEFAULT
),
665 m_CompressedSize(wxInvalidOffset
),
667 m_Key(wxInvalidOffset
),
668 m_Offset(wxInvalidOffset
),
670 m_InternalAttributes(0),
671 m_ExternalAttributes(0),
681 wxZipEntry::~wxZipEntry()
684 m_backlink
->Release(m_Key
);
686 Release(m_LocalExtra
);
689 wxZipEntry::wxZipEntry(const wxZipEntry
& e
)
691 m_SystemMadeBy(e
.m_SystemMadeBy
),
692 m_VersionMadeBy(e
.m_VersionMadeBy
),
693 m_VersionNeeded(e
.m_VersionNeeded
),
695 m_Method(e
.m_Method
),
696 m_DateTime(e
.m_DateTime
),
698 m_CompressedSize(e
.m_CompressedSize
),
702 m_Offset(e
.m_Offset
),
703 m_Comment(e
.m_Comment
),
704 m_DiskStart(e
.m_DiskStart
),
705 m_InternalAttributes(e
.m_InternalAttributes
),
706 m_ExternalAttributes(e
.m_ExternalAttributes
),
707 m_Extra(AddRef(e
.m_Extra
)),
708 m_LocalExtra(AddRef(e
.m_LocalExtra
)),
714 wxZipEntry
& wxZipEntry::operator=(const wxZipEntry
& e
)
717 m_SystemMadeBy
= e
.m_SystemMadeBy
;
718 m_VersionMadeBy
= e
.m_VersionMadeBy
;
719 m_VersionNeeded
= e
.m_VersionNeeded
;
721 m_Method
= e
.m_Method
;
722 m_DateTime
= e
.m_DateTime
;
724 m_CompressedSize
= e
.m_CompressedSize
;
728 m_Offset
= e
.m_Offset
;
729 m_Comment
= e
.m_Comment
;
730 m_DiskStart
= e
.m_DiskStart
;
731 m_InternalAttributes
= e
.m_InternalAttributes
;
732 m_ExternalAttributes
= e
.m_ExternalAttributes
;
733 Copy(m_Extra
, e
.m_Extra
);
734 Copy(m_LocalExtra
, e
.m_LocalExtra
);
735 m_zipnotifier
= NULL
;
737 m_backlink
->Release(m_Key
);
744 wxString
wxZipEntry::GetName(wxPathFormat format
/*=wxPATH_NATIVE*/) const
746 bool isDir
= IsDir() && !m_Name
.empty();
748 // optimisations for common (and easy) cases
749 switch (wxFileName::GetFormat(format
)) {
752 wxString
name(isDir
? m_Name
+ _T("\\") : m_Name
);
753 for (size_t i
= name
.length() - 1; i
> 0; --i
)
754 if (name
[i
] == _T('/'))
760 return isDir
? m_Name
+ _T("/") : m_Name
;
769 fn
.AssignDir(m_Name
, wxPATH_UNIX
);
771 fn
.Assign(m_Name
, wxPATH_UNIX
);
773 return fn
.GetFullPath(format
);
776 // Static - Internally tars and zips use forward slashes for the path
777 // separator, absolute paths aren't allowed, and directory names have a
778 // trailing slash. This function converts a path into this internal format,
779 // but without a trailing slash for a directory.
781 wxString
wxZipEntry::GetInternalName(const wxString
& name
,
782 wxPathFormat format
/*=wxPATH_NATIVE*/,
783 bool *pIsDir
/*=NULL*/)
787 if (wxFileName::GetFormat(format
) != wxPATH_UNIX
)
788 internal
= wxFileName(name
, format
).GetFullPath(wxPATH_UNIX
);
792 bool isDir
= !internal
.empty() && internal
.Last() == '/';
796 internal
.erase(internal
.length() - 1);
798 while (!internal
.empty() && *internal
.begin() == '/')
799 internal
.erase(0, 1);
800 while (!internal
.empty() && internal
.compare(0, 2, _T("./")) == 0)
801 internal
.erase(0, 2);
802 if (internal
== _T(".") || internal
== _T(".."))
803 internal
= wxEmptyString
;
808 void wxZipEntry::SetSystemMadeBy(int system
)
810 int mode
= GetMode();
811 bool wasUnix
= IsMadeByUnix();
813 m_SystemMadeBy
= (wxUint8
)system
;
815 if (!wasUnix
&& IsMadeByUnix()) {
818 } else if (wasUnix
&& !IsMadeByUnix()) {
819 m_ExternalAttributes
&= 0xffff;
823 void wxZipEntry::SetIsDir(bool isDir
/*=true*/)
826 m_ExternalAttributes
|= wxZIP_A_SUBDIR
;
828 m_ExternalAttributes
&= ~wxZIP_A_SUBDIR
;
830 if (IsMadeByUnix()) {
831 m_ExternalAttributes
&= ~wxZIP_S_IFMT
;
833 m_ExternalAttributes
|= wxZIP_S_IFDIR
;
835 m_ExternalAttributes
|= wxZIP_S_IFREG
;
839 // Return unix style permission bits
841 int wxZipEntry::GetMode() const
843 // return unix permissions if present
845 return (m_ExternalAttributes
>> 16) & 0777;
847 // otherwise synthesize from the dos attribs
849 if (m_ExternalAttributes
& wxZIP_A_RDONLY
)
851 if (m_ExternalAttributes
& wxZIP_A_SUBDIR
)
857 // Set unix permissions
859 void wxZipEntry::SetMode(int mode
)
861 // Set dos attrib bits to be compatible
863 m_ExternalAttributes
&= ~wxZIP_A_RDONLY
;
865 m_ExternalAttributes
|= wxZIP_A_RDONLY
;
867 // set the actual unix permission bits if the system type allows
868 if (IsMadeByUnix()) {
869 m_ExternalAttributes
&= ~(0777L << 16);
870 m_ExternalAttributes
|= (mode
& 0777L) << 16;
874 const char *wxZipEntry::GetExtra() const
876 return m_Extra
? m_Extra
->GetData() : NULL
;
879 size_t wxZipEntry::GetExtraLen() const
881 return m_Extra
? m_Extra
->GetSize() : 0;
884 void wxZipEntry::SetExtra(const char *extra
, size_t len
)
886 Unique(m_Extra
, len
);
888 memcpy(m_Extra
->GetData(), extra
, len
);
891 const char *wxZipEntry::GetLocalExtra() const
893 return m_LocalExtra
? m_LocalExtra
->GetData() : NULL
;
896 size_t wxZipEntry::GetLocalExtraLen() const
898 return m_LocalExtra
? m_LocalExtra
->GetSize() : 0;
901 void wxZipEntry::SetLocalExtra(const char *extra
, size_t len
)
903 Unique(m_LocalExtra
, len
);
905 memcpy(m_LocalExtra
->GetData(), extra
, len
);
908 void wxZipEntry::SetNotifier(wxZipNotifier
& notifier
)
910 wxArchiveEntry::UnsetNotifier();
911 m_zipnotifier
= ¬ifier
;
912 m_zipnotifier
->OnEntryUpdated(*this);
915 void wxZipEntry::Notify()
918 m_zipnotifier
->OnEntryUpdated(*this);
919 else if (GetNotifier())
920 GetNotifier()->OnEntryUpdated(*this);
923 void wxZipEntry::UnsetNotifier()
925 wxArchiveEntry::UnsetNotifier();
926 m_zipnotifier
= NULL
;
929 size_t wxZipEntry::ReadLocal(wxInputStream
& stream
, wxMBConv
& conv
)
931 wxUint16 nameLen
, extraLen
;
932 wxUint32 compressedSize
, size
, crc
;
934 wxZipHeader
ds(stream
, LOCAL_SIZE
- 4);
938 ds
>> m_VersionNeeded
>> m_Flags
>> m_Method
;
939 SetDateTime(wxDateTime().SetFromDOS(ds
.Read32()));
940 ds
>> crc
>> compressedSize
>> size
>> nameLen
>> extraLen
;
942 bool sumsValid
= (m_Flags
& wxZIP_SUMS_FOLLOW
) == 0;
944 if (sumsValid
|| crc
)
946 if ((sumsValid
|| compressedSize
) || m_Method
== wxZIP_METHOD_STORE
)
947 m_CompressedSize
= compressedSize
;
948 if ((sumsValid
|| size
) || m_Method
== wxZIP_METHOD_STORE
)
951 SetName(ReadString(stream
, nameLen
, conv
), wxPATH_UNIX
);
952 if (stream
.LastRead() != nameLen
+ 0u)
955 if (extraLen
|| GetLocalExtraLen()) {
956 Unique(m_LocalExtra
, extraLen
);
958 stream
.Read(m_LocalExtra
->GetData(), extraLen
);
959 if (stream
.LastRead() != extraLen
+ 0u)
964 return LOCAL_SIZE
+ nameLen
+ extraLen
;
967 size_t wxZipEntry::WriteLocal(wxOutputStream
& stream
, wxMBConv
& conv
) const
969 wxString unixName
= GetName(wxPATH_UNIX
);
970 const wxWX2MBbuf name_buf
= conv
.cWX2MB(unixName
);
971 const char *name
= name_buf
;
972 if (!name
) name
= "";
973 wxUint16 nameLen
= wx_truncate_cast(wxUint16
, strlen(name
));
975 wxDataOutputStream
ds(stream
);
977 ds
<< m_VersionNeeded
<< m_Flags
<< m_Method
;
978 ds
.Write32(GetDateTime().GetAsDOS());
981 ds
.Write32(m_CompressedSize
!= wxInvalidOffset
?
982 wx_truncate_cast(wxUint32
, m_CompressedSize
) : 0);
983 ds
.Write32(m_Size
!= wxInvalidOffset
?
984 wx_truncate_cast(wxUint32
, m_Size
) : 0);
987 wxUint16 extraLen
= wx_truncate_cast(wxUint16
, GetLocalExtraLen());
988 ds
.Write16(extraLen
);
990 stream
.Write(name
, nameLen
);
992 stream
.Write(m_LocalExtra
->GetData(), extraLen
);
994 return LOCAL_SIZE
+ nameLen
+ extraLen
;
997 size_t wxZipEntry::ReadCentral(wxInputStream
& stream
, wxMBConv
& conv
)
999 wxUint16 nameLen
, extraLen
, commentLen
;
1001 wxZipHeader
ds(stream
, CENTRAL_SIZE
- 4);
1005 ds
>> m_VersionMadeBy
>> m_SystemMadeBy
;
1007 SetVersionNeeded(ds
.Read16());
1008 SetFlags(ds
.Read16());
1009 SetMethod(ds
.Read16());
1010 SetDateTime(wxDateTime().SetFromDOS(ds
.Read32()));
1011 SetCrc(ds
.Read32());
1012 SetCompressedSize(ds
.Read32());
1013 SetSize(ds
.Read32());
1015 ds
>> nameLen
>> extraLen
>> commentLen
1016 >> m_DiskStart
>> m_InternalAttributes
>> m_ExternalAttributes
;
1017 SetOffset(ds
.Read32());
1019 SetName(ReadString(stream
, nameLen
, conv
), wxPATH_UNIX
);
1020 if (stream
.LastRead() != nameLen
+ 0u)
1023 if (extraLen
|| GetExtraLen()) {
1024 Unique(m_Extra
, extraLen
);
1026 stream
.Read(m_Extra
->GetData(), extraLen
);
1027 if (stream
.LastRead() != extraLen
+ 0u)
1033 m_Comment
= ReadString(stream
, commentLen
, conv
);
1034 if (stream
.LastRead() != commentLen
+ 0u)
1040 return CENTRAL_SIZE
+ nameLen
+ extraLen
+ commentLen
;
1043 size_t wxZipEntry::WriteCentral(wxOutputStream
& stream
, wxMBConv
& conv
) const
1045 wxString unixName
= GetName(wxPATH_UNIX
);
1046 const wxWX2MBbuf name_buf
= conv
.cWX2MB(unixName
);
1047 const char *name
= name_buf
;
1048 if (!name
) name
= "";
1049 wxUint16 nameLen
= wx_truncate_cast(wxUint16
, strlen(name
));
1051 const wxWX2MBbuf comment_buf
= conv
.cWX2MB(m_Comment
);
1052 const char *comment
= comment_buf
;
1053 if (!comment
) comment
= "";
1054 wxUint16 commentLen
= wx_truncate_cast(wxUint16
, strlen(comment
));
1056 wxUint16 extraLen
= wx_truncate_cast(wxUint16
, GetExtraLen());
1058 wxDataOutputStream
ds(stream
);
1060 ds
<< CENTRAL_MAGIC
<< m_VersionMadeBy
<< m_SystemMadeBy
;
1062 ds
.Write16(wx_truncate_cast(wxUint16
, GetVersionNeeded()));
1063 ds
.Write16(wx_truncate_cast(wxUint16
, GetFlags()));
1064 ds
.Write16(wx_truncate_cast(wxUint16
, GetMethod()));
1065 ds
.Write32(GetDateTime().GetAsDOS());
1066 ds
.Write32(GetCrc());
1067 ds
.Write32(wx_truncate_cast(wxUint32
, GetCompressedSize()));
1068 ds
.Write32(wx_truncate_cast(wxUint32
, GetSize()));
1069 ds
.Write16(nameLen
);
1070 ds
.Write16(extraLen
);
1072 ds
<< commentLen
<< m_DiskStart
<< m_InternalAttributes
1073 << m_ExternalAttributes
<< wx_truncate_cast(wxUint32
, GetOffset());
1075 stream
.Write(name
, nameLen
);
1077 stream
.Write(GetExtra(), extraLen
);
1078 stream
.Write(comment
, commentLen
);
1080 return CENTRAL_SIZE
+ nameLen
+ extraLen
+ commentLen
;
1083 // Info-zip prefixes this record with a signature, but pkzip doesn't. So if
1084 // the 1st value is the signature then it is probably an info-zip record,
1085 // though there is a small chance that it is in fact a pkzip record which
1086 // happens to have the signature as it's CRC.
1088 size_t wxZipEntry::ReadDescriptor(wxInputStream
& stream
)
1090 wxZipHeader
ds(stream
, SUMS_SIZE
);
1094 m_Crc
= ds
.Read32();
1095 m_CompressedSize
= ds
.Read32();
1096 m_Size
= ds
.Read32();
1098 // if 1st value is the signature then this is probably an info-zip record
1099 if (m_Crc
== SUMS_MAGIC
)
1101 wxZipHeader
buf(stream
, 8);
1102 wxUint32 u1
= buf
.GetSize() >= 4 ? buf
.Read32() : (wxUint32
)LOCAL_MAGIC
;
1103 wxUint32 u2
= buf
.GetSize() == 8 ? buf
.Read32() : 0;
1105 // look for the signature of the following record to decide which
1106 if ((u1
== LOCAL_MAGIC
|| u1
== CENTRAL_MAGIC
) &&
1107 (u2
!= LOCAL_MAGIC
&& u2
!= CENTRAL_MAGIC
))
1109 // it's a pkzip style record after all!
1110 if (buf
.GetSize() > 0)
1111 stream
.Ungetch(buf
.GetData(), buf
.GetSize());
1115 // it's an info-zip record as expected
1116 if (buf
.GetSize() > 4)
1117 stream
.Ungetch(buf
.GetData() + 4, buf
.GetSize() - 4);
1118 m_Crc
= wx_truncate_cast(wxUint32
, m_CompressedSize
);
1119 m_CompressedSize
= m_Size
;
1121 return SUMS_SIZE
+ 4;
1128 size_t wxZipEntry::WriteDescriptor(wxOutputStream
& stream
, wxUint32 crc
,
1129 wxFileOffset compressedSize
, wxFileOffset size
)
1132 m_CompressedSize
= compressedSize
;
1135 wxDataOutputStream
ds(stream
);
1138 ds
.Write32(wx_truncate_cast(wxUint32
, compressedSize
));
1139 ds
.Write32(wx_truncate_cast(wxUint32
, size
));
1145 /////////////////////////////////////////////////////////////////////////////
1146 // wxZipEndRec - holds the end of central directory record
1153 int GetDiskNumber() const { return m_DiskNumber
; }
1154 int GetStartDisk() const { return m_StartDisk
; }
1155 int GetEntriesHere() const { return m_EntriesHere
; }
1156 int GetTotalEntries() const { return m_TotalEntries
; }
1157 wxFileOffset
GetSize() const { return m_Size
; }
1158 wxFileOffset
GetOffset() const { return m_Offset
; }
1159 wxString
GetComment() const { return m_Comment
; }
1161 void SetDiskNumber(int num
)
1162 { m_DiskNumber
= wx_truncate_cast(wxUint16
, num
); }
1163 void SetStartDisk(int num
)
1164 { m_StartDisk
= wx_truncate_cast(wxUint16
, num
); }
1165 void SetEntriesHere(int num
)
1166 { m_EntriesHere
= wx_truncate_cast(wxUint16
, num
); }
1167 void SetTotalEntries(int num
)
1168 { m_TotalEntries
= wx_truncate_cast(wxUint16
, num
); }
1169 void SetSize(wxFileOffset size
)
1170 { m_Size
= wx_truncate_cast(wxUint32
, size
); }
1171 void SetOffset(wxFileOffset offset
)
1172 { m_Offset
= wx_truncate_cast(wxUint32
, offset
); }
1173 void SetComment(const wxString
& comment
)
1174 { m_Comment
= comment
; }
1176 bool Read(wxInputStream
& stream
, wxMBConv
& conv
);
1177 bool Write(wxOutputStream
& stream
, wxMBConv
& conv
) const;
1180 wxUint16 m_DiskNumber
;
1181 wxUint16 m_StartDisk
;
1182 wxUint16 m_EntriesHere
;
1183 wxUint16 m_TotalEntries
;
1189 wxZipEndRec::wxZipEndRec()
1199 bool wxZipEndRec::Write(wxOutputStream
& stream
, wxMBConv
& conv
) const
1201 const wxWX2MBbuf comment_buf
= conv
.cWX2MB(m_Comment
);
1202 const char *comment
= comment_buf
;
1203 if (!comment
) comment
= "";
1204 wxUint16 commentLen
= (wxUint16
)strlen(comment
);
1206 wxDataOutputStream
ds(stream
);
1208 ds
<< END_MAGIC
<< m_DiskNumber
<< m_StartDisk
<< m_EntriesHere
1209 << m_TotalEntries
<< m_Size
<< m_Offset
<< commentLen
;
1211 stream
.Write(comment
, commentLen
);
1213 return stream
.IsOk();
1216 bool wxZipEndRec::Read(wxInputStream
& stream
, wxMBConv
& conv
)
1218 wxZipHeader
ds(stream
, END_SIZE
- 4);
1222 wxUint16 commentLen
;
1224 ds
>> m_DiskNumber
>> m_StartDisk
>> m_EntriesHere
1225 >> m_TotalEntries
>> m_Size
>> m_Offset
>> commentLen
;
1228 m_Comment
= ReadString(stream
, commentLen
, conv
);
1229 if (stream
.LastRead() != commentLen
+ 0u)
1233 if (m_DiskNumber
!= 0 || m_StartDisk
!= 0 ||
1234 m_EntriesHere
!= m_TotalEntries
)
1235 wxLogWarning(_("assuming this is a multi-part zip concatenated"));
1241 /////////////////////////////////////////////////////////////////////////////
1242 // A weak link from an input stream to an output stream
1244 class wxZipStreamLink
1247 wxZipStreamLink(wxZipOutputStream
*stream
) : m_ref(1), m_stream(stream
) { }
1249 wxZipStreamLink
*AddRef() { m_ref
++; return this; }
1250 wxZipOutputStream
*GetOutputStream() const { return m_stream
; }
1252 void Release(class wxZipInputStream
*WXUNUSED(s
))
1253 { if (--m_ref
== 0) delete this; }
1254 void Release(class wxZipOutputStream
*WXUNUSED(s
))
1255 { m_stream
= NULL
; if (--m_ref
== 0) delete this; }
1258 ~wxZipStreamLink() { }
1261 wxZipOutputStream
*m_stream
;
1263 wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipStreamLink
)
1267 /////////////////////////////////////////////////////////////////////////////
1270 // leave the default wxZipEntryPtr free for users
1271 wxDECLARE_SCOPED_PTR(wxZipEntry
, wx__ZipEntryPtr
)
1272 wxDEFINE_SCOPED_PTR (wxZipEntry
, wx__ZipEntryPtr
)
1276 wxZipInputStream::wxZipInputStream(wxInputStream
& stream
,
1277 wxMBConv
& conv
/*=wxConvLocal*/)
1278 : wxArchiveInputStream(stream
, conv
)
1283 #if 1 //WXWIN_COMPATIBILITY_2_6
1285 // Part of the compatibility constructor, which has been made inline to
1286 // avoid a problem with it not being exported by mingw 3.2.3
1288 void wxZipInputStream::Init(const wxString
& file
)
1290 // no error messages
1293 m_allowSeeking
= true;
1294 m_ffile
= wx_static_cast(wxFFileInputStream
*, m_parent_i_stream
);
1295 wx__ZipEntryPtr entry
;
1297 if (m_ffile
->Ok()) {
1299 entry
.reset(GetNextEntry());
1301 while (entry
.get() != NULL
&& entry
->GetInternalName() != file
);
1304 if (entry
.get() == NULL
)
1305 m_lasterror
= wxSTREAM_READ_ERROR
;
1308 wxInputStream
& wxZipInputStream::OpenFile(const wxString
& archive
)
1311 return *new wxFFileInputStream(archive
);
1314 #endif // WXWIN_COMPATIBILITY_2_6
1316 void wxZipInputStream::Init()
1318 m_store
= new wxStoredInputStream(*m_parent_i_stream
);
1324 m_parentSeekable
= false;
1325 m_weaklinks
= new wxZipWeakLinks
;
1326 m_streamlink
= NULL
;
1327 m_offsetAdjustment
= 0;
1328 m_position
= wxInvalidOffset
;
1331 m_lasterror
= m_parent_i_stream
->GetLastError();
1333 #if 1 //WXWIN_COMPATIBILITY_2_6
1334 m_allowSeeking
= false;
1338 wxZipInputStream::~wxZipInputStream()
1340 CloseDecompressor(m_decomp
);
1347 m_weaklinks
->Release(this);
1350 m_streamlink
->Release(this);
1353 wxString
wxZipInputStream::GetComment()
1355 if (m_position
== wxInvalidOffset
)
1356 if (!LoadEndRecord())
1357 return wxEmptyString
;
1359 if (!m_parentSeekable
&& Eof() && m_signature
) {
1360 m_lasterror
= wxSTREAM_NO_ERROR
;
1361 m_lasterror
= ReadLocal(true);
1367 int wxZipInputStream::GetTotalEntries()
1369 if (m_position
== wxInvalidOffset
)
1371 return m_TotalEntries
;
1374 wxZipStreamLink
*wxZipInputStream::MakeLink(wxZipOutputStream
*out
)
1376 wxZipStreamLink
*link
= NULL
;
1378 if (!m_parentSeekable
&& (IsOpened() || !Eof())) {
1379 link
= new wxZipStreamLink(out
);
1381 m_streamlink
->Release(this);
1382 m_streamlink
= link
->AddRef();
1388 bool wxZipInputStream::LoadEndRecord()
1390 wxCHECK(m_position
== wxInvalidOffset
, false);
1396 // First find the end-of-central-directory record.
1397 if (!FindEndRecord()) {
1398 // failed, so either this is a non-seekable stream (ok), or not a zip
1399 if (m_parentSeekable
) {
1400 m_lasterror
= wxSTREAM_READ_ERROR
;
1401 wxLogError(_("invalid zip file"));
1406 wxFileOffset pos
= m_parent_i_stream
->TellI();
1407 if (pos
!= wxInvalidOffset
)
1408 m_offsetAdjustment
= m_position
= pos
;
1415 // Read in the end record
1416 wxFileOffset endPos
= m_parent_i_stream
->TellI() - 4;
1417 if (!endrec
.Read(*m_parent_i_stream
, GetConv()))
1420 m_TotalEntries
= endrec
.GetTotalEntries();
1421 m_Comment
= endrec
.GetComment();
1423 // Now find the central-directory. we have the file offset of
1424 // the CD, so look there first.
1425 if (m_parent_i_stream
->SeekI(endrec
.GetOffset()) != wxInvalidOffset
&&
1426 ReadSignature() == CENTRAL_MAGIC
) {
1427 m_signature
= CENTRAL_MAGIC
;
1428 m_position
= endrec
.GetOffset();
1429 m_offsetAdjustment
= 0;
1433 // If it's not there, then it could be that the zip has been appended
1434 // to a self extractor, so take the CD size (also in endrec), subtract
1435 // it from the file offset of the end-central-directory and look there.
1436 if (m_parent_i_stream
->SeekI(endPos
- endrec
.GetSize())
1437 != wxInvalidOffset
&& ReadSignature() == CENTRAL_MAGIC
) {
1438 m_signature
= CENTRAL_MAGIC
;
1439 m_position
= endPos
- endrec
.GetSize();
1440 m_offsetAdjustment
= m_position
- endrec
.GetOffset();
1444 wxLogError(_("can't find central directory in zip"));
1445 m_lasterror
= wxSTREAM_READ_ERROR
;
1449 // Find the end-of-central-directory record.
1450 // If found the stream will be positioned just past the 4 signature bytes.
1452 bool wxZipInputStream::FindEndRecord()
1454 if (!m_parent_i_stream
->IsSeekable())
1457 // usually it's 22 bytes in size and the last thing in the file
1460 if (m_parent_i_stream
->SeekI(-END_SIZE
, wxFromEnd
) == wxInvalidOffset
)
1464 m_parentSeekable
= true;
1467 if (m_parent_i_stream
->Read(magic
, 4).LastRead() != 4)
1469 if ((m_signature
= CrackUint32(magic
)) == END_MAGIC
)
1472 // unfortunately, the record has a comment field that can be up to 65535
1473 // bytes in length, so if the signature not found then search backwards.
1474 wxFileOffset pos
= m_parent_i_stream
->TellI();
1475 const int BUFSIZE
= 1024;
1476 wxCharBuffer
buf(BUFSIZE
);
1478 memcpy(buf
.data(), magic
, 3);
1479 wxFileOffset minpos
= wxMax(pos
- 65535L, 0);
1481 while (pos
> minpos
) {
1482 size_t len
= wx_truncate_cast(size_t,
1483 pos
- wxMax(pos
- (BUFSIZE
- 3), minpos
));
1484 memcpy(buf
.data() + len
, buf
, 3);
1487 if (m_parent_i_stream
->SeekI(pos
, wxFromStart
) == wxInvalidOffset
||
1488 m_parent_i_stream
->Read(buf
.data(), len
).LastRead() != len
)
1491 char *p
= buf
.data() + len
;
1493 while (p
-- > buf
.data()) {
1494 if ((m_signature
= CrackUint32(p
)) == END_MAGIC
) {
1495 size_t remainder
= buf
.data() + len
- p
;
1497 m_parent_i_stream
->Ungetch(p
+ 4, remainder
- 4);
1506 wxZipEntry
*wxZipInputStream::GetNextEntry()
1508 if (m_position
== wxInvalidOffset
)
1509 if (!LoadEndRecord())
1512 m_lasterror
= m_parentSeekable
? ReadCentral() : ReadLocal();
1516 wx__ZipEntryPtr
entry(new wxZipEntry(m_entry
));
1517 entry
->m_backlink
= m_weaklinks
->AddEntry(entry
.get(), entry
->GetKey());
1518 return entry
.release();
1521 wxStreamError
wxZipInputStream::ReadCentral()
1526 if (m_signature
== END_MAGIC
)
1527 return wxSTREAM_EOF
;
1529 if (m_signature
!= CENTRAL_MAGIC
) {
1530 wxLogError(_("error reading zip central directory"));
1531 return wxSTREAM_READ_ERROR
;
1534 if (QuietSeek(*m_parent_i_stream
, m_position
+ 4) == wxInvalidOffset
)
1535 return wxSTREAM_READ_ERROR
;
1537 size_t size
= m_entry
.ReadCentral(*m_parent_i_stream
, GetConv());
1540 return wxSTREAM_READ_ERROR
;
1544 m_signature
= ReadSignature();
1546 if (m_offsetAdjustment
)
1547 m_entry
.SetOffset(m_entry
.GetOffset() + m_offsetAdjustment
);
1548 m_entry
.SetKey(m_entry
.GetOffset());
1550 return wxSTREAM_NO_ERROR
;
1553 wxStreamError
wxZipInputStream::ReadLocal(bool readEndRec
/*=false*/)
1559 m_signature
= ReadSignature();
1561 if (m_signature
== CENTRAL_MAGIC
|| m_signature
== END_MAGIC
) {
1562 if (m_streamlink
&& !m_streamlink
->GetOutputStream()) {
1563 m_streamlink
->Release(this);
1564 m_streamlink
= NULL
;
1568 while (m_signature
== CENTRAL_MAGIC
) {
1569 if (m_weaklinks
->IsEmpty() && m_streamlink
== NULL
)
1570 return wxSTREAM_EOF
;
1572 size_t size
= m_entry
.ReadCentral(*m_parent_i_stream
, GetConv());
1576 return wxSTREAM_READ_ERROR
;
1578 wxZipEntry
*entry
= m_weaklinks
->GetEntry(m_entry
.GetOffset());
1580 entry
->SetSystemMadeBy(m_entry
.GetSystemMadeBy());
1581 entry
->SetVersionMadeBy(m_entry
.GetVersionMadeBy());
1582 entry
->SetComment(m_entry
.GetComment());
1583 entry
->SetDiskStart(m_entry
.GetDiskStart());
1584 entry
->SetInternalAttributes(m_entry
.GetInternalAttributes());
1585 entry
->SetExternalAttributes(m_entry
.GetExternalAttributes());
1586 Copy(entry
->m_Extra
, m_entry
.m_Extra
);
1588 m_weaklinks
->RemoveEntry(entry
->GetOffset());
1591 m_signature
= ReadSignature();
1594 if (m_signature
== END_MAGIC
) {
1595 if (readEndRec
|| m_streamlink
) {
1597 endrec
.Read(*m_parent_i_stream
, GetConv());
1598 m_Comment
= endrec
.GetComment();
1601 m_streamlink
->GetOutputStream()->SetComment(endrec
.GetComment());
1602 m_streamlink
->Release(this);
1603 m_streamlink
= NULL
;
1606 return wxSTREAM_EOF
;
1609 if (m_signature
!= LOCAL_MAGIC
) {
1610 wxLogError(_("error reading zip local header"));
1611 return wxSTREAM_READ_ERROR
;
1614 m_headerSize
= m_entry
.ReadLocal(*m_parent_i_stream
, GetConv());
1616 m_entry
.SetOffset(m_position
);
1617 m_entry
.SetKey(m_position
);
1619 if (!m_headerSize
) {
1620 return wxSTREAM_READ_ERROR
;
1623 return wxSTREAM_NO_ERROR
;
1627 wxUint32
wxZipInputStream::ReadSignature()
1630 m_parent_i_stream
->Read(magic
, 4);
1631 return m_parent_i_stream
->LastRead() == 4 ? CrackUint32(magic
) : 0;
1634 bool wxZipInputStream::OpenEntry(wxArchiveEntry
& entry
)
1636 wxZipEntry
*zipEntry
= wxStaticCast(&entry
, wxZipEntry
);
1637 return zipEntry
? OpenEntry(*zipEntry
) : false;
1642 bool wxZipInputStream::DoOpen(wxZipEntry
*entry
, bool raw
)
1644 if (m_position
== wxInvalidOffset
)
1645 if (!LoadEndRecord())
1647 if (m_lasterror
== wxSTREAM_READ_ERROR
)
1655 if (AfterHeader() && entry
->GetKey() == m_entry
.GetOffset())
1657 // can only open the current entry on a non-seekable stream
1658 wxCHECK(m_parentSeekable
, false);
1661 m_lasterror
= wxSTREAM_READ_ERROR
;
1666 if (m_parentSeekable
) {
1667 if (QuietSeek(*m_parent_i_stream
, m_entry
.GetOffset())
1670 if (ReadSignature() != LOCAL_MAGIC
) {
1671 wxLogError(_("bad zipfile offset to entry"));
1676 if (m_parentSeekable
|| AtHeader()) {
1677 m_headerSize
= m_entry
.ReadLocal(*m_parent_i_stream
, GetConv());
1678 if (m_headerSize
&& m_parentSeekable
) {
1679 wxZipEntry
*ref
= m_weaklinks
->GetEntry(m_entry
.GetKey());
1681 Copy(ref
->m_LocalExtra
, m_entry
.m_LocalExtra
);
1683 m_weaklinks
->RemoveEntry(ref
->GetKey());
1685 if (entry
&& entry
!= ref
) {
1686 Copy(entry
->m_LocalExtra
, m_entry
.m_LocalExtra
);
1693 m_lasterror
= wxSTREAM_NO_ERROR
;
1697 bool wxZipInputStream::OpenDecompressor(bool raw
/*=false*/)
1699 wxASSERT(AfterHeader());
1701 wxFileOffset compressedSize
= m_entry
.GetCompressedSize();
1707 if (compressedSize
!= wxInvalidOffset
) {
1708 m_store
->Open(compressedSize
);
1712 m_rawin
= new wxRawInputStream(*m_parent_i_stream
);
1713 m_decomp
= m_rawin
->Open(OpenDecompressor(m_rawin
->GetTee()));
1716 if (compressedSize
!= wxInvalidOffset
&&
1717 (m_entry
.GetMethod() != wxZIP_METHOD_DEFLATE
||
1718 wxZlibInputStream::CanHandleGZip())) {
1719 m_store
->Open(compressedSize
);
1720 m_decomp
= OpenDecompressor(*m_store
);
1722 m_decomp
= OpenDecompressor(*m_parent_i_stream
);
1726 m_crcAccumulator
= crc32(0, Z_NULL
, 0);
1727 m_lasterror
= m_decomp
? m_decomp
->GetLastError() : wxSTREAM_READ_ERROR
;
1731 // Can be overriden to add support for additional decompression methods
1733 wxInputStream
*wxZipInputStream::OpenDecompressor(wxInputStream
& stream
)
1735 switch (m_entry
.GetMethod()) {
1736 case wxZIP_METHOD_STORE
:
1737 if (m_entry
.GetSize() == wxInvalidOffset
) {
1738 wxLogError(_("stored file length not in Zip header"));
1741 m_store
->Open(m_entry
.GetSize());
1744 case wxZIP_METHOD_DEFLATE
:
1746 m_inflate
= new wxZlibInputStream2(stream
);
1748 m_inflate
->Open(stream
);
1752 wxLogError(_("unsupported Zip compression method"));
1758 bool wxZipInputStream::CloseDecompressor(wxInputStream
*decomp
)
1760 if (decomp
&& decomp
== m_rawin
)
1761 return CloseDecompressor(m_rawin
->GetFilterInputStream());
1762 if (decomp
!= m_store
&& decomp
!= m_inflate
)
1767 // Closes the current entry and positions the underlying stream at the start
1768 // of the next entry
1770 bool wxZipInputStream::CloseEntry()
1774 if (m_lasterror
== wxSTREAM_READ_ERROR
)
1777 if (!m_parentSeekable
) {
1778 if (!IsOpened() && !OpenDecompressor(true))
1781 const int BUFSIZE
= 8192;
1782 wxCharBuffer
buf(BUFSIZE
);
1784 Read(buf
.data(), BUFSIZE
);
1786 m_position
+= m_headerSize
+ m_entry
.GetCompressedSize();
1789 if (m_lasterror
== wxSTREAM_EOF
)
1790 m_lasterror
= wxSTREAM_NO_ERROR
;
1792 CloseDecompressor(m_decomp
);
1794 m_entry
= wxZipEntry();
1801 size_t wxZipInputStream::OnSysRead(void *buffer
, size_t size
)
1804 if ((AtHeader() && !DoOpen()) || !OpenDecompressor())
1805 m_lasterror
= wxSTREAM_READ_ERROR
;
1806 if (!IsOk() || !size
)
1809 size_t count
= m_decomp
->Read(buffer
, size
).LastRead();
1811 m_crcAccumulator
= crc32(m_crcAccumulator
, (Byte
*)buffer
, count
);
1813 m_lasterror
= m_decomp
->GetLastError();
1816 if ((m_entry
.GetFlags() & wxZIP_SUMS_FOLLOW
) != 0) {
1817 m_headerSize
+= m_entry
.ReadDescriptor(*m_parent_i_stream
);
1818 wxZipEntry
*entry
= m_weaklinks
->GetEntry(m_entry
.GetKey());
1821 entry
->SetCrc(m_entry
.GetCrc());
1822 entry
->SetCompressedSize(m_entry
.GetCompressedSize());
1823 entry
->SetSize(m_entry
.GetSize());
1829 m_lasterror
= wxSTREAM_READ_ERROR
;
1831 if (m_entry
.GetSize() != TellI())
1832 wxLogError(_("reading zip stream (entry %s): bad length"),
1833 m_entry
.GetName().c_str());
1834 else if (m_crcAccumulator
!= m_entry
.GetCrc())
1835 wxLogError(_("reading zip stream (entry %s): bad crc"),
1836 m_entry
.GetName().c_str());
1838 m_lasterror
= wxSTREAM_EOF
;
1845 #if 1 //WXWIN_COMPATIBILITY_2_6
1847 // Borrowed from VS's zip stream (c) 1999 Vaclav Slavik
1849 wxFileOffset
wxZipInputStream::OnSysSeek(wxFileOffset seek
, wxSeekMode mode
)
1851 // seeking works when the stream is created with the compatibility
1853 if (!m_allowSeeking
)
1854 return wxInvalidOffset
;
1856 if ((AtHeader() && !DoOpen()) || !OpenDecompressor())
1857 m_lasterror
= wxSTREAM_READ_ERROR
;
1859 return wxInvalidOffset
;
1861 // NB: since ZIP files don't natively support seeking, we have to
1862 // implement a brute force workaround -- reading all the data
1863 // between current and the new position (or between beginning of
1864 // the file and new position...)
1866 wxFileOffset nextpos
;
1867 wxFileOffset pos
= TellI();
1871 case wxFromCurrent
: nextpos
= seek
+ pos
; break;
1872 case wxFromStart
: nextpos
= seek
; break;
1873 case wxFromEnd
: nextpos
= GetLength() + seek
; break;
1874 default : nextpos
= pos
; break; /* just to fool compiler, never happens */
1877 wxFileOffset toskip
wxDUMMY_INITIALIZE(0);
1878 if ( nextpos
>= pos
)
1880 toskip
= nextpos
- pos
;
1884 wxZipEntry
current(m_entry
);
1885 if (!OpenEntry(current
))
1887 m_lasterror
= wxSTREAM_READ_ERROR
;
1895 const int BUFSIZE
= 4096;
1897 char buffer
[BUFSIZE
];
1898 while ( toskip
> 0 )
1900 sz
= wx_truncate_cast(size_t, wxMin(toskip
, BUFSIZE
));
1910 #endif // WXWIN_COMPATIBILITY_2_6
1913 /////////////////////////////////////////////////////////////////////////////
1916 #include "wx/listimpl.cpp"
1917 WX_DEFINE_LIST(wx__ZipEntryList
)
1919 wxZipOutputStream::wxZipOutputStream(wxOutputStream
& stream
,
1921 wxMBConv
& conv
/*=wxConvLocal*/)
1922 : wxArchiveOutputStream(stream
, conv
),
1923 m_store(new wxStoredOutputStream(stream
)),
1926 m_initialData(new char[OUTPUT_LATENCY
]),
1935 m_offsetAdjustment(wxInvalidOffset
)
1939 wxZipOutputStream::~wxZipOutputStream()
1942 WX_CLEAR_LIST(wx__ZipEntryList
, m_entries
);
1946 delete [] m_initialData
;
1948 m_backlink
->Release(this);
1951 bool wxZipOutputStream::PutNextEntry(
1952 const wxString
& name
,
1953 const wxDateTime
& dt
/*=wxDateTime::Now()*/,
1954 wxFileOffset size
/*=wxInvalidOffset*/)
1956 return PutNextEntry(new wxZipEntry(name
, dt
, size
));
1959 bool wxZipOutputStream::PutNextDirEntry(
1960 const wxString
& name
,
1961 const wxDateTime
& dt
/*=wxDateTime::Now()*/)
1963 wxZipEntry
*entry
= new wxZipEntry(name
, dt
);
1965 return PutNextEntry(entry
);
1968 bool wxZipOutputStream::CopyEntry(wxZipEntry
*entry
,
1969 wxZipInputStream
& inputStream
)
1971 wx__ZipEntryPtr
e(entry
);
1974 inputStream
.DoOpen(e
.get(), true) &&
1975 DoCreate(e
.release(), true) &&
1976 Write(inputStream
).IsOk() && inputStream
.Eof();
1979 bool wxZipOutputStream::PutNextEntry(wxArchiveEntry
*entry
)
1981 wxZipEntry
*zipEntry
= wxStaticCast(entry
, wxZipEntry
);
1984 return PutNextEntry(zipEntry
);
1987 bool wxZipOutputStream::CopyEntry(wxArchiveEntry
*entry
,
1988 wxArchiveInputStream
& stream
)
1990 wxZipEntry
*zipEntry
= wxStaticCast(entry
, wxZipEntry
);
1992 if (!zipEntry
|| !stream
.OpenEntry(*zipEntry
)) {
1997 return CopyEntry(zipEntry
, wx_static_cast(wxZipInputStream
&, stream
));
2000 bool wxZipOutputStream::CopyArchiveMetaData(wxZipInputStream
& inputStream
)
2002 m_Comment
= inputStream
.GetComment();
2004 m_backlink
->Release(this);
2005 m_backlink
= inputStream
.MakeLink(this);
2009 bool wxZipOutputStream::CopyArchiveMetaData(wxArchiveInputStream
& stream
)
2011 return CopyArchiveMetaData(wx_static_cast(wxZipInputStream
&, stream
));
2014 void wxZipOutputStream::SetLevel(int level
)
2016 if (level
!= m_level
) {
2017 if (m_comp
!= m_deflate
)
2024 bool wxZipOutputStream::DoCreate(wxZipEntry
*entry
, bool raw
/*=false*/)
2032 // write the signature bytes right away
2033 wxDataOutputStream
ds(*m_parent_o_stream
);
2036 // and if this is the first entry test for seekability
2037 if (m_headerOffset
== 0 && m_parent_o_stream
->IsSeekable()) {
2039 bool logging
= wxLog::IsEnabled();
2042 wxFileOffset here
= m_parent_o_stream
->TellO();
2044 if (here
!= wxInvalidOffset
&& here
>= 4) {
2045 if (m_parent_o_stream
->SeekO(here
- 4) == here
- 4) {
2046 m_offsetAdjustment
= here
- 4;
2048 wxLog::EnableLogging(logging
);
2050 m_parent_o_stream
->SeekO(here
);
2055 m_pending
->SetOffset(m_headerOffset
);
2057 m_crcAccumulator
= crc32(0, Z_NULL
, 0);
2062 m_lasterror
= wxSTREAM_NO_ERROR
;
2066 // Can be overriden to add support for additional compression methods
2068 wxOutputStream
*wxZipOutputStream::OpenCompressor(
2069 wxOutputStream
& stream
,
2071 const Buffer bufs
[])
2073 if (entry
.GetMethod() == wxZIP_METHOD_DEFAULT
) {
2075 && (IsParentSeekable()
2076 || entry
.GetCompressedSize() != wxInvalidOffset
2077 || entry
.GetSize() != wxInvalidOffset
)) {
2078 entry
.SetMethod(wxZIP_METHOD_STORE
);
2081 for (int i
= 0; bufs
[i
].m_data
; ++i
)
2082 size
+= bufs
[i
].m_size
;
2083 entry
.SetMethod(size
<= 6 ?
2084 wxZIP_METHOD_STORE
: wxZIP_METHOD_DEFLATE
);
2088 switch (entry
.GetMethod()) {
2089 case wxZIP_METHOD_STORE
:
2090 if (entry
.GetCompressedSize() == wxInvalidOffset
)
2091 entry
.SetCompressedSize(entry
.GetSize());
2094 case wxZIP_METHOD_DEFLATE
:
2096 int defbits
= wxZIP_DEFLATE_NORMAL
;
2097 switch (GetLevel()) {
2099 defbits
= wxZIP_DEFLATE_SUPERFAST
;
2101 case 2: case 3: case 4:
2102 defbits
= wxZIP_DEFLATE_FAST
;
2105 defbits
= wxZIP_DEFLATE_EXTRA
;
2108 entry
.SetFlags((entry
.GetFlags() & ~wxZIP_DEFLATE_MASK
) |
2109 defbits
| wxZIP_SUMS_FOLLOW
);
2112 m_deflate
= new wxZlibOutputStream2(stream
, GetLevel());
2114 m_deflate
->Open(stream
);
2120 wxLogError(_("unsupported Zip compression method"));
2126 bool wxZipOutputStream::CloseCompressor(wxOutputStream
*comp
)
2128 if (comp
== m_deflate
)
2130 else if (comp
!= m_store
)
2135 // This is called when OUPUT_LATENCY bytes has been written to the
2136 // wxZipOutputStream to actually create the zip entry.
2138 void wxZipOutputStream::CreatePendingEntry(const void *buffer
, size_t size
)
2140 wxASSERT(IsOk() && m_pending
&& !m_comp
);
2141 wx__ZipEntryPtr
spPending(m_pending
);
2145 { m_initialData
, m_initialSize
},
2146 { (const char*)buffer
, size
},
2153 m_comp
= OpenCompressor(*m_store
, *spPending
,
2154 m_initialSize
? bufs
: bufs
+ 1);
2156 if (IsParentSeekable()
2157 || (spPending
->m_Crc
2158 && spPending
->m_CompressedSize
!= wxInvalidOffset
2159 && spPending
->m_Size
!= wxInvalidOffset
))
2160 spPending
->m_Flags
&= ~wxZIP_SUMS_FOLLOW
;
2162 if (spPending
->m_CompressedSize
!= wxInvalidOffset
)
2163 spPending
->m_Flags
|= wxZIP_SUMS_FOLLOW
;
2165 m_headerSize
= spPending
->WriteLocal(*m_parent_o_stream
, GetConv());
2166 m_lasterror
= m_parent_o_stream
->GetLastError();
2169 m_entries
.push_back(spPending
.release());
2170 OnSysWrite(m_initialData
, m_initialSize
);
2176 // This is called to write out the zip entry when Close has been called
2177 // before OUTPUT_LATENCY bytes has been written to the wxZipOutputStream.
2179 void wxZipOutputStream::CreatePendingEntry()
2181 wxASSERT(IsOk() && m_pending
&& !m_comp
);
2182 wx__ZipEntryPtr
spPending(m_pending
);
2184 m_lasterror
= wxSTREAM_WRITE_ERROR
;
2187 // Initially compresses the data to memory, then fall back to 'store'
2188 // if the compressor makes the data larger rather than smaller.
2189 wxMemoryOutputStream mem
;
2190 Buffer bufs
[] = { { m_initialData
, m_initialSize
}, { NULL
, 0 } };
2191 wxOutputStream
*comp
= OpenCompressor(mem
, *spPending
, bufs
);
2195 if (comp
!= m_store
) {
2196 bool ok
= comp
->Write(m_initialData
, m_initialSize
).IsOk();
2197 CloseCompressor(comp
);
2202 m_entrySize
= m_initialSize
;
2203 m_crcAccumulator
= crc32(0, (Byte
*)m_initialData
, m_initialSize
);
2205 if (mem
.GetSize() > 0 && mem
.GetSize() < m_initialSize
) {
2206 m_initialSize
= mem
.GetSize();
2207 mem
.CopyTo(m_initialData
, m_initialSize
);
2209 spPending
->SetMethod(wxZIP_METHOD_STORE
);
2212 spPending
->SetSize(m_entrySize
);
2213 spPending
->SetCrc(m_crcAccumulator
);
2214 spPending
->SetCompressedSize(m_initialSize
);
2217 spPending
->m_Flags
&= ~wxZIP_SUMS_FOLLOW
;
2218 m_headerSize
= spPending
->WriteLocal(*m_parent_o_stream
, GetConv());
2220 if (m_parent_o_stream
->IsOk()) {
2221 m_entries
.push_back(spPending
.release());
2223 m_store
->Write(m_initialData
, m_initialSize
);
2227 m_lasterror
= m_parent_o_stream
->GetLastError();
2230 // Write the 'central directory' and the 'end-central-directory' records.
2232 bool wxZipOutputStream::Close()
2236 if (m_lasterror
== wxSTREAM_WRITE_ERROR
|| m_entries
.size() == 0)
2241 endrec
.SetEntriesHere(m_entries
.size());
2242 endrec
.SetTotalEntries(m_entries
.size());
2243 endrec
.SetOffset(m_headerOffset
);
2244 endrec
.SetComment(m_Comment
);
2246 wx__ZipEntryList::iterator it
;
2247 wxFileOffset size
= 0;
2249 for (it
= m_entries
.begin(); it
!= m_entries
.end(); ++it
) {
2250 size
+= (*it
)->WriteCentral(*m_parent_o_stream
, GetConv());
2255 endrec
.SetSize(size
);
2256 endrec
.Write(*m_parent_o_stream
, GetConv());
2258 m_lasterror
= m_parent_o_stream
->GetLastError();
2261 m_lasterror
= wxSTREAM_EOF
;
2265 // Finish writing the current entry
2267 bool wxZipOutputStream::CloseEntry()
2269 if (IsOk() && m_pending
)
2270 CreatePendingEntry();
2276 CloseCompressor(m_comp
);
2279 wxFileOffset compressedSize
= m_store
->TellO();
2281 wxZipEntry
& entry
= *m_entries
.back();
2283 // When writing raw the crc and size can't be checked
2285 m_crcAccumulator
= entry
.GetCrc();
2286 m_entrySize
= entry
.GetSize();
2289 // Write the sums in the trailing 'data descriptor' if necessary
2290 if (entry
.m_Flags
& wxZIP_SUMS_FOLLOW
) {
2291 wxASSERT(!IsParentSeekable());
2293 entry
.WriteDescriptor(*m_parent_o_stream
, m_crcAccumulator
,
2294 compressedSize
, m_entrySize
);
2295 m_lasterror
= m_parent_o_stream
->GetLastError();
2298 // If the local header didn't have the correct crc and size written to
2299 // it then seek back and fix it
2300 else if (m_crcAccumulator
!= entry
.GetCrc()
2301 || m_entrySize
!= entry
.GetSize()
2302 || compressedSize
!= entry
.GetCompressedSize())
2304 if (IsParentSeekable()) {
2305 wxFileOffset here
= m_parent_o_stream
->TellO();
2306 wxFileOffset headerOffset
= m_headerOffset
+ m_offsetAdjustment
;
2307 m_parent_o_stream
->SeekO(headerOffset
+ SUMS_OFFSET
);
2308 entry
.WriteDescriptor(*m_parent_o_stream
, m_crcAccumulator
,
2309 compressedSize
, m_entrySize
);
2310 m_parent_o_stream
->SeekO(here
);
2311 m_lasterror
= m_parent_o_stream
->GetLastError();
2313 m_lasterror
= wxSTREAM_WRITE_ERROR
;
2317 m_headerOffset
+= m_headerSize
+ compressedSize
;
2324 m_lasterror
= m_parent_o_stream
->GetLastError();
2326 wxLogError(_("error writing zip entry '%s': bad crc or length"),
2327 entry
.GetName().c_str());
2331 void wxZipOutputStream::Sync()
2333 if (IsOk() && m_pending
)
2334 CreatePendingEntry(NULL
, 0);
2336 m_lasterror
= wxSTREAM_WRITE_ERROR
;
2339 m_lasterror
= m_comp
->GetLastError();
2343 size_t wxZipOutputStream::OnSysWrite(const void *buffer
, size_t size
)
2345 if (IsOk() && m_pending
) {
2346 if (m_initialSize
+ size
< OUTPUT_LATENCY
) {
2347 memcpy(m_initialData
+ m_initialSize
, buffer
, size
);
2348 m_initialSize
+= size
;
2351 CreatePendingEntry(buffer
, size
);
2356 m_lasterror
= wxSTREAM_WRITE_ERROR
;
2357 if (!IsOk() || !size
)
2360 if (m_comp
->Write(buffer
, size
).LastWrite() != size
)
2361 m_lasterror
= wxSTREAM_WRITE_ERROR
;
2362 m_crcAccumulator
= crc32(m_crcAccumulator
, (Byte
*)buffer
, size
);
2363 m_entrySize
+= m_comp
->LastWrite();
2365 return m_comp
->LastWrite();
2368 #endif // wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM