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 __PAR2CREATOR_H__
21 #define __PAR2CREATOR_H__
22 
23 class MainPacket;
24 class CreatorPacket;
25 class CriticalPacket;
26 
27 class Par2Creator
28 {
29 public:
30   Par2Creator(void);
31   ~Par2Creator(void);
32 
33   // Create recovery files from the source files specified on the command line
34   Result Process(const CommandLine &commandline);
35 
36 protected:
37   // Steps in the creation process:
38 
39   // Compute block size from block count or vice versa depending on which was
40   // specified on the command line
41   bool ComputeBlockSizeAndBlockCount(const list<CommandLine::ExtraFile> &extrafiles);
42 
43   // Determine how many recovery blocks to create based on the source block
44   // count and the requested level of redundancy.
45   bool ComputeRecoveryBlockCount(u32 redundancy);
46 
47   // Determine how much recovery data can be computed on one pass
48   bool CalculateProcessBlockSize(size_t memorylimit);
49 
50   // Determine how many recovery files to create.
51   bool ComputeRecoveryFileCount(void);
52 
53   // Open all of the source files, compute the Hashes and CRC values, and store
54   // the results in the file verification and file description packets.
55   bool OpenSourceFiles(const list<CommandLine::ExtraFile> &extrafiles);
56 
57   // Create the main packet and determine the set_id_hash to use with all packets
58   bool CreateMainPacket(void);
59 
60   // Create the creator packet.
61   bool CreateCreatorPacket(void);
62 
63   // Initialise all of the source blocks ready to start reading data from the source files.
64   bool CreateSourceBlocks(void);
65 
66   // Create all of the output files and allocate all packets to appropriate file offets.
67   bool InitialiseOutputFiles(string par2filename);
68 
69   // Allocate memory buffers for reading and writing data to disk.
70   bool AllocateBuffers(void);
71 
72   // Compute the Reed Solomon matrix
73   bool ComputeRSMatrix(void);
74 
75   // Read source data, process it through the RS matrix and write it to disk.
76   bool ProcessData(u64 blockoffset, size_t blocklength);
77 
78   // Finish computation of the recovery packets and write the headers to disk.
79   bool WriteRecoveryPacketHeaders(void);
80 
81   // Finish computing the full file hash values of the source files
82   bool FinishFileHashComputation(void);
83 
84   // Fill in all remaining details in the critical packets.
85   bool FinishCriticalPackets(void);
86 
87   // Write all other critical packets to disk.
88   bool WriteCriticalPackets(void);
89 
90   // Close all files.
91   bool CloseFiles(void);
92 
93 protected:
94   CommandLine::NoiseLevel noiselevel; // How noisy we should be
95 
96   u64 blocksize;      // The size of each block.
97   size_t chunksize;   // How much of each block will be processed at a
98                       // time (due to memory constraints).
99 
100   void *inputbuffer;  // chunksize
101   void *outputbuffer; // chunksize * recoveryblockcount
102 
103   u32 sourcefilecount;   // Number of source files for which recovery data will be computed.
104   u32 sourceblockcount;  // Total number of data blocks that the source files will be
105                          // virtualy sliced into.
106 
107   u64 largestfilesize;   // The size of the largest source file
108 
109   CommandLine::Scheme recoveryfilescheme;  // What scheme will be used to select the
110                                            // sizes for the recovery files.
111 
112   u32 recoveryfilecount;  // The number of recovery files that will be created
113   u32 recoveryblockcount; // The number of recovery blocks that will be placed
114                           // in the recovery files.
115 
116   u32 firstrecoveryblock; // The lowest exponent value to use for the recovery blocks.
117 
118   MainPacket    *mainpacket;    // The main packet
119   CreatorPacket *creatorpacket; // The creator packet
120 
121   vector<Par2CreatorSourceFile*> sourcefiles;  // Array containing details of the source files
122                                                // as well as the file verification and file
123                                                // description packets for them.
124 
125   vector<DataBlock>          sourceblocks;     // Array with one entry for every source block.
126 
127   vector<DiskFile>           recoveryfiles;    // Array with one entry for every recovery file.
128   vector<RecoveryPacket>     recoverypackets;  // Array with one entry for every recovery packet.
129 
130   list<CriticalPacket*>      criticalpackets;  // A list of all of the critical packets.
131   list<CriticalPacketEntry>  criticalpacketentries; // A list of which critical packet will
132                                                     // be written to which recovery file.
133 
134   ReedSolomon<Galois16> rs;   // The Reed Solomon matrix.
135 
136   u64 progress;     // How much data has been processed.
137   u64 totaldata;    // Total amount of data to be processed.
138 
139   bool deferhashcomputation; // If we have enough memory to compute all recovery data
140                              // in one pass, then we can defer the computation of
141                              // the full file hash and block crc and hashes until
142                              // the recovery data is computed.
143 };
144 
145 #endif // __PAR2CREATOR_H__
146