1 : // Copyright 2012 Google Inc. All Rights Reserved.
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 "base/memory/scoped_ptr.h"
21 : #include "syzygy/core/serialization.h"
22 :
23 : // Forward declaration.
24 m : struct z_stream_s;
25 :
26 m : namespace core {
27 :
28 : // A zlib compressing out-stream. Acts as a filter, accepting the uncompressed
29 : // input that is pushed to it, and pushing compressed output to the chained
30 : // stream.
31 m : class ZOutStream : public OutStream {
32 m : public:
33 : // @{
34 : // Constructor.
35 : // @param out_stream the output stream to receive the compressed data.
36 m : explicit ZOutStream(OutStream* out_stream);
37 : // @}
38 :
39 : // Destructor.
40 m : virtual ~ZOutStream();
41 :
42 : // These are effectively forwarded from zlib.h.
43 m : static const int kZDefaultCompression = -1;
44 m : static const int kZNoCompression = 0;
45 m : static const int kZBestSpeed = 1;
46 m : static const int kZBestCompression = 9;
47 :
48 : // @{
49 : // Initializes this compressor. Must be called prior to calling Write.
50 : // @param level the level of compression. Must be kZDefaultCompression (-1),
51 : // or an integer in the range 0..9, inclusive. If not provided defaults to
52 : // Z_DEFAULT_COMPRESSION.
53 : // @returns true on success, false otherwise.
54 m : bool Init();
55 m : bool Init(int level);
56 : // @}
57 :
58 : // @name OutStream implementation.
59 : // @{
60 : // Writes the given buffer of data to the stream. This may or may not produce
61 : // output in the enclosed out-stream.
62 : // @param length the number of bytes to write.
63 : // @param bytes the buffer of data to write.
64 : // @returns true on success, false otherwise.
65 m : virtual bool Write(size_t length, const Byte* bytes) override;
66 : // After a call to Flush the compressed stream is closed and further calls to
67 : // Write will fail. Flush must be called after all writing is finished in
68 : // order for the output to be well-formed. This does not recursively call
69 : // flush on the child stream.
70 : // @returns true on success, false otherwise.
71 m : virtual bool Flush() override;
72 : // @}
73 :
74 m : private:
75 : // Functor that takes care of cleaning up a zstream object that was
76 : // initialized with deflateInit.
77 m : struct z_stream_s_close {
78 m : inline void operator()(z_stream_s* zstream) const;
79 m : };
80 :
81 m : bool FlushBuffer();
82 :
83 m : scoped_ptr<z_stream_s, z_stream_s_close> zstream_;
84 m : OutStream* out_stream_;
85 m : std::vector<uint8> buffer_;
86 m : };
87 :
88 : // A zlib decompressing in-stream, decompressing the data from the chained
89 : // input stream and returning decompressed data to the caller.
90 m : class ZInStream : public InStream {
91 m : public:
92 : // Constructor.
93 : // @param in_stream the input stream from which we read compressed data.
94 m : explicit ZInStream(InStream* in_stream);
95 :
96 : // Destructor.
97 m : virtual ~ZInStream();
98 :
99 : // Initializes this decompressor. Must be called prior to calling any read
100 : // functions.
101 m : bool Init();
102 :
103 m : protected:
104 : // InStream implementation.
105 m : virtual bool ReadImpl(size_t length,
106 m : Byte* bytes,
107 m : size_t* bytes_read) override;
108 :
109 m : private:
110 : // Functor that takes care of cleaning up a zstream object that was
111 : // initialized with inflateInit.
112 m : struct z_stream_s_close {
113 m : inline void operator()(z_stream_s* zstream) const;
114 m : };
115 :
116 m : scoped_ptr<z_stream_s, z_stream_s_close> zstream_;
117 m : InStream* in_stream_;
118 m : std::vector<uint8> buffer_;
119 m : };
120 :
121 m : } // namespace core
122 :
123 : #endif // SYZYGY_CORE_ZSTREAM_H_
|