File: zipstreamimpl.hpp

package info (click to toggle)
gdcm 2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 32,868 kB
  • sloc: cpp: 188,481; ansic: 124,526; xml: 41,799; sh: 7,162; python: 3,667; cs: 2,128; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 116
file content (829 lines) | stat: -rw-r--r-- 23,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
/*
zipstream Library License:
--------------------------

The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.

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.

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:

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.

2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution

Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003

Altered by: Andreas Zieringer 2003 for OpenSG project
            made it platform independent, gzip conform, fixed gzip footer

Altered by: Geoffrey Hutchison 2005 for Open Babel project
            minor namespace modifications, VC++ compatibility

Altered by: Mathieu Malaterre 2008, for GDCM project
            when reading deflate bit stream in DICOM special handling of \0 is needed
            also when writing deflate back to disk, the add_footer must be called
*/

//*****************************************************************************
//  template class basic_zip_streambuf
//*****************************************************************************

//-----------------------------------------------------------------------------
// PUBLIC
//-----------------------------------------------------------------------------

/** Construct a zip stream
 * More info on the following parameters can be found in the zlib documentation.
 */

template <class charT, class traits>
basic_zip_streambuf<charT, traits>::basic_zip_streambuf(ostream_reference ostream,
                                                        int level,
                                                        EStrategy strategy,
                                                        int window_size,
                                                        int memory_level,
                                                        size_t buffer_size)
    : _ostream(ostream),
      _output_buffer(buffer_size, 0),
      _buffer(buffer_size, 0),
      _crc(0)
{
    _zip_stream.zalloc = (alloc_func) 0;
    _zip_stream.zfree = (free_func) 0;

    _zip_stream.next_in = NULL;
    _zip_stream.avail_in = 0;
    _zip_stream.avail_out = 0;
    _zip_stream.next_out = NULL;

    if(level > 9)
        level = 9;

    if(memory_level > 9)
        memory_level = 9;

    _err=deflateInit2(&_zip_stream, level, Z_DEFLATED,
                      window_size, memory_level,
                      static_cast<int>(strategy));
    this->setp( &(_buffer[0]), &(_buffer[_buffer.size()-1]));
}

/** Destructor
 */
template <class charT, class traits>
basic_zip_streambuf<charT, traits>::~basic_zip_streambuf(void)
{
    flush();
 //   _ostream.flush(); CM already done in flush()
    _err=deflateEnd(&_zip_stream);
}

/** Do the synchronization
 * @todo
 * document correctly!
 */
template <class charT, class traits>
int basic_zip_streambuf<charT, traits>::sync(void)
{
    if(this->pptr() && this->pptr() > this->pbase())
    {
        /*int c =*/ overflow(EOF);

        // ACHTUNG wenn das drin ist hoert er nach dem ersten endl auf!
        /*
          if ( c == EOF)
          return -1;
        */
    }

    return 0;
}

/** <unknown purpose>
 * @todo
 * document correctly!
 */
template <class charT, class traits>
typename basic_zip_streambuf<charT, traits>::int_type
basic_zip_streambuf<charT, traits>::overflow(int_type c)
{
    int w = static_cast<int>(this->pptr() - this->pbase());
    if (c != EOF)
    {
        *this->pptr() = (char)c;
        ++w;
    }
    if (zip_to_stream(this->pbase(), w))
    {
        this->setp(this->pbase(), this->epptr() - 1);
        return c;
    }
    else
    {
        return EOF;
    }
}

/** flushes the zip buffer and output buffer.
 *
 *    This method should be called at the end of the compression. Calling flush
 *    multiple times, will lower the compression ratio.
 */
template <class charT, class traits>
std::streamsize basic_zip_streambuf<charT, traits>::flush(void)
{
    std::streamsize written_byte_size = 0, total_written_byte_size = 0;

    size_t remainder = 0;

    // updating crc
    _crc = crc32(_crc,  _zip_stream.next_in,
                 _zip_stream.avail_in);

    do
    {
        _err = deflate(&_zip_stream, Z_FINISH);
        if(_err == Z_OK || _err == Z_STREAM_END)
        {
            written_byte_size = static_cast<std::streamsize>(_output_buffer.size()) - _zip_stream.avail_out;
            total_written_byte_size += written_byte_size;
            // ouput buffer is full, dumping to ostream
            _ostream.write( (const char_type*) &(_output_buffer[0]),
                            static_cast<std::streamsize>(written_byte_size/sizeof(char_type)*sizeof(char)));

            // checking if some bytes were not written.
            if((remainder = written_byte_size%sizeof(char_type)) != 0)
            {
                // copy to the beginning of the stream
				std::streamsize theDiff = written_byte_size-remainder;
				//assert (theDiff > 0 && theDiff < std::numeric_limits<unsigned int>::max());

                memcpy(&(_output_buffer[0]),
                       &(_output_buffer[(unsigned int)theDiff]), remainder);

            }

            _zip_stream.avail_out = static_cast<uInt>(_output_buffer.size() - remainder);
            _zip_stream.next_out = &_output_buffer[remainder];
        }
    }
    while(_err == Z_OK);

    _ostream.flush();

    return total_written_byte_size;
}

