1 : // Copyright 2012 Google Inc.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Defines simple streams which can zlib compress or decompress data.
16 :
17 : #ifndef SYZYGY_CORE_ZSTREAM_H_
18 : #define SYZYGY_CORE_ZSTREAM_H_
19 :
20 : #include "syzygy/core/serialization.h"
21 :
22 : // Forward declaration.
23 m : struct z_stream_s;
24 :
25 m : namespace core {
26 :
27 : // A zlib compressing out-stream. Acts as a filter, accepting the uncompressed
28 : // input that is pushed to it, and pushing compressed output to the chained
29 : // stream.
30 m : class ZOutStream : public OutStream {
31 m : public:
32 : // @{
33 : // Constructor.
34 : // @param out_stream the output stream to receive the compressed data.
35 m : explicit ZOutStream(OutStream* out_stream);
36 : // @}
37 :
38 : // Destructor.
39 m : virtual ~ZOutStream();
40 :
41 : // These are effectively forwarded from zlib.h.
42 m : static const int kZDefaultCompression = -1;
43 m : static const int kZNoCompression = 0;
44 m : static const int kZBestSpeed = 1;
45 m : static const int kZBestCompression = 9;
46 :
47 : // @{
48 : // Initializes this compressor. Must be called prior to calling Write.
49 : // @param level the level of compression. Must be kZDefaultCompression (-1),
50 : // or an integer in the range 0..9, inclusive. If not provided defaults to
51 : // Z_DEFAULT_COMPRESSION.
52 : // @returns true on success, false otherwise.
53 m : bool Init();
54 m : bool Init(int level);
55 : // @}
56 :
57 : // @name OutStream implementation.
58 : // @{
59 : // Writes the given buffer of data to the stream. This may or may not produce
60 : // output in the enclosed out-stream.
61 : // @param length the number of bytes to write.
62 : // @param bytes the buffer of data to write.
63 : // @returns true on success, false otherwise.
64 m : virtual bool Write(size_t length, const Byte* bytes) OVERRIDE;
65 : // After a call to Flush the compressed stream is closed and further calls to
66 : // Write will fail. Flush must be called after all writing is finished in
67 : // order for the output to be well-formed. This does not recursively call
68 : // flush on the child stream.
69 : // @returns true on success, false otherwise.
70 m : virtual bool Flush() OVERRIDE;
71 : // @}
72 :
73 m : private:
74 m : struct z_stream_s_close;
75 :
76 m : bool FlushBuffer();
77 :
78 m : scoped_ptr_malloc<z_stream_s, z_stream_s_close> zstream_;
79 m : OutStream* out_stream_;
80 m : std::vector<uint8> buffer_;
81 m : };
82 :
83 : // A zlib decompressing in-stream, decompressing the data from the chained
84 : // input stream and returning decompressed data to the caller.
85 m : class ZInStream : public InStream {
86 m : public:
87 : // Constructor.
88 : // @param in_stream the input stream from which we read compressed data.
89 m : explicit ZInStream(InStream* in_stream);
90 :
91 : // Destructor.
92 m : virtual ~ZInStream();
93 :
94 : // Initializes this decompressor. Must be called prior to calling any read
95 : // functions.
96 m : bool Init();
97 :
98 m : protected:
99 : // InStream implementation.
100 m : virtual bool ReadImpl(
101 m : size_t length, Byte* bytes, size_t* bytes_read) OVERRIDE;
102 :
103 m : private:
104 m : struct z_stream_s_close;
105 :
106 m : scoped_ptr_malloc<z_stream_s, z_stream_s_close> zstream_;
107 m : InStream* in_stream_;
108 m : std::vector<uint8> buffer_;
109 m : };
110 :
111 m : } // namespace core
112 :
113 : #endif // SYZYGY_CORE_ZSTREAM_H_
|