File: gz.hpp

package info (click to toggle)
libshrinkwrap 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: cpp: 2,118; makefile: 4
file content (796 lines) | stat: -rw-r--r-- 23,293 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
#ifndef SHRINKWRAP_GZ_HPP
#define SHRINKWRAP_GZ_HPP

#include <streambuf>
#include <array>
#include <vector>
#include <stdio.h>
#include <zlib.h>
#include <assert.h>
#include <iostream>
#include <limits>
#include <cstring>

namespace shrinkwrap
{
  namespace gz
  {
    class ibuf : public std::streambuf
    {
    public:
      ibuf(FILE* fp)
        :
        zstrm_({0}),
        compressed_buffer_(default_block_size),
        decompressed_buffer_(default_block_size),
        discard_amount_(0),
        current_block_position_(0),
        uncompressed_block_offset_(0),
        fp_(fp),
        put_back_size_(0),
        at_block_boundary_(false)
      {
        if (fp_)
        {
          zlib_res_ = inflateInit2(&zstrm_, 15 + 16); // 16 for GZIP only.
          if (zlib_res_ != Z_OK)
          {
            // TODO: handle error.
          }
        }
        char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
        setg(end, end, end);
      }

      ibuf(const std::string& file_path) : ibuf(fopen(file_path.c_str(), "rb")) {}
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      ibuf(ibuf&& src)
        :
        std::streambuf(std::move(src))
      {
        this->move(std::move(src));
      }

      ibuf& operator=(ibuf&& src)
      {
        if (&src != this)
        {
          std::streambuf::operator=(std::move(src));
          this->destroy();
          this->move(std::move(src));
        }

        return *this;
      }
#endif
      virtual ~ibuf()
      {
        this->destroy();
      }

    private:
      //ixzbuf(const ixzbuf& src) = delete;
      //ixzbuf& operator=(const ixzbuf& src) = delete;

      void destroy()
      {
        if (fp_)
        {
          inflateEnd(&zstrm_);
          fclose(fp_);
        }
      }

      void move(ibuf&& src)
      {
        inflateCopy(&zstrm_, &(src.zstrm_)); //zstrm_ = src.zstrm_;
        zstrm_.msg = src.zstrm_.msg;
        src.zstrm_ = {0};
        compressed_buffer_ = std::move(src.compressed_buffer_);
        decompressed_buffer_ = std::move(src.decompressed_buffer_);
        discard_amount_ = src.discard_amount_;
        current_block_position_ = src.current_block_position_;
        uncompressed_block_offset_ = src.uncompressed_block_offset_;
        at_block_boundary_ = src.at_block_boundary_;
        fp_ = src.fp_;
        src.fp_ = nullptr;
        put_back_size_ = src.put_back_size_;
        zlib_res_ = src.zlib_res_;
      }

      void replenish_compressed_buffer()
      {
        zstrm_.next_in = compressed_buffer_.data();
        zstrm_.avail_in = fread(compressed_buffer_.data(), 1, compressed_buffer_.size(), fp_);
      }

    protected:

