1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __OGRE_DEFLATE_H__ |
---|
29 | #define __OGRE_DEFLATE_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | #include "OgreDataStream.h" |
---|
33 | #include "OgreHeaderPrefix.h" |
---|
34 | |
---|
35 | /// forward decls |
---|
36 | struct z_stream_s; |
---|
37 | typedef struct z_stream_s z_stream; |
---|
38 | |
---|
39 | namespace Ogre |
---|
40 | { |
---|
41 | /** Stream which compresses / uncompresses data using the 'deflate' compression |
---|
42 | algorithm. |
---|
43 | @remarks |
---|
44 | This stream is designed to wrap another stream for the actual source / destination |
---|
45 | of the compressed data, it has no concrete source / data itself. The idea is |
---|
46 | that you pass uncompressed data through this stream, and the underlying |
---|
47 | stream reads/writes compressed data to the final source. |
---|
48 | @note |
---|
49 | This is an alternative to using a compressed archive since it is able to |
---|
50 | compress & decompress regardless of the actual source of the stream. |
---|
51 | You should avoid using this with already compressed archives. |
---|
52 | Also note that this cannot be used as a read / write stream, only a read-only |
---|
53 | or write-only stream. |
---|
54 | */ |
---|
55 | class _OgreExport DeflateStream : public DataStream |
---|
56 | { |
---|
57 | protected: |
---|
58 | DataStreamPtr mCompressedStream; |
---|
59 | DataStreamPtr mTmpWriteStream; |
---|
60 | String mTempFileName; |
---|
61 | z_stream* mZStream; |
---|
62 | size_t mCurrentPos; |
---|
63 | size_t mAvailIn; |
---|
64 | |
---|
65 | /// Cache for read data in case skipping around |
---|
66 | StaticCache<16 * OGRE_STREAM_TEMP_SIZE> mReadCache; |
---|
67 | |
---|
68 | /// Intermediate buffer for read / write |
---|
69 | unsigned char *mTmp; |
---|
70 | |
---|
71 | /// Whether the underlying stream is valid compressed data |
---|
72 | bool mIsCompressedValid; |
---|
73 | |
---|
74 | void init(); |
---|
75 | void destroy(); |
---|
76 | void compressFinal(); |
---|
77 | |
---|
78 | size_t getAvailInForSinglePass(); |
---|
79 | public: |
---|
80 | /** Constructor for creating unnamed stream wrapping another stream. |
---|
81 | @param compressedStream The stream that this stream will use when reading / |
---|
82 | writing compressed data. The access mode from this stream will be matched. |
---|
83 | @param tmpFileName Path/Filename to be used for temporary storage of incoming data |
---|
84 | @param avail_in Available data length to be uncompressed. With it we can uncompress |
---|
85 | DataStream partly. |
---|
86 | */ |
---|
87 | DeflateStream(const DataStreamPtr& compressedStream, const String& tmpFileName = "", |
---|
88 | size_t avail_in = 0); |
---|
89 | /** Constructor for creating named stream wrapping another stream. |
---|
90 | @param name The name to give this stream |
---|
91 | @param compressedStream The stream that this stream will use when reading / |
---|
92 | writing compressed data. The access mode from this stream will be matched. |
---|
93 | @param tmpFileName Path/Filename to be used for temporary storage of incoming data |
---|
94 | @param avail_in Available data length to be uncompressed. With it we can uncompress |
---|
95 | DataStream partly. |
---|
96 | */ |
---|
97 | DeflateStream(const String& name, const DataStreamPtr& compressedStream, const String& tmpFileName="", |
---|
98 | size_t avail_in = 0); |
---|
99 | |
---|
100 | ~DeflateStream(); |
---|
101 | |
---|
102 | /** Returns whether the compressed stream is valid deflated data. |
---|
103 | @remarks |
---|
104 | If you pass this class a READ stream which is not compressed with the |
---|
105 | deflate algorithm, this method returns false and all read commands |
---|
106 | will actually be executed as passthroughs as a fallback. |
---|
107 | */ |
---|
108 | bool isCompressedStreamValid() const { return mIsCompressedValid; } |
---|
109 | |
---|
110 | /** @copydoc DataStream::read |
---|
111 | */ |
---|
112 | size_t read(void* buf, size_t count); |
---|
113 | |
---|
114 | /** @copydoc DataStream::write |
---|
115 | */ |
---|
116 | size_t write(const void* buf, size_t count); |
---|
117 | |
---|
118 | /** @copydoc DataStream::skip |
---|
119 | */ |
---|
120 | void skip(long count); |
---|
121 | |
---|
122 | /** @copydoc DataStream::seek |
---|
123 | */ |
---|
124 | void seek( size_t pos ); |
---|
125 | |
---|
126 | /** @copydoc DataStream::tell |
---|
127 | */ |
---|
128 | size_t tell(void) const; |
---|
129 | |
---|
130 | /** @copydoc DataStream::eof |
---|
131 | */ |
---|
132 | bool eof(void) const; |
---|
133 | |
---|
134 | /** @copydoc DataStream::close |
---|
135 | */ |
---|
136 | void close(void); |
---|
137 | |
---|
138 | }; |
---|
139 | } |
---|
140 | |
---|
141 | #include "OgreHeaderSuffix.h" |
---|
142 | |
---|
143 | #endif |
---|