X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/46263455960bc46a6b2e77d444061b6378fc6373..241cb04bd821f0810ef5dfdfd4b085a506f98402:/src/common/zipstrm.cpp?ds=sidebyside diff --git a/src/common/zipstrm.cpp b/src/common/zipstrm.cpp index 322aee5478..5081107137 100644 --- a/src/common/zipstrm.cpp +++ b/src/common/zipstrm.cpp @@ -7,10 +7,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "zipstrm.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -34,7 +30,7 @@ #include "wx/buffer.h" #include "wx/ptr_scpd.h" #include "wx/wfstream.h" -#include "wx/html/forcelnk.h" +#include "wx/link.h" #include "zlib.h" // value for the 'version needed to extract' field (20 means 2.0) @@ -82,7 +78,7 @@ enum { IMPLEMENT_DYNAMIC_CLASS(wxZipEntry, wxArchiveEntry) IMPLEMENT_DYNAMIC_CLASS(wxZipClassFactory, wxArchiveClassFactory) -FORCE_LINK_ME(zipstrm) +wxFORCE_LINK_THIS_MODULE(zipstrm) ///////////////////////////////////////////////////////////////////////////// @@ -133,6 +129,69 @@ static wxFileOffset QuietSeek(wxInputStream& stream, wxFileOffset pos) } +///////////////////////////////////////////////////////////////////////////// +// Read a zip header + +class wxZipHeader +{ +public: + wxZipHeader(wxInputStream& stream, size_t size); + + inline wxUint8 Read8(); + inline wxUint16 Read16(); + inline wxUint32 Read32(); + + const char *GetData() const { return m_data; } + size_t GetSize() const { return m_size; } + operator bool() const { return m_ok; } + + size_t Seek(size_t pos) { m_pos = pos; return m_pos; } + size_t Skip(size_t size) { m_pos += size; return m_pos; } + + wxZipHeader& operator>>(wxUint8& n) { n = Read8(); return *this; } + wxZipHeader& operator>>(wxUint16& n) { n = Read16(); return *this; } + wxZipHeader& operator>>(wxUint32& n) { n = Read32(); return *this; } + +private: + char m_data[64]; + size_t m_size; + size_t m_pos; + bool m_ok; +}; + +wxZipHeader::wxZipHeader(wxInputStream& stream, size_t size) + : m_size(0), + m_pos(0), + m_ok(false) +{ + wxCHECK_RET(size <= sizeof(m_data), _T("buffer too small")); + m_size = stream.Read(m_data, size).LastRead(); + m_ok = m_size == size; +} + +wxUint8 wxZipHeader::Read8() +{ + wxASSERT(m_pos < m_size); + return *wx_reinterpret_cast(wxUint8*, m_data + m_pos++); +} + +wxUint16 wxZipHeader::Read16() +{ + wxASSERT(m_pos + 2 <= m_size); + wxUint16 n = *wx_reinterpret_cast(wxUint16*, m_data + m_pos); + m_pos += 2; + return wxUINT16_SWAP_ON_BE(n); +} + +wxUint32 wxZipHeader::Read32() +{ + wxASSERT(m_pos + 4 <= m_size); + wxUint32 n = *wx_reinterpret_cast(wxUint32*, m_data + m_pos); + m_pos += 4; + return wxUINT32_SWAP_ON_BE(n); +} + + ///////////////////////////////////////////////////////////////////////////// // Stored input stream // Trival decompressor for files which are 'stored' in the zip file. @@ -168,14 +227,13 @@ wxStoredInputStream::wxStoredInputStream(wxInputStream& stream) size_t wxStoredInputStream::OnSysRead(void *buffer, size_t size) { - size_t count = wxMin(size, m_len - m_pos + (size_t)0); + size_t count = wx_truncate_cast(size_t, + wxMin(size + wxFileOffset(0), m_len - m_pos + size_t(0))); count = m_parent_i_stream->Read(buffer, count).LastRead(); m_pos += count; - if (m_pos == m_len) - m_lasterror = wxSTREAM_EOF; - else if (!*m_parent_i_stream) - m_lasterror = wxSTREAM_READ_ERROR; + if (count < size) + m_lasterror = m_pos == m_len ? wxSTREAM_EOF : wxSTREAM_READ_ERROR; return count; } @@ -298,7 +356,8 @@ wxInputStream& wxTeeInputStream::Read(void *buffer, size_t size) size_t wxTeeInputStream::OnSysRead(void *buffer, size_t size) { size_t count = m_parent_i_stream->Read(buffer, size).LastRead(); - m_lasterror = m_parent_i_stream->GetLastError(); + if (count < size) + m_lasterror = m_parent_i_stream->GetLastError(); return count; } @@ -312,6 +371,7 @@ size_t wxTeeInputStream::GetData(char *buffer, size_t size) wxFAIL; // we've already returned data that's now being ungot m_end = len; } + m_parent_i_stream->Reset(); m_parent_i_stream->Ungetch(m_wback, m_wbacksize); free(m_wback); m_wback = NULL; @@ -470,7 +530,6 @@ class wxZipMemory { public: wxZipMemory() : m_data(NULL), m_size(0), m_capacity(0), m_ref(1) { } - ~wxZipMemory() { delete m_data; } wxZipMemory *AddRef() { m_ref++; return this; } void Release() { if (--m_ref == 0) delete this; } @@ -482,11 +541,14 @@ public: wxZipMemory *Unique(size_t size); private: + ~wxZipMemory() { delete [] m_data; } char *m_data; size_t m_size; size_t m_capacity; int m_ref; + + wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipMemory) }; wxZipMemory *wxZipMemory::Unique(size_t size) @@ -501,7 +563,7 @@ wxZipMemory *wxZipMemory::Unique(size_t size) } if (zm->m_capacity < size) { - delete zm->m_data; + delete [] zm->m_data; zm->m_data = new char[size]; zm->m_capacity = size; } @@ -542,13 +604,12 @@ static void Unique(wxZipMemory*& zm, size_t size) // Collection of weak references to entries WX_DECLARE_HASH_MAP(long, wxZipEntry*, wxIntegerHash, - wxIntegerEqual, _wxOffsetZipEntryMap); + wxIntegerEqual, wx__OffsetZipEntryMap); class wxZipWeakLinks { public: wxZipWeakLinks() : m_ref(1) { } - ~wxZipWeakLinks() { wxASSERT(IsEmpty()); } void Release(const wxZipInputStream* WXUNUSED(x)) { if (--m_ref == 0) delete this; } @@ -556,26 +617,33 @@ public: { RemoveEntry(key); if (--m_ref == 0) delete this; } wxZipWeakLinks *AddEntry(wxZipEntry *entry, wxFileOffset key); - void RemoveEntry(wxFileOffset key) { m_entries.erase(key); } + void RemoveEntry(wxFileOffset key) + { m_entries.erase(wx_truncate_cast(key_type, key)); } wxZipEntry *GetEntry(wxFileOffset key) const; bool IsEmpty() const { return m_entries.empty(); } private: + ~wxZipWeakLinks() { wxASSERT(IsEmpty()); } + + typedef wx__OffsetZipEntryMap::key_type key_type; int m_ref; - _wxOffsetZipEntryMap m_entries; + wx__OffsetZipEntryMap m_entries; + + wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipWeakLinks) }; wxZipWeakLinks *wxZipWeakLinks::AddEntry(wxZipEntry *entry, wxFileOffset key) { - m_entries[key] = entry; + m_entries[wx_truncate_cast(key_type, key)] = entry; m_ref++; return this; } wxZipEntry *wxZipWeakLinks::GetEntry(wxFileOffset key) const { - _wxOffsetZipEntryMap::const_iterator it = m_entries.find(key); + wx__OffsetZipEntryMap::const_iterator it = + m_entries.find(wx_truncate_cast(key_type, key)); return it != m_entries.end() ? it->second : NULL; } @@ -587,7 +655,7 @@ wxZipEntry::wxZipEntry( const wxString& name /*=wxEmptyString*/, const wxDateTime& dt /*=wxDateTime::Now()*/, wxFileOffset size /*=wxInvalidOffset*/) - : + : m_SystemMadeBy(wxZIP_SYSTEM_MSDOS), m_VersionMadeBy(wxMAJOR_VERSION * 10 + wxMINOR_VERSION), m_VersionNeeded(VERSION_NEEDED_TO_EXTRACT), @@ -620,7 +688,8 @@ wxZipEntry::~wxZipEntry() } wxZipEntry::wxZipEntry(const wxZipEntry& e) - : m_SystemMadeBy(e.m_SystemMadeBy), + : wxArchiveEntry(e), + m_SystemMadeBy(e.m_SystemMadeBy), m_VersionMadeBy(e.m_VersionMadeBy), m_VersionNeeded(e.m_VersionNeeded), m_Flags(e.m_Flags), @@ -638,7 +707,7 @@ wxZipEntry::wxZipEntry(const wxZipEntry& e) m_ExternalAttributes(e.m_ExternalAttributes), m_Extra(AddRef(e.m_Extra)), m_LocalExtra(AddRef(e.m_LocalExtra)), - m_zipnotifier(e.m_zipnotifier), + m_zipnotifier(NULL), m_backlink(NULL) { } @@ -664,7 +733,7 @@ wxZipEntry& wxZipEntry::operator=(const wxZipEntry& e) m_ExternalAttributes = e.m_ExternalAttributes; Copy(m_Extra, e.m_Extra); Copy(m_LocalExtra, e.m_LocalExtra); - m_zipnotifier = e.m_zipnotifier; + m_zipnotifier = NULL; if (m_backlink) { m_backlink->Release(m_Key); m_backlink = NULL; @@ -677,6 +746,7 @@ wxString wxZipEntry::GetName(wxPathFormat format /*=wxPATH_NATIVE*/) const { bool isDir = IsDir() && !m_Name.empty(); + // optimisations for common (and easy) cases switch (wxFileName::GetFormat(format)) { case wxPATH_DOS: { @@ -741,7 +811,7 @@ void wxZipEntry::SetSystemMadeBy(int system) int mode = GetMode(); bool wasUnix = IsMadeByUnix(); - m_SystemMadeBy = system; + m_SystemMadeBy = (wxUint8)system; if (!wasUnix && IsMadeByUnix()) { SetIsDir(IsDir()); @@ -862,7 +932,9 @@ size_t wxZipEntry::ReadLocal(wxInputStream& stream, wxMBConv& conv) wxUint16 nameLen, extraLen; wxUint32 compressedSize, size, crc; - wxDataInputStream ds(stream); + wxZipHeader ds(stream, LOCAL_SIZE - 4); + if (!ds) + return 0; ds >> m_VersionNeeded >> m_Flags >> m_Method; SetDateTime(wxDateTime().SetFromDOS(ds.Read32())); @@ -878,11 +950,16 @@ size_t wxZipEntry::ReadLocal(wxInputStream& stream, wxMBConv& conv) m_Size = size; SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); + if (stream.LastRead() != nameLen + 0u) + return 0; if (extraLen || GetLocalExtraLen()) { Unique(m_LocalExtra, extraLen); - if (extraLen) + if (extraLen) { stream.Read(m_LocalExtra->GetData(), extraLen); + if (stream.LastRead() != extraLen + 0u) + return 0; + } } return LOCAL_SIZE + nameLen + extraLen; @@ -894,19 +971,21 @@ size_t wxZipEntry::WriteLocal(wxOutputStream& stream, wxMBConv& conv) const const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); const char *name = name_buf; if (!name) name = ""; - wxUint16 nameLen = strlen(name); + wxUint16 nameLen = wx_truncate_cast(wxUint16, strlen(name)); wxDataOutputStream ds(stream); ds << m_VersionNeeded << m_Flags << m_Method; ds.Write32(GetDateTime().GetAsDOS()); - + ds.Write32(m_Crc); - ds.Write32(m_CompressedSize != wxInvalidOffset ? m_CompressedSize : 0); - ds.Write32(m_Size != wxInvalidOffset ? m_Size : 0); + ds.Write32(m_CompressedSize != wxInvalidOffset ? + wx_truncate_cast(wxUint32, m_CompressedSize) : 0); + ds.Write32(m_Size != wxInvalidOffset ? + wx_truncate_cast(wxUint32, m_Size) : 0); ds << nameLen; - wxUint16 extraLen = GetLocalExtraLen(); + wxUint16 extraLen = wx_truncate_cast(wxUint16, GetLocalExtraLen()); ds.Write16(extraLen); stream.Write(name, nameLen); @@ -920,7 +999,9 @@ size_t wxZipEntry::ReadCentral(wxInputStream& stream, wxMBConv& conv) { wxUint16 nameLen, extraLen, commentLen; - wxDataInputStream ds(stream); + wxZipHeader ds(stream, CENTRAL_SIZE - 4); + if (!ds) + return 0; ds >> m_VersionMadeBy >> m_SystemMadeBy; @@ -935,19 +1016,27 @@ size_t wxZipEntry::ReadCentral(wxInputStream& stream, wxMBConv& conv) ds >> nameLen >> extraLen >> commentLen >> m_DiskStart >> m_InternalAttributes >> m_ExternalAttributes; SetOffset(ds.Read32()); - + SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); + if (stream.LastRead() != nameLen + 0u) + return 0; if (extraLen || GetExtraLen()) { Unique(m_Extra, extraLen); - if (extraLen) + if (extraLen) { stream.Read(m_Extra->GetData(), extraLen); + if (stream.LastRead() != extraLen + 0u) + return 0; + } } - if (commentLen) + if (commentLen) { m_Comment = ReadString(stream, commentLen, conv); - else + if (stream.LastRead() != commentLen + 0u) + return 0; + } else { m_Comment.clear(); + } return CENTRAL_SIZE + nameLen + extraLen + commentLen; } @@ -958,32 +1047,32 @@ size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); const char *name = name_buf; if (!name) name = ""; - wxUint16 nameLen = strlen(name); + wxUint16 nameLen = wx_truncate_cast(wxUint16, strlen(name)); const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); const char *comment = comment_buf; if (!comment) comment = ""; - wxUint16 commentLen = strlen(comment); + wxUint16 commentLen = wx_truncate_cast(wxUint16, strlen(comment)); - wxUint16 extraLen = GetExtraLen(); + wxUint16 extraLen = wx_truncate_cast(wxUint16, GetExtraLen()); wxDataOutputStream ds(stream); ds << CENTRAL_MAGIC << m_VersionMadeBy << m_SystemMadeBy; - ds.Write16(GetVersionNeeded()); - ds.Write16(GetFlags()); - ds.Write16(GetMethod()); + ds.Write16(wx_truncate_cast(wxUint16, GetVersionNeeded())); + ds.Write16(wx_truncate_cast(wxUint16, GetFlags())); + ds.Write16(wx_truncate_cast(wxUint16, GetMethod())); ds.Write32(GetDateTime().GetAsDOS()); ds.Write32(GetCrc()); - ds.Write32(GetCompressedSize()); - ds.Write32(GetSize()); + ds.Write32(wx_truncate_cast(wxUint32, GetCompressedSize())); + ds.Write32(wx_truncate_cast(wxUint32, GetSize())); ds.Write16(nameLen); ds.Write16(extraLen); ds << commentLen << m_DiskStart << m_InternalAttributes - << m_ExternalAttributes << (wxUint32)GetOffset(); - + << m_ExternalAttributes << wx_truncate_cast(wxUint32, GetOffset()); + stream.Write(name, nameLen); if (extraLen) stream.Write(GetExtra(), extraLen); @@ -999,8 +1088,10 @@ size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const // size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) { - wxDataInputStream ds(stream); - + wxZipHeader ds(stream, SUMS_SIZE); + if (!ds) + return 0; + m_Crc = ds.Read32(); m_CompressedSize = ds.Read32(); m_Size = ds.Read32(); @@ -1008,23 +1099,24 @@ size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) // if 1st value is the signature then this is probably an info-zip record if (m_Crc == SUMS_MAGIC) { - char buf[8]; - stream.Read(buf, sizeof(buf)); - wxUint32 u1 = CrackUint32(buf); - wxUint32 u2 = CrackUint32(buf + 4); - + wxZipHeader buf(stream, 8); + wxUint32 u1 = buf.GetSize() >= 4 ? buf.Read32() : LOCAL_MAGIC; + wxUint32 u2 = buf.GetSize() == 8 ? buf.Read32() : 0; + // look for the signature of the following record to decide which if ((u1 == LOCAL_MAGIC || u1 == CENTRAL_MAGIC) && (u2 != LOCAL_MAGIC && u2 != CENTRAL_MAGIC)) { // it's a pkzip style record after all! - stream.Ungetch(buf, sizeof(buf)); + if (buf.GetSize() > 0) + stream.Ungetch(buf.GetData(), buf.GetSize()); } else { // it's an info-zip record as expected - stream.Ungetch(buf + 4, sizeof(buf) - 4); - m_Crc = m_CompressedSize; + if (buf.GetSize() > 4) + stream.Ungetch(buf.GetData() + 4, buf.GetSize() - 4); + m_Crc = wx_truncate_cast(wxUint32, m_CompressedSize); m_CompressedSize = m_Size; m_Size = u1; return SUMS_SIZE + 4; @@ -1044,8 +1136,8 @@ size_t wxZipEntry::WriteDescriptor(wxOutputStream& stream, wxUint32 crc, wxDataOutputStream ds(stream); ds.Write32(crc); - ds.Write32(compressedSize); - ds.Write32(size); + ds.Write32(wx_truncate_cast(wxUint32, compressedSize)); + ds.Write32(wx_truncate_cast(wxUint32, size)); return SUMS_SIZE; } @@ -1067,13 +1159,20 @@ public: wxFileOffset GetOffset() const { return m_Offset; } wxString GetComment() const { return m_Comment; } - void SetDiskNumber(int num) { m_DiskNumber = num; } - void SetStartDisk(int num) { m_StartDisk = num; } - void SetEntriesHere(int num) { m_EntriesHere = num; } - void SetTotalEntries(int num) { m_TotalEntries = num; } - void SetSize(wxFileOffset size) { m_Size = (wxUint32)size; } - void SetOffset(wxFileOffset offset) { m_Offset = (wxUint32)offset; } - void SetComment(const wxString& comment) { m_Comment = comment; } + void SetDiskNumber(int num) + { m_DiskNumber = wx_truncate_cast(wxUint16, num); } + void SetStartDisk(int num) + { m_StartDisk = wx_truncate_cast(wxUint16, num); } + void SetEntriesHere(int num) + { m_EntriesHere = wx_truncate_cast(wxUint16, num); } + void SetTotalEntries(int num) + { m_TotalEntries = wx_truncate_cast(wxUint16, num); } + void SetSize(wxFileOffset size) + { m_Size = wx_truncate_cast(wxUint32, size); } + void SetOffset(wxFileOffset offset) + { m_Offset = wx_truncate_cast(wxUint32, offset); } + void SetComment(const wxString& comment) + { m_Comment = comment; } bool Read(wxInputStream& stream, wxMBConv& conv); bool Write(wxOutputStream& stream, wxMBConv& conv) const; @@ -1103,7 +1202,7 @@ bool wxZipEndRec::Write(wxOutputStream& stream, wxMBConv& conv) const const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); const char *comment = comment_buf; if (!comment) comment = ""; - wxUint16 commentLen = strlen(comment); + wxUint16 commentLen = (wxUint16)strlen(comment); wxDataOutputStream ds(stream); @@ -1117,23 +1216,26 @@ bool wxZipEndRec::Write(wxOutputStream& stream, wxMBConv& conv) const bool wxZipEndRec::Read(wxInputStream& stream, wxMBConv& conv) { - wxDataInputStream ds(stream); + wxZipHeader ds(stream, END_SIZE - 4); + if (!ds) + return false; + wxUint16 commentLen; ds >> m_DiskNumber >> m_StartDisk >> m_EntriesHere >> m_TotalEntries >> m_Size >> m_Offset >> commentLen; - if (commentLen) + if (commentLen) { m_Comment = ReadString(stream, commentLen, conv); + if (stream.LastRead() != commentLen + 0u) + return false; + } - if (stream.IsOk()) - if (m_DiskNumber == 0 && m_StartDisk == 0 && - m_EntriesHere == m_TotalEntries) - return true; - else - wxLogError(_("unsupported zip archive")); + if (m_DiskNumber != 0 || m_StartDisk != 0 || + m_EntriesHere != m_TotalEntries) + wxLogWarning(_("assuming this is a multi-part zip concatenated")); - return false; + return true; } @@ -1144,7 +1246,6 @@ class wxZipStreamLink { public: wxZipStreamLink(wxZipOutputStream *stream) : m_ref(1), m_stream(stream) { } - ~wxZipStreamLink() { } wxZipStreamLink *AddRef() { m_ref++; return this; } wxZipOutputStream *GetOutputStream() const { return m_stream; } @@ -1155,17 +1256,21 @@ public: { m_stream = NULL; if (--m_ref == 0) delete this; } private: + ~wxZipStreamLink() { } int m_ref; wxZipOutputStream *m_stream; + + wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipStreamLink) }; ///////////////////////////////////////////////////////////////////////////// // Input stream -wxDECLARE_SCOPED_PTR(wxZipEntry, _wxZipEntryPtr) -wxDEFINE_SCOPED_PTR (wxZipEntry, _wxZipEntryPtr) +// leave the default wxZipEntryPtr free for users +wxDECLARE_SCOPED_PTR(wxZipEntry, wx__ZipEntryPtr) +wxDEFINE_SCOPED_PTR (wxZipEntry, wx__ZipEntryPtr) // constructor // @@ -1173,20 +1278,22 @@ wxZipInputStream::wxZipInputStream(wxInputStream& stream, wxMBConv& conv /*=wxConvLocal*/) : wxArchiveInputStream(stream, conv) { - m_ffile = NULL; Init(); } -// Compatibility constructor +#if 1 //WXWIN_COMPATIBILITY_2_6 + +// Part of the compatibility constructor, which has been made inline to +// avoid a problem with it not being exported by mingw 3.2.3 // -wxZipInputStream::wxZipInputStream(const wxString& archive, - const wxString& file) - : wxArchiveInputStream(OpenFile(archive), wxConvLocal) +void wxZipInputStream::Init(const wxString& file) { // no error messages wxLogNull nolog; Init(); - _wxZipEntryPtr entry; + m_allowSeeking = true; + m_ffile = wx_static_cast(wxFFileInputStream*, m_parent_i_stream); + wx__ZipEntryPtr entry; if (m_ffile->Ok()) { do { @@ -1202,10 +1309,11 @@ wxZipInputStream::wxZipInputStream(const wxString& archive, wxInputStream& wxZipInputStream::OpenFile(const wxString& archive) { wxLogNull nolog; - m_ffile = new wxFFileInputStream(archive); - return *m_ffile; + return *new wxFFileInputStream(archive); } +#endif // WXWIN_COMPATIBILITY_2_6 + void wxZipInputStream::Init() { m_store = new wxStoredInputStream(*m_parent_i_stream); @@ -1222,6 +1330,10 @@ void wxZipInputStream::Init() m_signature = 0; m_TotalEntries = 0; m_lasterror = m_parent_i_stream->GetLastError(); + m_ffile = NULL; +#if 1 //WXWIN_COMPATIBILITY_2_6 + m_allowSeeking = false; +#endif } wxZipInputStream::~wxZipInputStream() @@ -1234,7 +1346,7 @@ wxZipInputStream::~wxZipInputStream() delete m_ffile; m_weaklinks->Release(this); - + if (m_streamlink) m_streamlink->Release(this); } @@ -1293,9 +1405,7 @@ bool wxZipInputStream::LoadEndRecord() else { wxLogNull nolog; wxFileOffset pos = m_parent_i_stream->TellI(); - // FIXME - //if (pos != wxInvalidOffset) - if (pos >= 0 && pos <= LONG_MAX) + if (pos != wxInvalidOffset) m_offsetAdjustment = m_position = pos; return true; } @@ -1305,20 +1415,14 @@ bool wxZipInputStream::LoadEndRecord() // Read in the end record wxFileOffset endPos = m_parent_i_stream->TellI() - 4; - if (!endrec.Read(*m_parent_i_stream, GetConv())) { - if (!*m_parent_i_stream) { - m_lasterror = wxSTREAM_READ_ERROR; - return false; - } - // TODO: try this out - wxLogWarning(_("assuming this is a multi-part zip concatenated")); - } + if (!endrec.Read(*m_parent_i_stream, GetConv())) + return false; m_TotalEntries = endrec.GetTotalEntries(); m_Comment = endrec.GetComment(); // Now find the central-directory. we have the file offset of - // the CD, so look there first. + // the CD, so look there first. if (m_parent_i_stream->SeekI(endrec.GetOffset()) != wxInvalidOffset && ReadSignature() == CENTRAL_MAGIC) { m_signature = CENTRAL_MAGIC; @@ -1326,7 +1430,7 @@ bool wxZipInputStream::LoadEndRecord() m_offsetAdjustment = 0; return true; } - + // If it's not there, then it could be that the zip has been appended // to a self extractor, so take the CD size (also in endrec), subtract // it from the file offset of the end-central-directory and look there. @@ -1348,8 +1452,11 @@ bool wxZipInputStream::LoadEndRecord() // bool wxZipInputStream::FindEndRecord() { + if (!m_parent_i_stream->IsSeekable()) + return false; + // usually it's 22 bytes in size and the last thing in the file - { + { wxLogNull nolog; if (m_parent_i_stream->SeekI(-END_SIZE, wxFromEnd) == wxInvalidOffset) return false; @@ -1373,10 +1480,11 @@ bool wxZipInputStream::FindEndRecord() wxFileOffset minpos = wxMax(pos - 65535L, 0); while (pos > minpos) { - size_t len = pos - wxMax(pos - (BUFSIZE - 3), minpos); + size_t len = wx_truncate_cast(size_t, + pos - wxMax(pos - (BUFSIZE - 3), minpos)); memcpy(buf.data() + len, buf, 3); pos -= len; - + if (m_parent_i_stream->SeekI(pos, wxFromStart) == wxInvalidOffset || m_parent_i_stream->Read(buf.data(), len).LastRead() != len) return false; @@ -1406,7 +1514,7 @@ wxZipEntry *wxZipInputStream::GetNextEntry() if (!IsOk()) return NULL; - _wxZipEntryPtr entry(new wxZipEntry(m_entry)); + wx__ZipEntryPtr entry(new wxZipEntry(m_entry)); entry->m_backlink = m_weaklinks->AddEntry(entry.get(), entry->GetKey()); return entry.release(); } @@ -1427,12 +1535,13 @@ wxStreamError wxZipInputStream::ReadCentral() if (QuietSeek(*m_parent_i_stream, m_position + 4) == wxInvalidOffset) return wxSTREAM_READ_ERROR; - m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); - if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { + size_t size = m_entry.ReadCentral(*m_parent_i_stream, GetConv()); + if (!size) { m_signature = 0; return wxSTREAM_READ_ERROR; } + m_position += size; m_signature = ReadSignature(); if (m_offsetAdjustment) @@ -1460,10 +1569,11 @@ wxStreamError wxZipInputStream::ReadLocal(bool readEndRec /*=false*/) while (m_signature == CENTRAL_MAGIC) { if (m_weaklinks->IsEmpty() && m_streamlink == NULL) return wxSTREAM_EOF; - - m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); + + size_t size = m_entry.ReadCentral(*m_parent_i_stream, GetConv()); + m_position += size; m_signature = 0; - if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) + if (!size) return wxSTREAM_READ_ERROR; wxZipEntry *entry = m_weaklinks->GetEntry(m_entry.GetOffset()); @@ -1496,7 +1606,7 @@ wxStreamError wxZipInputStream::ReadLocal(bool readEndRec /*=false*/) } return wxSTREAM_EOF; } - + if (m_signature != LOCAL_MAGIC) { wxLogError(_("error reading zip local header")); return wxSTREAM_READ_ERROR; @@ -1507,7 +1617,7 @@ wxStreamError wxZipInputStream::ReadLocal(bool readEndRec /*=false*/) m_entry.SetOffset(m_position); m_entry.SetKey(m_position); - if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { + if (!m_headerSize) { return wxSTREAM_READ_ERROR; } else { m_TotalEntries++; @@ -1537,7 +1647,8 @@ bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) return false; if (m_lasterror == wxSTREAM_READ_ERROR) return false; - wxCHECK(!IsOpened(), false); + if (IsOpened()) + CloseEntry(); m_raw = raw; @@ -1547,9 +1658,9 @@ bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) // can only open the current entry on a non-seekable stream wxCHECK(m_parentSeekable, false); } - + m_lasterror = wxSTREAM_READ_ERROR; - + if (entry) m_entry = *entry; @@ -1565,7 +1676,7 @@ bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) if (m_parentSeekable || AtHeader()) { m_headerSize = m_entry.ReadLocal(*m_parent_i_stream, GetConv()); - if (m_parentSeekable) { + if (m_headerSize && m_parentSeekable) { wxZipEntry *ref = m_weaklinks->GetEntry(m_entry.GetKey()); if (ref) { Copy(ref->m_LocalExtra, m_entry.m_LocalExtra); @@ -1579,7 +1690,8 @@ bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) } } - m_lasterror = m_parent_i_stream->GetLastError(); + if (m_headerSize) + m_lasterror = wxSTREAM_NO_ERROR; return IsOk(); } @@ -1698,7 +1810,8 @@ size_t wxZipInputStream::OnSysRead(void *buffer, size_t size) size_t count = m_decomp->Read(buffer, size).LastRead(); if (!m_raw) m_crcAccumulator = crc32(m_crcAccumulator, (Byte*)buffer, count); - m_lasterror = m_decomp->GetLastError(); + if (count < size) + m_lasterror = m_decomp->GetLastError(); if (Eof()) { if ((m_entry.GetFlags() & wxZIP_SUMS_FOLLOW) != 0) { @@ -1716,27 +1829,34 @@ size_t wxZipInputStream::OnSysRead(void *buffer, size_t size) if (!m_raw) { m_lasterror = wxSTREAM_READ_ERROR; - if (m_parent_i_stream->IsOk()) { - if (m_entry.GetSize() != TellI()) - wxLogError(_("reading zip stream (entry %s): bad length"), - m_entry.GetName().c_str()); - else if (m_crcAccumulator != m_entry.GetCrc()) - wxLogError(_("reading zip stream (entry %s): bad crc"), - m_entry.GetName().c_str()); - else - m_lasterror = wxSTREAM_EOF; - } + if (m_entry.GetSize() != TellI()) + wxLogError(_("reading zip stream (entry %s): bad length"), + m_entry.GetName().c_str()); + else if (m_crcAccumulator != m_entry.GetCrc()) + wxLogError(_("reading zip stream (entry %s): bad crc"), + m_entry.GetName().c_str()); + else + m_lasterror = wxSTREAM_EOF; } } return count; } +#if 1 //WXWIN_COMPATIBILITY_2_6 + // Borrowed from VS's zip stream (c) 1999 Vaclav Slavik // wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) { - if (!m_ffile || AtHeader()) + // seeking works when the stream is created with the compatibility + // constructor + if (!m_allowSeeking) + return wxInvalidOffset; + if (!IsOpened()) + if ((AtHeader() && !DoOpen()) || !OpenDecompressor()) + m_lasterror = wxSTREAM_READ_ERROR; + if (!IsOk()) return wxInvalidOffset; // NB: since ZIP files don't natively support seeking, we have to @@ -1751,11 +1871,11 @@ wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) { case wxFromCurrent : nextpos = seek + pos; break; case wxFromStart : nextpos = seek; break; - case wxFromEnd : nextpos = GetLength() - 1 + seek; break; + case wxFromEnd : nextpos = GetLength() + seek; break; default : nextpos = pos; break; /* just to fool compiler, never happens */ } - size_t toskip; + wxFileOffset toskip wxDUMMY_INITIALIZE(0); if ( nextpos >= pos ) { toskip = nextpos - pos; @@ -1763,7 +1883,6 @@ wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) else { wxZipEntry current(m_entry); - CloseEntry(); if (!OpenEntry(current)) { m_lasterror = wxSTREAM_READ_ERROR; @@ -1774,12 +1893,12 @@ wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) if ( toskip > 0 ) { - const size_t BUFSIZE = 4096; + const int BUFSIZE = 4096; size_t sz; char buffer[BUFSIZE]; while ( toskip > 0 ) { - sz = wxMin(toskip, BUFSIZE); + sz = wx_truncate_cast(size_t, wxMin(toskip, BUFSIZE)); Read(buffer, sz); toskip -= sz; } @@ -1789,12 +1908,14 @@ wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) return pos; } +#endif // WXWIN_COMPATIBILITY_2_6 + ///////////////////////////////////////////////////////////////////////////// // Output stream -#include -WX_DEFINE_LIST(_wxZipEntryList); +#include "wx/listimpl.cpp" +WX_DEFINE_LIST(wx__ZipEntryList) wxZipOutputStream::wxZipOutputStream(wxOutputStream& stream, int level /*=-1*/, @@ -1815,11 +1936,11 @@ wxZipOutputStream::wxZipOutputStream(wxOutputStream& stream, m_offsetAdjustment(wxInvalidOffset) { } - + wxZipOutputStream::~wxZipOutputStream() { Close(); - WX_CLEAR_LIST(_wxZipEntryList, m_entries); + WX_CLEAR_LIST(wx__ZipEntryList, m_entries); delete m_store; delete m_deflate; delete m_pending; @@ -1848,7 +1969,7 @@ bool wxZipOutputStream::PutNextDirEntry( bool wxZipOutputStream::CopyEntry(wxZipEntry *entry, wxZipInputStream& inputStream) { - _wxZipEntryPtr e(entry); + wx__ZipEntryPtr e(entry); return inputStream.DoOpen(e.get(), true) && @@ -1914,15 +2035,19 @@ bool wxZipOutputStream::DoCreate(wxZipEntry *entry, bool raw /*=false*/) ds << LOCAL_MAGIC; // and if this is the first entry test for seekability - if (m_headerOffset == 0) { + if (m_headerOffset == 0 && m_parent_o_stream->IsSeekable()) { +#if wxUSE_LOG bool logging = wxLog::IsEnabled(); wxLogNull nolog; +#endif // wxUSE_LOG wxFileOffset here = m_parent_o_stream->TellO(); if (here != wxInvalidOffset && here >= 4) { if (m_parent_o_stream->SeekO(here - 4) == here - 4) { m_offsetAdjustment = here - 4; +#if wxUSE_LOG wxLog::EnableLogging(logging); +#endif // wxUSE_LOG m_parent_o_stream->SeekO(here); } } @@ -2014,7 +2139,7 @@ bool wxZipOutputStream::CloseCompressor(wxOutputStream *comp) void wxZipOutputStream::CreatePendingEntry(const void *buffer, size_t size) { wxASSERT(IsOk() && m_pending && !m_comp); - _wxZipEntryPtr spPending(m_pending); + wx__ZipEntryPtr spPending(m_pending); m_pending = NULL; Buffer bufs[] = { @@ -2055,7 +2180,7 @@ void wxZipOutputStream::CreatePendingEntry(const void *buffer, size_t size) void wxZipOutputStream::CreatePendingEntry() { wxASSERT(IsOk() && m_pending && !m_comp); - _wxZipEntryPtr spPending(m_pending); + wx__ZipEntryPtr spPending(m_pending); m_pending = NULL; m_lasterror = wxSTREAM_WRITE_ERROR; @@ -2119,7 +2244,7 @@ bool wxZipOutputStream::Close() endrec.SetOffset(m_headerOffset); endrec.SetComment(m_Comment); - _wxZipEntryList::iterator it; + wx__ZipEntryList::iterator it; wxFileOffset size = 0; for (it = m_entries.begin(); it != m_entries.end(); ++it) { @@ -2191,7 +2316,8 @@ bool wxZipOutputStream::CloseEntry() } m_headerOffset += m_headerSize + compressedSize; - m_headerSize = m_entrySize = 0; + m_headerSize = 0; + m_entrySize = 0; m_store->Close(); m_raw = false;