Disk ARchive  2.4.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
header_version.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 HEADER_VERSION_HPP
27 #define HEADER_VERSION_HPP
28 
29 #include "../my_config.h"
30 #include "infinint.hpp"
31 #include "generic_file.hpp"
32 #include "tools.hpp"
33 #include "archive_version.hpp"
34 
35 namespace libdar
36 {
37 
40 
41  const U_I VERSION_FLAG_SAVED_EA_ROOT = 0x80; //< no more used since version "05"
42  const U_I VERSION_FLAG_SAVED_EA_USER = 0x40; //< no more used since version "05"
43  const U_I VERSION_FLAG_SCRAMBLED = 0x20; //< scrambled or strong encryption used
44  const U_I VERSION_FLAG_SEQUENCE_MARK = 0x10; //< escape sequence marks present for sequential reading
45  const U_I VERSION_FLAG_INITIAL_OFFSET = 0x08; //< whether the header contains the initial offset (size of clear data before encrypted) NOTE : This value is set internally by header_version, no need to set flag with it! But that's OK to set it or not, it will be updated according to initial_offset's value.
46  const U_I VERSION_FLAG_HAS_AN_EXTENDED_SIZE = 0x01; //< reserved for future use
47  const U_I VERSION_SIZE = 3; //< size of the version field
48  const U_I HEADER_CRC_SIZE = 2; //< size of the CRC (deprecated, now only used when reading old archives)
49 
50 
53  {
54  archive_version edition;
55  char algo_zip;
56  std::string cmd_line; // used long ago to store cmd_line, then abandonned, then recycled as a user comment field
57  unsigned char flag; // added at edition 02
58  infinint initial_offset; // not dropped to archive if set to zero (at dump() time, the flag is also updated with VERSION_FLAG_INITIAL_OFFSET accordingly to this value)
59 
61  {
62  algo_zip = ' ';
63  cmd_line = "";
64  flag = 0;
65  initial_offset = 0;
66  }
67 
68  void read(generic_file &f)
69  {
70  crc *ctrl = NULL;
71 
72  f.reset_crc(HEADER_CRC_SIZE);
73  edition.read(f);
74  f.read(&algo_zip, sizeof(algo_zip));
75  tools_read_string(f, cmd_line);
76  if(edition > 1)
77  f.read((char *)&flag, 1);
78  else
79  flag = 0;
80  if((flag & VERSION_FLAG_INITIAL_OFFSET) != 0)
81  initial_offset.read(f);
82  else
83  initial_offset = 0;
84 
85  ctrl = f.get_crc();
86  if(ctrl == NULL)
87  throw SRC_BUG;
88  try
89  {
90  if((edition == empty_archive_version()))
91  throw Erange("header_version::read", gettext("Consistency check failed for archive header"));
92  if(edition > 7)
93  {
94  crc *coh = create_crc_from_file(f);
95 
96  if(coh == NULL)
97  throw SRC_BUG;
98  try
99  {
100  if(typeid(*coh) != typeid(*ctrl))
101  {
102  if(coh->get_size() != ctrl->get_size())
103  throw SRC_BUG;
104  else
105  throw SRC_BUG; // both case lead to a bug, but we need to know which one is met
106  }
107  if(*coh != *ctrl)
108  throw Erange("header_version::read", gettext("Consistency check failed for archive header"));
109  }
110  catch(...)
111  {
112  if(coh != NULL)
113  delete coh;
114  throw;
115  }
116  if(coh != NULL)
117  delete coh;
118  }
119  if(initial_offset == 0)
120  initial_offset = f.get_position();
121  }
122  catch(...)
123  {
124  if(ctrl != NULL)
125  delete ctrl;
126  throw;
127  }
128 
129  if(ctrl != NULL)
130  delete ctrl;
131  };
132 
133  void write(generic_file &f)
134  {
135  crc *ctrl = NULL;
136 
137  // preparing the data
138 
139  if(initial_offset != 0)
140  flag |= VERSION_FLAG_INITIAL_OFFSET; // adding it to the flag
141  else
142  flag &= ~VERSION_FLAG_INITIAL_OFFSET; // removing it from the flag
143 
144  // writing down the data
145 
146  f.reset_crc(HEADER_CRC_SIZE);
147  edition.dump(f);
148  f.write(&algo_zip, sizeof(algo_zip));
149  tools_write_string(f, cmd_line);
150  f.write((char *)&flag, 1);
151  if(initial_offset != 0)
152  initial_offset.dump(f);
153 
154  ctrl = f.get_crc();
155  if(ctrl == NULL)
156  throw SRC_BUG;
157  try
158  {
159  ctrl->dump(f);
160  }
161  catch(...)
162  {
163  if(ctrl != NULL)
164  delete ctrl;
165  throw;
166  }
167  if(ctrl != NULL)
168  delete ctrl;
169  };
170  };
171 
172 } // end of namespace
173 
174 #endif
a set of general purpose routines
void tools_write_string(generic_file &f, const std::string &s)
write a string to a file with a &#39;\0&#39; at then end
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
U_I read(char *a, U_I size)
read data from the generic_file
crc * get_crc()
get CRC of the transfered date since last reset
void tools_read_string(generic_file &f, std::string &s)
read a string from a file expecting it to terminate by &#39;\0&#39;
switch module to limitint (32 ou 64 bits integers) or infinint
exception used to signal range error
Definition: erreurs.hpp:172
this is the interface class from which all other data transfer classes inherit
manages of the archive header and trailer
void write(const char *a, U_I size)
write data to the generic_file
class archive_version that rules which archive format to follow
void reset_crc(const infinint &width)
reset CRC on read or writen data
the arbitrary large positive integer class
class archive_version manages the version of the archive format
virtual infinint get_position()=0
get the current read/write position