]> git.sesse.net Git - freerainbowtables/blob - Client Applications/rti2rto/rti2rto.cpp
UINT4 -> uint32
[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         // Info
91         printf("%s:\n", sFileName.c_str());
92         FILE *fResult = fopen(sResultFileName.c_str(), "wb");
93         if(fResult == NULL)
94         {
95                 printf("Could not open %s for write access", sResultFileName.c_str());
96                 return;
97         }
98         static CMemoryPool mp;
99         unsigned int nAllocatedSize;
100         BaseRTReader *reader = NULL;
101
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
112         RainbowChainCP* pChain = (RainbowChainCP*)mp.Allocate(reader->GetChainsLeft() * sizeof(RainbowChainCP), nAllocatedSize);
113         if (pChain != NULL)
114         {
115                 nAllocatedSize = nAllocatedSize / sizeof(RainbowChainCP) * sizeof(RainbowChainCP);              // Round to boundary
116                 unsigned int nChains = nAllocatedSize / sizeof(RainbowChainCP);
117                 while(reader->GetChainsLeft() > 0)
118                 {
119                         reader->ReadChains(nChains, pChain);
120                         for(uint32 i = 0; i < nChains; i++)
121                         {
122                                 fwrite(&pChain[i], 1, 16, fResult);
123                         }
124                 }
125         }
126         fclose(fResult);
127         if(reader != NULL)
128                 delete reader;
129 }
130 int main(int argc, char* argv[])
131 {
132 #ifdef _WIN32
133         if (argc != 2)
134         {
135                 Usage();
136                 
137                 return 0;
138         }
139         string sWildCharPathName = argv[1];
140         vector<string> vPathName;
141         GetTableList(sWildCharPathName, vPathName);
142 #else
143         if (argc < 2)
144         {
145                 Usage();
146                 return 0;
147         }
148         for(int i = 0; i < argc; i++)
149         {
150                 printf("%i: %s\n", i, argv[i]);
151         }
152         // vPathName
153         vector<string> vPathName;
154         GetTableList(argc, argv, vPathName);
155 #endif
156         if (vPathName.size() == 0)
157         {
158                 printf("no rainbow table found\n");
159                 return 0;
160         }
161         for (uint32 i = 0; i < vPathName.size(); i++)
162         {
163                 string sResultFile, sType;
164                         
165                 if(vPathName[i].substr(vPathName[i].length() - 4, vPathName[i].length()) == "rti2")
166                 {
167                         sResultFile = vPathName[i].substr(0, vPathName[i].length() - 2); // Resulting file is .rt, not .rti2
168                         sType = "RTI2";
169                 }
170                 else if(vPathName[i].substr(vPathName[i].length() - 3, vPathName[i].length()) == "rti")
171                 {
172                         sResultFile = vPathName[i].substr(0, vPathName[i].length() - 1); // Resulting file is .rt, not .rti
173                         sType = "RTI";
174                 }
175                 else 
176                 {
177                         printf("File %s is not a RTI or a RTI2 file", vPathName[i].c_str());
178                         continue;
179                 }
180                 ConvertRainbowTable(vPathName[i], sResultFile, sType);
181                 printf("\n");
182         }
183         return 0;
184 }