/** returns a reference to the output stream
 */
template <class charT, class traits> inline
typename basic_zip_streambuf<charT, traits>::ostream_reference
basic_zip_streambuf<charT, traits>::get_ostream(void) const
{
    return _ostream;
}

/** returns the latest zlib error status
 */
template <class charT, class traits> inline
int basic_zip_streambuf<charT, traits>::get_zerr(void) const
{
    return _err;
}

/** returns the crc of the input data compressed so far.
 */
template <class charT, class traits> inline
unsigned long
basic_zip_streambuf<charT, traits>:: get_crc(void) const
{
    return _crc;
}

/**  returns the size (bytes) of the input data compressed so far.
 */
template <class charT, class traits> inline
unsigned long
basic_zip_streambuf<charT, traits>::get_in_size(void) const
{
    return _zip_stream.total_in;
}

/**  returns the size (bytes) of the compressed data so far.
 */
template <class charT, class traits> inline
long
basic_zip_streambuf<charT, traits>::get_out_size(void) const
{
    return _zip_stream.total_out;
}

//-----------------------------------------------------------------------------
// PRIVATE
//-----------------------------------------------------------------------------

/** <undocumented>
 * @todo
 * document!
 */
template <class charT, class traits>
bool basic_zip_streambuf<charT, traits>::zip_to_stream(
    char_type *buffer,
    std::streamsize buffer_size)
{
    std::streamsize written_byte_size = 0, total_written_byte_size = 0;

    _zip_stream.next_in = (byte_buffer_type) buffer;
    _zip_stream.avail_in = static_cast<uInt>(buffer_size * sizeof(char_type));
    _zip_stream.avail_out = static_cast<uInt>(_output_buffer.size());
    _zip_stream.next_out = &_output_buffer[0];
    size_t remainder = 0;

    // updating crc
    _crc = crc32(_crc, _zip_stream.next_in,
                 _zip_stream.avail_in);

    do
    {
        _err = deflate(&_zip_stream, 0);

        if (_err == Z_OK  || _err == Z_STREAM_END)
        {
            written_byte_size= static_cast<std::streamsize>(_output_buffer.size()) -
                _zip_stream.avail_out;
            total_written_byte_size += written_byte_size;
            // ouput buffer is full, dumping to ostream

            _ostream.write((const char_type*) &_output_buffer[0],
                           static_cast<std::streamsize>(written_byte_size / sizeof(char_type)));

            // checking if some bytes were not written.
            if((remainder = written_byte_size % sizeof(char_type)) != 0)
            {
                // copy to the beginning of the stream
				std::streamsize theDiff = written_byte_size-remainder;
				//assert(theDiff > 0 && theDiff < std::numeric_limits<unsigned int>::max());
                memcpy(&_output_buffer[0],
                       &_output_buffer[(unsigned int)theDiff],
                       remainder);
            }

            _zip_stream.avail_out = static_cast<uInt>(_output_buffer.size()-remainder);
            _zip_stream.next_out = &_output_buffer[remainder];
        }
    }
    while(_zip_stream.avail_in != 0 && _err == Z_OK);

    return _err == Z_OK;
}







//*****************************************************************************
//  template class basic_unzip_streambuf
//*****************************************************************************

//-----------------------------------------------------------------------------
// PUBLIC
//-----------------------------------------------------------------------------

/** Constructor
 */
template <class charT, class traits>
basic_unzip_streambuf<charT, traits>::basic_unzip_streambuf(istream_reference istream,
                                                            int window_size,
                                                            size_t read_buffer_size,
                                                            size_t input_buffer_size)
    : _istream(istream),
      _input_buffer(input_buffer_size),
      _buffer(read_buffer_size),
      _crc(0)
{
    // setting zalloc, zfree and opaque
    _zip_stream.zalloc = (alloc_func) 0;
    _zip_stream.zfree = (free_func) 0;

    _zip_stream.next_in = NULL;
    _zip_stream.avail_in = 0;
    _zip_stream.avail_out = 0;
    _zip_stream.next_out = NULL;

    _err = inflateInit2(&_zip_stream, window_size);

    this->setg(&_buffer[0] + 4,     // beginning of putback area
               &_buffer[0] + 4,     // read position
               &_buffer[0] + 4);    // end position
}

