+ return str;
+}
+
+// Decode a little endian wxUint32 number from a character array
+//
+static inline wxUint32 CrackUint32(const char *m)
+{
+ const unsigned char *n = (const unsigned char*)m;
+ return (n[3] << 24) | (n[2] << 16) | (n[1] << 8) | n[0];
+}
+
+// Decode a little endian wxUint16 number from a character array
+//
+static inline wxUint16 CrackUint16(const char *m)
+{
+ const unsigned char *n = (const unsigned char*)m;
+ return (n[1] << 8) | n[0];
+}
+
+// Temporarily lower the logging level in debug mode to avoid a warning
+// from SeekI about seeking on a stream with data written back to it.
+//
+static wxFileOffset QuietSeek(wxInputStream& stream, wxFileOffset pos)
+{
+#if defined(__WXDEBUG__) && wxUSE_LOG
+ wxLogLevel level = wxLog::GetLogLevel();
+ wxLog::SetLogLevel(wxLOG_Debug - 1);
+ wxFileOffset result = stream.SeekI(pos);
+ wxLog::SetLogLevel(level);
+ return result;
+#else
+ return stream.SeekI(pos);
+#endif
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Class factory
+
+wxZipClassFactory g_wxZipClassFactory;
+
+wxZipClassFactory::wxZipClassFactory()
+{
+ if (this == &g_wxZipClassFactory)
+ PushFront();
+}
+
+const wxChar * const *
+wxZipClassFactory::GetProtocols(wxStreamProtocolType type) const
+{
+ static const wxChar *protocols[] = { _T("zip"), NULL };
+ static const wxChar *mimetypes[] = { _T("application/zip"), NULL };
+ static const wxChar *fileexts[] = { _T(".zip"), _T(".htb"), NULL };
+ static const wxChar *empty[] = { NULL };
+
+ switch (type) {
+ case wxSTREAM_PROTOCOL: return protocols;
+ case wxSTREAM_MIMETYPE: return mimetypes;
+ case wxSTREAM_FILEEXT: return fileexts;
+ default: return empty;
+ }
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// 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;
+}
+
+inline wxUint8 wxZipHeader::Read8()
+{
+ wxASSERT(m_pos < m_size);
+ return m_data[m_pos++];
+}
+
+inline wxUint16 wxZipHeader::Read16()
+{
+ wxASSERT(m_pos + 2 <= m_size);
+ wxUint16 n = CrackUint16(m_data + m_pos);
+ m_pos += 2;
+ return n;
+}
+
+inline wxUint32 wxZipHeader::Read32()
+{
+ wxASSERT(m_pos + 4 <= m_size);
+ wxUint32 n = CrackUint32(m_data + m_pos);
+ m_pos += 4;
+ return n;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Stored input stream
+// Trival decompressor for files which are 'stored' in the zip file.
+
+class wxStoredInputStream : public wxFilterInputStream
+{
+public:
+ wxStoredInputStream(wxInputStream& stream);
+
+ void Open(wxFileOffset len) { Close(); m_len = len; }
+ void Close() { m_pos = 0; m_lasterror = wxSTREAM_NO_ERROR; }
+
+ virtual char Peek() { return wxInputStream::Peek(); }
+ virtual wxFileOffset GetLength() const { return m_len; }
+
+protected:
+ virtual size_t OnSysRead(void *buffer, size_t size);
+ virtual wxFileOffset OnSysTell() const { return m_pos; }
+
+private:
+ wxFileOffset m_pos;
+ wxFileOffset m_len;
+
+ DECLARE_NO_COPY_CLASS(wxStoredInputStream)
+};
+
+wxStoredInputStream::wxStoredInputStream(wxInputStream& stream)
+ : wxFilterInputStream(stream),
+ m_pos(0),
+ m_len(0)
+{
+}
+
+size_t wxStoredInputStream::OnSysRead(void *buffer, size_t size)
+{
+ 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 (count < size)
+ m_lasterror = m_pos == m_len ? wxSTREAM_EOF : wxSTREAM_READ_ERROR;
+
+ return count;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Stored output stream
+// Trival compressor for files which are 'stored' in the zip file.
+
+class wxStoredOutputStream : public wxFilterOutputStream
+{
+public:
+ wxStoredOutputStream(wxOutputStream& stream) :
+ wxFilterOutputStream(stream), m_pos(0) { }
+
+ bool Close() {
+ m_pos = 0;
+ m_lasterror = wxSTREAM_NO_ERROR;
+ return true;
+ }
+
+protected:
+ virtual size_t OnSysWrite(const void *buffer, size_t size);
+ virtual wxFileOffset OnSysTell() const { return m_pos; }
+
+private:
+ wxFileOffset m_pos;
+ DECLARE_NO_COPY_CLASS(wxStoredOutputStream)
+};
+
+size_t wxStoredOutputStream::OnSysWrite(const void *buffer, size_t size)
+{
+ if (!IsOk() || !size)
+ return 0;
+ size_t count = m_parent_o_stream->Write(buffer, size).LastWrite();
+ if (count != size)
+ m_lasterror = wxSTREAM_WRITE_ERROR;
+ m_pos += count;
+ return count;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// wxRawInputStream
+//
+// Used to handle the unusal case of raw copying an entry of unknown
+// length. This can only happen when the zip being copied from is being
+// read from a non-seekable stream, and also was original written to a
+// non-seekable stream.
+//
+// In this case there's no option but to decompress the stream to find
+// it's length, but we can still write the raw compressed data to avoid the
+// compression overhead (which is the greater one).
+//
+// Usage is like this:
+// m_rawin = new wxRawInputStream(*m_parent_i_stream);
+// m_decomp = m_rawin->Open(OpenDecompressor(m_rawin->GetTee()));
+//
+// The wxRawInputStream owns a wxTeeInputStream object, the role of which
+// is something like the unix 'tee' command; it is a transparent filter, but
+// allows the data read to be read a second time via an extra method 'GetData'.
+//
+// The wxRawInputStream then draws data through the tee using a decompressor
+// then instead of returning the decompressed data, retuns the raw data
+// from wxTeeInputStream::GetData().
+
+class wxTeeInputStream : public wxFilterInputStream
+{
+public:
+ wxTeeInputStream(wxInputStream& stream);
+
+ size_t GetCount() const { return m_end - m_start; }
+ size_t GetData(char *buffer, size_t size);
+
+ void Open();
+ bool Final();
+
+ wxInputStream& Read(void *buffer, size_t size);
+
+protected:
+ virtual size_t OnSysRead(void *buffer, size_t size);
+ virtual wxFileOffset OnSysTell() const { return m_pos; }
+
+private:
+ wxFileOffset m_pos;
+ wxMemoryBuffer m_buf;
+ size_t m_start;
+ size_t m_end;
+
+ DECLARE_NO_COPY_CLASS(wxTeeInputStream)
+};
+
+wxTeeInputStream::wxTeeInputStream(wxInputStream& stream)
+ : wxFilterInputStream(stream),
+ m_pos(0), m_buf(8192), m_start(0), m_end(0)
+{
+}
+
+void wxTeeInputStream::Open()
+{
+ m_pos = m_start = m_end = 0;
+ m_lasterror = wxSTREAM_NO_ERROR;
+}
+
+bool wxTeeInputStream::Final()
+{
+ bool final = m_end == m_buf.GetDataLen();
+ m_end = m_buf.GetDataLen();
+ return final;
+}
+
+wxInputStream& wxTeeInputStream::Read(void *buffer, size_t size)
+{
+ size_t count = wxInputStream::Read(buffer, size).LastRead();
+ m_end = m_buf.GetDataLen();
+ m_buf.AppendData(buffer, count);
+ return *this;
+}
+
+size_t wxTeeInputStream::OnSysRead(void *buffer, size_t size)
+{
+ size_t count = m_parent_i_stream->Read(buffer, size).LastRead();
+ if (count < size)
+ m_lasterror = m_parent_i_stream->GetLastError();
+ return count;
+}
+
+size_t wxTeeInputStream::GetData(char *buffer, size_t size)
+{
+ if (m_wbacksize) {
+ size_t len = m_buf.GetDataLen();
+ len = len > m_wbacksize ? len - m_wbacksize : 0;
+ m_buf.SetDataLen(len);
+ if (m_end > len) {
+ 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;
+ m_wbacksize = 0;
+ m_wbackcur = 0;
+ }
+
+ if (size > GetCount())
+ size = GetCount();
+ if (size) {
+ memcpy(buffer, m_buf + m_start, size);
+ m_start += size;
+ wxASSERT(m_start <= m_end);
+ }
+
+ if (m_start == m_end && m_start > 0 && m_buf.GetDataLen() > 0) {
+ size_t len = m_buf.GetDataLen();
+ char *buf = (char*)m_buf.GetWriteBuf(len);
+ len -= m_end;
+ memmove(buf, buf + m_end, len);
+ m_buf.UngetWriteBuf(len);
+ m_start = m_end = 0;
+ }
+
+ return size;
+}
+
+class wxRawInputStream : public wxFilterInputStream
+{
+public:
+ wxRawInputStream(wxInputStream& stream);
+ virtual ~wxRawInputStream() { delete m_tee; }
+
+ wxInputStream* Open(wxInputStream *decomp);
+ wxInputStream& GetTee() const { return *m_tee; }
+
+protected:
+ virtual size_t OnSysRead(void *buffer, size_t size);
+ virtual wxFileOffset OnSysTell() const { return m_pos; }
+
+private:
+ wxFileOffset m_pos;
+ wxTeeInputStream *m_tee;
+
+ enum { BUFSIZE = 8192 };
+ wxCharBuffer m_dummy;
+
+ DECLARE_NO_COPY_CLASS(wxRawInputStream)
+};
+
+wxRawInputStream::wxRawInputStream(wxInputStream& stream)
+ : wxFilterInputStream(stream),
+ m_pos(0),
+ m_tee(new wxTeeInputStream(stream)),
+ m_dummy(BUFSIZE)
+{
+}
+
+wxInputStream *wxRawInputStream::Open(wxInputStream *decomp)
+{
+ if (decomp) {
+ m_parent_i_stream = decomp;
+ m_pos = 0;
+ m_lasterror = wxSTREAM_NO_ERROR;
+ m_tee->Open();
+ return this;
+ } else {
+ return NULL;
+ }
+}
+
+size_t wxRawInputStream::OnSysRead(void *buffer, size_t size)
+{
+ char *buf = (char*)buffer;
+ size_t count = 0;
+
+ while (count < size && IsOk())
+ {
+ while (m_parent_i_stream->IsOk() && m_tee->GetCount() == 0)
+ m_parent_i_stream->Read(m_dummy.data(), BUFSIZE);
+
+ size_t n = m_tee->GetData(buf + count, size - count);
+ count += n;
+
+ if (n == 0 && m_tee->Final())
+ m_lasterror = m_parent_i_stream->GetLastError();
+ }
+
+ m_pos += count;
+ return count;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Zlib streams than can be reused without recreating.
+
+class wxZlibOutputStream2 : public wxZlibOutputStream
+{
+public:
+ wxZlibOutputStream2(wxOutputStream& stream, int level) :
+ wxZlibOutputStream(stream, level, wxZLIB_NO_HEADER) { }
+
+ bool Open(wxOutputStream& stream);
+ bool Close() { DoFlush(true); m_pos = wxInvalidOffset; return IsOk(); }
+};
+
+bool wxZlibOutputStream2::Open(wxOutputStream& stream)
+{
+ wxCHECK(m_pos == wxInvalidOffset, false);
+
+ m_deflate->next_out = m_z_buffer;
+ m_deflate->avail_out = m_z_size;
+ m_pos = 0;
+ m_lasterror = wxSTREAM_NO_ERROR;
+ m_parent_o_stream = &stream;
+
+ if (deflateReset(m_deflate) != Z_OK) {
+ wxLogError(_("can't re-initialize zlib deflate stream"));
+ m_lasterror = wxSTREAM_WRITE_ERROR;
+ return false;
+ }
+
+ return true;
+}
+
+class wxZlibInputStream2 : public wxZlibInputStream
+{
+public:
+ wxZlibInputStream2(wxInputStream& stream) :
+ wxZlibInputStream(stream, wxZLIB_NO_HEADER) { }
+
+ bool Open(wxInputStream& stream);
+};
+
+bool wxZlibInputStream2::Open(wxInputStream& stream)
+{
+ m_inflate->avail_in = 0;
+ m_pos = 0;
+ m_lasterror = wxSTREAM_NO_ERROR;
+ m_parent_i_stream = &stream;
+
+ if (inflateReset(m_inflate) != Z_OK) {
+ wxLogError(_("can't re-initialize zlib inflate stream"));
+ m_lasterror = wxSTREAM_READ_ERROR;
+ return false;
+ }
+
+ return true;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Class to hold wxZipEntry's Extra and LocalExtra fields
+
+class wxZipMemory
+{
+public:
+ wxZipMemory() : m_data(NULL), m_size(0), m_capacity(0), m_ref(1) { }
+
+ wxZipMemory *AddRef() { m_ref++; return this; }
+ void Release() { if (--m_ref == 0) delete this; }
+
+ char *GetData() const { return m_data; }
+ size_t GetSize() const { return m_size; }
+ size_t GetCapacity() const { return m_capacity; }
+
+ 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)
+{
+ wxZipMemory *zm;
+
+ if (m_ref > 1) {
+ --m_ref;
+ zm = new wxZipMemory;
+ } else {
+ zm = this;
+ }
+
+ if (zm->m_capacity < size) {
+ delete [] zm->m_data;
+ zm->m_data = new char[size];
+ zm->m_capacity = size;
+ }
+
+ zm->m_size = size;
+ return zm;
+}
+
+static inline wxZipMemory *AddRef(wxZipMemory *zm)
+{
+ if (zm)
+ zm->AddRef();
+ return zm;
+}
+
+static inline void Release(wxZipMemory *zm)
+{
+ if (zm)
+ zm->Release();
+}
+
+static void Copy(wxZipMemory*& dest, wxZipMemory *src)
+{
+ Release(dest);
+ dest = AddRef(src);
+}
+
+static void Unique(wxZipMemory*& zm, size_t size)
+{
+ if (!zm && size)
+ zm = new wxZipMemory;
+ if (zm)
+ zm = zm->Unique(size);
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// Collection of weak references to entries
+
+WX_DECLARE_HASH_MAP(long, wxZipEntry*, wxIntegerHash,
+ wxIntegerEqual, wxOffsetZipEntryMap_);
+
+class wxZipWeakLinks
+{
+public:
+ wxZipWeakLinks() : m_ref(1) { }
+
+ void Release(const wxZipInputStream* WXUNUSED(x))
+ { if (--m_ref == 0) delete this; }
+ void Release(wxFileOffset key)
+ { RemoveEntry(key); if (--m_ref == 0) delete this; }
+
+ wxZipWeakLinks *AddEntry(wxZipEntry *entry, wxFileOffset 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 wxOffsetZipEntryMap_::key_type key_type;
+
+ int m_ref;
+ wxOffsetZipEntryMap_ m_entries;
+
+ wxSUPPRESS_GCC_PRIVATE_DTOR_WARNING(wxZipWeakLinks)
+};
+
+wxZipWeakLinks *wxZipWeakLinks::AddEntry(wxZipEntry *entry, wxFileOffset key)
+{
+ 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(wx_truncate_cast(key_type, key));
+ return it != m_entries.end() ? it->second : NULL;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// ZipEntry
+
+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),
+ m_Flags(0),
+ m_Method(wxZIP_METHOD_DEFAULT),
+ m_DateTime(dt),
+ m_Crc(0),
+ m_CompressedSize(wxInvalidOffset),
+ m_Size(size),
+ m_Key(wxInvalidOffset),
+ m_Offset(wxInvalidOffset),
+ m_DiskStart(0),
+ m_InternalAttributes(0),
+ m_ExternalAttributes(0),
+ m_Extra(NULL),
+ m_LocalExtra(NULL),
+ m_zipnotifier(NULL),
+ m_backlink(NULL)
+{
+ if (!name.empty())
+ SetName(name);
+}
+
+wxZipEntry::~wxZipEntry()
+{
+ if (m_backlink)
+ m_backlink->Release(m_Key);
+ Release(m_Extra);
+ Release(m_LocalExtra);
+}
+
+wxZipEntry::wxZipEntry(const wxZipEntry& e)
+ : wxArchiveEntry(e),
+ m_SystemMadeBy(e.m_SystemMadeBy),
+ m_VersionMadeBy(e.m_VersionMadeBy),
+ m_VersionNeeded(e.m_VersionNeeded),
+ m_Flags(e.m_Flags),
+ m_Method(e.m_Method),
+ m_DateTime(e.m_DateTime),
+ m_Crc(e.m_Crc),
+ m_CompressedSize(e.m_CompressedSize),
+ m_Size(e.m_Size),
+ m_Name(e.m_Name),
+ m_Key(e.m_Key),
+ m_Offset(e.m_Offset),
+ m_Comment(e.m_Comment),
+ m_DiskStart(e.m_DiskStart),
+ m_InternalAttributes(e.m_InternalAttributes),
+ m_ExternalAttributes(e.m_ExternalAttributes),
+ m_Extra(AddRef(e.m_Extra)),
+ m_LocalExtra(AddRef(e.m_LocalExtra)),
+ m_zipnotifier(NULL),
+ m_backlink(NULL)
+{
+}
+
+wxZipEntry& wxZipEntry::operator=(const wxZipEntry& e)
+{
+ if (&e != this) {
+ m_SystemMadeBy = e.m_SystemMadeBy;
+ m_VersionMadeBy = e.m_VersionMadeBy;
+ m_VersionNeeded = e.m_VersionNeeded;
+ m_Flags = e.m_Flags;
+ m_Method = e.m_Method;
+ m_DateTime = e.m_DateTime;
+ m_Crc = e.m_Crc;
+ m_CompressedSize = e.m_CompressedSize;
+ m_Size = e.m_Size;
+ m_Name = e.m_Name;
+ m_Key = e.m_Key;
+ m_Offset = e.m_Offset;
+ m_Comment = e.m_Comment;
+ m_DiskStart = e.m_DiskStart;
+ m_InternalAttributes = e.m_InternalAttributes;
+ m_ExternalAttributes = e.m_ExternalAttributes;
+ Copy(m_Extra, e.m_Extra);
+ Copy(m_LocalExtra, e.m_LocalExtra);
+ m_zipnotifier = NULL;
+ if (m_backlink) {
+ m_backlink->Release(m_Key);
+ m_backlink = NULL;
+ }
+ }
+ return *this;
+}
+
+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:
+ {
+ wxString name(isDir ? m_Name + _T("\\") : m_Name);
+ for (size_t i = 0; i < name.length(); i++)
+ if (name[i] == _T('/'))
+ name[i] = _T('\\');
+ return name;
+ }
+
+ case wxPATH_UNIX:
+ return isDir ? m_Name + _T("/") : m_Name;
+
+ default:
+ ;
+ }
+
+ wxFileName fn;
+
+ if (isDir)
+ fn.AssignDir(m_Name, wxPATH_UNIX);
+ else
+ fn.Assign(m_Name, wxPATH_UNIX);
+
+ return fn.GetFullPath(format);
+}
+
+// Static - Internally tars and zips use forward slashes for the path
+// separator, absolute paths aren't allowed, and directory names have a
+// trailing slash. This function converts a path into this internal format,
+// but without a trailing slash for a directory.
+//
+wxString wxZipEntry::GetInternalName(const wxString& name,
+ wxPathFormat format /*=wxPATH_NATIVE*/,
+ bool *pIsDir /*=NULL*/)
+{
+ wxString internal;
+
+ if (wxFileName::GetFormat(format) != wxPATH_UNIX)
+ internal = wxFileName(name, format).GetFullPath(wxPATH_UNIX);
+ else
+ internal = name;
+
+ bool isDir = !internal.empty() && internal.Last() == '/';
+ if (pIsDir)
+ *pIsDir = isDir;
+ if (isDir)
+ internal.erase(internal.length() - 1);
+
+ while (!internal.empty() && *internal.begin() == '/')
+ internal.erase(0, 1);
+ while (!internal.empty() && internal.compare(0, 2, _T("./")) == 0)
+ internal.erase(0, 2);
+ if (internal == _T(".") || internal == _T(".."))
+ internal = wxEmptyString;
+
+ return internal;
+}
+
+void wxZipEntry::SetSystemMadeBy(int system)
+{
+ int mode = GetMode();
+ bool wasUnix = IsMadeByUnix();
+
+ m_SystemMadeBy = (wxUint8)system;
+
+ if (!wasUnix && IsMadeByUnix()) {
+ SetIsDir(IsDir());
+ SetMode(mode);
+ } else if (wasUnix && !IsMadeByUnix()) {
+ m_ExternalAttributes &= 0xffff;
+ }
+}
+
+void wxZipEntry::SetIsDir(bool isDir /*=true*/)
+{
+ if (isDir)
+ m_ExternalAttributes |= wxZIP_A_SUBDIR;
+ else
+ m_ExternalAttributes &= ~wxZIP_A_SUBDIR;
+
+ if (IsMadeByUnix()) {
+ m_ExternalAttributes &= ~wxZIP_S_IFMT;
+ if (isDir)
+ m_ExternalAttributes |= wxZIP_S_IFDIR;
+ else
+ m_ExternalAttributes |= wxZIP_S_IFREG;
+ }
+}
+
+// Return unix style permission bits
+//
+int wxZipEntry::GetMode() const
+{
+ // return unix permissions if present
+ if (IsMadeByUnix())
+ return (m_ExternalAttributes >> 16) & 0777;
+
+ // otherwise synthesize from the dos attribs
+ int mode = 0644;
+ if (m_ExternalAttributes & wxZIP_A_RDONLY)
+ mode &= ~0200;
+ if (m_ExternalAttributes & wxZIP_A_SUBDIR)
+ mode |= 0111;
+
+ return mode;
+}
+
+// Set unix permissions
+//
+void wxZipEntry::SetMode(int mode)
+{
+ // Set dos attrib bits to be compatible
+ if (mode & 0222)
+ m_ExternalAttributes &= ~wxZIP_A_RDONLY;
+ else
+ m_ExternalAttributes |= wxZIP_A_RDONLY;
+
+ // set the actual unix permission bits if the system type allows
+ if (IsMadeByUnix()) {
+ m_ExternalAttributes &= ~(0777L << 16);
+ m_ExternalAttributes |= (mode & 0777L) << 16;
+ }
+}
+
+const char *wxZipEntry::GetExtra() const
+{
+ return m_Extra ? m_Extra->GetData() : NULL;
+}
+
+size_t wxZipEntry::GetExtraLen() const
+{
+ return m_Extra ? m_Extra->GetSize() : 0;
+}
+
+void wxZipEntry::SetExtra(const char *extra, size_t len)
+{
+ Unique(m_Extra, len);
+ if (len)
+ memcpy(m_Extra->GetData(), extra, len);
+}
+
+const char *wxZipEntry::GetLocalExtra() const
+{
+ return m_LocalExtra ? m_LocalExtra->GetData() : NULL;
+}
+
+size_t wxZipEntry::GetLocalExtraLen() const
+{
+ return m_LocalExtra ? m_LocalExtra->GetSize() : 0;
+}
+
+void wxZipEntry::SetLocalExtra(const char *extra, size_t len)
+{
+ Unique(m_LocalExtra, len);
+ if (len)
+ memcpy(m_LocalExtra->GetData(), extra, len);
+}
+
+void wxZipEntry::SetNotifier(wxZipNotifier& notifier)
+{
+ wxArchiveEntry::UnsetNotifier();
+ m_zipnotifier = ¬ifier;
+ m_zipnotifier->OnEntryUpdated(*this);
+}
+
+void wxZipEntry::Notify()
+{
+ if (m_zipnotifier)
+ m_zipnotifier->OnEntryUpdated(*this);
+ else if (GetNotifier())
+ GetNotifier()->OnEntryUpdated(*this);
+}
+
+void wxZipEntry::UnsetNotifier()
+{
+ wxArchiveEntry::UnsetNotifier();
+ m_zipnotifier = NULL;
+}
+
+size_t wxZipEntry::ReadLocal(wxInputStream& stream, wxMBConv& conv)
+{
+ wxUint16 nameLen, extraLen;
+ wxUint32 compressedSize, size, crc;
+
+ wxZipHeader ds(stream, LOCAL_SIZE - 4);
+ if (!ds)
+ return 0;
+
+ ds >> m_VersionNeeded >> m_Flags >> m_Method;
+ SetDateTime(wxDateTime().SetFromDOS(ds.Read32()));
+ ds >> crc >> compressedSize >> size >> nameLen >> extraLen;
+
+ bool sumsValid = (m_Flags & wxZIP_SUMS_FOLLOW) == 0;
+
+ if (sumsValid || crc)
+ m_Crc = crc;
+ if ((sumsValid || compressedSize) || m_Method == wxZIP_METHOD_STORE)
+ m_CompressedSize = compressedSize;
+ if ((sumsValid || size) || m_Method == wxZIP_METHOD_STORE)
+ 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) {
+ stream.Read(m_LocalExtra->GetData(), extraLen);
+ if (stream.LastRead() != extraLen + 0u)
+ return 0;
+ }
+ }
+
+ return LOCAL_SIZE + nameLen + extraLen;
+}
+
+size_t wxZipEntry::WriteLocal(wxOutputStream& stream, wxMBConv& conv) const
+{
+ wxString unixName = GetName(wxPATH_UNIX);
+ const wxWX2MBbuf name_buf = unixName.mb_str(conv);
+ const char *name = name_buf;
+ if (!name) 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 ?
+ wx_truncate_cast(wxUint32, m_CompressedSize) : 0);
+ ds.Write32(m_Size != wxInvalidOffset ?
+ wx_truncate_cast(wxUint32, m_Size) : 0);
+
+ ds << nameLen;
+ wxUint16 extraLen = wx_truncate_cast(wxUint16, GetLocalExtraLen());
+ ds.Write16(extraLen);
+
+ stream.Write(name, nameLen);
+ if (extraLen)
+ stream.Write(m_LocalExtra->GetData(), extraLen);
+
+ return LOCAL_SIZE + nameLen + extraLen;
+}
+
+size_t wxZipEntry::ReadCentral(wxInputStream& stream, wxMBConv& conv)
+{
+ wxUint16 nameLen, extraLen, commentLen;
+
+ wxZipHeader ds(stream, CENTRAL_SIZE - 4);
+ if (!ds)
+ return 0;
+
+ ds >> m_VersionMadeBy >> m_SystemMadeBy;
+
+ SetVersionNeeded(ds.Read16());
+ SetFlags(ds.Read16());
+ SetMethod(ds.Read16());
+ SetDateTime(wxDateTime().SetFromDOS(ds.Read32()));
+ SetCrc(ds.Read32());
+ SetCompressedSize(ds.Read32());
+ SetSize(ds.Read32());
+
+ 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) {
+ stream.Read(m_Extra->GetData(), extraLen);
+ if (stream.LastRead() != extraLen + 0u)
+ return 0;
+ }
+ }
+
+ if (commentLen) {
+ m_Comment = ReadString(stream, commentLen, conv);
+ if (stream.LastRead() != commentLen + 0u)
+ return 0;
+ } else {
+ m_Comment.clear();
+ }
+
+ return CENTRAL_SIZE + nameLen + extraLen + commentLen;
+}
+
+size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const
+{
+ wxString unixName = GetName(wxPATH_UNIX);
+ const wxWX2MBbuf name_buf = unixName.mb_str(conv);
+ const char *name = name_buf;
+ if (!name) name = "";
+ wxUint16 nameLen = wx_truncate_cast(wxUint16, strlen(name));
+
+ const wxWX2MBbuf comment_buf = m_Comment.mb_str(conv);
+ const char *comment = comment_buf;
+ if (!comment) comment = "";
+ wxUint16 commentLen = wx_truncate_cast(wxUint16, strlen(comment));
+
+ wxUint16 extraLen = wx_truncate_cast(wxUint16, GetExtraLen());
+
+ wxDataOutputStream ds(stream);
+
+ ds << CENTRAL_MAGIC << m_VersionMadeBy << m_SystemMadeBy;
+
+ 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(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 << wx_truncate_cast(wxUint32, GetOffset());
+
+ stream.Write(name, nameLen);
+ if (extraLen)
+ stream.Write(GetExtra(), extraLen);
+ stream.Write(comment, commentLen);
+
+ return CENTRAL_SIZE + nameLen + extraLen + commentLen;
+}
+
+// Info-zip prefixes this record with a signature, but pkzip doesn't. So if
+// the 1st value is the signature then it is probably an info-zip record,
+// though there is a small chance that it is in fact a pkzip record which
+// happens to have the signature as it's CRC.
+//
+size_t wxZipEntry::ReadDescriptor(wxInputStream& stream)
+{
+ wxZipHeader ds(stream, SUMS_SIZE);
+ if (!ds)
+ return 0;
+
+ m_Crc = ds.Read32();
+ m_CompressedSize = ds.Read32();
+ m_Size = ds.Read32();
+
+ // if 1st value is the signature then this is probably an info-zip record
+ if (m_Crc == SUMS_MAGIC)
+ {
+ wxZipHeader buf(stream, 8);
+ wxUint32 u1 = buf.GetSize() >= 4 ? buf.Read32() : (wxUint32)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!
+ if (buf.GetSize() > 0)
+ stream.Ungetch(buf.GetData(), buf.GetSize());
+ }
+ else
+ {
+ // it's an info-zip record as expected
+ 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;
+ }
+ }
+
+ return SUMS_SIZE;
+}
+
+size_t wxZipEntry::WriteDescriptor(wxOutputStream& stream, wxUint32 crc,
+ wxFileOffset compressedSize, wxFileOffset size)
+{
+ m_Crc = crc;
+ m_CompressedSize = compressedSize;
+ m_Size = size;
+
+ wxDataOutputStream ds(stream);
+
+ ds.Write32(crc);
+ ds.Write32(wx_truncate_cast(wxUint32, compressedSize));
+ ds.Write32(wx_truncate_cast(wxUint32, size));
+
+ return SUMS_SIZE;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// wxZipEndRec - holds the end of central directory record
+
+class wxZipEndRec
+{
+public:
+ wxZipEndRec();
+
+ int GetDiskNumber() const { return m_DiskNumber; }
+ int GetStartDisk() const { return m_StartDisk; }
+ int GetEntriesHere() const { return m_EntriesHere; }
+ int GetTotalEntries() const { return m_TotalEntries; }
+ wxFileOffset GetSize() const { return m_Size; }
+ wxFileOffset GetOffset() const { return m_Offset; }
+ wxString GetComment() const { return m_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;
+
+private:
+ wxUint16 m_DiskNumber;
+ wxUint16 m_StartDisk;
+ wxUint16 m_EntriesHere;
+ wxUint16 m_TotalEntries;
+ wxUint32 m_Size;
+ wxUint32 m_Offset;
+ wxString m_Comment;
+};
+
+wxZipEndRec::wxZipEndRec()
+ : m_DiskNumber(0),
+ m_StartDisk(0),
+ m_EntriesHere(0),
+ m_TotalEntries(0),
+ m_Size(0),
+ m_Offset(0)
+{
+}
+
+bool wxZipEndRec::Write(wxOutputStream& stream, wxMBConv& conv) const
+{
+ const wxWX2MBbuf comment_buf = m_Comment.mb_str(conv);
+ const char *comment = comment_buf;
+ if (!comment) comment = "";
+ wxUint16 commentLen = (wxUint16)strlen(comment);
+
+ wxDataOutputStream ds(stream);
+
+ ds << END_MAGIC << m_DiskNumber << m_StartDisk << m_EntriesHere
+ << m_TotalEntries << m_Size << m_Offset << commentLen;
+
+ stream.Write(comment, commentLen);
+
+ return stream.IsOk();
+}
+
+bool wxZipEndRec::Read(wxInputStream& stream, wxMBConv& conv)
+{
+ 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) {
+ m_Comment = ReadString(stream, commentLen, conv);
+ if (stream.LastRead() != commentLen + 0u)
+ return false;
+ }
+
+ if (m_DiskNumber != 0 || m_StartDisk != 0 ||
+ m_EntriesHere != m_TotalEntries)
+ wxLogWarning(_("assuming this is a multi-part zip concatenated"));
+
+ return true;
+}
+
+
+/////////////////////////////////////////////////////////////////////////////
+// A weak link from an input stream to an output stream
+
+class wxZipStreamLink
+{
+public:
+ wxZipStreamLink(wxZipOutputStream *stream) : m_ref(1), m_stream(stream) { }
+
+ wxZipStreamLink *AddRef() { m_ref++; return this; }
+ wxZipOutputStream *GetOutputStream() const { return m_stream; }
+
+ void Release(class wxZipInputStream *WXUNUSED(s))
+ { if (--m_ref == 0) delete this; }
+ void Release(class wxZipOutputStream *WXUNUSED(s))
+ { 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
+
+// leave the default wxZipEntryPtr free for users
+wxDECLARE_SCOPED_PTR(wxZipEntry, wxZipEntryPtr_)
+wxDEFINE_SCOPED_PTR (wxZipEntry, wxZipEntryPtr_)
+
+// constructor
+//
+wxZipInputStream::wxZipInputStream(wxInputStream& stream,
+ wxMBConv& conv /*=wxConvLocal*/)
+ : wxArchiveInputStream(stream, conv)
+{
+ Init();
+}
+
+wxZipInputStream::wxZipInputStream(wxInputStream *stream,
+ wxMBConv& conv /*=wxConvLocal*/)
+ : wxArchiveInputStream(stream, conv)
+{
+ Init();
+}
+
+#if WXWIN_COMPATIBILITY_2_6 && wxUSE_FFILE
+
+// Part of the compatibility constructor, which has been made inline to
+// avoid a problem with it not being exported by mingw 3.2.3
+//
+void wxZipInputStream::Init(const wxString& file)
+{
+ // no error messages
+ wxLogNull nolog;
+ Init();
+ m_allowSeeking = true;
+ wxFFileInputStream *ffile;
+ ffile = static_cast<wxFFileInputStream*>(m_parent_i_stream);
+ wxZipEntryPtr_ entry;
+
+ if (ffile->Ok()) {
+ do {
+ entry.reset(GetNextEntry());
+ }
+ while (entry.get() != NULL && entry->GetInternalName() != file);
+ }
+
+ if (entry.get() == NULL)
+ m_lasterror = wxSTREAM_READ_ERROR;
+}
+
+wxInputStream* wxZipInputStream::OpenFile(const wxString& archive)
+{
+ wxLogNull nolog;
+ return new wxFFileInputStream(archive);
+}
+
+#endif // WXWIN_COMPATIBILITY_2_6 && wxUSE_FFILE
+
+void wxZipInputStream::Init()
+{
+ m_store = new wxStoredInputStream(*m_parent_i_stream);
+ m_inflate = NULL;
+ m_rawin = NULL;
+ m_raw = false;
+ m_headerSize = 0;
+ m_decomp = NULL;
+ m_parentSeekable = false;
+ m_weaklinks = new wxZipWeakLinks;
+ m_streamlink = NULL;
+ m_offsetAdjustment = 0;
+ m_position = wxInvalidOffset;
+ m_signature = 0;
+ m_TotalEntries = 0;
+ m_lasterror = m_parent_i_stream->GetLastError();
+#if WXWIN_COMPATIBILITY_2_6
+ m_allowSeeking = false;
+#endif
+}
+
+wxZipInputStream::~wxZipInputStream()
+{
+ CloseDecompressor(m_decomp);
+
+ delete m_store;
+ delete m_inflate;
+ delete m_rawin;
+
+ m_weaklinks->Release(this);
+
+ if (m_streamlink)
+ m_streamlink->Release(this);
+}
+
+wxString wxZipInputStream::GetComment()
+{
+ if (m_position == wxInvalidOffset)
+ if (!LoadEndRecord())
+ return wxEmptyString;
+
+ if (!m_parentSeekable && Eof() && m_signature) {
+ m_lasterror = wxSTREAM_NO_ERROR;
+ m_lasterror = ReadLocal(true);
+ }
+
+ return m_Comment;
+}
+
+int wxZipInputStream::GetTotalEntries()
+{
+ if (m_position == wxInvalidOffset)
+ LoadEndRecord();
+ return m_TotalEntries;
+}
+
+wxZipStreamLink *wxZipInputStream::MakeLink(wxZipOutputStream *out)
+{
+ wxZipStreamLink *link = NULL;
+
+ if (!m_parentSeekable && (IsOpened() || !Eof())) {
+ link = new wxZipStreamLink(out);
+ if (m_streamlink)
+ m_streamlink->Release(this);
+ m_streamlink = link->AddRef();
+ }
+
+ return link;
+}
+
+bool wxZipInputStream::LoadEndRecord()
+{
+ wxCHECK(m_position == wxInvalidOffset, false);
+ if (!IsOk())
+ return false;
+
+ m_position = 0;
+
+ // First find the end-of-central-directory record.
+ if (!FindEndRecord()) {
+ // failed, so either this is a non-seekable stream (ok), or not a zip
+ if (m_parentSeekable) {
+ m_lasterror = wxSTREAM_READ_ERROR;
+ wxLogError(_("invalid zip file"));
+ return false;
+ }
+ else {
+ wxLogNull nolog;
+ wxFileOffset pos = m_parent_i_stream->TellI();
+ if (pos != wxInvalidOffset)
+ m_offsetAdjustment = m_position = pos;
+ return true;
+ }
+ }
+
+ wxZipEndRec endrec;
+
+ // Read in the end record
+ wxFileOffset endPos = m_parent_i_stream->TellI() - 4;
+ if (!endrec.Read(*m_parent_i_stream, GetConv()))
+ return false;
+
+ m_TotalEntries = endrec.GetTotalEntries();
+ m_Comment = endrec.GetComment();
+
+ wxUint32 magic = m_TotalEntries ? CENTRAL_MAGIC : END_MAGIC;
+
+ // Now find the central-directory. we have the file offset of
+ // the CD, so look there first.
+ if (m_parent_i_stream->SeekI(endrec.GetOffset()) != wxInvalidOffset &&
+ ReadSignature() == magic) {
+ m_signature = magic;
+ m_position = endrec.GetOffset();
+ 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.
+ if (m_parent_i_stream->SeekI(endPos - endrec.GetSize())
+ != wxInvalidOffset && ReadSignature() == magic) {
+ m_signature = magic;
+ m_position = endPos - endrec.GetSize();
+ m_offsetAdjustment = m_position - endrec.GetOffset();
+ return true;
+ }
+
+ wxLogError(_("can't find central directory in zip"));
+ m_lasterror = wxSTREAM_READ_ERROR;
+ return false;
+}