      virtual std::streambuf::int_type underflow()
      {
        if (!fp_)
          return traits_type::eof();
        if (gptr() < egptr()) // buffer not exhausted
          return traits_type::to_int_type(*gptr());

        while ((zlib_res_ == Z_OK || zlib_res_ == Z_STREAM_END) && gptr() >= egptr() && (zstrm_.avail_in > 0 || (!feof(fp_) && !ferror(fp_))))
        {
          zstrm_.next_out = decompressed_buffer_.data();
          zstrm_.avail_out = static_cast<std::uint32_t>(decompressed_buffer_.size());

          if (zstrm_.avail_in == 0 && !feof(fp_) && !ferror(fp_))
          {
            replenish_compressed_buffer();
          }



          //assert(zstrm_.avail_in > 0);
          if (zlib_res_ == Z_STREAM_END && zstrm_.avail_in > 0)
          {
            zlib_res_ = inflateReset(&zstrm_);
            uncompressed_block_offset_ = 0;
            current_block_position_ = std::size_t(ftell(fp_)) - zstrm_.avail_in;
          }

          zlib_res_ = inflate(&zstrm_, Z_NO_FLUSH);


          char* start = ((char*) decompressed_buffer_.data());
          setg(start, start, start + (decompressed_buffer_.size() - zstrm_.avail_out));
          uncompressed_block_offset_ += (egptr() - gptr());

          if (discard_amount_ > 0)
          {
            std::uint64_t advance_amount = discard_amount_;
            if ((egptr() - gptr()) < advance_amount)
              advance_amount = (egptr() - gptr());
            setg(start, gptr() + advance_amount, egptr());
            discard_amount_ -= advance_amount;
          }
        }

        if (zlib_res_ != Z_OK && zlib_res_ != Z_STREAM_END)
          return traits_type::eof();
        else if (gptr() >= egptr())
          return traits_type::eof();

        return traits_type::to_int_type(*gptr());
      }

      virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode which)
      {
        return pos_type(off_type(-1));
      }

    private:
      std::vector<std::uint8_t> compressed_buffer_;
      std::vector<std::uint8_t> decompressed_buffer_;
      std::size_t put_back_size_;
      bool at_block_boundary_;
    protected:
      static const std::size_t default_block_size = 64 * 1024;
      int zlib_res_;
      z_stream zstrm_;
      std::uint16_t discard_amount_;
      std::size_t current_block_position_;
      std::size_t uncompressed_block_offset_;
      FILE* fp_;
    };

    class obuf : public std::streambuf
    {
    public:
      obuf(FILE* fp, int clevel = Z_DEFAULT_COMPRESSION)
        :
        zstrm_({0}),
        fp_(fp),
        compressed_buffer_(default_block_size),
        decompressed_buffer_(default_block_size),
        compression_level_(clevel)
      {
        if (!fp_)
        {
          char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
          setp(end, end);
        }
        else
        {
          zlib_res_ = deflateInit2(&zstrm_, compression_level_, Z_DEFLATED, (15 | 16), 8, Z_DEFAULT_STRATEGY); // |16 for GZIP
          if (zlib_res_ != Z_OK)
          {
            // TODO: handle error.
          }

          zstrm_.next_out = compressed_buffer_.data();
          zstrm_.avail_out = static_cast<std::uint32_t>(compressed_buffer_.size());

          char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
          setp((char*) decompressed_buffer_.data(), end);
        }
      }

      obuf(const std::string& file_path, int clevel = Z_DEFAULT_COMPRESSION) : obuf(fopen(file_path.c_str(), "wb"), clevel) {}
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      obuf(obuf&& src)
        :
        std::streambuf(std::move(src))
      {
        this->move(std::move(src));
      }

      obuf& operator=(obuf&& src)
      {
        if (&src != this)
        {
          std::streambuf::operator=(std::move(src));
          this->close();
          this->move(std::move(src));
        }

        return *this;
      }
#endif
      virtual ~obuf()
      {
        this->close();
      }

    private:
      void move(obuf&& src)
      {
        compressed_buffer_ = std::move(src.compressed_buffer_);
        decompressed_buffer_ = std::move(src.decompressed_buffer_);
        zstrm_ = src.zstrm_;
        fp_ = src.fp_;
        src.fp_ = nullptr;
        zlib_res_ = src.zlib_res_;
        compression_level_ = src.compression_level_;
      }