/**
 * @todo document!
 */
template <class charT, class traits>
basic_unzip_streambuf<charT, traits>::~basic_unzip_streambuf(void)
{
    inflateEnd(&_zip_stream);
}


/**
 * @todo document!
 */
template <class charT, class traits>
typename basic_unzip_streambuf<charT, traits>::int_type
basic_unzip_streambuf<charT, traits>::underflow(void)
{
    if(this->gptr() && ( this->gptr() < this->egptr()))
        return * reinterpret_cast<unsigned char *>(this->gptr());

    int n_putback = static_cast<int>(this->gptr() - this->eback());
    if(n_putback > 4)
        n_putback = 4;

    memcpy(&_buffer[0] + (4 - n_putback),
           this->gptr() - n_putback,
           n_putback * sizeof(char_type));

    std::streamsize num =
        unzip_from_stream(&_buffer[0] + 4,
                          static_cast<std::streamsize>((_buffer.size() - 4) *
                                                       sizeof(char_type)));

    if(num <= 0) // ERROR or EOF
        return EOF;

    // reset buffer pointers
    this->setg(&_buffer[0] + (4 - n_putback),   // beginning of putback area
               &_buffer[0] + 4,                 // read position
               &_buffer[0] + 4 + num);          // end of buffer

    // return next character
    return * reinterpret_cast<unsigned char *>(this->gptr());
}

/** returns the compressed input istream
 */
template <class charT, class traits> inline
typename basic_unzip_streambuf<charT, traits>::istream_reference
basic_unzip_streambuf<charT, traits>::get_istream(void)
{
    return _istream;
}

/** returns the zlib stream structure
 */
template <class charT, class traits> inline
z_stream &
basic_unzip_streambuf<charT, traits>::get_zip_stream(void)
{
    return _zip_stream;
}

/** returns the latest zlib error state
 */
template <class charT, class traits> inline
int
basic_unzip_streambuf<charT, traits>::get_zerr(void) const
{
    return _err;
}

/** returns the crc of the uncompressed data so far
 */
template <class charT, class traits> inline
unsigned long
basic_unzip_streambuf<charT, traits>::get_crc(void) const
{
    return _crc;
}

/** returns the number of uncompressed bytes
 */
template <class charT, class traits> inline
long
basic_unzip_streambuf<charT, traits>::get_out_size(void) const
{
    return _zip_stream.total_out;
}

/** returns the number of read compressed bytes
 */
template <class charT, class traits> inline
long
basic_unzip_streambuf<charT, traits>::get_in_size(void) const
{
    return _zip_stream.total_in;
}


//-----------------------------------------------------------------------------
// PRIVATE
//-----------------------------------------------------------------------------

/**
 */
template <class charT, class traits> inline
void
basic_unzip_streambuf<charT, traits>::put_back_from_zip_stream(void)
{
    if(_zip_stream.avail_in == 0)
        return;

    _istream.clear(std::ios::goodbit);
    _istream.seekg(-intf(_zip_stream.avail_in),
                   std::ios_base::cur);

    _zip_stream.avail_in = 0;
}

/**
 */
template <class charT, class traits> inline
std::streamsize
basic_unzip_streambuf<charT, traits>::unzip_from_stream(char_type* buffer,
                                                        std::streamsize buffer_size)
{
    _zip_stream.next_out  =
        (byte_buffer_type) buffer;
    _zip_stream.avail_out =
        static_cast<uInt>(buffer_size * sizeof(char_type));
    size_t count = _zip_stream.avail_in;

    do
    {
        if(_zip_stream.avail_in == 0)
            count=fill_input_buffer();

        if(_zip_stream.avail_in)
        {
            _err = inflate(&_zip_stream,  Z_SYNC_FLUSH);
        }
    }
    while(_err==Z_OK && _zip_stream.avail_out != 0 && count != 0);

	std::streamsize theSize = buffer_size - ((std::streamsize)_zip_stream.avail_out) / sizeof(char_type);
//	assert (theSize >= 0 && theSize < std::numeric_limits<uInt>::max());

    // updating crc
    _crc = crc32(_crc, (byte_buffer_type) buffer,(uInt)theSize);

    std::streamsize n_read =
        buffer_size - _zip_stream.avail_out / sizeof(char_type);

    // check if it is the end
    if (_err == Z_STREAM_END)
        put_back_from_zip_stream();

    return n_read;
}


