2 zipstream Library License:
3 --------------------------
5 The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.
7 This software is provided 'as-is', without any express or implied warranty. In
8 no event will the authors be held liable for any damages arising from the use
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it freely,
13 subject to the following restrictions:
15 1. The origin of this software must not be misrepresented; you must not claim
16 that you wrote the original software. If you use this software in a
17 product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
20 2. Altered source versions must be plainly marked as such, and must not be
21 misrepresented as being the original software.
23 3. This notice may not be removed or altered from any source distribution
25 Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
27 Altered by: Andreas Zieringer 2003
28 made it platform independent, gzip conform, fixed gzip footer)
31 #ifndef _OSGZSTREAM_H_
32 #define _OSGZSTREAM_H_
43 /*! Helper function to check whether stream is compressed or not.
44 \ingroup GrpSystemFileIOHelper
48 bool isGZip(std::istream
&is
)
50 const int gz_magic
[2] = {0x1f, 0x8b};
52 int c1
= int(is
.get());
59 int c2
= int(is
.get());
76 #define OSG_ZSTREAM_SUPPORTED
77 #define OSG_ZSTREAM_DATA_HAND_OVER_SUPPORTED
81 #ifdef WIN32 /* Window 95 & Windows NT */
84 #if defined(MACOS) || defined(TARGET_OS_MAC)
88 # define OS_CODE 0x03 /* assume Unix */
92 #pragma warning(disable : 4355)
99 const int gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
102 const int gz_ascii_flag
= 0x01; /* bit 0 set: file probably ascii text */
103 const int gz_head_crc
= 0x02; /* bit 1 set: header CRC present */
104 const int gz_extra_field
= 0x04; /* bit 2 set: extra field present */
105 const int gz_orig_name
= 0x08; /* bit 3 set: original file name present*/
107 const int gz_comment
= 0x10; /* bit 4 set: file comment present */
108 const int gz_reserved
= 0xE0; /* bits 5..7: reserved */
111 /// default gzip buffer size,
112 /// change this to suite your needs
113 const size_t zstream_default_buffer_size
= 4096;
115 /// Compression strategy, see zlib doc.
118 StrategyFiltered
= 1,
119 StrategyHuffmanOnly
= 2,
124 //*****************************************************************************
125 // template class basic_zip_streambuf
126 //*****************************************************************************
128 /*! \brief A stream decorator that takes raw input and zips it to a ostream.
129 \ingroup GrpSystemFileIOHelper
130 The class wraps up the inflate method of the zlib library 1.1.4
131 http://www.gzip.org/zlib/
133 template <class charT
,
134 class traits
= std::char_traits
<charT
> >
135 class basic_zip_streambuf
: public std::basic_streambuf
<charT
, traits
>
140 typedef std::basic_ostream
<charT
, traits
>& ostream_reference
;
141 typedef unsigned char byte_type
;
142 typedef char char_type
;
143 typedef byte_type
* byte_buffer_type
;
144 typedef std::vector
<byte_type
> byte_vector_type
;
145 typedef std::vector
<char_type
> char_vector_type
;
146 typedef int int_type
;
147 typedef basic_zip_streambuf Self
;
148 typedef std::basic_streambuf
<charT
, traits
> Inherited
;
150 basic_zip_streambuf(ostream_reference ostream
,
156 ~basic_zip_streambuf(void);
159 int_type
overflow (int_type c
);
160 std::streamsize
flush (void);
161 ostream_reference
get_ostream (void) const;
162 int get_zerr (void) const;
163 unsigned long get_crc (void) const;
164 unsigned long get_in_size (void) const;
165 long get_out_size(void) const;
170 bool zip_to_stream(char_type
*buffer
, std::streamsize buffer_size
);
172 ostream_reference _ostream
;
173 z_stream _zip_stream
;
175 byte_vector_type _output_buffer
;
176 char_vector_type _buffer
;
180 //*****************************************************************************
181 // template class basic_unzip_streambuf
182 //*****************************************************************************
184 /*! \brief A stream decorator that takes compressed input and unzips it to a
187 \ingroup GrpSystemFileIOHelper
189 The class wraps up the deflate method of the zlib library 1.1.4
190 http://www.gzip.org/zlib/
193 template <class charT
,
194 class traits
= std::char_traits
<charT
> >
195 class basic_unzip_streambuf
:
196 public std::basic_streambuf
<charT
, traits
>
200 typedef std::basic_istream
<charT
,traits
>& istream_reference
;
201 typedef unsigned char byte_type
;
202 typedef char char_type
;
203 typedef byte_type
* byte_buffer_type
;
204 typedef std::vector
<byte_type
> byte_vector_type
;
205 typedef std::vector
<char_type
> char_vector_type
;
206 typedef int int_type
;
208 /** Construct a unzip stream
209 * More info on the following parameters can be found in the zlib
212 basic_unzip_streambuf(istream_reference istream
,
214 size_t read_buffer_size
,
215 size_t input_buffer_size
);
216 ~basic_unzip_streambuf(void);
219 int_type
underflow (void);
220 istream_reference
get_istream (void);
221 z_stream
&get_zip_stream(void);
222 int get_zerr (void) const;
223 unsigned long get_crc (void) const;
224 long get_out_size (void) const;
225 long get_in_size (void) const;
229 byte_vector_type _input_buffer
;
230 char_vector_type _buffer
;
232 enum StreamType
{ UNKNOWN_ST
, GZIP_ST
, DATA_ST
};
234 StreamType _streamType
;
238 void put_back_from_zip_stream(void );
239 std::streamsize
unzip_from_stream (char_type
*buffer
,
240 std::streamsize buffer_size
);
241 size_t fill_input_buffer (void );
243 istream_reference _istream
;
244 z_stream _zip_stream
;
249 // ****************************************************************************
250 // template class basic_zip_ostream
251 // ****************************************************************************
253 /*! \ingroup GrpSystemFileIOHelper
256 template <class charT
,
257 class traits
= std::char_traits
<charT
> >
258 class basic_zip_ostream
:
259 private basic_zip_streambuf
<charT
, traits
>,
260 public std::basic_ostream
<charT
, traits
>
264 typedef char char_type
;
265 typedef std::basic_ostream
<charT
, traits
>& ostream_reference
;
267 explicit basic_zip_ostream(ostream_reference ostream
,
269 int level
= Z_DEFAULT_COMPRESSION
,
270 EStrategy strategy
= DefaultStrategy
,
271 int window_size
= -15 /*windowBits is passed < 0 to suppress zlib header */,
272 int memory_level
= 8,
273 size_t buffer_size
= zstream_default_buffer_size
) ;
274 ~basic_zip_ostream(void);
276 bool is_gzip (void) const;
277 basic_zip_ostream
<charT
, traits
> &zflush (void);
282 basic_zip_ostream
<charT
,traits
>& add_header(void);
283 basic_zip_ostream
<charT
,traits
>& add_footer(void);
289 // ****************************************************************************
290 // template class basic_zip_istream
291 // ****************************************************************************
293 /*! \ingroup GrpSystemFileIOHelper
296 template <class charT
,
297 class traits
= std::char_traits
<charT
> >
298 class basic_zip_istream
:
299 public basic_unzip_streambuf
<charT
, traits
>,
300 public std::basic_istream
<charT
, traits
>
303 typedef basic_unzip_streambuf
<charT
, traits
> BufferType
;
305 typedef std::basic_istream
<charT
, traits
>& istream_reference
;
307 explicit basic_zip_istream(istream_reference istream
,
308 int window_size
= -15 /*windowBits is passed < 0 to suppress zlib header */,
309 size_t read_buffer_size
= zstream_default_buffer_size
,
310 size_t input_buffer_size
= zstream_default_buffer_size
);
312 bool is_gzip (void) const;
313 bool check_crc (void);
314 bool check_data_size (void) const;
315 long get_gzip_crc (void) const;
316 long get_gzip_data_size(void) const;
320 int check_header(void);
321 void read_footer(void);
325 long _gzip_data_size
;
330 #include "OSGZStream.inl"
334 /*! A typedef for basic_zip_ostream<char>
335 \ingroup GrpSystemFileIOHelper
338 typedef basic_zip_ostream
<char> zip_ostream
;
340 /// A typedef for basic_zip_ostream<wchar_t>
341 //typedef basic_zip_ostream<wchar_t> zip_wostream;
343 /*! A typedef for basic_zip_istream<char>
344 \ingroup GrpSystemFileIOHelper
347 typedef basic_zip_istream
<char> zip_istream
;
349 /// A typedef for basic_zip_istream<wchart>
350 //typedef basic_zip_istream<wchar_t> zip_wistream;
355 #pragma warning(default : 4355)
358 #endif // OSG_WITH_ZLIB
360 #endif // _OSGZSTREAM_H_