| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/zstream.h |
| 3 | // Purpose: Memory stream classes |
| 4 | // Author: Guilhem Lavaux |
| 5 | // Modified by: Mike Wetherell |
| 6 | // Created: 11/07/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Guilhem Lavaux |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | #ifndef _WX_WXZSTREAM_H__ |
| 12 | #define _WX_WXZSTREAM_H__ |
| 13 | |
| 14 | #include "wx/defs.h" |
| 15 | |
| 16 | #if wxUSE_ZLIB && wxUSE_STREAMS |
| 17 | |
| 18 | #include "wx/stream.h" |
| 19 | #include "wx/versioninfo.h" |
| 20 | |
| 21 | // Compression level |
| 22 | enum wxZlibCompressionLevels { |
| 23 | wxZ_DEFAULT_COMPRESSION = -1, |
| 24 | wxZ_NO_COMPRESSION = 0, |
| 25 | wxZ_BEST_SPEED = 1, |
| 26 | wxZ_BEST_COMPRESSION = 9 |
| 27 | }; |
| 28 | |
| 29 | // Flags |
| 30 | enum wxZLibFlags { |
| 31 | wxZLIB_NO_HEADER = 0, // raw deflate stream, no header or checksum |
| 32 | wxZLIB_ZLIB = 1, // zlib header and checksum |
| 33 | wxZLIB_GZIP = 2, // gzip header and checksum, requires zlib 1.2.1+ |
| 34 | wxZLIB_AUTO = 3 // autodetect header zlib or gzip |
| 35 | }; |
| 36 | |
| 37 | class WXDLLIMPEXP_BASE wxZlibInputStream: public wxFilterInputStream { |
| 38 | public: |
| 39 | wxZlibInputStream(wxInputStream& stream, int flags = wxZLIB_AUTO); |
| 40 | wxZlibInputStream(wxInputStream *stream, int flags = wxZLIB_AUTO); |
| 41 | virtual ~wxZlibInputStream(); |
| 42 | |
| 43 | char Peek() { return wxInputStream::Peek(); } |
| 44 | wxFileOffset GetLength() const { return wxInputStream::GetLength(); } |
| 45 | |
| 46 | static bool CanHandleGZip(); |
| 47 | |
| 48 | bool SetDictionary(const char *data, const size_t datalen); |
| 49 | bool SetDictionary(const wxMemoryBuffer &buf); |
| 50 | |
| 51 | protected: |
| 52 | size_t OnSysRead(void *buffer, size_t size); |
| 53 | wxFileOffset OnSysTell() const { return m_pos; } |
| 54 | |
| 55 | private: |
| 56 | void Init(int flags); |
| 57 | |
| 58 | protected: |
| 59 | size_t m_z_size; |
| 60 | unsigned char *m_z_buffer; |
| 61 | struct z_stream_s *m_inflate; |
| 62 | wxFileOffset m_pos; |
| 63 | |
| 64 | wxDECLARE_NO_COPY_CLASS(wxZlibInputStream); |
| 65 | }; |
| 66 | |
| 67 | class WXDLLIMPEXP_BASE wxZlibOutputStream: public wxFilterOutputStream { |
| 68 | public: |
| 69 | wxZlibOutputStream(wxOutputStream& stream, int level = -1, int flags = wxZLIB_ZLIB); |
| 70 | wxZlibOutputStream(wxOutputStream *stream, int level = -1, int flags = wxZLIB_ZLIB); |
| 71 | virtual ~wxZlibOutputStream() { Close(); } |
| 72 | |
| 73 | void Sync() { DoFlush(false); } |
| 74 | bool Close(); |
| 75 | wxFileOffset GetLength() const { return m_pos; } |
| 76 | |
| 77 | static bool CanHandleGZip(); |
| 78 | |
| 79 | bool SetDictionary(const char *data, const size_t datalen); |
| 80 | bool SetDictionary(const wxMemoryBuffer &buf); |
| 81 | |
| 82 | protected: |
| 83 | size_t OnSysWrite(const void *buffer, size_t size); |
| 84 | wxFileOffset OnSysTell() const { return m_pos; } |
| 85 | |
| 86 | virtual void DoFlush(bool final); |
| 87 | |
| 88 | private: |
| 89 | void Init(int level, int flags); |
| 90 | |
| 91 | protected: |
| 92 | size_t m_z_size; |
| 93 | unsigned char *m_z_buffer; |
| 94 | struct z_stream_s *m_deflate; |
| 95 | wxFileOffset m_pos; |
| 96 | |
| 97 | wxDECLARE_NO_COPY_CLASS(wxZlibOutputStream); |
| 98 | }; |
| 99 | |
| 100 | class WXDLLIMPEXP_BASE wxZlibClassFactory: public wxFilterClassFactory |
| 101 | { |
| 102 | public: |
| 103 | wxZlibClassFactory(); |
| 104 | |
| 105 | wxFilterInputStream *NewStream(wxInputStream& stream) const |
| 106 | { return new wxZlibInputStream(stream); } |
| 107 | wxFilterOutputStream *NewStream(wxOutputStream& stream) const |
| 108 | { return new wxZlibOutputStream(stream, -1); } |
| 109 | wxFilterInputStream *NewStream(wxInputStream *stream) const |
| 110 | { return new wxZlibInputStream(stream); } |
| 111 | wxFilterOutputStream *NewStream(wxOutputStream *stream) const |
| 112 | { return new wxZlibOutputStream(stream, -1); } |
| 113 | |
| 114 | const wxChar * const *GetProtocols(wxStreamProtocolType type |
| 115 | = wxSTREAM_PROTOCOL) const; |
| 116 | |
| 117 | private: |
| 118 | DECLARE_DYNAMIC_CLASS(wxZlibClassFactory) |
| 119 | }; |
| 120 | |
| 121 | class WXDLLIMPEXP_BASE wxGzipClassFactory: public wxFilterClassFactory |
| 122 | { |
| 123 | public: |
| 124 | wxGzipClassFactory(); |
| 125 | |
| 126 | wxFilterInputStream *NewStream(wxInputStream& stream) const |
| 127 | { return new wxZlibInputStream(stream); } |
| 128 | wxFilterOutputStream *NewStream(wxOutputStream& stream) const |
| 129 | { return new wxZlibOutputStream(stream, -1); } |
| 130 | wxFilterInputStream *NewStream(wxInputStream *stream) const |
| 131 | { return new wxZlibInputStream(stream); } |
| 132 | wxFilterOutputStream *NewStream(wxOutputStream *stream) const |
| 133 | { return new wxZlibOutputStream(stream, -1); } |
| 134 | |
| 135 | const wxChar * const *GetProtocols(wxStreamProtocolType type |
| 136 | = wxSTREAM_PROTOCOL) const; |
| 137 | |
| 138 | private: |
| 139 | DECLARE_DYNAMIC_CLASS(wxGzipClassFactory) |
| 140 | }; |
| 141 | |
| 142 | WXDLLIMPEXP_BASE wxVersionInfo wxGetZlibVersionInfo(); |
| 143 | |
| 144 | #endif |
| 145 | // wxUSE_ZLIB && wxUSE_STREAMS |
| 146 | |
| 147 | #endif |
| 148 | // _WX_WXZSTREAM_H__ |
| 149 | |