1 //  This file is part of par2cmdline (a PAR 2.0 compatible file verification and
2 //  repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
3 //
4 //  Copyright (c) 2003 Peter Brian Clements
5 //
6 //  par2cmdline is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 2 of the License, or
9 //  (at your option) any later version.
10 //
11 //  par2cmdline is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19 
20 #ifndef __RECOVERYPACKET_H__
21 #define __RECOVERYPACKET_H__
22 
23 // The RecoveryPacket object is used to access a specific recovery
24 // packet within a recovery file (for when creating them, or using
25 // them during a repair operation).
26 
27 // Because the actual recovery data for the packet may be large, the
28 // RecoveryPacket object only contains a copy of packet header and
29 // exponent value for the block and uses a DataBlock object for
30 // all accesses to the actual recovery data in the packet.
31 
32 class RecoveryPacket
33 {
34 public:
35   RecoveryPacket(void);
36   ~RecoveryPacket(void);
37 
38 public:
39   // Create a recovery packet for a specified file.
40   void Create(DiskFile      *diskfile,  // Which file will the packet be stored in
41               u64            offset,    // At what offset will the packet be stored
42               u64            blocksize, // How much recovery data will it contain
43               u32            exponent,  // What exponent value will be used
44               const MD5Hash &setid);    // What is the SetId
45   // Write some data to the recovery data block and update the recovery packet.
46   bool WriteData(u64         position,  // Relative position within the data block
47                  size_t      size,      // Size of data to write to block
48                  const void *buffer);   // Buffer containing the data to write
49   // Finish computing the hash of the recovery packet and write the header to disk.
50   bool WriteHeader(void);
51 
52 public:
53   // Load a recovery packet from a specified file
54   bool Load(DiskFile *diskfile, u64 offset, PACKET_HEADER &header);
55 
56 public:
57   // Get the lenght of the packet.
58   u64 PacketLength(void) const;
59 
60   // The the exponent of the packet.
61   u32 Exponent(void) const;
62 
63   // The length of the recovery data
64   u64 BlockSize(void) const;
65 
66   // The data block
67   DataBlock* GetDataBlock(void);
68 
69 protected:
70   DiskFile           *diskfile;       // The specific file that this packet is stored in
71   u64                 offset;         // The offset at which the packet is stored
72 
73   RECOVERYBLOCKPACKET packet;         // The packet (excluding the actual recovery data)
74 
75   MD5Context         *packetcontext;  // MD5 Context used to compute the packet hash
76 
77   DataBlock           datablock;      // The recovery data block.
78 };
79 
PacketLength(void)80 inline u64 RecoveryPacket::PacketLength(void) const
81 {
82   return packet.header.length;
83 }
84 
Exponent(void)85 inline u32 RecoveryPacket::Exponent(void) const
86 {
87   return packet.exponent;
88 }
89 
BlockSize(void)90 inline u64 RecoveryPacket::BlockSize(void) const
91 {
92   return packet.header.length - sizeof(packet);
93 }
94 
GetDataBlock(void)95 inline DataBlock* RecoveryPacket::GetDataBlock(void)
96 {
97   return &datablock;
98 }
99 
100 #endif // __RECOVERYPACKET_H__
101