1 |
tim |
741 |
/* |
2 |
|
|
zipstream Library License: |
3 |
|
|
-------------------------- |
4 |
|
|
|
5 |
|
|
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux. |
6 |
|
|
|
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 |
9 |
|
|
of this software. |
10 |
|
|
|
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: |
14 |
|
|
|
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. |
19 |
|
|
|
20 |
|
|
2. Altered source versions must be plainly marked as such, and must not be |
21 |
|
|
misrepresented as being the original software. |
22 |
|
|
|
23 |
|
|
3. This notice may not be removed or altered from any source distribution |
24 |
|
|
|
25 |
|
|
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003 |
26 |
|
|
|
27 |
|
|
Altered by: Andreas Zieringer 2003 for OpenSG project |
28 |
|
|
made it platform independent, gzip conform, fixed gzip footer |
29 |
|
|
|
30 |
|
|
Altered by: Geoffrey Hutchison 2005 for Open Babel project |
31 |
|
|
minor namespace modifications, VC++ compatibility |
32 |
|
|
*/ |
33 |
|
|
|
34 |
|
|
#ifndef _ZIPSTREAM_H_ |
35 |
|
|
#define _ZIPSTREAM_H_ |
36 |
|
|
|
37 |
|
|
#include <vector> |
38 |
|
|
#include <string> |
39 |
|
|
#include <streambuf> |
40 |
|
|
#include <sstream> |
41 |
|
|
#include <iostream> |
42 |
|
|
#include <algorithm> |
43 |
|
|
|
44 |
|
|
#include <zlib.h> |
45 |
|
|
|
46 |
|
|
#ifdef WIN32 /* Window 95 & Windows NT */ |
47 |
|
|
# define OS_CODE 0x0b |
48 |
|
|
#endif |
49 |
|
|
#if defined(MACOS) || defined(TARGET_OS_MAC) |
50 |
|
|
# define OS_CODE 0x07 |
51 |
|
|
#endif |
52 |
|
|
#ifndef OS_CODE |
53 |
|
|
# define OS_CODE 0x03 /* assume Unix */ |
54 |
|
|
#endif |
55 |
|
|
|
56 |
|
|
namespace zlib_stream { |
57 |
|
|
|
58 |
|
|
namespace detail |
59 |
|
|
{ |
60 |
|
|
const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ |
61 |
|
|
|
62 |
|
|
/* gzip flag byte */ |
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 */ |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/// default gzip buffer size, |
72 |
|
|
/// change this to suite your needs |
73 |
|
|
const size_t zstream_default_buffer_size = 4096; |
74 |
|
|
|
75 |
|
|
/// Compression strategy, see zlib doc. |
76 |
|
|
enum EStrategy |
77 |
|
|
{ |
78 |
|
|
StrategyFiltered = 1, |
79 |
|
|
StrategyHuffmanOnly = 2, |
80 |
|
|
DefaultStrategy = 0 |
81 |
|
|
}; |
82 |
|
|
|
83 |
|
|
//***************************************************************************** |
84 |
|
|
// template class basic_zip_streambuf |
85 |
|
|
//***************************************************************************** |
86 |
|
|
|
87 |
|
|
/** \brief A stream decorator that takes raw input and zips it to a ostream. |
88 |
|
|
|
89 |
|
|
The class wraps up the inflate method of the zlib library 1.1.4 http://www.gzip.org/zlib/ |
90 |
|
|
*/ |
91 |
|
|
template <class charT, |
92 |
|
|
class traits = std::char_traits<charT> > |
93 |
|
|
class basic_zip_streambuf : public std::basic_streambuf<charT, traits> |
94 |
|
|
{ |
95 |
|
|
public: |
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; |
103 |
|
|
|
104 |
|
|
basic_zip_streambuf(ostream_reference ostream, |
105 |
|
|
int level, |
106 |
|
|
EStrategy strategy, |
107 |
|
|
int window_size, |
108 |
|
|
int memory_level, |
109 |
|
|
size_t buffer_size); |
110 |
|
|
|
111 |
|
|
~basic_zip_streambuf(void); |
112 |
|
|
|
113 |
|
|
int sync (void); |
114 |
|
|
int_type overflow (int_type c); |
115 |
|
|
std::streamsize flush (void); |
116 |
|
|
inline |
117 |
|
|
ostream_reference get_ostream (void) const; |
118 |
|
|
inline |
119 |
|
|
int get_zerr (void) const; |
120 |
|
|
inline |
121 |
|
|
unsigned long get_crc (void) const; |
122 |
|
|
inline |
123 |
|
|
unsigned long get_in_size (void) const; |
124 |
|
|
inline |
125 |
|
|
long get_out_size(void) const; |
126 |
|
|
|
127 |
|
|
private: |
128 |
|
|
|
129 |
|
|
bool zip_to_stream(char_type *buffer, |
130 |
|
|
std::streamsize buffer_size); |
131 |
|
|
|
132 |
|
|
ostream_reference _ostream; |
133 |
|
|
z_stream _zip_stream; |
134 |
|
|
int _err; |
135 |
|
|
byte_vector_type _output_buffer; |
136 |
|
|
char_vector_type _buffer; |
137 |
|
|
unsigned long _crc; |
138 |
|
|
}; |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
//***************************************************************************** |
142 |
|
|
// template class basic_unzip_streambuf |
143 |
|
|
//***************************************************************************** |
144 |
|
|
|
145 |
|
|
/** \brief A stream decorator that takes compressed input and unzips it to a istream. |
146 |
|
|
|
147 |
|
|
The class wraps up the deflate method of the zlib library 1.1.4 http://www.gzip.org/zlib/ |
148 |
|
|
*/ |
149 |
|
|
template <class charT, |
150 |
|
|
class traits = std::char_traits<charT> > |
151 |
|
|
class basic_unzip_streambuf : |
152 |
|
|
public std::basic_streambuf<charT, traits> |
153 |
|
|
{ |
154 |
|
|
public: |
155 |
|
|
typedef std::basic_istream<charT,traits>& istream_reference; |
156 |
|
|
typedef unsigned char byte_type; |
157 |
chuckv |
787 |
typedef charT char_type; |
158 |
tim |
741 |
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; |
162 |
|
|
|
163 |
|
|
/** Construct a unzip stream |
164 |
|
|
* More info on the following parameters can be found in the zlib documentation. |
165 |
|
|
*/ |
166 |
|
|
basic_unzip_streambuf(istream_reference istream, |
167 |
|
|
int window_size, |
168 |
|
|
size_t read_buffer_size, |
169 |
|
|
size_t input_buffer_size); |
170 |
|
|
|
171 |
|
|
~basic_unzip_streambuf(void); |
172 |
|
|
|
173 |
|
|
int_type underflow(void); |
174 |
|
|
|
175 |
|
|
/// returns the compressed input istream |
176 |
|
|
inline |
177 |
|
|
istream_reference get_istream (void); |
178 |
|
|
inline |
179 |
|
|
z_stream& get_zip_stream (void); |
180 |
|
|
inline |
181 |
|
|
int get_zerr (void) const; |
182 |
|
|
inline |
183 |
|
|
unsigned long get_crc (void) const; |
184 |
|
|
inline |
185 |
|
|
long get_out_size (void) const; |
186 |
|
|
inline |
187 |
|
|
long get_in_size (void) const; |
188 |
|
|
|
189 |
|
|
|
190 |
|
|
private: |
191 |
|
|
|
192 |
|
|
void put_back_from_zip_stream (void); |
193 |
|
|
|
194 |
|
|
std::streamsize unzip_from_stream (char_type* buffer, |
195 |
|
|
std::streamsize buffer_size); |
196 |
|
|
|
197 |
|
|
size_t fill_input_buffer (void); |
198 |
|
|
|
199 |
|
|
istream_reference _istream; |
200 |
|
|
z_stream _zip_stream; |
201 |
|
|
int _err; |
202 |
|
|
byte_vector_type _input_buffer; |
203 |
|
|
char_vector_type _buffer; |
204 |
|
|
unsigned long _crc; |
205 |
|
|
}; |
206 |
|
|
|
207 |
|
|
//***************************************************************************** |
208 |
|
|
// template class basic_zip_ostream |
209 |
|
|
//***************************************************************************** |
210 |
|
|
|
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> |
216 |
|
|
{ |
217 |
|
|
public: |
218 |
|
|
|
219 |
|
|
typedef char char_type; |
220 |
|
|
typedef std::basic_ostream<charT, traits>& ostream_reference; |
221 |
|
|
typedef std::basic_ostream<charT, traits> ostream_type; |
222 |
|
|
|
223 |
|
|
inline |
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); |
231 |
|
|
|
232 |
|
|
~basic_zip_ostream(void); |
233 |
|
|
|
234 |
|
|
inline |
235 |
|
|
bool is_gzip (void) const; |
236 |
|
|
inline |
237 |
|
|
basic_zip_ostream<charT, traits>& zflush (void); |
238 |
|
|
void finished (void); |
239 |
|
|
|
240 |
|
|
void make_gzip() |
241 |
|
|
{ add_header(); _is_gzip = true; } |
242 |
|
|
|
243 |
|
|
private: |
244 |
|
|
|
245 |
|
|
basic_zip_ostream<charT,traits>& add_header(void); |
246 |
|
|
basic_zip_ostream<charT,traits>& add_footer(void); |
247 |
|
|
|
248 |
|
|
bool _is_gzip; |
249 |
|
|
bool _added_footer; |
250 |
|
|
}; |
251 |
|
|
|
252 |
|
|
//***************************************************************************** |
253 |
|
|
// template class basic_zip_istream |
254 |
|
|
//***************************************************************************** |
255 |
|
|
|
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> |
261 |
|
|
{ |
262 |
|
|
public: |
263 |
|
|
typedef std::basic_istream<charT, traits>& istream_reference; |
264 |
|
|
typedef std::basic_istream<charT, traits> istream_type; |
265 |
|
|
|
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); |
270 |
|
|
|
271 |
|
|
inline |
272 |
|
|
bool is_gzip (void) const; |
273 |
|
|
inline |
274 |
|
|
bool check_crc (void); |
275 |
|
|
inline |
276 |
|
|
bool check_data_size (void) const; |
277 |
|
|
inline |
278 |
|
|
long get_gzip_crc (void) const; |
279 |
|
|
inline |
280 |
|
|
long get_gzip_data_size(void) const; |
281 |
|
|
|
282 |
|
|
protected: |
283 |
|
|
|
284 |
|
|
int check_header (void); |
285 |
|
|
void read_footer (void); |
286 |
|
|
|
287 |
|
|
bool _is_gzip; |
288 |
|
|
long _gzip_crc; |
289 |
|
|
long _gzip_data_size; |
290 |
|
|
}; |
291 |
|
|
|
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; |
296 |
|
|
|
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; |
301 |
|
|
|
302 |
|
|
//! Helper function to check whether stream is compressed or not. |
303 |
|
|
inline bool isGZip(std::istream &is) |
304 |
|
|
{ |
305 |
|
|
const int gz_magic[2] = {0x1f, 0x8b}; |
306 |
|
|
|
307 |
|
|
int c1 = (int) is.get(); |
308 |
|
|
if(c1 != gz_magic[0]) |
309 |
|
|
{ |
310 |
|
|
is.putback(c1); |
311 |
|
|
return false; |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
int c2 = (int) is.get(); |
315 |
|
|
if(c2 != gz_magic[1]) |
316 |
|
|
{ |
317 |
|
|
is.putback(c2); |
318 |
|
|
is.putback(c1); |
319 |
|
|
return false; |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
is.putback(c2); |
323 |
|
|
is.putback(c1); |
324 |
|
|
return true; |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
#include "zipstreamimpl.hpp" |
328 |
|
|
|
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
#endif // _ZIPSTREAM_H_ |