Disk ARchive  2.6.0
Full featured and portable backup and archiving tool
compressor.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2018 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef COMPRESSOR_HPP
27 #define COMPRESSOR_HPP
28 
29 #include "../my_config.h"
30 
31 #include "infinint.hpp"
32 #include "generic_file.hpp"
33 #include "integers.hpp"
34 #include "wrapperlib.hpp"
35 #include "compression.hpp"
36 
37 namespace libdar
38 {
39 
40 
43 
45  class compressor : public generic_file
46  {
47  public :
48  compressor(compression algo, generic_file & compressed_side, U_I compression_level = 9);
49  // compressed_side is not owned by the object and will remains
50  // after the objet destruction
51 
52  compressor(compression algo, generic_file *compressed_side, U_I compression_level = 9);
53  // compressed_side is owned by the object and will be
54  // deleted a destructor time
55 
56  compressor(const compressor & ref) = delete;
57  compressor(compressor && ref) = delete;
58  compressor & operator = (const compressor & ref) = delete;
59  compressor & operator = (compressor && ref) = delete;
60  ~compressor();
61 
62  compression get_algo() const { return (current_algo == compression::lzo1x_1_15 || current_algo == compression::lzo1x_1) ? compression::lzo : current_algo; };
63 
64  void suspend_compression();
65  void resume_compression();
66  bool is_compression_suspended() const { 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 infinint get_position() const override { return compressed->get_position(); };
75 
76  protected :
77  virtual void inherited_read_ahead(const infinint & amount) override { compressed->read_ahead(amount); };
78  virtual U_I inherited_read(char *a, U_I size) override { return (this->*read_ptr)(a, size); };
79  virtual void inherited_write(const char *a, U_I size) override { (this->*write_ptr)(a, size); };
80  virtual void inherited_sync_write() override { compr_flush_write(); };
81  virtual void inherited_flush_read() override { compr_flush_read(); clean_read(); };
82  virtual void inherited_terminate() override { local_terminate(); };
83 
84  private :
85  struct xfer
86  {
87  wrapperlib wrap;
88  char *buffer;
89  U_I size;
90 
91  xfer(U_I sz, wrapperlib_mode mode);
92  ~xfer();
93  };
94 
95  struct lzo_block_header
96  {
97  char type;
98  infinint size;
99 
100  void dump(generic_file & f);
101  void set_from(generic_file & f);
102  };
103 
104 
105  xfer *compr, *decompr;
106 
115  char *lzo_wrkmem;
116 
117  generic_file *compressed;
118  bool compressed_owner;
119  compression current_algo;
120  bool suspended;
121  compression suspended_compr;
122  U_I current_level;
123 
124  void init(compression algo, generic_file *compressed_side, U_I compression_level);
125  void local_terminate();
126  U_I (compressor::*read_ptr) (char *a, U_I size);
127  U_I none_read(char *a, U_I size);
128  U_I gzip_read(char *a, U_I size);
129  // U_I zip_read(char *a, U_I size);
130  // U_I bzip2_read(char *a, U_I size); // using gzip_read, same code thanks to wrapperlib
131  U_I lzo_read(char *a, U_I size);
132 
133  void (compressor::*write_ptr) (const char *a, U_I size);
134  void none_write(const char *a, U_I size);
135  void gzip_write(const char *a, U_I size);
136  // void zip_write(char *a, U_I size);
137  // void bzip2_write(char *a, U_I size); // using gzip_write, same code thanks to wrapperlib
138  void lzo_write(const char *a, U_I size);
139 
140  void lzo_compress_buffer_and_write();
141  void lzo_read_and_uncompress_to_buffer();
142 
144 
149  void change_algo(compression new_algo, U_I new_compression_level);
150 
151 
153 
154  void change_algo(compression new_algo)
155  { change_algo(new_algo, current_level); };
156 
157 
158  void compr_flush_write(); // flush all data to compressed_side, and reset the compressor
159  // for that additional write can be uncompresssed starting at this point.
160  void compr_flush_read(); // reset decompression engine to be able to read the next block of compressed data
161  // if not called, furthur read return EOF
162  void clean_read(); // discard any byte buffered and not yet returned by read()
163  void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
164  };
165 
167 
168 } // end of namespace
169 
170 #endif
lzo degraded algo corresponding to lzop -1
are defined here basic integer types that tend to be portable
char * lzo_read_buffer
stores clear data (uncompressed) read from the compressed generic_file
Definition: compressor.hpp:107
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...
Definition: compressor.hpp:77
virtual void inherited_sync_write() override
write down any pending data
Definition: compressor.hpp:80
void change_algo(compression new_algo)
changes the compression algorithm keeping the same compression level
Definition: compressor.hpp:154
xfer * decompr
datastructure for bzip2 an gzip compression
Definition: compressor.hpp:105
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
lzo compression
virtual infinint get_position() const =0
get the current read/write position
U_I lzo_write_size
number of available bytes to compress and next place where to add more data in the wite buffer ...
Definition: compressor.hpp:110
virtual bool skip(const infinint &pos)=0
skip at the absolute position
virtual infinint get_position() const override
get the current read/write position
Definition: compressor.hpp:74
char * lzo_wrkmem
work memory for LZO library
Definition: compressor.hpp:115
lzo degraded algo corresponding to lzo -2 to lzo -6
virtual bool skip_relative(S_I x)=0
skip relatively to the current position
bool lzo_read_reached_eof
whether reading reached end of file and the lzo engine has to be reset to uncompress further data ...
Definition: compressor.hpp:113
void read_ahead(const infinint &amount)
compression parameters for API
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
Definition: compressor.hpp:82
libz and libbz2 wrapper to have identical interface to these libraries.libz and libbz2 library differ...
this class encapsulates calls to libz or libbz2
Definition: wrapperlib.hpp:74
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
Definition: compressor.hpp:70
switch module to limitint (32 ou 64 bits integers) or infinint
compression class for gzip and bzip2 algorithms
Definition: compressor.hpp:45
virtual bool skip(const infinint &pos) override
skip at the absolute position
Definition: compressor.hpp:71
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position ...
Definition: compressor.hpp:81
virtual bool skip_to_eof() override
skip to the end of file
Definition: compressor.hpp:72
compression
the different compression algorithm available
Definition: compression.hpp:45
this is the interface class from which all other data transfer classes inherit
U_I lzo_read_size
number of available bytes in the read buffer for lzo decompression
Definition: compressor.hpp:109
the arbitrary large positive integer class
void change_algo(compression new_algo, U_I new_compression_level)
changes compression algorithm used by the compressor
virtual bool skip_relative(S_I x) override
skip relatively to the current position
Definition: compressor.hpp:73
U_I lzo_read_start
location of the next byte to read out from the read buffer
Definition: compressor.hpp:111
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
Definition: compressor.hpp:78
virtual bool skip_to_eof()=0
skip to the end of file
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
Definition: compressor.hpp:79
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:46
virtual bool skippable(skippability direction, const infinint &amount)=0
whether the implementation is able to skip
bool lzo_write_flushed
whether write flushing has been done
Definition: compressor.hpp:112
char * lzo_compressed
compressed data just read or about to be written
Definition: compressor.hpp:114
char * lzo_write_buffer
stores the clear data to be compressed and written to the compressed generic_file ...
Definition: compressor.hpp:108