      void close()
      {
        if (fp_)
        {
          sync();
          int res = deflateEnd(&zstrm_);
          if (zlib_res_ == Z_OK)
            zlib_res_ = res;
          fclose(fp_);
          fp_ = nullptr;
        }
      }
    protected:
      virtual int overflow(int c)
      {
        if (!fp_)
          return traits_type::eof();

        if ((epptr() - pptr()) > 0)
        {
          assert(!"Put buffer not empty, this should never happen");
          this->sputc(static_cast<char>(0xFF & c));
        }
        else
        {
          zstrm_.next_in = decompressed_buffer_.data();
          zstrm_.avail_in = static_cast<std::uint32_t>(decompressed_buffer_.size());
          while (zlib_res_ == Z_OK && zstrm_.avail_in > 0)
          {
            zlib_res_ = deflate(&zstrm_, Z_SYNC_FLUSH);

            if ((compressed_buffer_.size() - zstrm_.avail_out) > 0 && !fwrite(compressed_buffer_.data(), compressed_buffer_.size() - zstrm_.avail_out, 1, fp_))
            {
              // TODO: handle error.
              return traits_type::eof();
            }
            zstrm_.next_out = compressed_buffer_.data();
            zstrm_.avail_out = static_cast<std::uint32_t>(compressed_buffer_.size());
          }

          if (zlib_res_ == Z_STREAM_END)
            zlib_res_ = deflateReset(&zstrm_);

          assert(zstrm_.avail_in == 0);
          decompressed_buffer_[0] = reinterpret_cast<unsigned char&>(c);
          setp((char*) decompressed_buffer_.data() + 1, (char*) decompressed_buffer_.data() + decompressed_buffer_.size());
        }

        return (zlib_res_ == Z_OK ? traits_type::to_int_type(c) : traits_type::eof());
      }

      virtual int sync()
      {
        if (!fp_)
          return -1;

        zstrm_.next_in = decompressed_buffer_.data();
        zstrm_.avail_in = static_cast<std::uint32_t>(decompressed_buffer_.size() - (epptr() - pptr()));
        if (zstrm_.avail_in)
        {
          while (zlib_res_ == Z_OK && zstrm_.avail_in > 0)
          {
            zlib_res_ = deflate(&zstrm_, Z_SYNC_FLUSH);

            if ((compressed_buffer_.size() - zstrm_.avail_out) > 0 && !fwrite(compressed_buffer_.data(), compressed_buffer_.size() - zstrm_.avail_out, 1, fp_))
            {
              // TODO: handle error.
              return -1;
            }
            zstrm_.next_out = compressed_buffer_.data();
            zstrm_.avail_out = static_cast<std::uint32_t>(compressed_buffer_.size());

          }

          if (zlib_res_ != Z_OK)
            return -1;

          assert(zstrm_.avail_in == 0);
          setp((char*) decompressed_buffer_.data(), (char*) decompressed_buffer_.data() + decompressed_buffer_.size());
        }

        if (fflush(fp_) != 0)
          return -1;

        return 0;
      }

    private:
      static const std::size_t default_block_size = 64 * 1024;
      std::vector<std::uint8_t> compressed_buffer_;
      std::vector<std::uint8_t> decompressed_buffer_;
      z_stream zstrm_;
      FILE* fp_;
      int zlib_res_;
      int compression_level_;
    };

    class istream : public std::istream
    {
    public:
      istream(const std::string& file_path)
        :
        std::istream(&sbuf_),
        sbuf_(file_path)
      {
      }
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      istream(istream&& src)
        :
        std::istream(&sbuf_),
        sbuf_(std::move(src.sbuf_))
      {
      }

      istream& operator=(istream&& src)
      {
        if (&src != this)
        {
          std::istream::operator=(std::move(src));
          sbuf_ = std::move(src.sbuf_);
        }
        return *this;
      }
#endif
    private:
      ::shrinkwrap::gz::ibuf sbuf_;
    };



    class ostream : public std::ostream
    {
    public:
      ostream(const std::string& file_path)
        :
        std::ostream(&sbuf_),
        sbuf_(file_path)
      {
      }
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      ostream(ostream&& src)
        :
        std::ostream(&sbuf_),
        sbuf_(std::move(src.sbuf_))
      {
      }

