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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 length 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   // Sort critical packets by type and FileId
48   static bool CompareLess(const CriticalPacket* const &left, const CriticalPacket* const &right);
49 
50 protected:
51   u8     *packetdata;
52   size_t  packetlength;
53 };
54 
CriticalPacket(void)55 inline CriticalPacket::CriticalPacket(void)
56 {
57   // There is no data initially
58   packetdata = 0;
59   packetlength = 0;
60 }
61 
~CriticalPacket(void)62 inline CriticalPacket::~CriticalPacket(void)
63 {
64   // Delete the data for the packet
65   delete [] packetdata;
66 }
67 
PacketLength(void)68 inline size_t CriticalPacket::PacketLength(void) const
69 {
70   return packetlength;
71 }
72 
AllocatePacket(size_t length,size_t extra)73 inline void* CriticalPacket::AllocatePacket(size_t length, size_t extra)
74 {
75   // Hey! We can't allocate the packet twice
76   assert(packetlength == 0 && packetdata == 0);
77 
78   // Remember the requested packet length
79   packetlength = length;
80 
81   // Allocate and clear the requested packet length plus the extra.
82   packetdata = new u8[length+extra];
83   memset(packetdata, 0, length+extra);
84 
85   return packetdata;
86 }
87 
88 // Class used to record the fact that a copy of a particular critical packet
89 // will be written to a particular file at a specific offset.
90 
91 class CriticalPacketEntry
92 {
93 public:
CriticalPacketEntry(DiskFile * _diskfile,u64 _offset,const CriticalPacket * _packet)94   CriticalPacketEntry(DiskFile *_diskfile,
95                       u64 _offset,
96                       const CriticalPacket *_packet)
97     : diskfile(_diskfile)
98     , offset(_offset)
99     , packet(_packet)
100   {}
CriticalPacketEntry(void)101   CriticalPacketEntry(void)
102     : diskfile(0)
103     , offset(0)
104     , packet(0)
105   {}
CriticalPacketEntry(const CriticalPacketEntry & other)106   CriticalPacketEntry(const CriticalPacketEntry &other)
107     : diskfile(other.diskfile)
108     , offset(other.offset)
109     , packet(other.packet)
110   {}
111   CriticalPacketEntry& operator=(const CriticalPacketEntry &other)
112   {
113     diskfile = other.diskfile;
114     offset = other.offset;
115     packet = other.packet;
116     return *this;
117   }
118 
119 public:
120   // Write the packet to disk.
121   bool   WritePacket(void) const;
122 
123   // Obtain the length of the packet.
124   u64    PacketLength(void) const;
125 
126 protected:
127   DiskFile             *diskfile;
128   u64                   offset;
129   const CriticalPacket *packet;
130 };
131 
WritePacket(void)132 inline bool CriticalPacketEntry::WritePacket(void) const
133 {
134   assert(packet != 0 && diskfile != 0);
135 
136   // Tell the packet to write itself to disk
137   return packet->WritePacket(*diskfile, offset);
138 }
139 
PacketLength(void)140 inline u64 CriticalPacketEntry::PacketLength(void) const
141 {
142   assert(packet != 0);
143 
144   // Ask the packet how big it is.
145   return packet->PacketLength();
146 }
147 
148 #endif // __CRITICALPACKET_H__
149