/**
 */
template <class charT, class traits> inline
size_t
basic_unzip_streambuf<charT, traits>::fill_input_buffer(void)
{
    _zip_stream.next_in = &_input_buffer[0];
    _istream.read((char_type*) &_input_buffer[0],
                  static_cast<std::streamsize>(_input_buffer.size() /
                                               sizeof(char_type)));
    std::streamsize nbytesread = _istream.gcount()*sizeof(char_type);
    if( !_istream )
      {
      if( _istream.eof() )
        {
        // Ok so we reached the end of file, since we did not read no header
        // we have to explicitely tell zlib the compress stream ends, therefore
        // we add an extra \0 character...it may not always be needed...
        assert( nbytesread < (std::streamsize)(_input_buffer.size() / sizeof(char_type)) );
        _input_buffer[ (unsigned int)nbytesread ] = 0;
        ++nbytesread;
        }
      }

    return _zip_stream.avail_in = (uInt)nbytesread;
}







//*****************************************************************************
//  template class basic_zip_ostream
//*****************************************************************************

//-----------------------------------------------------------------------------
// PUBLIC
//-----------------------------------------------------------------------------

/**
 */
template <class charT, class traits> inline
basic_zip_ostream<charT, traits>::basic_zip_ostream(ostream_reference ostream,
                                                    bool isgzip,
                                                    int level,
                                                    EStrategy strategy,
                                                    int window_size,
                                                    int memory_level,
                                                    size_t buffer_size) :
    basic_zip_streambuf<charT, traits>(ostream, level, strategy, window_size,
                                       memory_level, buffer_size),
    std::basic_ostream<charT, traits>(this),
    _is_gzip(isgzip),
    _added_footer(false)
{
    if(_is_gzip)
        add_header();
}

/** Destructor
 */
template <class charT, class traits>
basic_zip_ostream<charT, traits>::~basic_zip_ostream(void)
{
    //if(_is_gzip)
        add_footer();
}

/** returns true if it is a gzip
 */
template <class charT, class traits> inline
bool basic_zip_ostream<charT, traits>::is_gzip(void) const
{
    return _is_gzip;
}

/** flush inner buffer and zipper buffer
 */

template <class charT, class traits> inline
basic_zip_ostream<charT, traits>& basic_zip_ostream<charT, traits>::zflush(void)
{
    static_cast<std::basic_ostream<charT, traits> *>(this)->flush();
    static_cast<basic_zip_streambuf<charT, traits> *>(this)->flush();
    return *this;
}

template <class charT, class traits> inline
void basic_zip_ostream<charT, traits>::finished(void)
{
    if(_is_gzip)
        add_footer();
    else
        zflush();
}


//-----------------------------------------------------------------------------
// PRIVATE
//-----------------------------------------------------------------------------

/**
 * @todo document!
 */
template <class charT, class traits>
basic_zip_ostream<charT,traits>& basic_zip_ostream<charT, traits>::add_header(void)
{
    char_type zero = 0;

    this->get_ostream() << static_cast<char_type>(detail::gz_magic[0])
                        << static_cast<char_type>(detail::gz_magic[1])
                        << static_cast<char_type>(Z_DEFLATED)
                        << zero //flags
                        << zero<<zero<<zero<<zero // time
                        << zero //xflags
                        << static_cast<char_type>(OS_CODE);

    return *this;
}

/**
 * @todo document!
 */
template <class charT, class traits>
basic_zip_ostream<charT,traits>& basic_zip_ostream<charT, traits>::add_footer(void)
{
    if(_added_footer)
        return *this;

    zflush();

    _added_footer = true;

    // Writes crc and length in LSB order to the stream.
    unsigned long crc = this->get_crc();
    for(int n=0;n<4;++n)
    {
        this->get_ostream().put((char)(crc & 0xff));
        crc >>= 8;
    }

    unsigned long length = this->get_in_size();
    for(int m=0;m<4;++m)
    {
        this->get_ostream().put((char)(length & 0xff));
        length >>= 8;
    }

    return *this;
}






//*****************************************************************************
//  template class basic_zip_istream
//*****************************************************************************

//-----------------------------------------------------------------------------
// PUBLIC
//-----------------------------------------------------------------------------

