]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rti2rto/rti2rto.cpp
(C)
[freerainbowtables] / Client Applications / rti2rto / rti2rto.cpp
1 #include <string>
2 #include <vector>
3 #ifdef _WIN32
4 #include <io.h>
5 #else
6         #include <sys/types.h>
7         #include <sys/stat.h>
8         #include <unistd.h>
9 #endif
10
11 #include <time.h>
12 #include "Public.h"
13 #include "MemoryPool.h"
14 #include "RTI2Reader.h"
15 #include "RTIReader.h"
16 using namespace std;
17
18 void Usage()
19 {
20         printf("rti2rto - Indexed to Original rainbow table converter\n");
21         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
22         printf("http://www.freerainbowtables.com\n\n");
23
24         printf("usage: rti2rto rainbow_table_pathname\n");
25         printf("rainbow_table_pathname: pathname of the rainbow table(s), wildchar(*, ?) supported\n");
26         printf("\n");
27         printf("example: rti2rto *.rti\n");
28         printf("         rti2rto md5_*.rti\n");
29 }
30 #ifdef _WIN32
31 void GetTableList(string sWildCharPathName, vector<string>& vPathName)
32 {
33         vPathName.clear();
34
35         string sPath;
36         int n = sWildCharPathName.find_last_of('\\');
37         if (n != -1)
38                 sPath = sWildCharPathName.substr(0, n + 1);
39
40         _finddata_t fd;
41         long handle = _findfirst(sWildCharPathName.c_str(), &fd);
42         if (handle != -1)
43         {
44                 do
45                 {
46                         string sName = fd.name;
47                         if (sName != "." && sName != ".." && !(fd.attrib & _A_SUBDIR))
48                         {
49                                 string sPathName = sPath + sName;
50                                 vPathName.push_back(sPathName);
51                         }
52                 } while (_findnext(handle, &fd) == 0);
53
54                 _findclose(handle);
55         }
56 }
57 #else
58 void GetTableList(int argc, char* argv[], vector<string>& vPathName)
59 {
60         vPathName.clear();
61
62         int i;
63         for (i = 1; i < argc; i++)
64         {
65                 string sPathName = argv[i];
66                 struct stat buf;
67                 if (lstat(sPathName.c_str(), &buf) == 0)
68                 {
69                         if (S_ISREG(buf.st_mode))
70                                 vPathName.push_back(sPathName);
71
72                 }
73         }
74 }
75 #endif
76
77
78 void ConvertRainbowTable(string sPathName, string sResultFileName, string sType)
79 {
80 #ifdef _WIN32
81         int nIndex = sPathName.find_last_of('\\');
82 #else
83         int nIndex = sPathName.find_last_of('/');
84 #endif
85         string sFileName;
86         if (nIndex != -1)
87                 sFileName = sPathName.substr(nIndex + 1);
88         else
89                 sFileName = sPathName;
90
91         // Info
92         printf("%s:\n", sFileName.c_str());
93         FILE *fResult = fopen(sResultFileName.c_str(), "wb");
94         if(fResult == NULL)
95         {
96                 printf("Could not open %s for write access", sResultFileName.c_str());
97                 return;
98         }
99         static CMemoryPool mp;
100         uint64 nAllocatedSize;
101         BaseRTReader *reader = NULL;
102         if(sType == "RTI2")
103                 reader = (BaseRTReader*)new RTI2Reader(sFileName);
104         else if(sType == "RTI")
105                 reader = (BaseRTReader*)new RTIReader(sFileName);
106         else 
107         {
108                 printf("Invalid table type '%s'", sType.c_str());
109                 return ;
110         }
111         uint64 size = reader->GetChainsLeft() * sizeof(RainbowChain);
112 #ifdef _MEMORYDEBUG
113         printf("Starting allocation of %i bytes\n", size);
114 #endif
115         RainbowChain* pChain = (RainbowChain*)mp.Allocate(size, nAllocatedSize);
116 #ifdef _MEMORYDEBUG
117         printf("Finished. Got %i bytes\n", nAllocatedSize);
118 #endif
119         if (pChain != NULL)
120         {
121                 nAllocatedSize = nAllocatedSize / sizeof(RainbowChain) * sizeof(RainbowChain);          // Round to boundary
122                 unsigned int nChains = nAllocatedSize / sizeof(RainbowChain);
123                 while(reader->GetChainsLeft() > 0)
124                 {
125 #ifdef _MEMORYDEBUG
126                         printf("Grabbing %i chains from file\n", nChains);
127 #endif
128                         reader->ReadChains(nChains, pChain);
129 #ifdef _MEMORYDEBUG
130                         printf("Recieved %i chains from file\n", nChains);
131 #endif
132                         for(uint32 i = 0; i < nChains; i++)
133                         {
134                                 fwrite(&pChain[i], 1, 16, fResult);
135                         }
136                 }
137         }
138         fclose(fResult);
139         if(reader != NULL)
140                 delete reader;
141 }
142 int main(int argc, char* argv[])
143 {
144 #ifdef _WIN32
145         if (argc != 2)
146         {
147                 Usage();
148                 
149                 return 0;
150         }
151         string sWildCharPathName = argv[1];
152         vector<string> vPathName;
153         GetTableList(sWildCharPathName, vPathName);
154 #else
155         if (argc < 2)
156         {
157                 Usage();
158                 return 0;
159         }
160         for(int i = 0; i < argc; i++)
161         {
162                 printf("%i: %s\n", i, argv[i]);
163         }
164         // vPathName
165         vector<string> vPathName;
166         GetTableList(argc, argv, vPathName);
167 #endif
168         if (vPathName.size() == 0)
169         {
170                 printf("no rainbow table found\n");
171                 return 0;
172         }
173         for (uint32 i = 0; i < vPathName.size(); i++)
174         {
175                 string sResultFile, sType;
176                         
177                 if(vPathName[i].substr(vPathName[i].length() - 4, vPathName[i].length()) == "rti2")
178                 {
179                         sResultFile = vPathName[i].substr(0, vPathName[i].length() - 2); // Resulting file is .rt, not .rti2
180                         sType = "RTI2";
181                 }
182                 else if(vPathName[i].substr(vPathName[i].length() - 3, vPathName[i].length()) == "rti")
183                 {
184                         sResultFile = vPathName[i].substr(0, vPathName[i].length() - 1); // Resulting file is .rt, not .rti
185                         sType = "RTI";
186                 }
187                 else 
188                 {
189                         printf("File %s is not a RTI or a RTI2 file", vPathName[i].c_str());
190                         continue;
191                 }
192                 ConvertRainbowTable(vPathName[i], sResultFile, sType);
193                 printf("\n");
194         }
195         return 0;
196 }