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 for OpenSG project
28
made it platform independent, gzip conform, fixed gzip footer
30
Altered by: Geoffrey Hutchison 2005 for Open Babel project
31
minor namespace modifications, VC++ compatibility
46
#ifdef WIN32 /* Window 95 & Windows NT */
49
#if defined(MACOS) || defined(TARGET_OS_MAC)
53
# define OS_CODE 0x03 /* assume Unix */
56
namespace zlib_stream {
60
const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
63
const int gz_ascii_flag = 0x01; /* bit 0 set: file probably ascii text */
64
const int gz_head_crc = 0x02; /* bit 1 set: header CRC present */
65
const int gz_extra_field = 0x04; /* bit 2 set: extra field present */
66
const int gz_orig_name = 0x08; /* bit 3 set: original file name present */
67
const int gz_comment = 0x10; /* bit 4 set: file comment present */
68
const int gz_reserved = 0xE0; /* bits 5..7: reserved */
71
/// default gzip buffer size,
72
/// change this to suite your needs
73
const size_t zstream_default_buffer_size = 4096;
75
/// Compression strategy, see zlib doc.
79
StrategyHuffmanOnly = 2,
83
//*****************************************************************************
84
// template class basic_zip_streambuf
85
//*****************************************************************************
87
/** \brief A stream decorator that takes raw input and zips it to a ostream.
89
The class wraps up the inflate method of the zlib library 1.1.4 http://www.gzip.org/zlib/
91
template <class charT,
92
class traits = std::char_traits<charT> >
93
class basic_zip_streambuf : public std::basic_streambuf<charT, traits>
96
typedef std::basic_ostream<charT, traits>& ostream_reference;
97
typedef unsigned char byte_type;
98
typedef char char_type;
99
typedef byte_type* byte_buffer_type;
100
typedef std::vector<byte_type> byte_vector_type;
101
typedef std::vector<char_type> char_vector_type;
102
typedef int int_type;
104
basic_zip_streambuf(ostream_reference ostream,
111
~basic_zip_streambuf(void);
114
int_type overflow (int_type c);
115
std::streamsize flush (void);
117
ostream_reference get_ostream (void) const;
119
int get_zerr (void) const;
121
unsigned long get_crc (void) const;
123
unsigned long get_in_size (void) const;
125
long get_out_size(void) const;
129
bool zip_to_stream(char_type *buffer,
130
std::streamsize buffer_size);
132
ostream_reference _ostream;
133
z_stream _zip_stream;
135
byte_vector_type _output_buffer;
136
char_vector_type _buffer;
141
//*****************************************************************************
142
// template class basic_unzip_streambuf
143
//*****************************************************************************
145
/** \brief A stream decorator that takes compressed input and unzips it to a istream.
147
The class wraps up the deflate method of the zlib library 1.1.4 http://www.gzip.org/zlib/
149
template <class charT,
150
class traits = std::char_traits<charT> >
151
class basic_unzip_streambuf :
152
public std::basic_streambuf<charT, traits>
155
typedef std::basic_istream<charT,traits>& istream_reference;
156
typedef unsigned char byte_type;
157
typedef char char_type;
158
typedef byte_type* byte_buffer_type;
159
typedef std::vector<byte_type> byte_vector_type;
160
typedef std::vector<char_type> char_vector_type;
161
typedef int int_type;
163
/** Construct a unzip stream
164
* More info on the following parameters can be found in the zlib documentation.
166
basic_unzip_streambuf(istream_reference istream,
168
size_t read_buffer_size,
169
size_t input_buffer_size);
171
~basic_unzip_streambuf(void);
173
int_type underflow(void);
175
/// returns the compressed input istream
177
istream_reference get_istream (void);
179
z_stream& get_zip_stream (void);
181
int get_zerr (void) const;
183
unsigned long get_crc (void) const;
185
long get_out_size (void) const;
187
long get_in_size (void) const;
192
void put_back_from_zip_stream (void);
194
std::streamsize unzip_from_stream (char_type* buffer,
195
std::streamsize buffer_size);
197
size_t fill_input_buffer (void);
199
istream_reference _istream;
200
z_stream _zip_stream;
202
byte_vector_type _input_buffer;
203
char_vector_type _buffer;
207
//*****************************************************************************
208
// template class basic_zip_ostream
209
//*****************************************************************************
211
template <class charT,
212
class traits = std::char_traits<charT> >
213
class basic_zip_ostream :
214
public basic_zip_streambuf<charT, traits>,
215
public std::basic_ostream<charT, traits>
219
typedef char char_type;
220
typedef std::basic_ostream<charT, traits>& ostream_reference;
221
typedef std::basic_ostream<charT, traits> ostream_type;
224
explicit basic_zip_ostream(ostream_reference ostream,
225
bool is_gzip = false,
226
int level = Z_DEFAULT_COMPRESSION,
227
EStrategy strategy = DefaultStrategy,
228
int window_size = -15 /*windowBits is passed < 0 to suppress zlib header */,
229
int memory_level = 8,
230
size_t buffer_size = zstream_default_buffer_size);
232
~basic_zip_ostream(void);
235
bool is_gzip (void) const;
237
basic_zip_ostream<charT, traits>& zflush (void);
238
void finished (void);
241
{ add_header(); _is_gzip = true; }
245
basic_zip_ostream<charT,traits>& add_header(void);
246
basic_zip_ostream<charT,traits>& add_footer(void);
252
//*****************************************************************************
253
// template class basic_zip_istream
254
//*****************************************************************************
256
template <class charT,
257
class traits = std::char_traits<charT> >
258
class basic_zip_istream :
259
public basic_unzip_streambuf<charT, traits>,
260
public std::basic_istream<charT, traits>
263
typedef std::basic_istream<charT, traits>& istream_reference;
264
typedef std::basic_istream<charT, traits> istream_type;
266
explicit basic_zip_istream(istream_reference istream,
267
int window_size = -15 /*windowBits is passed < 0 to suppress zlib header */,
268
size_t read_buffer_size = zstream_default_buffer_size,
269
size_t input_buffer_size = zstream_default_buffer_size);
272
bool is_gzip (void) const;
274
bool check_crc (void);
276
bool check_data_size (void) const;
278
long get_gzip_crc (void) const;
280
long get_gzip_data_size(void) const;
284
int check_header (void);
285
void read_footer (void);
289
long _gzip_data_size;
292
/// A typedef for basic_zip_ostream<char>
293
typedef basic_zip_ostream<char> zip_ostream;
294
/// A typedef for basic_zip_istream<char>
295
typedef basic_zip_istream<char> zip_istream;
297
/// A typedef for basic_zip_ostream<wchar_t>
298
//typedef basic_zip_ostream<wchar_t> zip_wostream;
299
/// A typedef for basic_zip_istream<wchart>
300
//typedef basic_zip_istream<wchar_t> zip_wistream;
302
//! Helper function to check whether stream is compressed or not.
303
inline bool isGZip(std::istream &is)
305
const int gz_magic[2] = {0x1f, 0x8b};
307
int c1 = (int) is.get();
308
if(c1 != gz_magic[0])
314
int c2 = (int) is.get();
315
if(c2 != gz_magic[1])
327
#include "zipstreamimpl.h"
331
#endif // _ZIPSTREAM_H_