criticalpacket.h

Go to the documentation of this file.
00001 // This file is part of par2cmdline (a PAR 2.0 compatible file verification and
00002 // repair tool). See https://parchive.sourceforge.net for details of PAR 2.0.
00003 //
00004 // Copyright (c) 2003 Peter Brian Clements
00005 //
00006 // par2cmdline is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // par2cmdline is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 
00020 #ifndef __CRITICALPACKET_H__
00021 #define __CRITICALPACKET_H__
00022 
00023 // Base class for main packet, file verification packet, file description packet
00024 // and creator packet.
00025 
00026 // These packets are all small and are held in memory in their entirity
00027 
00028 class CriticalPacket
00029 {
00030 public:
00031   CriticalPacket(void);
00032   ~CriticalPacket(void);
00033 
00034 public:
00035   // Write a copy of the packet to the specified file at the specified offset
00036   bool    WritePacket(DiskFile &diskfile, u64 fileoffset) const;
00037 
00038   // Obtain the lenght of the packet.
00039   size_t  PacketLength(void) const;
00040 
00041   // Allocate some memory for the packet (plus some extra padding).
00042   void*   AllocatePacket(size_t length, size_t extra = 0);
00043 
00044   // Finish a packet (by storing the set_id_hash and then computing the packet_hash).
00045   void    FinishPacket(const MD5Hash &set_id_hash);
00046 
00047 protected:
00048   u8     *packetdata;
00049   size_t  packetlength;
00050 };
00051 
00052 inline CriticalPacket::CriticalPacket(void)
00053 {
00054   // There is no data initially
00055   packetdata = 0;
00056   packetlength = 0;
00057 }
00058 
00059 inline CriticalPacket::~CriticalPacket(void)
00060 {
00061   // Delete the data for the packet
00062   delete [] packetdata;
00063 }
00064 
00065 inline size_t CriticalPacket::PacketLength(void) const
00066 {
00067   return packetlength;
00068 }
00069 
00070 inline void* CriticalPacket::AllocatePacket(size_t length, size_t extra)
00071 {
00072   // Hey! We can't allocate the packet twice
00073   assert(packetlength == 0 && packetdata == 0);
00074 
00075   // Remember the requested packet length
00076   packetlength = length;
00077 
00078   // Allocate and clear the requested packet length plus the extra.
00079   packetdata = new u8[length+extra];
00080   memset(packetdata, 0, length+extra);
00081 
00082   return packetdata;
00083 }
00084 
00085 // Class used to record the fact that a copy of a particular critical packet
00086 // will be written to a particular file at a specific offset.
00087 
00088 class CriticalPacketEntry
00089 {
00090 public:
00091   CriticalPacketEntry(DiskFile *_diskfile, 
00092                       u64 _offset, 
00093                       const CriticalPacket *_packet)
00094     : diskfile(_diskfile)
00095     , offset(_offset)
00096     , packet(_packet)
00097   {}
00098   CriticalPacketEntry(void)
00099     : diskfile(0)
00100     , offset(0)
00101     , packet(0)
00102   {}
00103   CriticalPacketEntry(const CriticalPacketEntry &other)
00104     : diskfile(other.diskfile)
00105     , offset(other.offset)
00106     , packet(other.packet)
00107   {}
00108   CriticalPacketEntry& operator=(const CriticalPacketEntry &other)
00109   {
00110     diskfile = other.diskfile;
00111     offset = other.offset;
00112     packet = other.packet;
00113     return *this;
00114   }
00115 
00116 public:
00117   // Write the packet to disk.
00118   bool   WritePacket(void) const;
00119 
00120   // Obtain the length of the packet.
00121   u64    PacketLength(void) const;
00122 
00123 protected:
00124   DiskFile             *diskfile;
00125   u64                   offset;
00126   const CriticalPacket *packet;
00127 };
00128 
00129 inline bool CriticalPacketEntry::WritePacket(void) const
00130 {
00131   assert(packet != 0 && diskfile != 0);
00132 
00133   // Tell the packet to write itself to disk
00134   return packet->WritePacket(*diskfile, offset);
00135 }
00136 
00137 inline u64 CriticalPacketEntry::PacketLength(void) const
00138 {
00139   assert(packet != 0);
00140 
00141   // Ask the packet how big it is.
00142   return packet->PacketLength();
00143 }
00144 
00145 #endif // __CRITICALPACKET_H__

Generated on Sun Oct 12 01:45:29 2008 for NNTPGrab by  1.5.4