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 no event will the authors be held liable for any damages arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
11
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
13
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15
3. This notice may not be removed or altered from any source distribution
17
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
22
#include "zipstream.h"
25
namespace zlib_stream{
28
const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
31
const int gz_ascii_flag = 0x01; /* bit 0 set: file probably ascii text */
32
const int gz_head_crc = 0x02; /* bit 1 set: header CRC present */
33
const int gz_extra_field = 0x04; /* bit 2 set: extra field present */
34
const int gz_orig_name = 0x08; /* bit 3 set: original file name present */
35
const int gz_comment = 0x10; /* bit 4 set: file comment present */
36
const int gz_reserved = 0xE0; /* bits 5..7: reserved */
47
Elem,Tr,ElemA,ByteT,ByteAT
48
>::basic_zip_streambuf(
49
ostream_reference ostream_,
58
m_output_buffer(buffer_size_,0),
59
m_buffer(buffer_size_,0),
62
m_zip_stream.zalloc=(alloc_func)0;
63
m_zip_stream.zfree=(free_func)0;
65
m_zip_stream.next_in=NULL;
66
m_zip_stream.avail_in=0;
67
m_zip_stream.avail_out=0;
68
m_zip_stream.next_out=NULL;
72
std::min( 9, static_cast<int>(level_)),
74
- static_cast<int>(window_size_), // <-- changed
75
std::min( 9, static_cast<int>(memory_level_) ),
76
static_cast<int>(strategy_)
79
setp( &(m_buffer[0]), &(m_buffer[m_buffer.size()-1]));
90
Elem,Tr,ElemA,ByteT,ByteAT
91
>::~basic_zip_streambuf()
95
m_err=deflateEnd(&m_zip_stream);
105
int basic_zip_streambuf<
106
Elem,Tr,ElemA,ByteT,ByteAT
109
if ( pptr() && pptr() > pbase())
111
int c = overflow( EOF);
127
typename basic_zip_streambuf<
128
Elem,Tr,ElemA,ByteT,ByteAT
131
Elem,Tr,ElemA,ByteT,ByteAT
133
typename basic_zip_streambuf<
134
Elem,Tr,ElemA,ByteT,ByteAT
138
int w = static_cast<int>(pptr() - pbase());
143
if ( zip_to_stream( pbase(), w)) {
144
setp( pbase(), epptr() - 1);
157
bool basic_zip_streambuf<
158
Elem,Tr,ElemA,ByteT,ByteAT
160
typename basic_zip_streambuf<
161
Elem,Tr,ElemA,ByteT,ByteAT
162
>::char_type* buffer_,
163
std::streamsize buffer_size_
166
std::streamsize written_byte_size=0, total_written_byte_size = 0;
168
m_zip_stream.next_in=(byte_buffer_type)buffer_;
169
m_zip_stream.avail_in=static_cast<uInt>(buffer_size_*sizeof(char_type));
170
m_zip_stream.avail_out=static_cast<uInt>(m_output_buffer.size());
171
m_zip_stream.next_out=&(m_output_buffer[0]);
177
m_zip_stream.next_in,
178
m_zip_stream.avail_in
183
m_err = deflate(&m_zip_stream, 0);
185
if (m_err == Z_OK || m_err == Z_STREAM_END)
188
static_cast<std::streamsize>(m_output_buffer.size())
189
- m_zip_stream.avail_out;
190
total_written_byte_size+=written_byte_size;
191
// ouput buffer is full, dumping to ostream
193
(const char_type*) &(m_output_buffer[0]),
194
static_cast<std::streamsize>(
195
written_byte_size/sizeof(char_type)
199
// checking if some bytes were not written.
200
if ( (remainder = written_byte_size%sizeof(char_type))!=0)
202
// copy to the beginning of the stream
204
&(m_output_buffer[0]),
205
&(m_output_buffer[written_byte_size-remainder]),
210
m_zip_stream.avail_out=
211
static_cast<uInt>(m_output_buffer.size()-remainder);
212
m_zip_stream.next_out=&m_output_buffer[remainder];
215
while (m_zip_stream.avail_in != 0 && m_err == Z_OK);
217
return m_err == Z_OK;
227
std::streamsize basic_zip_streambuf<
228
Elem,Tr,ElemA,ByteT,ByteAT
231
std::streamsize written_byte_size=0, total_written_byte_size=0;
238
m_zip_stream.next_in,
239
m_zip_stream.avail_in
244
m_err = deflate(&m_zip_stream, Z_FINISH);
245
if (m_err == Z_OK || m_err == Z_STREAM_END)
248
static_cast<std::streamsize>(m_output_buffer.size())
249
- m_zip_stream.avail_out;
250
total_written_byte_size+=written_byte_size;
251
// ouput buffer is full, dumping to ostream
253
(const char_type*) &(m_output_buffer[0]),
254
static_cast<std::streamsize>(
255
written_byte_size/sizeof(char_type)*sizeof(byte_type)
259
// checking if some bytes were not written.
260
if ( (remainder = written_byte_size%sizeof(char_type))!=0)
262
// copy to the beginning of the stream
264
&(m_output_buffer[0]),
265
&(m_output_buffer[written_byte_size-remainder]),
270
m_zip_stream.avail_out=static_cast<uInt>(m_output_buffer.size()-remainder);
271
m_zip_stream.next_out=&m_output_buffer[remainder];
273
} while (m_err == Z_OK);
277
return total_written_byte_size;
288
basic_unzip_streambuf<
289
Elem,Tr,ElemA,ByteT,ByteAT
290
>::basic_unzip_streambuf(
291
istream_reference istream_,
293
size_t read_buffer_size_,
294
size_t input_buffer_size_
298
m_input_buffer(input_buffer_size_),
299
m_buffer(read_buffer_size_),
302
// setting zalloc, zfree and opaque
303
m_zip_stream.zalloc=(alloc_func)0;
304
m_zip_stream.zfree=(free_func)0;
306
m_zip_stream.next_in=NULL;
307
m_zip_stream.avail_in=0;
308
m_zip_stream.avail_out=0;
309
m_zip_stream.next_out=NULL;
313
-static_cast<int>(window_size_)
316
setg( &(m_buffer[0])+4, // beginning of putback area
317
&(m_buffer[0])+4, // read position
318
&(m_buffer[0])+4); // end position
328
size_t basic_unzip_streambuf<
329
Elem,Tr,ElemA,ByteT,ByteAT
330
>::fill_input_buffer()
332
m_zip_stream.next_in=&(m_input_buffer[0]);
334
(char_type*)(&(m_input_buffer[0])),
335
static_cast<std::streamsize>(m_input_buffer.size()/sizeof(char_type))
337
return m_zip_stream.avail_in=m_istream.gcount()*sizeof(char_type);
348
basic_unzip_streambuf<
349
Elem,Tr,ElemA,ByteT,ByteAT
350
>::~basic_unzip_streambuf()
352
inflateEnd(&m_zip_stream);
362
typename basic_unzip_streambuf<
363
Elem,Tr,ElemA,ByteT,ByteAT
365
basic_unzip_streambuf<
366
Elem,Tr,ElemA,ByteT,ByteAT
369
if ( gptr() && ( gptr() < egptr()))
370
return * reinterpret_cast<unsigned char *>( gptr());
372
int n_putback = static_cast<int>(gptr() - eback());
376
&(m_buffer[0]) + (4 - n_putback),
378
n_putback*sizeof(char_type)
381
int num = unzip_from_stream(
383
static_cast<std::streamsize>((m_buffer.size()-4)*sizeof(char_type))
385
if (num <= 0) // ERROR or EOF
388
// reset buffer pointers
389
setg( &(m_buffer[0]) + (4 - n_putback), // beginning of putback area
390
&(m_buffer[0]) + 4, // read position
391
&(m_buffer[0]) + 4 + num); // end of buffer
393
// return next character
394
return* reinterpret_cast<unsigned char *>( gptr());
404
std::streamsize basic_unzip_streambuf<
405
Elem,Tr,ElemA,ByteT,ByteAT
406
>::unzip_from_stream(
408
std::streamsize buffer_size_
411
m_zip_stream.next_out=(byte_buffer_type)buffer_;
412
m_zip_stream.avail_out=static_cast<uInt>(buffer_size_*sizeof(char_type));
413
size_t count =m_zip_stream.avail_in;
417
if (m_zip_stream.avail_in==0)
418
count=fill_input_buffer();
420
if (m_zip_stream.avail_in)
422
m_err = inflate( &m_zip_stream, Z_SYNC_FLUSH );
424
} while (m_err==Z_OK && m_zip_stream.avail_out != 0 && count != 0);
429
(byte_buffer_type)buffer_,
430
buffer_size_ - m_zip_stream.avail_out/sizeof(char_type)
432
std::streamsize n_read = buffer_size_ - m_zip_stream.avail_out/sizeof(char_type);
434
// check if it is the end
435
if (m_err==Z_STREAM_END)
436
put_back_from_zip_stream();
448
void basic_unzip_streambuf<
449
Elem,Tr,ElemA,ByteT,ByteAT
450
>::put_back_from_zip_stream()
452
if (m_zip_stream.avail_in==0)
455
m_istream.clear( ios::goodbit );
457
-static_cast<int>(m_zip_stream.avail_in),
461
m_zip_stream.avail_in=0;
471
int basic_zip_istream<
472
Elem,Tr,ElemA,ByteT,ByteAT
475
int method; /* method byte */
476
int flags; /* flags byte */
480
z_stream& zip_stream = rdbuf()->get_zip_stream();
482
/* Check the gzip magic header */
483
for (len = 0; len < 2; len++)
485
c = (int)rdbuf()->get_istream().get();
486
if (c != detail::gz_magic[len])
489
rdbuf()->get_istream().unget();
492
rdbuf()->get_istream().unget();
495
err = zip_stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
502
method = (int)rdbuf()->get_istream().get();
503
flags = (int)rdbuf()->get_istream().get();
504
if (method != Z_DEFLATED || (flags & detail::gz_reserved) != 0)
510
/* Discard time, xflags and OS code: */
511
for (len = 0; len < 6; len++)
512
rdbuf()->get_istream().get();
514
if ((flags & detail::gz_extra_field) != 0)
516
/* skip the extra field */
517
len = (uInt)rdbuf()->get_istream().get();
518
len += ((uInt)rdbuf()->get_istream().get())<<8;
519
/* len is garbage if EOF but the loop below will quit anyway */
520
while (len-- != 0 && rdbuf()->get_istream().get() != EOF) ;
522
if ((flags & detail::gz_orig_name) != 0)
524
/* skip the original file name */
525
while ((c = rdbuf()->get_istream().get()) != 0 && c != EOF) ;
527
if ((flags & detail::gz_comment) != 0)
529
/* skip the .gz file comment */
530
while ((c = rdbuf()->get_istream().get()) != 0 && c != EOF) ;
532
if ((flags & detail::gz_head_crc) != 0)
533
{ /* skip the header crc */
534
for (len = 0; len < 2; len++)
535
rdbuf()->get_istream().get();
537
err = rdbuf()->get_istream().eof() ? Z_DATA_ERROR : Z_OK;
549
void basic_zip_istream<
550
Elem,Tr,ElemA,ByteT,ByteAT
555
read_long( rdbuf()->get_istream(), m_gzip_crc );
556
read_long( rdbuf()->get_istream(), m_gzip_data_size );
567
void basic_zip_ostream<
568
Elem,Tr,ElemA,ByteT,ByteAT
570
typename basic_zip_ostream<
571
Elem,Tr,ElemA,ByteT,ByteAT
572
>::ostream_reference out_,
576
static const int size_ul = sizeof(unsigned long);
577
static const int size_c = sizeof(char_type);
578
static const int n_end = size_ul/size_c;
579
out_.write(reinterpret_cast<char_type const*>(&x_), n_end);
589
void basic_zip_istream<
590
Elem,Tr,ElemA,ByteT,ByteAT
592
istream_reference in_,
596
static const int size_ul = sizeof(unsigned long);
597
static const int size_c = sizeof(char_type);
598
static const int n_end = size_ul/size_c;
599
in_.read(reinterpret_cast<char*>(&x_),n_end);
609
void basic_zip_ostream<
610
Elem,Tr,ElemA,ByteT,ByteAT
615
rdbuf()->get_ostream()
616
.put(static_cast<char_type>(detail::gz_magic[0]))
617
.put(static_cast<char_type>(detail::gz_magic[1]))
618
.put(static_cast<char_type>(Z_DEFLATED))
620
.put(zero).put(zero).put(zero).put(zero) // time
622
.put(static_cast<char_type>(OS_CODE));
632
void basic_zip_ostream<
633
Elem,Tr,ElemA,ByteT,ByteAT
636
put_long( rdbuf()->get_ostream(), rdbuf()->get_crc() );
637
put_long( rdbuf()->get_ostream(), rdbuf()->get_in_size() );
b'\\ No newline at end of file'