]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/Public.cpp
f917d3720fa69301ba8767697ca46289ea4d0431
[freerainbowtables] / Common / rt api / Public.cpp
1 /*
2    RainbowCrack - a general propose implementation of Philippe Oechslin's faster time-memory trade-off technique.
3
4    Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
5 */
6
7 #ifdef _WIN32
8         #pragma warning(disable : 4786)
9 #endif
10
11 #ifdef _WIN32
12         #ifdef BOINC
13                 #include "boinc_win.h"
14         #endif
15 #else
16 #include "config.h"
17 #include <cstdio>
18 #include <cctype>
19 #include <ctime>
20 #include <cstring>
21 #include <cstdlib>
22 #include <csignal>
23 #include <unistd.h>
24
25 #endif
26 #ifdef BOINC
27         #include "filesys.h"
28         #include "boinc_api.h"
29 #endif
30 #include "Public.h"
31
32 #ifdef _WIN32
33         #include <windows.h>
34 #else
35         #include <sys/sysinfo.h>
36 #endif
37
38 //////////////////////////////////////////////////////////////////////
39
40 unsigned int GetFileLen(FILE* file)
41 {
42         unsigned int pos = ftell(file);
43         fseek(file, 0, SEEK_END);
44         unsigned int len = ftell(file);
45         fseek(file, pos, SEEK_SET);
46
47         return len;
48 }
49
50 string TrimString(string s)
51 {
52         while (s.size() > 0)
53         {
54                 if (s[0] == ' ' || s[0] == '\t')
55                         s = s.substr(1);
56                 else
57                         break;
58         }
59
60         while (s.size() > 0)
61         {
62                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
63                         s = s.substr(0, s.size() - 1);
64                 else
65                         break;
66         }
67
68         return s;
69 }
70 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
71 {
72         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
73         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
74                 return false;
75         size_t nEnd = sCharset.rfind(')');
76         size_t nStart = sCharset.rfind('(');
77         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
78         vector<string> vParts;
79         SeperateString(sChar, ",", vParts);
80         for(int i = 0; i < vParts.size(); i++)
81         {
82                 tCharset stCharset;
83                 vector<string> vParts2;
84                 SeperateString(vParts[i], "#", vParts2);
85                 stCharset.sName = vParts2[0];
86                 vector<string> vParts3;
87                 SeperateString(vParts2[1], "-", vParts3);
88                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
89                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
90                 vCharset.push_back(stCharset);
91         }
92         return true;
93 }
94 #ifdef BOINC
95 bool boinc_ReadLinesFromFile(string sPathName, vector<string>& vLine)
96 {
97         vLine.clear();
98     char input_path[512];
99     boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
100     FILE *file = boinc_fopen(input_path, "rb");
101     if (!file) {
102         fprintf(stderr,
103             "Couldn't find input file, resolved name %s.\n", input_path
104         );
105         exit(-1);
106     }
107         if (file != NULL)
108         {
109                 unsigned int len = GetFileLen(file);
110                 char* data = new char[len + 1];
111                 fread(data, 1, len, file);
112                 data[len] = '\0';
113                 string content = data;
114                 content += "\n";
115                 delete data;
116
117                 unsigned int i;
118                 for (i = 0; i < content.size(); i++)
119                 {
120                         if (content[i] == '\r')
121                                 content[i] = '\n';
122                 }
123
124                 int n;
125                 while ((n = content.find("\n", 0)) != -1)
126                 {
127                         string line = content.substr(0, n);
128                         line = TrimString(line);
129                         if (line != "")
130                                 vLine.push_back(line);
131                         content = content.substr(n + 1);
132                 }
133
134                 fclose(file);
135         }
136         else
137                 return false;
138
139         return true;
140 }
141 #endif 
142 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
143 {
144         vLine.clear();
145     FILE *file = fopen(sPathName.c_str(), "rb");
146         if (file != NULL)
147         {
148                 unsigned int len = GetFileLen(file);
149                 char* data = new char[len + 1];
150                 fread(data, 1, len, file);
151                 data[len] = '\0';
152                 string content = data;
153                 content += "\n";
154                 delete data;
155
156                 unsigned int i;
157                 for (i = 0; i < content.size(); i++)
158                 {
159                         if (content[i] == '\r')
160                                 content[i] = '\n';
161                 }
162
163                 int n;
164                 while ((n = content.find("\n", 0)) != -1)
165                 {
166                         string line = content.substr(0, n);
167                         line = TrimString(line);
168                         if (line != "")
169                                 vLine.push_back(line);
170                         content = content.substr(n + 1);
171                 }
172
173                 fclose(file);
174         }
175         else
176                 return false;
177
178         return true;
179 }
180
181
182 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
183 {
184         vPart.clear();
185
186         unsigned int i;
187         for (i = 0; i < sSeperator.size(); i++)
188         {
189                 int n = s.find(sSeperator[i]);
190                 if (n != -1)
191                 {
192                         vPart.push_back(s.substr(0, n));
193                         s = s.substr(n + 1);
194                 }
195                 else
196                         return false;
197         }
198         vPart.push_back(s);
199
200         return true;
201 }
202
203 string uint64tostr(uint64 n)
204 {
205         char str[32];
206
207 #ifdef _WIN32
208         sprintf(str, "%I64u", n);
209 #else
210         sprintf(str, "%llu", n);
211 #endif
212
213         return str;
214 }
215
216 string uint64tohexstr(uint64 n)
217 {
218         char str[32];
219
220 #ifdef _WIN32
221         sprintf(str, "%016I64x", n);
222 #else
223         sprintf(str, "%016llx", n);
224 #endif
225
226         return str;
227 }
228
229 string HexToStr(const unsigned char* pData, int nLen)
230 {
231         string sRet;
232         int i;
233         for (i = 0; i < nLen; i++)
234         {
235                 char szByte[3];
236                 sprintf(szByte, "%02x", pData[i]);
237                 sRet += szByte;
238         }
239
240         return sRet;
241 }
242
243 unsigned int GetAvailPhysMemorySize()
244 {
245 #ifdef _WIN32
246                 MEMORYSTATUS ms;
247                 GlobalMemoryStatus(&ms);
248                 return ms.dwAvailPhys;
249 #else
250         struct sysinfo info;
251         sysinfo(&info);                 // This function is Linux-specific
252         return info.freeram;
253 #endif
254 }
255
256 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
257 {
258         int i;
259         for (i = 0; i < sHash.size() / 2; i++)
260         {
261                 string sSub = sHash.substr(i * 2, 2);
262                 int nValue;
263                 sscanf(sSub.c_str(), "%02x", &nValue);
264                 pHash[i] = (unsigned char)nValue;
265         }
266
267         nHashLen = sHash.size() / 2;
268 }
269
270 void Logo()
271 {
272         printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
273         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
274         printf("http://www.freerainbowtables.com/\n");
275         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
276         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
277 }