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: zstream.h 54688 2008-07-18 08:06:44Z MW $
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 
20 // Compression level
21 enum {
22     wxZ_DEFAULT_COMPRESSION = -1,
23     wxZ_NO_COMPRESSION = 0,
24     wxZ_BEST_SPEED = 1,
25     wxZ_BEST_COMPRESSION = 9
26 };
27 
28 // Flags
29 enum {
30 #if WXWIN_COMPATIBILITY_2_4
31     wxZLIB_24COMPATIBLE = 4, // read v2.4.x data without error
32 #endif
33     wxZLIB_NO_HEADER = 0,    // raw deflate stream, no header or checksum
34     wxZLIB_ZLIB = 1,         // zlib header and checksum
35     wxZLIB_GZIP = 2,         // gzip header and checksum, requires zlib 1.2.1+
36     wxZLIB_AUTO = 3          // autodetect header zlib or gzip
37 };
38 
39 class WXDLLIMPEXP_BASE wxZlibInputStream: public wxFilterInputStream {
40  public:
41   wxZlibInputStream(wxInputStream& stream, int flags = wxZLIB_AUTO);
42   wxZlibInputStream(wxInputStream *stream, int flags = wxZLIB_AUTO);
43   virtual ~wxZlibInputStream();
44 
Peek()45   char Peek() { return wxInputStream::Peek(); }
GetLength()46   wxFileOffset GetLength() const { return wxInputStream::GetLength(); }
47 
48   static bool CanHandleGZip();
49 
50  protected:
51   size_t OnSysRead(void *buffer, size_t size);
OnSysTell()52   wxFileOffset OnSysTell() const { return m_pos; }
53 
54  private:
55   void Init(int flags);
56 
57  protected:
58   size_t m_z_size;
59   unsigned char *m_z_buffer;
60   struct z_stream_s *m_inflate;
61   wxFileOffset m_pos;
62 #if WXWIN_COMPATIBILITY_2_4
63   bool m_24compatibilty;
64 #endif
65 
66   DECLARE_NO_COPY_CLASS(wxZlibInputStream)
67 };
68 
69 class WXDLLIMPEXP_BASE wxZlibOutputStream: public wxFilterOutputStream {
70  public:
71   wxZlibOutputStream(wxOutputStream& stream, int level = -1, int flags = wxZLIB_ZLIB);
72   wxZlibOutputStream(wxOutputStream *stream, int level = -1, int flags = wxZLIB_ZLIB);
~wxZlibOutputStream()73   virtual ~wxZlibOutputStream() { Close(); }
74 
Sync()75   void Sync() { DoFlush(false); }
76   bool Close();
GetLength()77   wxFileOffset GetLength() const { return m_pos; }
78 
79   static bool CanHandleGZip();
80 
81  protected:
82   size_t OnSysWrite(const void *buffer, size_t size);
OnSysTell()83   wxFileOffset OnSysTell() const { return m_pos; }
84 
85   virtual void DoFlush(bool final);
86 
87  private:
88   void Init(int level, int flags);
89 
90  protected:
91   size_t m_z_size;
92   unsigned char *m_z_buffer;
93   struct z_stream_s *m_deflate;
94   wxFileOffset m_pos;
95 
96   DECLARE_NO_COPY_CLASS(wxZlibOutputStream)
97 };
98 
99 class WXDLLIMPEXP_BASE wxZlibClassFactory: public wxFilterClassFactory
100 {
101 public:
102     wxZlibClassFactory();
103 
NewStream(wxInputStream & stream)104     wxFilterInputStream *NewStream(wxInputStream& stream) const
105         { return new wxZlibInputStream(stream); }
NewStream(wxOutputStream & stream)106     wxFilterOutputStream *NewStream(wxOutputStream& stream) const
107         { return new wxZlibOutputStream(stream, -1); }
NewStream(wxInputStream * stream)108     wxFilterInputStream *NewStream(wxInputStream *stream) const
109         { return new wxZlibInputStream(stream); }
NewStream(wxOutputStream * stream)110     wxFilterOutputStream *NewStream(wxOutputStream *stream) const
111         { return new wxZlibOutputStream(stream, -1); }
112 
113     const wxChar * const *GetProtocols(wxStreamProtocolType type
114                                        = wxSTREAM_PROTOCOL) const;
115 
116 private:
117     DECLARE_DYNAMIC_CLASS(wxZlibClassFactory)
118 };
119 
120 class WXDLLIMPEXP_BASE wxGzipClassFactory: public wxFilterClassFactory
121 {
122 public:
123     wxGzipClassFactory();
124 
NewStream(wxInputStream & stream)125     wxFilterInputStream *NewStream(wxInputStream& stream) const
126         { return new wxZlibInputStream(stream); }
NewStream(wxOutputStream & stream)127     wxFilterOutputStream *NewStream(wxOutputStream& stream) const
128         { return new wxZlibOutputStream(stream, -1, wxZLIB_GZIP); }
NewStream(wxInputStream * stream)129     wxFilterInputStream *NewStream(wxInputStream *stream) const
130         { return new wxZlibInputStream(stream); }
NewStream(wxOutputStream * stream)131     wxFilterOutputStream *NewStream(wxOutputStream *stream) const
132         { return new wxZlibOutputStream(stream, -1, wxZLIB_GZIP); }
133 
134     const wxChar * const *GetProtocols(wxStreamProtocolType type
135                                        = wxSTREAM_PROTOCOL) const;
136 
137 private:
138     DECLARE_DYNAMIC_CLASS(wxGzipClassFactory)
139 };
140 
141 #endif
142   // wxUSE_ZLIB && wxUSE_STREAMS
143 
144 #endif
145    // _WX_WXZSTREAM_H__
146 
147