Disk ARchive  2.4.5
 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 // $Id: compressor.hpp,v 1.26.2.2 2012/02/19 22:15:05 edrusb Rel $
22 //
23 /*********************************************************************/
27 
28 #ifndef COMPRESSOR_HPP
29 #define COMPRESSOR_HPP
30 
31 #include "../my_config.h"
32 
33 #include "infinint.hpp"
34 #include "generic_file.hpp"
35 #include "integers.hpp"
36 #include "wrapperlib.hpp"
37 
38 namespace libdar
39 {
40 
42 
46  {
47  none = 'n',
48  gzip = 'z',
49  bzip2 = 'y',
50  lzo = 'l'
51  };
52 
55 
56  extern compression char2compression(char a);
57  extern char compression2char(compression c);
58  extern std::string compression2string(compression c);
59  extern compression string2compression(const std::string & a); // throw Erange if an unknown string is given
60 
62  class compressor : public generic_file
63  {
64  public :
65  compressor(compression algo, generic_file & compressed_side, U_I compression_level = 9);
66  // compressed_side is not owned by the object and will remains
67  // after the objet destruction
68  compressor(compression algo, generic_file *compressed_side, U_I compression_level = 9);
69  // compressed_side is owned by the object and will be
70  // deleted a destructor time
71  ~compressor();
72 
73  void flush_write(); // flush all data to compressed_side, and reset the compressor
74  // for that additional write can be uncompresssed starting at this point.
75  void flush_read(); // reset decompression engine to be able to read the next block of compressed data
76  // if not called, furthur read return EOF
77  void clean_read(); // discard any byte buffered and not yet returned by read()
78  void clean_write(); // discard any byte buffered and not yet wrote to compressed_side;
79 
80  compression get_algo() const { return current_algo; };
81 
83 
88  void change_algo(compression new_algo, U_I new_compression_level);
89 
90 
92 
93  void change_algo(compression new_algo)
94  {
95  change_algo(new_algo, current_level);
96  };
97 
98  // inherited from generic file
99  bool skip(const infinint & pos) { flush_write(); flush_read(); clean_read(); return compressed->skip(pos); };
100  bool skip_to_eof() { flush_write(); flush_read(); clean_read(); return compressed->skip_to_eof(); };
101  bool skip_relative(S_I x) { flush_write(); flush_read(); clean_read(); return compressed->skip_relative(x); };
102  infinint get_position() { return compressed->get_position(); };
103 
104  protected :
105  U_I inherited_read(char *a, U_I size) { return (this->*read_ptr)(a, size); };
106  void inherited_write(const char *a, U_I size) { (this->*write_ptr)(a, size); };
107  void inherited_sync_write() { flush_write(); };
108  void inherited_terminate() { local_terminate(); };
109  private :
110  struct xfer
111  {
112  wrapperlib wrap;
113  char *buffer;
114  U_I size;
115 
116  xfer(U_I sz, wrapperlib_mode mode);
117  ~xfer();
118  };
119 
120  struct lzo_block_header
121  {
122  char type; //< let the possibility to extend this architecture (for now type is fixed)
123  infinint size; //< size of the following compressed block of data
124 
125  void dump(generic_file & f);
126  void set_from(generic_file & f);
127  };
128 
129 
130  xfer *compr, *decompr; //< datastructure for bzip2 an gzip compression
131 
132  char *lzo_read_buffer; //< stores clear data (uncompressed) read from the compressed generic_file
133  char *lzo_write_buffer; //< stores the clear data to be compressed and written to the compressed generic_file
134  U_I lzo_read_size; //< number of available bytes in the read buffer for lzo decompression
135  U_I lzo_write_size; //< number of available bytes to compress and next place where to add more data in the wite buffer
136  U_I lzo_read_start; //< location of the next byte to read out from the read buffer
137  bool lzo_write_flushed; //< whether write flushing has been done
138  bool lzo_read_reached_eof; //< whether reading reached end of file and the lzo engine has to be reset to uncompress further data
139  char *lzo_compressed; //< compressed data just read or about to be written
140  char *lzo_wrkmem; //< work memory for LZO library
141 
142  generic_file *compressed;
143  bool compressed_owner;
144  compression current_algo;
145  U_I current_level;
146 
147  void init(compression algo, generic_file *compressed_side, U_I compression_level);
148  void local_terminate();
149  U_I (compressor::*read_ptr) (char *a, U_I size);
150  U_I none_read(char *a, U_I size);
151  U_I gzip_read(char *a, U_I size);
152  // U_I zip_read(char *a, U_I size);
153  // U_I bzip2_read(char *a, U_I size); // using gzip_read, same code thanks to wrapperlib
154  U_I lzo_read(char *a, U_I size);
155 
156  void (compressor::*write_ptr) (const char *a, U_I size);
157  void none_write(const char *a, U_I size);
158  void gzip_write(const char *a, U_I size);
159  // void zip_write(char *a, U_I size);
160  // void bzip2_write(char *a, U_I size); // using gzip_write, same code thanks to wrapperlib
161  void lzo_write(const char *a, U_I size);
162 
163  void lzo_compress_buffer_and_write();
164  void lzo_read_and_uncompress_to_buffer();
165  };
166 
168 
169 } // end of namespace
170 
171 #endif