Disk ARchive  2.7.15
Full featured and portable backup and archiving tool
compressor_zstd.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2024 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author, see the AUTHOR file
20 /*********************************************************************/
21 
25 
26 #ifndef COMPRESSOR_ZSTD_HPP
27 #define COMPRESSOR_ZSTD_HPP
28 
29 #include "../my_config.h"
30 
31 extern "C"
32 {
33 #if HAVE_ZSTD_H
34 #include <zstd.h>
35 #endif
36 }
37 
38 #include "proto_compressor.hpp"
39 
40 namespace libdar
41 {
42 
43 
46 
48 
49  class compressor_zstd : public proto_compressor
50  {
51  public :
52  compressor_zstd(generic_file & compressed_side, U_I compression_level = 9);
53  // compressed_side is not owned by the object and will remains
54  // after the objet destruction
55 
56  compressor_zstd(const compressor_zstd & ref) = delete;
57  compressor_zstd(compressor_zstd && ref) noexcept = delete;
58  compressor_zstd & operator = (const compressor_zstd & ref) = delete;
59  compressor_zstd & operator = (compressor_zstd && ref) noexcept = delete;
60  ~compressor_zstd();
61 
62  virtual compression get_algo() const override;
63 
64  virtual void suspend_compression() override;
65  virtual void resume_compression() override;
66  virtual bool is_compression_suspended() const override { return suspended; };
67 
68 
69  // inherited from generic file
70  virtual bool skippable(skippability direction, const infinint & amount) override { return compressed->skippable(direction, amount); };
71  virtual bool skip(const infinint & pos) override { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip(pos); };
72  virtual bool skip_to_eof() override { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip_to_eof(); };
73  virtual bool skip_relative(S_I x) override { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip_relative(x); };
74  virtual bool truncatable(const infinint & pos) const override { return compressed->truncatable(pos); };
75  virtual infinint get_position() const override { return compressed->get_position(); };
76 
77  protected :
78  virtual void inherited_read_ahead(const infinint & amount) override { compressed->read_ahead(amount); };
79  virtual U_I inherited_read(char *a, U_I size) override;
80  virtual void inherited_write(const char *a, U_I size) override;
81  virtual void inherited_truncate(const infinint & pos) override;
82  virtual void inherited_sync_write() override { compr_flush_write(); };
83  virtual void inherited_flush_read() override { compr_flush_read(); clean_read(); };
84  virtual void inherited_terminate() override;
85 
86  private :
87  generic_file *compressed;
88  bool suspended;
89 
90 #if LIBZSTD_AVAILABLE
91  ZSTD_CStream *comp;
92  ZSTD_DStream *decomp;
93 
94  ZSTD_inBuffer inbuf;
95  ZSTD_outBuffer outbuf;
96  char *below_tampon;
97  U_I below_tampon_size;
98  U_I above_tampon_size;
99 
100  bool flueof;
101  bool no_comp_data;
102 #endif
103 
104 
105 
107 
108  void compr_flush_write(); // flush all data to compressed_side, and reset the compressor_zstd
109  // for that additional write can be uncompresssed starting at this point.
110  void compr_flush_read(); // reset decompression engine to be able to read the next block of compressed data
111  // if not called, furthur read return EOF
112  void clean_read(); // discard any byte buffered and not yet returned by read()
113  void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
114 
116 
117  void clear_inbuf();
118  void clear_outbuf();
119  void release_mem();
120  void setup_context(U_I compression_level);
121 
122  };
123 
125 
126 } // end of namespace
127 
128 #endif
compression class for zstd algorithms
virtual void inherited_read_ahead(const infinint &amount) override
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
void reset_compr_engine()
reset the compression engine ready for use
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
virtual bool skip_to_eof() override
skip to the end of file
virtual bool skip(const infinint &pos) override
skip at the absolute position
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
void clear_inbuf()
from zstd
virtual bool skip_relative(S_I x) override
skip relatively to the current position
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
virtual void inherited_sync_write() override
write down any pending data
virtual bool truncatable(const infinint &pos) const override
whether the implementation is able to truncate to the given position
virtual infinint get_position() const override
get the current read/write position
this is the interface class from which all other data transfer classes inherit
virtual bool truncatable(const infinint &pos) const =0
whether the implementation is able to truncate to the given position
virtual bool skip(const infinint &pos)=0
skip at the absolute position
virtual bool skippable(skippability direction, const infinint &amount)=0
whether the implementation is able to skip
void read_ahead(const infinint &amount)
virtual infinint get_position() const =0
get the current read/write position
virtual bool skip_relative(S_I x)=0
skip relatively to the current position
virtual bool skip_to_eof()=0
skip to the end of file
the arbitrary large positive integer class
compression
the different compression algorithm available
Definition: compression.hpp:46
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
abstracted ancestor class for compressor and parallel_compressor classes