]> git.sesse.net Git - freerainbowtables/blob - Server Applications/rtperfecter0.1/main.cpp
initial
[freerainbowtables] / Server Applications / rtperfecter0.1 / main.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 <time.h>
23 #include <ctime>
24 #include "RTWrite.h"
25 #include "RTRead.h"
26
27 #ifdef _WIN32
28         #include <conio.h>
29         #define DIRECTORY_SEPERATOR '\\'
30 #else
31         #include <fcntl.h>
32         #define DIRECTORY_SEPERATOR '/'
33 #endif
34
35 struct RTChain2
36 {
37         uint64 startpt;
38         unsigned int endpt32_1;
39         unsigned int endpt32_2;
40         unsigned short checkpoint;
41 };
42
43 union RTChainU
44 {
45         RTChain c;
46         RTChain2 c2;    
47 };
48
49 void usage(char *runStr);
50
51 int main(int argc, char **argv)
52 {
53 #ifdef _WIN32
54         uint64 maxIndex = 0xffffffffffffffff;
55 #else
56         uint64 maxIndex = 0xffffffffffffffffll;
57 #endif
58         int argi = 1, i, argsUsed = 0;
59         unsigned int maxChainsPerFile = 67108864;
60
61         // Get arguments
62         if (argc > 3 && argc < 6)
63         {
64                 for (; argi < argc - 2; argi++)
65                 {
66                         if (strcmp(argv[argi], "-v") == 0 && (argsUsed & 1) == 0)
67                         {
68                                 // Enable verbose mode
69                                 argsUsed |= 1;
70                         }
71                         else if (strncmp(argv[argi], "-s=", 3) == 0 && (argsUsed & 2) == 0)
72                         {
73                                 // Max file size in MiB
74                                 argsUsed |= 2;
75                                 maxChainsPerFile = 0;
76                                 for (i = 3; argv[argi][i] >= '0' && argv[argi][i] <= '9'; i++)
77                                 {
78                                         maxChainsPerFile *= 10;
79                                         maxChainsPerFile += ((int) argv[argi][i]) - 0x30;
80                                 }
81                                 if (argv[argi][i] != '\0')
82                                 {
83                                         printf("Error: Invalid number.\n\n");
84                                         usage(argv[0]);
85                                         return 1;
86                                 }
87                                 if (i > 8 || maxChainsPerFile > 65535) // i - 3 > 5
88                                 {
89                                         printf("Error: Number is to large.\n\n");
90                                         usage(argv[0]);
91                                         return 1;
92                                 }
93                                 maxChainsPerFile <<= 16; // maxChainsPerFile *= 1024 * 1024 / 16
94                         }
95                         else if (strncmp(argv[argi], "-i=", 3) == 0 && (argsUsed & 4) == 0)
96                         {
97                                 // Maximum index for chains
98                                 argsUsed |= 4;
99                                 maxIndex = 0;
100                                 for (i = 3; argv[argi][i] >= '0' && argv[argi][i] <= '9'; i++)
101                                 {
102                                         maxIndex *= 10;
103                                         maxIndex += ((int) argv[argi][i]) - 0x30;
104                                 }
105                                 if (argv[argi][i] != '\0')
106                                 {
107                                         printf("Error: Invalid number.\n\n");
108                                         usage(argv[0]);
109                                         return 1;
110                                 }
111                                 if (i > 23) // i - 3 > 20
112                                 {
113                                         printf("Error: Number is to large.\n\n");
114                                         usage(argv[0]);
115                                         return 1;
116                                 }
117                         }
118                         else
119                         {
120                                 printf("Error: Unknown argument.\n\n");
121                                 usage(argv[0]);
122                                 return 1;
123                         }
124                 }
125         }
126         else if (argc != 3)
127         {
128                 if (argc == 1 || argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
129                 {
130                         usage(argv[0]);
131                         return 0;
132                 }
133                 printf("Error: Wrong number of arguments.\n\n");
134                 usage(argv[0]);
135                 return 1;
136         }
137
138         // Init
139         RTChainU chain, prevChain;
140         memset(&prevChain.c, 0x00, sizeof(prevChain.c));
141         uint64 uniqueChains = 0;
142         RTRead inRt(argv[argi], maxIndex, argsUsed & 1);
143         RTWrite outRt(argv[argi + 1], maxChainsPerFile);
144
145 #ifndef _WIN32
146         // Set stdin to non-blocking
147         fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NDELAY);
148 #endif
149         clock_t t = clock();
150
151         for (int ret = inRt.readChain(&(chain.c)); ret == 0; ret = inRt.readChain(&(chain.c)))
152         {
153 /*
154                 // Show status
155 #ifdef _WIN32
156                 if (kbhit())
157                 {
158                         int ch = getchar();
159                         if (ch == (int)'\n')
160                         {
161                                 inRt.printStatus();
162                         }
163                 }
164 #else
165                 int ch = getchar();
166                 if (ch == (int)'\n')
167                 {
168                         inRt.printStatus();
169                 }
170 #endif
171 */
172                 // Check the least significate 32 bits first
173                 if (chain.c2.endpt32_1 != prevChain.c2.endpt32_1 || chain.c2.endpt32_2 != prevChain.c2.endpt32_2)
174                 {
175                         outRt.writeChain(&(chain.c));
176                         uniqueChains++;
177                 }
178                 prevChain = chain;
179         }
180 #ifdef _WIN32
181         printf("Unique Chains: %I64u\nTotal time:    %1.2f\n", uniqueChains, (clock() - t) / (double)CLOCKS_PER_SEC);
182 #else
183         printf("Unique Chains: %llu\nTotal time:    %1.2f\n", uniqueChains, (clock() - t) / (double)CLOCKS_PER_SEC);
184 #endif
185         return 0;
186 }
187
188 void usage(char *runStr)
189 {
190         printf("\n                          **** RT Perfecter v0.0 ****\n\n");
191         printf("Converts sorted rt files in a directory to a perfect rainbow table. All rt files\n");
192         printf("in the directory must all be from the same rainbow table.\n\n");
193         printf("%s [Options] input-directory output-file-pattern\n\n", runStr);
194         printf("Options:\n");
195         printf("  -i=number\n");
196         printf("    Maximum index for chains.\n\n");
197         printf("  -s=number\n");
198         printf("    Maximum size for output files in MiB [default is 1024, max is 65535].\n\n");
199         printf("  -v\n");
200         printf("    Verbose mode, displays extra info.\n\n");
201         printf("output-file-pattern:\n");
202         printf("  This will be passed into sprintf() with the number of chains in the file and\n");
203         printf("  the current file number starting at 0. This could look like:\n");
204         printf("  'some-folder%csome-name%%u-%%03u.rt'\n", DIRECTORY_SEPERATOR);
205         printf("  Where '%%u' would be the number of chains in the file and '%%03u' would be the\n");
206         printf("  file number formated like 000, 001, 002, and so on.\n\n");
207         printf("Run-time:\n");
208         printf("  Press [Enter] to display a status message.\n\n");
209         printf("Copyright (c) 2008 Steve Thomas <SMT837784@yahoo.com>\n");
210         printf("  This is free software: you can redistribute it and/or modify it under the\n");
211         printf("  terms of the GNU General Public License as published by the Free Software\n");
212         printf("  Foundation, either version 3 of the License, or (at your option) any later\n");
213         printf("  version. There is NO WARRANTY, to the extent permitted by law.\n");
214 }
215