]> git.sesse.net Git - freerainbowtables/blob - Server Applications/rtperfectp/RTWrite.cpp
initial
[freerainbowtables] / Server Applications / rtperfectp / RTWrite.cpp
1 /*
2         Copyright (C) 2008 Steve Thomas <SMT837784@yahoo.com>
3
4         This file is part of RT Perfecter v0.0.
5
6         RT Perfecter v0.0 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 3 of the License, or
9         (at your option) any later version.
10
11         RT Perfecter v0.0 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 RT Perfecter v0.0.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include "RTWrite.h"
24
25 RTWrite::RTWrite(char *fileName, int maxChainsPerFile)
26 {
27         unsigned int len = strlen(fileName);
28
29         m_file = new char[len + 1];
30         m_fileTemp = new char[len + 21];
31         strncpy(m_file, fileName, len);
32         m_file[len] = '\0';
33
34         m_curFile = 0;
35         m_curFileChains = 0;
36         m_chainsPerFile = maxChainsPerFile;
37         m_pFile = NULL;
38         m_prevEndpt = 0;
39 }
40
41 RTWrite::~RTWrite()
42 {
43         if (m_pFile != NULL)
44         {
45                 fclose(m_pFile);
46                 sprintf(m_fileTemp, m_file, m_curFileChains, m_curFile);
47                 if (rename("temp.rt", m_fileTemp) != 0)
48                 {
49                         perror(m_fileTemp);
50                         exit(1);
51                 }
52         }
53         if (m_file != NULL)
54         {
55                 delete [] m_file;
56         }
57         if (m_fileTemp != NULL)
58         {
59                 delete [] m_fileTemp;
60         }
61 }
62
63 void RTWrite::writeChain(RTChainCP *chain)
64 {
65         if(chain->startpt == 0 || chain->endpt == 0)
66                 return;
67
68         if (m_prevEndpt > chain->endpt)
69         {
70                 printf("**** Error writeChain(): Tring to write unsorted data. (%llu > %llu)****\n", m_prevEndpt, chain->endpt);
71                 exit(1);
72         }
73         /*
74 #ifdef _WIN32
75         if(chain->startpt > 0x0000ffffffffffffI64)
76 #else
77         if(chain->startpt > 0x0000ffffffffffff11u)
78 #endif
79         {
80                 printf("**** Error writeChain(): Prefix is bigger than 6 bytes. (%llx) %u chains ****\n", chain->startpt, m_curFileChains);
81                 exit(1);                                                                                
82                 return;
83         }
84         */
85         if (m_pFile == NULL)
86         {
87                 m_pFile = fopen("temp.rt", "wb");
88                 if (m_pFile == NULL)
89                 {
90                         perror(m_fileTemp);
91                         exit(1);
92                 }
93         }
94         if (fwrite((void*)chain, 16, 1, m_pFile) != 1)
95         {
96                 perror("temp.rt");
97                 exit(1);
98         }
99         if (fwrite((void*)&chain->checkpoint, 2, 1, m_pFile) != 1)
100         {
101                 perror("temp.rt");
102                 exit(1);
103         }
104
105         m_curFileChains++;
106         if (m_curFileChains >= m_chainsPerFile)
107         {
108                 fclose(m_pFile);
109                 m_pFile = NULL;
110                 sprintf(m_fileTemp, m_file, m_curFileChains, m_curFile);
111                 if (rename("temp.rt", m_fileTemp) != 0)
112                 {
113                         perror(m_fileTemp);
114                         exit(1);
115                 }
116                 m_curFile++;
117                 m_curFileChains = 0;
118         }
119 }
120