Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/libdar/header_version.hpp
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$
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 = NULL;
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 
00088             ctrl = f.get_crc();
00089             if(ctrl == NULL)
00090                 throw SRC_BUG;
00091             try
00092             {
00093                 if((edition == empty_archive_version()))
00094                     throw Erange("header_version::read", gettext("Consistency check failed for archive header"));
00095                 if(edition > 7)
00096                 {
00097                     crc *coh = create_crc_from_file(f);
00098 
00099                     if(coh == NULL)
00100                         throw SRC_BUG;
00101                     try
00102                     {
00103                         if(typeid(*coh) != typeid(*ctrl))
00104                         {
00105                             if(coh->get_size() != ctrl->get_size())
00106                                 throw SRC_BUG;
00107                             else
00108                                 throw SRC_BUG; // both case lead to a bug, but we need to know which one is met
00109                         }
00110                         if(*coh != *ctrl)
00111                             throw Erange("header_version::read", gettext("Consistency check failed for archive header"));
00112                     }
00113                     catch(...)
00114                     {
00115                         if(coh != NULL)
00116                             delete coh;
00117                         throw;
00118                     }
00119                     if(coh != NULL)
00120                         delete coh;
00121                 }
00122                 if(initial_offset == 0)
00123                     initial_offset = f.get_position();
00124             }
00125             catch(...)
00126             {
00127                 if(ctrl != NULL)
00128                     delete ctrl;
00129                 throw;
00130             }
00131 
00132             if(ctrl != NULL)
00133                 delete ctrl;
00134         };
00135 
00136         void write(generic_file &f)
00137         {
00138             crc *ctrl = NULL;
00139 
00140                 // preparing the data
00141 
00142             if(initial_offset != 0)
00143                 flag |= VERSION_FLAG_INITIAL_OFFSET; // adding it to the flag
00144             else
00145                 flag &= ~VERSION_FLAG_INITIAL_OFFSET; // removing it from the flag
00146 
00147                 // writing down the data
00148 
00149             f.reset_crc(HEADER_CRC_SIZE);
00150             edition.dump(f);
00151             f.write(&algo_zip, sizeof(algo_zip));
00152             tools_write_string(f, cmd_line);
00153             f.write((char *)&flag, 1);
00154             if(initial_offset != 0)
00155                 initial_offset.dump(f);
00156 
00157             ctrl = f.get_crc();
00158             if(ctrl == NULL)
00159                 throw SRC_BUG;
00160             try
00161             {
00162                 ctrl->dump(f);
00163             }
00164             catch(...)
00165             {
00166                 if(ctrl != NULL)
00167                     delete ctrl;
00168                 throw;
00169             }
00170             if(ctrl != NULL)
00171                 delete ctrl;
00172         };
00173     };
00174 
00175 } // end of namespace
00176 
00177 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines