Disk ARchive
2.4.2
|
00001 /*********************************************************************/ 00002 // dar - disk archive - a backup/restoration program 00003 // Copyright (C) 2002-2052 Denis Corbin 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // to contact the author : http://dar.linux.free.fr/email.html 00020 /*********************************************************************/ 00021 // $Id: header_version.hpp,v 1.22 2011/01/09 17:25:58 edrusb Rel $ 00022 // 00023 /*********************************************************************/ 00024 00028 00029 #ifndef HEADER_VERSION_HPP 00030 #define HEADER_VERSION_HPP 00031 00032 #include "../my_config.h" 00033 #include "infinint.hpp" 00034 #include "generic_file.hpp" 00035 #include "tools.hpp" 00036 #include "archive_version.hpp" 00037 00038 namespace libdar 00039 { 00040 00043 00044 const U_I VERSION_FLAG_SAVED_EA_ROOT = 0x80; //< no more used since version "05" 00045 const U_I VERSION_FLAG_SAVED_EA_USER = 0x40; //< no more used since version "05" 00046 const U_I VERSION_FLAG_SCRAMBLED = 0x20; //< scrambled or strong encryption used 00047 const U_I VERSION_FLAG_SEQUENCE_MARK = 0x10; //< escape sequence marks present for sequential reading 00048 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. 00049 const U_I VERSION_FLAG_HAS_AN_EXTENDED_SIZE = 0x01; //< reserved for future use 00050 const U_I VERSION_SIZE = 3; //< size of the version field 00051 const U_I HEADER_CRC_SIZE = 2; //< size of the CRC (deprecated, now only used when reading old archives) 00052 00053 00055 struct header_version 00056 { 00057 archive_version edition; 00058 char algo_zip; 00059 std::string cmd_line; // used long ago to store cmd_line, then abandonned, then recycled as a user comment field 00060 unsigned char flag; // added at edition 02 00061 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) 00062 00063 header_version() 00064 { 00065 algo_zip = ' '; 00066 cmd_line = ""; 00067 flag = 0; 00068 initial_offset = 0; 00069 } 00070 00071 void read(generic_file &f) 00072 { 00073 crc ctrl; 00074 00075 f.reset_crc(HEADER_CRC_SIZE); 00076 edition.read(f); 00077 f.read(&algo_zip, sizeof(algo_zip)); 00078 tools_read_string(f, cmd_line); 00079 if(edition > 1) 00080 f.read((char *)&flag, 1); 00081 else 00082 flag = 0; 00083 if((flag & VERSION_FLAG_INITIAL_OFFSET) != 0) 00084 initial_offset.read(f); 00085 else 00086 initial_offset = 0; 00087 f.get_crc(ctrl); 00088 if((edition == empty_archive_version())) 00089 throw Erange("header_version::read", gettext("Consistency check failed for archive header")); 00090 if(edition > 7) 00091 { 00092 crc coh = infinint(HEADER_CRC_SIZE); 00093 coh.read(f); 00094 if(coh != ctrl) 00095 throw Erange("header_version::read", gettext("Consistency check failed for archive header")); 00096 } 00097 if(initial_offset == 0) 00098 initial_offset = f.get_position(); 00099 }; 00100 00101 void write(generic_file &f) 00102 { 00103 crc ctrl; 00104 00105 // preparing the data 00106 00107 if(initial_offset != 0) 00108 flag |= VERSION_FLAG_INITIAL_OFFSET; // adding it to the flag 00109 else 00110 flag &= ~VERSION_FLAG_INITIAL_OFFSET; // removing it from the flag 00111 00112 // writing down the data 00113 00114 f.reset_crc(HEADER_CRC_SIZE); 00115 edition.dump(f); 00116 f.write(&algo_zip, sizeof(algo_zip)); 00117 tools_write_string(f, cmd_line); 00118 f.write((char *)&flag, 1); 00119 if(initial_offset != 0) 00120 initial_offset.dump(f); 00121 f.get_crc(ctrl); 00122 ctrl.dump(f); 00123 }; 00124 }; 00125 00126 } // end of namespace 00127 00128 #endif