X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/26481315da88023dc827d8a441e27f41aa85d942..89e94a4bffda38bdb53d875f022982273f9580f2:/src/common/zipstrm.cpp diff --git a/src/common/zipstrm.cpp b/src/common/zipstrm.cpp index 6b2722fc04..b29a18371b 100644 --- a/src/common/zipstrm.cpp +++ b/src/common/zipstrm.cpp @@ -34,7 +34,6 @@ #include "wx/buffer.h" #include "wx/ptr_scpd.h" #include "wx/wfstream.h" -#include "wx/html/forcelnk.h" #include "zlib.h" // value for the 'version needed to extract' field (20 means 2.0) @@ -82,7 +81,12 @@ enum { IMPLEMENT_DYNAMIC_CLASS(wxZipEntry, wxArchiveEntry) IMPLEMENT_DYNAMIC_CLASS(wxZipClassFactory, wxArchiveClassFactory) -FORCE_LINK_ME(zipstrm) +//FORCE_LINK_ME(zipstrm) +int _wx_link_dummy_func_zipstrm(); +int _wx_link_dummy_func_zipstrm() +{ + return 1; +} ///////////////////////////////////////////////////////////////////////////// @@ -470,7 +474,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,6 +485,7 @@ public: wxZipMemory *Unique(size_t size); private: + ~wxZipMemory() { delete [] m_data; } char *m_data; size_t m_size; @@ -501,7 +505,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 +546,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; } @@ -557,27 +560,28 @@ public: wxZipWeakLinks *AddEntry(wxZipEntry *entry, wxFileOffset key); void RemoveEntry(wxFileOffset key) - { m_entries.erase((_wxOffsetZipEntryMap::key_type)key); } + { m_entries.erase((wx__OffsetZipEntryMap::key_type)key); } wxZipEntry *GetEntry(wxFileOffset key) const; bool IsEmpty() const { return m_entries.empty(); } private: + ~wxZipWeakLinks() { wxASSERT(IsEmpty()); } int m_ref; - _wxOffsetZipEntryMap m_entries; + wx__OffsetZipEntryMap m_entries; }; wxZipWeakLinks *wxZipWeakLinks::AddEntry(wxZipEntry *entry, wxFileOffset key) { - m_entries[(_wxOffsetZipEntryMap::key_type)key] = entry; + m_entries[(wx__OffsetZipEntryMap::key_type)key] = entry; m_ref++; return this; } wxZipEntry *wxZipWeakLinks::GetEntry(wxFileOffset key) const { - _wxOffsetZipEntryMap::const_iterator it = - m_entries.find((_wxOffsetZipEntryMap::key_type)key); + wx__OffsetZipEntryMap::const_iterator it = + m_entries.find((wx__OffsetZipEntryMap::key_type)key); return it != m_entries.end() ? it->second : NULL; } @@ -589,7 +593,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), @@ -622,7 +626,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), @@ -640,7 +645,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) { } @@ -666,7 +671,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; @@ -679,6 +684,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: { @@ -902,7 +908,7 @@ size_t wxZipEntry::WriteLocal(wxOutputStream& stream, wxMBConv& conv) const ds << m_VersionNeeded << m_Flags << m_Method; ds.Write32(GetDateTime().GetAsDOS()); - + ds.Write32(m_Crc); ds.Write32(m_CompressedSize != wxInvalidOffset ? (wxUint32)m_CompressedSize : 0); ds.Write32(m_Size != wxInvalidOffset ? (wxUint32)m_Size : 0); @@ -937,7 +943,7 @@ 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 (extraLen || GetExtraLen()) { @@ -985,7 +991,7 @@ size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const ds << commentLen << m_DiskStart << m_InternalAttributes << m_ExternalAttributes << (wxUint32)GetOffset(); - + stream.Write(name, nameLen); if (extraLen) stream.Write(GetExtra(), extraLen); @@ -1002,7 +1008,7 @@ size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) { wxDataInputStream ds(stream); - + m_Crc = ds.Read32(); m_CompressedSize = ds.Read32(); m_Size = ds.Read32(); @@ -1014,7 +1020,7 @@ size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) stream.Read(buf, sizeof(buf)); wxUint32 u1 = CrackUint32(buf); wxUint32 u2 = CrackUint32(buf + 4); - + // look for the signature of the following record to decide which if ((u1 == LOCAL_MAGIC || u1 == CENTRAL_MAGIC) && (u2 != LOCAL_MAGIC && u2 != CENTRAL_MAGIC)) @@ -1146,7 +1152,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; } @@ -1157,6 +1162,7 @@ public: { m_stream = NULL; if (--m_ref == 0) delete this; } private: + ~wxZipStreamLink() { } int m_ref; wxZipOutputStream *m_stream; @@ -1166,8 +1172,9 @@ private: ///////////////////////////////////////////////////////////////////////////// // 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 // @@ -1175,20 +1182,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 { @@ -1204,10 +1213,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); @@ -1224,6 +1234,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() @@ -1236,7 +1250,7 @@ wxZipInputStream::~wxZipInputStream() delete m_ffile; m_weaklinks->Release(this); - + if (m_streamlink) m_streamlink->Release(this); } @@ -1320,7 +1334,7 @@ bool wxZipInputStream::LoadEndRecord() 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; @@ -1328,7 +1342,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. @@ -1350,8 +1364,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; @@ -1378,7 +1395,7 @@ bool wxZipInputStream::FindEndRecord() size_t len = (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; @@ -1408,7 +1425,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(); } @@ -1462,7 +1479,7 @@ 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()); m_signature = 0; if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) @@ -1498,7 +1515,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; @@ -1549,9 +1566,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; @@ -1734,11 +1751,20 @@ size_t wxZipInputStream::OnSysRead(void *buffer, size_t size) 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 @@ -1753,11 +1779,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; + size_t toskip wxDUMMY_INITIALIZE(0); if ( nextpos >= pos ) { toskip = (size_t)(nextpos - pos); @@ -1791,12 +1817,14 @@ wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) return pos; } +#endif // WXWIN_COMPATIBILITY_2_6 + ///////////////////////////////////////////////////////////////////////////// // Output stream #include "wx/listimpl.cpp" -WX_DEFINE_LIST(_wxZipEntryList); +WX_DEFINE_LIST(wx__ZipEntryList); wxZipOutputStream::wxZipOutputStream(wxOutputStream& stream, int level /*=-1*/, @@ -1817,11 +1845,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; @@ -1850,7 +1878,7 @@ bool wxZipOutputStream::PutNextDirEntry( bool wxZipOutputStream::CopyEntry(wxZipEntry *entry, wxZipInputStream& inputStream) { - _wxZipEntryPtr e(entry); + wx__ZipEntryPtr e(entry); return inputStream.DoOpen(e.get(), true) && @@ -1916,15 +1944,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); } } @@ -2016,7 +2048,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[] = { @@ -2057,7 +2089,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; @@ -2121,7 +2153,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) {