File: zstd.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 (446 lines) | stat: -rw-r--r-- 12,181 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
#ifndef SHRINKWRAP_ZSTD_HPP
#define SHRINKWRAP_ZSTD_HPP

//#ifndef ZSTD_STATIC_LINKING_ONLY
//#define ZSTD_STATIC_LINKING_ONLY
//#endif

#include <zstd.h>

#include <streambuf>
#include <stdio.h>
#include <vector>

namespace shrinkwrap
{
  namespace zstd
  {
    class ibuf : public std::streambuf
    {
    public:
      ibuf(FILE* fp)
        :
        strm_(ZSTD_createDStream()),
        input_({0}),
        compressed_buffer_(ZSTD_DStreamInSize()),
        decompressed_buffer_(ZSTD_DStreamOutSize()),
        current_block_position_(0),
        fp_(fp)
      {
        if (fp_)
        {
          res_ = ZSTD_initDStream(strm_); // 16 for GZIP only.
          if (ZSTD_isError(res_))
          {
            // 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_)
        {
          ZSTD_freeDStream(strm_);
          fclose(fp_);
        }
      }

      void move(ibuf&& src)
      {
        strm_ = src.strm_;
        src.strm_ = nullptr;
        compressed_buffer_ = std::move(src.compressed_buffer_);
        decompressed_buffer_ = std::move(src.decompressed_buffer_);
        current_block_position_ = src.current_block_position_;
        fp_ = src.fp_;
        src.fp_ = nullptr;
        res_ = src.res_;
        input_ = src.input_;
      }

      void replenish_compressed_buffer()
      {
         input_ = {compressed_buffer_.data(), fread(compressed_buffer_.data(), 1, compressed_buffer_.size(), fp_), 0 };
      }

    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 (!ZSTD_isError(res_) && gptr() >= egptr() && (input_.pos < input_.size || (!feof(fp_) && !ferror(fp_))))
        {
          if (input_.pos == input_.size && !feof(fp_) && !ferror(fp_))
          {
            replenish_compressed_buffer();
          }

          if (res_ == 0 && input_.pos < input_.size)
          {
            res_ = ZSTD_initDStream(strm_); //ZSTD_resetDStream(strm_);
            current_block_position_ = std::size_t(ftell(fp_)) - (input_.size - input_.pos);
          }

          ZSTD_outBuffer output = {decompressed_buffer_.data(), decompressed_buffer_.size(), 0};
          res_ = ZSTD_decompressStream(strm_, &output , &input_);

          if (!ZSTD_isError(res_))
          {
            char* start = ((char*) decompressed_buffer_.data());
            setg(start, start, start + output.pos);
          }
        }

        if (ZSTD_isError(res_))
          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)
      {
        if (off == 0 && way == std::ios::cur)
        {
          if (egptr() - gptr() == 0 && res_ == 0)
          {
            std::uint64_t compressed_offset = std::size_t(ftell(fp_)) - (input_.size - input_.pos);
            return pos_type(off_type(compressed_offset));
          }
          else
          {
            std::uint64_t compressed_offset = current_block_position_;
            return pos_type(off_type(compressed_offset));
          }
        }
        return pos_type(off_type(-1));
      }

      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);

        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));

        input_.src = nullptr;
        input_.pos = 0;
        input_.size = 0;
        res_ = 0;
        char* end = egptr();
        setg(end, end, end);

