]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC client apps/distrrtgen/distrrtgen.cpp
64-bit fixes for distrrtgen
[freerainbowtables] / BOINC software / BOINC client apps / distrrtgen / distrrtgen.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18 // This program serves as both
19 // - An example BOINC application, illustrating the use of the BOINC API
20 // - A program for testing various features of BOINC
21 //
22 // NOTE: this file exists as both
23 // boinc/apps/upper_case.C
24 // and
25 // boinc_samples/example_app/uc2.C
26 // If you update one, please update the other!
27
28 // The program converts a mixed-case file to upper case:
29 // read "in", convert to upper case, write to "out"
30 //
31 // command line options
32 // -run_slow: sleep 1 second after each character
33 // -cpu_time N: use about N CPU seconds after copying files
34 // -early_exit: exit(10) after 30 chars
35 // -early_crash: crash after 30 chars
36 //
37
38 #ifdef _WIN32
39 #include "boinc_win.h"
40 #else
41 #include "config.h"
42 #include <cstdio>
43 #include <cctype>
44 #include <ctime>
45 #include <cstring>
46 #include <cstdlib>
47 #include <csignal>
48 #include <unistd.h>
49 #endif
50
51 #include <string>
52 #include <fstream>
53 #include <iostream>
54 #include "str_util.h"
55 #include "util.h"
56 #include "filesys.h"
57 #include "boinc_api.h"
58 #include "Public.h"
59 // Rainbowcrack code
60 #include "ChainWalkContext.h"
61
62 using std::string;
63
64 /*
65 bool early_exit = false;
66 bool early_crash = false;
67 bool early_sleep = false;
68 double cpu_time = 20, comp_result;
69 */
70 int QuickSortPartition(RainbowChainCP* pChain, int nLow, int nHigh)
71 {
72         int nRandomIndex = nLow + ((uint32)rand() * ((uint32)RAND_MAX + 1) + (uint32)rand()) % (nHigh - nLow + 1);
73         RainbowChainCP TempChain;
74         TempChain = pChain[nLow];
75         pChain[nLow] = pChain[nRandomIndex];
76         pChain[nRandomIndex] = TempChain;
77
78         TempChain = pChain[nLow];
79         uint64 nPivotKey = pChain[nLow].nIndexE;
80         while (nLow < nHigh)
81         {
82                 while (nLow < nHigh && pChain[nHigh].nIndexE >= nPivotKey)
83                         nHigh--;
84                 pChain[nLow] = pChain[nHigh];
85                 while (nLow < nHigh && pChain[nLow].nIndexE <= nPivotKey)
86                         nLow++;
87                 pChain[nHigh] = pChain[nLow];
88         }
89         pChain[nLow] = TempChain;
90         return nLow;
91 }
92
93 void QuickSort(RainbowChainCP* pChain, int nLow, int nHigh)
94 {
95         if (nLow < nHigh)
96         {
97                 int nPivotLoc = QuickSortPartition(pChain, nLow, nHigh);
98                 QuickSort(pChain, nLow, nPivotLoc - 1);
99                 QuickSort(pChain, nPivotLoc + 1, nHigh);
100         }
101 }
102
103 int main(int argc, char **argv) {    
104     int retval;
105     double fd;
106     char output_path[512], chkpt_path[512];
107     FILE* state;        
108     retval = boinc_init();
109     if (retval) {
110         fprintf(stderr, "boinc_init returned %d\n", retval);
111         exit(retval);
112     }
113         
114
115     // get size of input file (used to compute fraction done)
116     //
117     //file_size(input_path, fsize);
118
119     // See if there's a valid checkpoint file.
120     // If so seek input file and truncate output file
121     //
122
123
124         if(argc < 10)
125         {
126                 fprintf(stderr, "Not enough parameters");
127                 return -1;
128         }
129         string sHashRoutineName, sCharsetName, sSalt, sCheckPoints;
130         int nRainbowChainCount, nPlainLenMin, nPlainLenMax, nRainbowTableIndex, nRainbowChainLen;
131         uint64 nChainStart;
132         sHashRoutineName = argv[1];
133         sCharsetName = argv[2];
134         nPlainLenMin = atoi(argv[3]);
135         nPlainLenMax = atoi(argv[4]);
136         nRainbowTableIndex = atoi(argv[5]);
137         nRainbowChainLen = atoi(argv[6]);
138         nRainbowChainCount = atoi(argv[7]);
139 #ifdef _WIN32
140
141         nChainStart = _atoi64(argv[8]);
142
143 #else
144         nChainStart = atoll(argv[8]);
145 #endif
146         sCheckPoints = argv[9];
147         vector<int> vCPPositions;
148         char *cp = strtok((char *)sCheckPoints.c_str(), ",");
149         while(cp != NULL)
150         {
151                 vCPPositions.push_back(atoi(cp));
152                 cp = strtok(NULL, ",");
153         }
154         if(argc == 11)
155         {
156                 sSalt = argv[10];
157         }
158         //std::cout << "Starting ChainGenerator" << std::endl;
159         // Setup CChainWalkContext
160         //std::cout << "ChainGenerator started." << std::endl;
161
162         if (!CChainWalkContext::SetHashRoutine(sHashRoutineName))
163         {
164                 fprintf(stderr, "hash routine %s not supported\n", sHashRoutineName.c_str());
165                 return 1;
166         }
167         //std::cout << "Hash routine validated" << std::endl;
168
169         if (!CChainWalkContext::SetPlainCharset(sCharsetName, nPlainLenMin, nPlainLenMax))
170         {       
171                 std::cerr << "charset " << sCharsetName << " not supported" << std::endl;
172                 return 2;
173         }
174         //std::cout << "Plain charset validated" << std::endl;
175
176         if (!CChainWalkContext::SetRainbowTableIndex(nRainbowTableIndex))
177         {
178                 std::cerr << "invalid rainbow table index " << nRainbowTableIndex << std::endl;
179                 return 3;
180         }
181         //std::cout << "Rainbowtable index validated" << std::endl;
182
183         if(sHashRoutineName == "mscache")// || sHashRoutineName == "lmchall" || sHashRoutineName == "halflmchall")
184         {
185                 // Convert username to unicode
186                 const char *szSalt = sSalt.c_str();
187                 int salt_length = strlen(szSalt);
188                 unsigned char cur_salt[256];
189                 for (int i=0; i<salt_length; i++)
190                 {
191                         cur_salt[i*2] = szSalt[i];
192                         cur_salt[i*2+1] = 0x00;
193                 }
194                 CChainWalkContext::SetSalt(cur_salt, salt_length*2);
195         }
196         else if(sHashRoutineName == "halflmchall")
197         { // The salt is hardcoded into the hash routine
198         //      CChainWalkContext::SetSalt((unsigned char*)&salt, 8);
199         }
200         else if(sHashRoutineName == "oracle")
201         {
202                 CChainWalkContext::SetSalt((unsigned char *)sSalt.c_str(), sSalt.length());
203         }
204         //std::cout << "Opening chain file" << std::endl;
205
206         
207         // Open file
208 //      fclose(fopen(sFilename.c_str(), "a"));
209 //      FILE* file = fopen(sFilename.c_str(), "r+b");
210     boinc_resolve_filename("result", output_path, sizeof(output_path));
211         fclose(boinc_fopen(output_path, "a"));
212         FILE *outfile = boinc_fopen(output_path, "r+b");
213         
214         if (outfile == NULL)
215         {
216                 std::cerr << "failed to create " << output_path << std::endl;
217                 return 4;
218         }
219         
220         
221         // Check existing chains
222         unsigned int nDataLen = (unsigned int)GetFileLen(outfile);
223         unsigned int nFileLen;
224         
225         // Round to boundary
226         nDataLen = nDataLen / 18 * 18;
227         if ((int)nDataLen == nRainbowChainCount * 18)
228         {               
229                 std::cerr << "precomputation of this rainbow table already finished" << std::endl;
230                 fclose(outfile);
231                 return 0;
232         }
233         nChainStart += (nDataLen / 18);
234         fseek(outfile, nDataLen, SEEK_SET);
235         size_t nReturn;
236         CChainWalkContext cwc;
237         uint64 nIndex[2];
238         time_t tStart = time(NULL);
239 //      std::cout << "Starting to generate chains" << std::endl;
240         for(int nCurrentCalculatedChains = nDataLen / 18; nCurrentCalculatedChains < nRainbowChainCount; nCurrentCalculatedChains++)
241         {               
242                 uint32 cpcheck = 0;
243                 unsigned short checkpoint = 0;
244                 fd = (double)nCurrentCalculatedChains / (double)nRainbowChainCount;
245                 boinc_fraction_done(fd);
246                 cwc.SetIndex(nChainStart++); // use a given index now!
247                 nIndex[0] = cwc.GetIndex();
248                 
249                 for (int nPos = 0; nPos < nRainbowChainLen - 1; nPos++)
250                 {
251                 //      std::cout << "IndexToPlain()" << std::endl;
252                         cwc.IndexToPlain();
253                 //      std::cout << "PlainToHash()" << std::endl;
254                         cwc.PlainToHash();
255                 //      std::cout << "HashToIndex()" << std::endl;
256                         cwc.HashToIndex(nPos);
257                         if(cpcheck < vCPPositions.size() && nPos == vCPPositions[cpcheck])
258                         {
259                                 
260                                 checkpoint |= (1 << cpcheck) & (unsigned short)cwc.GetIndex() << cpcheck;
261                                 cpcheck++;
262                         }
263                 }
264                 //std::cout << "GetIndex()" << std::endl;
265
266                 nIndex[1] = cwc.GetIndex();
267                 // Write chain to disk
268                 if ((nReturn = fwrite(nIndex, 1, 16, outfile)) != 16)
269                 {
270                         std::cerr << "disk write fail" << std::endl;
271                         fclose(outfile);
272                         return 9;
273                 }
274                 if((nReturn = fwrite(&checkpoint, 1, 2, outfile)) != 2)
275                 {
276                         std::cerr << "disk write fail" << std::endl;
277                         fclose(outfile);
278                         return 9;
279                 }
280 //              fflush(file);
281         }
282         //std::cout << "Generation completed" << std::endl;
283     fseek(outfile, 0, SEEK_SET);
284         nFileLen = GetFileLen(outfile);
285         nRainbowChainCount = nFileLen / 18;
286
287         RainbowChainCP* pChain = (RainbowChainCP*)new unsigned char[sizeof(RainbowChainCP) * nRainbowChainCount];
288
289         if (pChain != NULL)
290         {
291                 // Load file
292                 fseek(outfile, 0, SEEK_SET);
293                 for(int i = 0; i < nRainbowChainCount; i++)
294                 {
295                         if(fread(&pChain[i], 1, 16, outfile) != 16)
296                         {
297                                 printf("disk read fail\n");
298                                 return 9;
299                         }
300                         if(fread(&pChain[i].nCheckPoint, 1, sizeof(pChain[i].nCheckPoint), outfile) != 2)
301                         {
302                                 printf("disk read fail\n");
303                                 return 9;
304                         }
305                 }
306
307                 // Sort file
308                 QuickSort(pChain, 0, nRainbowChainCount - 1);
309
310                 // Write file
311                 fseek(outfile, 0, SEEK_SET);
312                 for(int i = 0; i < nRainbowChainCount; i++)
313                 {
314                         fwrite(&pChain[i], 1, 16, outfile);
315                         fwrite(&pChain[i].nCheckPoint, 2, 1, outfile);
316                 }
317                 delete[] pChain;
318         }
319
320         fclose(outfile);
321     
322         // main loop - read characters, convert to UC, write
323     //
324
325     boinc_fraction_done(1);
326     boinc_finish(0);
327 }
328
329 #ifdef _WIN32
330 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
331     LPSTR command_line;
332     char* argv[100];
333     int argc;
334
335     command_line = GetCommandLine();
336     argc = parse_command_line( command_line, argv );
337     return main(argc, argv);
338 }
339 #endif
340
341 const char *BOINC_RCSID_33ac47a071 = "$Id: upper_case.C 12135 2007-02-21 20:04:14Z davea $";
342