Disk ARchive  2.5.0
Full featured and portable backup and archiving tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
compressor.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 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 
36 namespace libdar
37 {
38 
40 
44  {
45  none = 'n',
46  gzip = 'z',
47  bzip2 = 'y',
48  lzo = 'l',
49  xz = 'x'
50  };
51 
54 
55  extern compression char2compression(char a);
56  extern char compression2char(compression c);
57  extern std::string compression2string(compression c);
58  extern compression string2compression(const std::string & a); // throw Erange if an unknown string is given
59 
61  class compressor : public generic_file
62  {
63  public :
64  compressor(compression algo, generic_file & compressed_side, U_I compression_level = 9);
65  // compressed_side is not owned by the object and will remains
66  // after the objet destruction
67  compressor(compression algo, generic_file *compressed_side, U_I compression_level = 9);
68  // compressed_side is owned by the object and will be
69  // deleted a destructor time
70  ~compressor();
71 
72  compression get_algo() const { return current_algo; };
73 
74  void suspend_compression();
75  void resume_compression();
76  bool is_compression_suspended() const { return suspended; };
77 
78 
79  // inherited from generic file
80  bool skippable(skippability direction, const infinint & amount) { return compressed->skippable(direction, amount); };
81  bool skip(const infinint & pos) { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip(pos); };
82  bool skip_to_eof() { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip_to_eof(); };
83  bool skip_relative(S_I x) { compr_flush_write(); compr_flush_read(); clean_read(); return compressed->skip_relative(x); };
84  infinint get_position() const { return compressed->get_position(); };
85 
86  protected :
87  void inherited_read_ahead(const infinint & amount) { compressed->read_ahead(amount); };
88  U_I inherited_read(char *a, U_I size) { return (this->*read_ptr)(a, size); };
89  void inherited_write(const char *a, U_I size) { (this->*write_ptr)(a, size); };
90  void inherited_sync_write() { compr_flush_write(); };
91  void inherited_flush_read() { compr_flush_read(); clean_read(); };
92  void inherited_terminate() { local_terminate(); };
93 
94  private :
95  struct xfer : public on_pool
96  {
97  wrapperlib wrap;
98  char *buffer;
99  U_I size;
100 
101  xfer(U_I sz, wrapperlib_mode mode);
102  ~xfer();
103  };
104 
105  struct lzo_block_header
106  {
107  char type; //< let the possibility to extend this architecture (for now type is fixed)
108  infinint size; //< size of the following compressed block of data
109 
110  void dump(generic_file & f);
111  void set_from(generic_file & f);
112  };
113 
114 
115  xfer *compr, *decompr; //< datastructure for bzip2 an gzip compression
116 
117  char *lzo_read_buffer; //< stores clear data (uncompressed) read from the compressed generic_file
118  char *lzo_write_buffer; //< stores the clear data to be compressed and written to the compressed generic_file
119  U_I lzo_read_size; //< number of available bytes in the read buffer for lzo decompression
120  U_I lzo_write_size; //< number of available bytes to compress and next place where to add more data in the wite buffer
121  U_I lzo_read_start; //< location of the next byte to read out from the read buffer
122  bool lzo_write_flushed; //< whether write flushing has been done
123  bool lzo_read_reached_eof; //< whether reading reached end of file and the lzo engine has to be reset to uncompress further data
124  char *lzo_compressed; //< compressed data just read or about to be written
125  char *lzo_wrkmem; //< work memory for LZO library
126 
127  generic_file *compressed;
128  bool compressed_owner;
129  compression current_algo;
130  bool suspended;
131  compression suspended_compr;
132  U_I current_level;
133 
134  void init(compression algo, generic_file *compressed_side, U_I compression_level);
135  void local_terminate();
136  U_I (compressor::*read_ptr) (char *a, U_I size);
137  U_I none_read(char *a, U_I size);
138  U_I gzip_read(char *a, U_I size);
139  // U_I zip_read(char *a, U_I size);
140  // U_I bzip2_read(char *a, U_I size); // using gzip_read, same code thanks to wrapperlib
141  U_I lzo_read(char *a, U_I size);
142 
143  void (compressor::*write_ptr) (const char *a, U_I size);
144  void none_write(const char *a, U_I size);
145  void gzip_write(const char *a, U_I size);
146  // void zip_write(char *a, U_I size);
147  // void bzip2_write(char *a, U_I size); // using gzip_write, same code thanks to wrapperlib
148  void lzo_write(const char *a, U_I size);
149 
150  void lzo_compress_buffer_and_write();
151  void lzo_read_and_uncompress_to_buffer();
152 
154 
159  void change_algo(compression new_algo, U_I new_compression_level);
160 
161 
163 
164  void change_algo(compression new_algo)
165  { change_algo(new_algo, current_level); };
166 
167 
168  void compr_flush_write(); // flush all data to compressed_side, and reset the compressor
169  // for that additional write can be uncompresssed starting at this point.
170  void compr_flush_read(); // reset decompression engine to be able to read the next block of compressed data
171  // if not called, furthur read return EOF
172  void clean_read(); // discard any byte buffered and not yet returned by read()
173  void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
174  };
175 
177 
178 } // end of namespace
179 
180 #endif
void inherited_flush_read()
Definition: compressor.hpp:91
are defined here basic integer types that tend to be portable
void inherited_read_ahead(const infinint &amount)
Definition: compressor.hpp:87
lzma compression
Definition: compressor.hpp:49
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
virtual infinint get_position() const =0
get the current read/write position
virtual bool skip(const infinint &pos)=0
virtual void read_ahead(const infinint &amount)
bzip2 compression
Definition: compressor.hpp:47
void inherited_terminate()
destructor-like call, except that it is allowed to throw exceptions
Definition: compressor.hpp:92
gzip compression
Definition: compressor.hpp:46
virtual bool skip_relative(S_I x)=0
skip relatively to the current position
no compression
Definition: compressor.hpp:45
bool skip_to_eof()
skip to the end of file
Definition: compressor.hpp:82
bool skip_relative(S_I x)
skip relatively to the current position
Definition: compressor.hpp:83
void inherited_sync_write()
write down any pending data
Definition: compressor.hpp:90
lzo compression
Definition: compressor.hpp:48
generic_file(gf_mode m)
main constructor
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:77
switch module to limitint (32 ou 64 bits integers) or infinint
compression class for gzip and bzip2 algorithms
Definition: compressor.hpp:61
compression
the different compression algorithm available
Definition: compressor.hpp:43
this is the interface class from which all other data transfer classes inherit
infinint get_position() const
get the current read/write position
Definition: compressor.hpp:84
void inherited_write(const char *a, U_I size)
implementation of the write() operation
Definition: compressor.hpp:89
the arbitrary large positive integer class
bool skip(const infinint &pos)
Definition: compressor.hpp:81
virtual bool skip_to_eof()=0
skip to the end of file
U_I inherited_read(char *a, U_I size)
implementation of read() operation
Definition: compressor.hpp:88
bool skippable(skippability direction, const infinint &amount)
Definition: compressor.hpp:80
compression char2compression(char a)
virtual bool skippable(skippability direction, const infinint &amount)=0