/** Constructor
 */
template <class charT, class traits>
basic_zip_istream<charT, traits>::basic_zip_istream(istream_reference istream,
                                                    int window_size,
                                                    size_t read_buffer_size,
                                                    size_t input_buffer_size)
    : basic_unzip_streambuf<charT, traits>(istream, window_size,
                                           read_buffer_size, input_buffer_size),
      std::basic_istream<charT, traits>(this),
      _is_gzip(false),
      _gzip_crc(0),
      _gzip_data_size(0)
{
  if(this->get_zerr() == Z_OK)
    {
    int check = check_header();(void)check;
    //std::cerr << "check_header:" << check << std::endl;
    }
}

/** returns true if it is a gzip file
 */
template <class charT, class traits> inline
bool
basic_zip_istream<charT, traits>::is_gzip(void) const
{
    return _is_gzip;
}

/** return crc check result
 *
 * This must be called after the reading of compressed data is finished!  This
 * method compares it to the crc of the uncompressed data.
 *
 *    \return true if crc check is succesful
 */
template <class charT, class traits> inline
bool
basic_zip_istream<charT, traits>::check_crc(void)
{
    read_footer();
    return this->get_crc() == _gzip_crc;
}

/** return data size check
 */
template <class charT, class traits> inline
bool
basic_zip_istream<charT, traits>::check_data_size(void) const
{
    return this->get_out_size() == _gzip_data_size;
}

/** return the crc value in the file
 */
template <class charT, class traits> inline
long
basic_zip_istream<charT, traits>::get_gzip_crc(void) const
{
    return _gzip_crc;
}

/** return the data size in the file
 */
template <class charT, class traits> inline
long
basic_zip_istream<charT, traits>::get_gzip_data_size(void) const
{
    return _gzip_data_size;
}

//-----------------------------------------------------------------------------
// PROTECTED
//-----------------------------------------------------------------------------

/**
 * @todo document!
 */
template <class charT, class traits>
int
basic_zip_istream<charT, traits>::check_header(void)
{
    int method; /* method byte */
    int flagsbyte;  /* flags byte */
    uInt len;
    int c;
    int err=0;
    z_stream &zip_stream = this->get_zip_stream();

    /* Check the gzip magic header */
    for(len = 0; len < 2; len++)
    {
        c = (int)this->get_istream().get();
        if (c != detail::gz_magic[len])
        {
            if (len != 0)
                this->get_istream().unget();
            if (c!= EOF)
            {
                this->get_istream().unget();
            }

            err = zip_stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
            _is_gzip = false;
            return err;
        }
    }

    _is_gzip = true;
    method = (int)this->get_istream().get();
    flagsbyte = (int)this->get_istream().get();
    if (method != Z_DEFLATED || (flagsbyte & detail::gz_reserved) != 0)
    {
        err = Z_DATA_ERROR;
        return err;
    }

    /* Discard time, xflags and OS code: */
    for (len = 0; len < 6; len++)
        this->get_istream().get();

    if ((flagsbyte & detail::gz_extra_field) != 0)
    {
        /* skip the extra field */
        len  =  (uInt)this->get_istream().get();
        len += ((uInt)this->get_istream().get())<<8;
        /* len is garbage if EOF but the loop below will quit anyway */
        while (len-- != 0 && this->get_istream().get() != EOF) ;
    }
    if ((flagsbyte & detail::gz_orig_name) != 0)
    {
        /* skip the original file name */
        while ((c = this->get_istream().get()) != 0 && c != EOF) ;
    }
    if ((flagsbyte & detail::gz_comment) != 0)
    {
        /* skip the .gz file comment */
        while ((c = this->get_istream().get()) != 0 && c != EOF) ;
    }
    if ((flagsbyte & detail::gz_head_crc) != 0)
    {  /* skip the header crc */
        for (len = 0; len < 2; len++)
            this->get_istream().get();
    }
    err = this->get_istream().eof() ? Z_DATA_ERROR : Z_OK;

    return err;
}

/**
 * @todo document!
 */
template <class charT, class traits>
void
basic_zip_istream<charT, traits>::read_footer(void)
{
    if(_is_gzip)
    {
        _gzip_crc = 0;
        for(int n=0;n<4;++n)
            _gzip_crc += ((((int) this->get_istream().get()) & 0xff) << (8*n));

        _gzip_data_size = 0;
        for(int n=0;n<4;++n)
            _gzip_data_size +=
                ((((int) this->get_istream().get()) & 0xff) << (8*n));
    }
}