root/trunk/whisperlib/common/io/zlib/zlibwrapper.h

Revision 7, 6.2 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Authors: Catalin Popescu
31
32 //
33 // A wrapper for zlib to be used with our own io::MemoryStream
34 //
35 #ifndef __COMMON_IO_ZLIB_ZLIBWRAPPER_H__
36 #define __COMMON_IO_ZLIB_ZLIBWRAPPER_H__
37
38 #include <zlib.h>
39 #include <whisperlib/common/io/buffer/memory_stream.h>
40
41 namespace io {
42
43 //////////////////////////////////////////////////////////////////////
44
45 class ZlibDeflateWrapper {
46  public:
47   explicit ZlibDeflateWrapper(int compress_level = Z_DEFAULT_COMPRESSION);
48   ~ZlibDeflateWrapper();
49   void Clear();
50   // Compresses *at most* *size bytes from in and writes the result to out.
51   // Updates *size to reflect the leftover bytes (*size -= in->Size())
52   // If we are able to extract *size bytes from in we also flush the
53   // compression trailer in out and end the compression process.
54   // You can call multiple times Compress, as data becomes available in 'in'
55   // Return false *iff* we got some Zlib errors.
56   bool DeflateSize(io::MemoryStream* in, io::MemoryStream* out,
57                    int32* size);
58
59   // Compresses the entire content of in and appends it to out.
60   // Returns true on success and false on some error.
61   bool Deflate(io::MemoryStream* in, io::MemoryStream* out) {
62     Clear();   // just in case..
63     int32 size = in->Size();
64     return DeflateSize(in, out, &size);
65   }
66   // Compresses the entire buffer of in and appends it to out.
67   // Returns true on success and false on some error.
68   bool Deflate(const char* in, int in_size, io::MemoryStream* out);
69
70  private:
71   bool Initialize();
72   const int compress_level_;
73   bool initialized_;
74   z_stream strm_;
75
76   DISALLOW_EVIL_CONSTRUCTORS(ZlibDeflateWrapper);
77 };
78
79 //////////////////////////////////////////////////////////////////////
80
81 class ZlibInflateWrapper {
82  public:
83   ZlibInflateWrapper();
84   ~ZlibInflateWrapper();
85   void Clear();
86
87   // Decompresses the entire content of in and appends it to out.
88   // Return Zlib error (see bellow)
89   int Inflate(io::MemoryStream* in, io::MemoryStream* out) {
90     Clear();      // just in case..
91     int32 size = in->Size();
92     return InflateSize(in, out, &size);
93   }
94   // Decompresses *at most* *size bytes from in and writes the result to out.
95   // Updates *size to reflect the leftover bytes (*size -= in->Size())
96   // If we are able to extract *size bytes from in we also flush the
97   // compression trailer in out and end the compression process.
98   // You can call multiple times DecompressSize, as data becomes available
99   // in 'in'.
100   // A NULL size would decompress untill the stream end..
101   // Returns the Zlib error:
102   //   -- on Z_OK we decompressed ok up to size (or the available data in in)
103   //   -- on Z_STREAM_END - the entire data begun to be decompressed
104   //      was decompressed ok
105   //   -- anything else - some sort of error..
106   int InflateSize(io::MemoryStream* in, io::MemoryStream* out,
107                   int32* size = NULL);
108  private:
109   bool initialized_;
110   z_stream strm_;
111
112   DISALLOW_EVIL_CONSTRUCTORS(ZlibInflateWrapper);
113 };
114
115 ////////////////////////////////////////////////////////////////////////////////
116
117 class ZlibGzipEncodeWrapper {
118  public:
119   explicit ZlibGzipEncodeWrapper(int compress_level = Z_DEFAULT_COMPRESSION);
120   ~ZlibGzipEncodeWrapper();
121
122   // Compresses the entire content of in and appends it to out. It
123   // writes a new gzip header, computes size and crc
124   void Encode(io::MemoryStream* in, io::MemoryStream* out);
125
126   // Begins encoding in the given buffer
127   void BeginEncoding(io::MemoryStream* out);
128   void ContinueEncoding(io::MemoryStream* in, io::MemoryStream* out);
129   void EndEncoding(io::MemoryStream* out);
130
131  private:
132   void InitStream();
133   const int compress_level_;
134   z_stream strm_;
135   int32 crc_;
136   int32 input_size_;
137
138   DISALLOW_EVIL_CONSTRUCTORS(ZlibGzipEncodeWrapper);
139 };
140
141 //////////////////////////////////////////////////////////////////////
142
143 class ZlibGzipDecodeWrapper {
144  public:
145   explicit ZlibGzipDecodeWrapper(bool strict_trailer_checking = true);
146   ~ZlibGzipDecodeWrapper();
147
148   // Decompreses the entire content and appends the decoded content to out.
149   // Returns Zlib error code.
150   //   -- on Z_OK we decompressed ok up to size (or the available data in in)
151   //   -- on Z_STREAM_END - the entire data begun to be decompressed
152   //      was decompressed ok
153   //   -- anything else - some sort of error..
154   int Decode(io::MemoryStream* in, io::MemoryStream* out);
155
156   // Basically if we have less then this, there is no data..
157   static const int kMinGzipDataSize = 18;
158
159  private:
160   void InitStream();
161
162   enum State {
163     INITIALIZED,
164     DECODING,
165     CHECKING,
166     FINALIZED,
167   };
168
169   const bool strict_trailer_checking_;
170
171   State state_;
172   z_stream strm_;
173   int last_zlib_err_;
174   bool header_passed_;
175   uint32 running_crc_;
176   uint32 running_size_;
177
178   DISALLOW_EVIL_CONSTRUCTORS(ZlibGzipDecodeWrapper);
179 };
180 }
181
182 #endif  // __COMMON_IO_ZLIB_ZLIBWRAPPER_H__
Note: See TracBrowser for help on using the browser.