      ostream& operator=(ostream&& src)
      {
        if (&src != this)
        {
          std::ostream::operator=(std::move(src));
          sbuf_ = std::move(src.sbuf_);
        }
        return *this;
      }
#endif
    private:
      ::shrinkwrap::gz::obuf sbuf_;
    };
  }

  namespace bgzf
  {
    class ibuf : public gz::ibuf
    {
    public:
      using gz::ibuf::ibuf;
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      ibuf(ibuf&& src)
        :
        gz::ibuf(std::move(src))
      {
      }

      ibuf& operator=(ibuf&& src)
      {
        if (&src != this)
        {
          ibuf::operator=(std::move(src));
        }

        return *this;
      }
#endif

      virtual ~ibuf()
      {
      }

    protected:
      virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) // Supports tellg for virtual offset.
      {
        if (off == 0 && way == std::ios::cur)
        {
          if (egptr() - gptr() == 0 && zlib_res_ == Z_STREAM_END)
          {
            std::uint64_t compressed_offset = std::size_t(ftell(fp_)) - zstrm_.avail_in;
            std::uint16_t uncompressed_offset = 0;
            std::uint64_t virtual_offset = ((compressed_offset << 16) | uncompressed_offset);
            return pos_type(off_type(virtual_offset));
          }
          else
          {
            std::uint64_t compressed_offset = current_block_position_;
            std::uint16_t uncompressed_offset = (std::uint16_t(uncompressed_block_offset_) - std::uint16_t(egptr() - gptr())) + discard_amount_;
            std::uint64_t virtual_offset = ((compressed_offset << 16) | uncompressed_offset);
            return pos_type(off_type(virtual_offset));
          }
        }
        return pos_type(off_type(-1));
      }

      //coffset << 16 | uoffset
      virtual std::streambuf::pos_type seekpos(std::streambuf::pos_type pos, std::ios_base::openmode which)
      {
        std::uint64_t compressed_offset = ((static_cast<std::uint64_t>(pos) >> 16) & 0x0000FFFFFFFFFFFF);
        std::uint16_t uncompressed_offset = (std::uint16_t) (static_cast<std::uint64_t>(pos) & 0x000000000000FFFF);

        if (fp_ == 0 || sync())
          return pos_type(off_type(-1));

        long seek_amount = static_cast<long>(compressed_offset);
        if (fseek(fp_, seek_amount, SEEK_SET))
          return pos_type(off_type(-1));

        current_block_position_ = compressed_offset;
        discard_amount_ = uncompressed_offset;

        zstrm_.next_in = nullptr;
        zstrm_.avail_in = 0;
        zlib_res_ = inflateReset(&zstrm_);
        char* end = egptr();
        setg(end, end, end);
        uncompressed_block_offset_ = 0;

        return pos;
      }
    };

    class obuf : public std::streambuf
    {
    public:
      obuf(FILE* fp, int clevel = 6, std::ios::openmode mode = std::ios::out)
        :
        fp_(fp),
        compressed_buffer_(bgzf_block_size),
        decompressed_buffer_(bgzf_block_size),
        compression_level_(clevel)
      {
        if (!fp_ || ferror(fp_))
        {
          char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
          setp(end, end);
        }
        else
        {
          char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
          setp((char*) decompressed_buffer_.data(), end);

          if (mode & std::ios::app)
          {
            bool has_eof = false;

            const std::array<std::uint8_t, 28> empty_block = {31, 139, 10, 4, 0, 0, 0, 0, 0, 255, 6, 0, 66, 67, 2, 0, 27, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            std::array<std::uint8_t, 28>  buf;

            fseek(fp_, -28, SEEK_END);
            fread(buf.data(), buf.size(), 1, fp_);

            if (memcmp(empty_block.data(), buf.data(), buf.size()) == 0)
            {
              // Overwrite the trailing EOF.
              fseek(fp_, -28, SEEK_END);
            }
            else
            {
              // No trailing EOF block, so go to the end
              fseek(fp_, 0, SEEK_END);
            }

          }
        }
      }

      obuf(const std::string& file_path, int clevel = 6, std::ios::openmode mode = std::ios::out) : obuf(fopen(file_path.c_str(), mode & std::ios::app ? "r+b" : "wb"), clevel, mode) {}
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      obuf(obuf&& src)
        :
        std::streambuf(std::move(src))
      {
        this->move(std::move(src));
      }

      obuf& operator=(obuf&& src)
      {
        if (&src != this)
        {
          std::streambuf::operator=(std::move(src));
          this->close();
          this->move(std::move(src));
        }

        return *this;
      }
#endif
      virtual ~obuf()
      {
        this->close();
      }

    private:
      void move(obuf&& src)
      {
        compressed_buffer_ = std::move(src.compressed_buffer_);
        decompressed_buffer_ = std::move(src.decompressed_buffer_);
        fp_ = src.fp_;
        src.fp_ = nullptr;
        compression_level_ = src.compression_level_;
      }

      void close()
      {
        if (fp_)
        {
          sync();
          write_compressed_block(0); // write an empty block

          fclose(fp_);
          fp_ = nullptr;
        }
      }
    protected:
      // tellp won't work because there is no guarantee that the uncompressed block
      // won't be divided into multiple compressed blocks when synced.
      //virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode which);


      virtual int overflow(int c)
      {
        if (!fp_)
          return traits_type::eof();

        if ((epptr() - pptr()) > 0)
        {
          assert(!"Put buffer not empty, this should never happen");
          this->sputc(static_cast<char>(0xFF & c));
        }
        else
        {
          if (sync() != 0)
            return traits_type::eof();


          (*pptr()) = reinterpret_cast<unsigned char&>(c);
          setp(pptr() + 1, (char*) decompressed_buffer_.data() + decompressed_buffer_.size());
        }

        return traits_type::to_int_type(c);
      }

      virtual int sync()
      {
        std::uint32_t block_length = static_cast<std::uint32_t>(decompressed_buffer_.size() - (epptr() - pptr()));
        if (block_length)
          return write_compressed_block(block_length);

        if (fflush(fp_) != 0)
          return -1;

        return 0;
      }

      int write_compressed_block(std::uint32_t block_length)
      {
        if (!fp_)
          return -1;

        /* BGZF/GZIP header (speciallized from RFC 1952; little endian):
         * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
         * | 31|139|  8|  4|              0|  0|255|      6| 66| 67|      2|BLK_LEN|
         * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
         */
        const std::array<uint8_t, block_header_length> block_header = {31, 139, 8, 4, 0, 0, 0, 0, 0, 255, 6, 0, 66, 67, 2, 0, 0, 0};

        std::uint32_t input_length = block_length;
        std::uint8_t *buffer = compressed_buffer_.data();
        std::uint32_t buffer_size = bgzf_block_size;

        std::uint32_t compressed_length = 0;
        std::uint32_t remaining;
        std::uint32_t crc;

        assert(block_length <= bgzf_block_size); // guaranteed by the caller

        std::memcpy(buffer, block_header.data(), block_header_length); // the last two bytes are a place holder for the length of the block

        z_stream zs;
        int zlib_res = Z_OK;
        while (zlib_res == Z_OK) // loop to retry for blocks that do not compress enough
        {
          zs = {0};
          zs.next_in = decompressed_buffer_.data();
          zs.avail_in = input_length;
          zs.next_out = &buffer[block_header_length];
          zs.avail_out = static_cast<std::uint32_t>(compressed_buffer_.size());
          //zs.avail_out = buffer_size - BLOCK_HEADER_LENGTH - BLOCK_FOOTER_LENGTH;

          zlib_res = deflateInit2(&zs, compression_level_, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); // -15 to disable zlib header/footer
          if (zlib_res == Z_OK)
          {
            zlib_res = deflate(&zs, Z_FINISH);
            if (zlib_res == Z_OK) //zlib_res_ != Z_STREAM_END)  // not compressed enough
            {
              input_length -= 1024;
              assert(input_length > 0); // logically, this should not happen
              deflateEnd(&zs);
            }
          }
        }

        if (deflateEnd(&zs) == Z_OK && zlib_res == Z_STREAM_END)
        {
          compressed_length = static_cast<std::uint32_t>(zs.total_out);
          compressed_length += block_header_length + block_footer_length;
          assert(compressed_length <= bgzf_block_size);

          assert(compressed_length > 0);
          pack_int_16(&buffer[16], static_cast<std::uint16_t>(compressed_length - 1)); // write the compressed_length; -1 to fit 2 bytes
          crc = crc32(0L, NULL, 0L);
          crc = crc32(crc, decompressed_buffer_.data(), input_length);
          pack_int_32(&buffer[compressed_length - 8], crc);
          pack_int_32(&buffer[compressed_length - 4], input_length);

          if (!fwrite(compressed_buffer_.data(), compressed_length, 1, fp_) || ferror(fp_))
          {
            // TODO: handle error.
            return -1;
          }

          remaining = block_length - input_length;
          if (remaining > 0)
          {
            assert(remaining <= input_length);
            memcpy(decompressed_buffer_.data(), decompressed_buffer_.data() + input_length, remaining);
          }

          setp((char *) decompressed_buffer_.data() + remaining, (char *) decompressed_buffer_.data() + decompressed_buffer_.size());
          return 0;
        }

        return -1;
      }

      static void pack_int_16(uint8_t *buffer, uint16_t value)
      {
        buffer[0] = uint8_t(value);
        buffer[1] = uint8_t(value >> 8);
      }

      static void pack_int_32(uint8_t *buffer, uint32_t value)
      {
        buffer[0] = uint8_t(value);
        buffer[1] = uint8_t(value >> 8);
        buffer[2] = uint8_t(value >> 16);
        buffer[3] = uint8_t(value >> 24);
      }

    private:
      static const std::size_t block_header_length = 18;
      static const std::size_t block_footer_length = 8;
      static const std::size_t bgzf_block_size = 0x10000; // 64k


      static const std::size_t default_block_size = 64 * 1024;
      std::vector<std::uint8_t> compressed_buffer_;
      std::vector<std::uint8_t> decompressed_buffer_;
      FILE* fp_;
      int compression_level_;
    };

    class istream : public std::istream
    {
    public:
      istream(const std::string& file_path)
        :
        std::istream(&sbuf_),
        sbuf_(file_path)
      {
      }
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      istream(istream&& src)
        :
        std::istream(&sbuf_),
        sbuf_(std::move(src.sbuf_))
      {
      }

      istream& operator=(istream&& src)
      {
        if (&src != this)
        {
          std::istream::operator=(std::move(src));
          sbuf_ = std::move(src.sbuf_);
        }
        return *this;
      }
#endif
    private:
      ::shrinkwrap::bgzf::ibuf sbuf_;
    };

    class ostream : public std::ostream
    {
    public:
      ostream(const std::string& file_path, std::ios::openmode mode = std::ios::out)
        :
        std::ostream(&sbuf_),
        sbuf_(file_path)
      {
      }
#if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4
      ostream(ostream&& src)
        :
        std::ostream(&sbuf_),
        sbuf_(std::move(src.sbuf_))
      {
      }

      ostream& operator=(ostream&& src)
      {
        if (&src != this)
        {
          std::ostream::operator=(std::move(src));
          sbuf_ = std::move(src.sbuf_);
        }
        return *this;
      }
#endif
    private:
      ::shrinkwrap::bgzf::obuf sbuf_;
    };
  }
}

#endif //SHRINKWRAP_GZ_HPP