        return pos;
      }

    private:
      std::vector<std::uint8_t> compressed_buffer_;
      std::vector<std::uint8_t> decompressed_buffer_;
      ZSTD_DStream* strm_;
      ZSTD_inBuffer input_;
      FILE* fp_;
      std::size_t res_;
      std::size_t current_block_position_;
    };

    class obuf : public std::streambuf
    {
    public:
      obuf(FILE* fp, int compression_level)
        :
        strm_(ZSTD_createCStream()),
        fp_(fp),
        compressed_buffer_(ZSTD_CStreamOutSize()),
        decompressed_buffer_(ZSTD_CStreamInSize()),
        block_position_(0),
        total_compressed_bytes_written_(0),
        compression_level_(compression_level),
        res_(0)
      {
        if (!fp_)
        {
          char* end = ((char*) decompressed_buffer_.data()) + decompressed_buffer_.size();
          setp(end, end);
        }
        else
        {
          res_ = ZSTD_initCStream(strm_, compression_level_);
          if (ZSTD_isError(res_))
          {
            // TODO: handle error.
          }

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

      obuf(const std::string& file_path, int compression_level = 3) : obuf(fopen(file_path.c_str(), "wb"), compression_level) {}

#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_);
        block_position_ = std::move(src.block_position_);
        total_compressed_bytes_written_ = src.total_compressed_bytes_written_;
        strm_ = src.strm_;
        fp_ = src.fp_;
        src.fp_ = nullptr;
        res_ = src.res_;
      }

      void close()
      {
        if (fp_)
        {
          sync();
          res_ = ZSTD_freeCStream(strm_);
          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
        {
          ZSTD_inBuffer input = {decompressed_buffer_.data(), decompressed_buffer_.size(), 0};
          while (!ZSTD_isError(res_) && input.pos < input.size)
          {
            ZSTD_outBuffer output = {compressed_buffer_.data(), compressed_buffer_.size(), 0};
            res_ = ZSTD_compressStream(strm_, &output, &input);

            if (output.pos && !fwrite(compressed_buffer_.data(), output.pos, 1, fp_))
            {
              // TODO: handle error.
              return traits_type::eof();
            }
            total_compressed_bytes_written_ += output.pos;
          }

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

        return (!ZSTD_isError(res_) ? traits_type::to_int_type(c) : traits_type::eof());
      }

      virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir way, std::ios_base::openmode which)
      {
        if (off == 0 && way == std::ios::cur)
        {
          return block_position_;
        }
        return pos_type(off_type(-1));
      }


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

        ZSTD_inBuffer input = {decompressed_buffer_.data(), (decompressed_buffer_.size() - (epptr() - pptr())), 0};

        if (input.pos < input.size)
        {
          while (!ZSTD_isError(res_) && input.pos < input.size)
          {
            ZSTD_outBuffer output = {compressed_buffer_.data(), compressed_buffer_.size(), 0};
            res_ = ZSTD_compressStream(strm_, &output, &input);

            if (output.pos && !fwrite(compressed_buffer_.data(), output.pos, 1, fp_))
            {
              // TODO: handle error.
              return -1;
            }
            total_compressed_bytes_written_ += output.pos;
          }

          while (!ZSTD_isError(res_) && res_ != 0)
          {
            ZSTD_outBuffer output = {compressed_buffer_.data(), compressed_buffer_.size(), 0};
            res_ = ZSTD_endStream(strm_, &output);
            if (output.pos && !fwrite(compressed_buffer_.data(), output.pos, 1, fp_))
            {
              // TODO: handle error.
              return -1;
            }
            total_compressed_bytes_written_ += output.pos;
          }

          if (ZSTD_isError(res_))
            return -1;

          res_ = ZSTD_initCStream(strm_, compression_level_); //ZSTD_resetCStream(strm_, 0);

          setp((char*) decompressed_buffer_.data(), (char*) decompressed_buffer_.data() + decompressed_buffer_.size());
          assert(ftell(fp_) == -1 || ftell(fp_) == total_compressed_bytes_written_); // ftell() returns -1 when output file is piped stream
          block_position_ = total_compressed_bytes_written_; // ftell(fp_);
        }

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

        return 0;
      }

    private:
      std::vector<std::uint8_t> compressed_buffer_;
      std::vector<std::uint8_t> decompressed_buffer_;
      std::streambuf::pos_type block_position_;
      std::size_t total_compressed_bytes_written_;
      ZSTD_CStream* strm_;
      FILE* fp_;
      int compression_level_;
      std::size_t res_;
    };

    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::zstd::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::zstd::obuf sbuf_;
    };
  }
}

#endif //SHRINKWRAP_ZSTD_HPP