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 __CRITICALPACKET_H__
21 #define __CRITICALPACKET_H__
22 
23 // Base class for main packet, file verification packet, file description packet
24 // and creator packet.
25 
26 // These packets are all small and are held in memory in their entirity
27 
28 class CriticalPacket
29 {
30 public:
31   CriticalPacket(void);
32   ~CriticalPacket(void);
33 
34 public:
35   // Write a copy of the packet to the specified file at the specified offset
36   bool    WritePacket(DiskFile &diskfile, u64 fileoffset) const;
37 
38   // Obtain the lenght of the packet.
39   size_t  PacketLength(void) const;
40 
41   // Allocate some memory for the packet (plus some extra padding).
42   void*   AllocatePacket(size_t length, size_t extra = 0);
43 
44   // Finish a packet (by storing the set_id_hash and then computing the packet_hash).
45   void    FinishPacket(const MD5Hash &set_id_hash);
46 
47 protected:
48   u8     *packetdata;
49   size_t  packetlength;
50 };
51 
CriticalPacket(void)52 inline CriticalPacket::CriticalPacket(void)
53 {
54   // There is no data initially
55   packetdata = 0;
56   packetlength = 0;
57 }
58 
~CriticalPacket(void)59 inline CriticalPacket::~CriticalPacket(void)
60 {
61   // Delete the data for the packet
62   delete [] packetdata;
63 }
64 
PacketLength(void)65 inline size_t CriticalPacket::PacketLength(void) const
66 {
67   return packetlength;
68 }
69 
AllocatePacket(size_t length,size_t extra)70 inline void* CriticalPacket::AllocatePacket(size_t length, size_t extra)
71 {
72   // Hey! We can't allocate the packet twice
73   assert(packetlength == 0 && packetdata == 0);
74 
75   // Remember the requested packet length
76   packetlength = length;
77 
78   // Allocate and clear the requested packet length plus the extra.
79   packetdata = new u8[length+extra];
80   memset(packetdata, 0, length+extra);
81 
82   return packetdata;
83 }
84 
85 // Class used to record the fact that a copy of a particular critical packet
86 // will be written to a particular file at a specific offset.
87 
88 class CriticalPacketEntry
89 {
90 public:
CriticalPacketEntry(DiskFile * _diskfile,u64 _offset,const CriticalPacket * _packet)91   CriticalPacketEntry(DiskFile *_diskfile,
92                       u64 _offset,
93                       const CriticalPacket *_packet)
94     : diskfile(_diskfile)
95     , offset(_offset)
96     , packet(_packet)
97   {}
CriticalPacketEntry(void)98   CriticalPacketEntry(void)
99     : diskfile(0)
100     , offset(0)
101     , packet(0)
102   {}
CriticalPacketEntry(const CriticalPacketEntry & other)103   CriticalPacketEntry(const CriticalPacketEntry &other)
104     : diskfile(other.diskfile)
105     , offset(other.offset)
106     , packet(other.packet)
107   {}
108   CriticalPacketEntry& operator=(const CriticalPacketEntry &other)
109   {
110     diskfile = other.diskfile;
111     offset = other.offset;
112     packet = other.packet;
113     return *this;
114   }
115 
116 public:
117   // Write the packet to disk.
118   bool   WritePacket(void) const;
119 
120   // Obtain the length of the packet.
121   u64    PacketLength(void) const;
122 
123 protected:
124   DiskFile             *diskfile;
125   u64                   offset;
126   const CriticalPacket *packet;
127 };
128 
WritePacket(void)129 inline bool CriticalPacketEntry::WritePacket(void) const
130 {
131   assert(packet != 0 && diskfile != 0);
132 
133   // Tell the packet to write itself to disk
134   return packet->WritePacket(*diskfile, offset);
135 }
136 
PacketLength(void)137 inline u64 CriticalPacketEntry::PacketLength(void) const
138 {
139   assert(packet != 0);
140 
141   // Ask the packet how big it is.
142   return packet->PacketLength();
143 }
144 
145 #endif // __CRITICALPACKET_H__
146