]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/Public.cpp
merged paths
[freerainbowtables] / Common / rt api / Public.cpp
1 /*
2  * freerainbowtables is a project for generating, distributing, and using
3  * perfect rainbow tables
4  *
5  * Copyright (C) Zhu Shuanglei <shuanglei@hotmail.com>
6  * Copyright Martin Westergaard Jørgensen <martinwj2005@gmail.com>
7  * Copyright 2009, 2010 Daniël Niggebrugge <niggebrugge@fox-it.com>
8  * Copyright 2009, 2010 James Nobis <frt@quelrod.net>
9  *
10  * This file is part of freerainbowtables.
11  *
12  * freerainbowtables is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * freerainbowtables is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with freerainbowtables.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #ifdef _WIN32
27         #pragma warning(disable : 4786)
28 #endif
29
30 #ifdef _WIN32
31         #ifdef BOINC
32                 #include "boinc_win.h"
33         #endif
34 #else
35 #include "config.h"
36 #include <cstdio>
37 #include <cctype>
38 #include <ctime>
39 #include <cstring>
40 #include <cstdlib>
41 #include <csignal>
42 #include <unistd.h>
43
44 #endif
45 #ifdef BOINC
46         #include "filesys.h"
47         #include "boinc_api.h"
48 #endif
49 #include "Public.h"
50
51 #ifdef _WIN32
52         #include <windows.h>
53 #elif defined(__APPLE__) || \
54         ((defined(__unix__) || defined(unix)) && !defined(USG))
55
56         #include <sys/param.h>
57
58         #if defined(BSD)
59                 #include <sys/sysctl.h>
60         #elif defined(__linux__)
61                 #include <sys/sysinfo.h>
62         #else
63                 #error Unsupported Operating system
64         #endif
65 #endif
66
67 //////////////////////////////////////////////////////////////////////
68
69 unsigned int GetFileLen(FILE* file)
70 {
71         unsigned int pos = ftell(file);
72         fseek(file, 0, SEEK_END);
73         unsigned int len = ftell(file);
74         fseek(file, pos, SEEK_SET);
75
76         return len;
77 }
78
79 string TrimString(string s)
80 {
81         while (s.size() > 0)
82         {
83                 if (s[0] == ' ' || s[0] == '\t')
84                         s = s.substr(1);
85                 else
86                         break;
87         }
88
89         while (s.size() > 0)
90         {
91                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
92                         s = s.substr(0, s.size() - 1);
93                 else
94                         break;
95         }
96
97         return s;
98 }
99 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
100 {
101         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
102         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
103                 return false;
104
105         UINT4 nEnd = (int) sCharset.rfind(')');
106         UINT4 nStart = (int) sCharset.rfind('(');
107         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
108         vector<string> vParts;
109         SeperateString(sChar, ",", vParts);
110         for(UINT4 i = 0; i < vParts.size(); i++)
111         {
112                 tCharset stCharset;
113                 vector<string> vParts2;
114                 SeperateString(vParts[i], "#", vParts2);
115                 stCharset.sName = vParts2[0];
116                 vector<string> vParts3;
117                 SeperateString(vParts2[1], "-", vParts3);
118                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
119                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
120                 vCharset.push_back(stCharset);
121         }
122         return true;
123 }
124 #ifdef BOINC
125 bool boinc_ReadLinesFromFile(string sPathName, vector<string>& vLine)
126 {
127         vLine.clear();
128         vLine.push_back("byte                        = []");
129         vLine.push_back("alpha                       = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]");
130         vLine.push_back("alpha-space                 = [ABCDEFGHIJKLMNOPQRSTUVWXYZ ]");
131         vLine.push_back("alpha-numeric               = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]");
132         vLine.push_back("alpha-numeric-space         = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]");
133         vLine.push_back("alpha-numeric-symbol14      = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
134         vLine.push_back("alpha-numeric-symbol14-space= [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x20]");
135         vLine.push_back("all                         = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
136         vLine.push_back("all-space                   = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
137         vLine.push_back("alpha-numeric-symbol32-space = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
138         vLine.push_back("lm-frt-cp437                = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xE0\xE1\xE2\xE3\xE4\xE6\xE7\xE8\xE9\xEA\xEB\xEE]");
139         vLine.push_back("lm-frt-cp850                = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9C\x9D\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE5\xE6\xE8\xE9\xEA\xEB\xED\xEF]");
140         vLine.push_back("lm-frt-cp437-850            = [\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x7B\x7C\x7D\x7E\x80\x8E\x8F\x90\x92\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA5\xB5\xB6\xB7\xBD\xBE\xC7\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xDE\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xED\xEE\xEF]");
141         vLine.push_back("numeric                     = [0123456789]");
142         vLine.push_back("numeric-space               = [0123456789 ]");
143         vLine.push_back("loweralpha                  = [abcdefghijklmnopqrstuvwxyz]");
144         vLine.push_back("loweralpha-space            = [abcdefghijklmnopqrstuvwxyz ]");
145         vLine.push_back("loweralpha-numeric          = [abcdefghijklmnopqrstuvwxyz0123456789]");
146         vLine.push_back("loweralpha-numeric-space    = [abcdefghijklmnopqrstuvwxyz0123456789 ]");
147         vLine.push_back("loweralpha-numeric-symbol14 = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
148         vLine.push_back("loweralpha-numeric-all      = [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
149         vLine.push_back("loweralpha-numeric-symbol32-space= [abcdefghijklmnopqrstuvwxyz0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
150         vLine.push_back("mixalpha                    = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]");
151         vLine.push_back("mixalpha-space              = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ]");
152         vLine.push_back("mixalpha-numeric            = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]");
153         vLine.push_back("mixalpha-numeric-space      = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ]");
154         vLine.push_back("mixalpha-numeric-symbol14   = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D]");
155         vLine.push_back("mixalpha-numeric-all        = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F]");
156         vLine.push_back("mixalpha-numeric-symbol32-space  = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
157         vLine.push_back("mixalpha-numeric-all-space  = [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\x21\x40\x23\x24\x25\x5E\x26\x2A\x28\x29\x2D\x5F\x2B\x3D\x7E\x60\x5B\x5D\x7B\x7D\x7C\x5C\x3A\x3B\x22\x27\x3C\x3E\x2C\x2E\x3F\x2F\x20]");
158
159 /*      char input_path[512];
160         boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
161         FILE *file = boinc_fopen(input_path, "rb");
162         if (!file) {
163                 fprintf(stderr,
164                         "Couldn't find input file, resolved name %s.\n", input_path
165                 );
166                 exit(-1);
167         }
168
169         if (file != NULL)
170         {
171                 unsigned int len = GetFileLen(file);
172                 char* data = new char[len + 1];
173                 fread(data, 1, len, file);
174                 data[len] = '\0';
175                 string content = data;
176                 content += "\n";
177                 delete [] data;
178
179                 unsigned int i;
180                 for (i = 0; i < content.size(); i++)
181                 {
182                         if (content[i] == '\r')
183                                 content[i] = '\n';
184                 }
185
186                 int n;
187                 while ((n = content.find("\n", 0)) != -1)
188                 {
189                         string line = content.substr(0, n);
190                         line = TrimString(line);
191                         if (line != "")
192                                 vLine.push_back(line);
193                         content = content.substr(n + 1);
194                 }
195
196                 fclose(file);
197         }
198         else
199                 return false;
200                 */
201         return true;
202 }
203 #endif 
204 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
205 {
206         vLine.clear();
207     FILE *file = fopen(sPathName.c_str(), "rb");
208         if (file != NULL)
209         {
210                 unsigned int len = GetFileLen(file);
211                 char* data = new char[len + 1];
212                 fread(data, 1, len, file);
213                 data[len] = '\0';
214                 string content = data;
215                 content += "\n";
216                 delete data;
217
218                 unsigned int i;
219                 for (i = 0; i < content.size(); i++)
220                 {
221                         if (content[i] == '\r')
222                                 content[i] = '\n';
223                 }
224
225                 int n;
226                 while ((n = content.find("\n", 0)) != -1)
227                 {
228                         string line = content.substr(0, n);
229                         line = TrimString(line);
230                         if (line != "")
231                                 vLine.push_back(line);
232                         content = content.substr(n + 1);
233                 }
234
235                 fclose(file);
236         }
237         else
238                 return false;
239
240         return true;
241 }
242
243
244 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
245 {
246         vPart.clear();
247
248         unsigned int i;
249         for (i = 0; i < sSeperator.size(); i++)
250         {
251                 int n = s.find(sSeperator[i]);
252                 if (n != -1)
253                 {
254                         vPart.push_back(s.substr(0, n));
255                         s = s.substr(n + 1);
256                 }
257                 else
258                         return false;
259         }
260         vPart.push_back(s);
261
262         return true;
263 }
264
265 string uint64tostr(uint64 n)
266 {
267         char str[32];
268
269 #ifdef _WIN32
270         sprintf(str, "%I64u", n);
271 #else
272         sprintf(str, "%llu", n);
273 #endif
274
275         return str;
276 }
277
278 string uint64tohexstr(uint64 n)
279 {
280         char str[32];
281
282 #ifdef _WIN32
283         sprintf(str, "%016I64x", n);
284 #else
285         sprintf(str, "%016llx", n);
286 #endif
287
288         return str;
289 }
290
291 string HexToStr(const unsigned char* pData, int nLen)
292 {
293         string sRet;
294         int i;
295         for (i = 0; i < nLen; i++)
296         {
297                 char szByte[3];
298                 sprintf(szByte, "%02x", pData[i]);
299                 sRet += szByte;
300         }
301
302         return sRet;
303 }
304
305 unsigned int GetAvailPhysMemorySize()
306 {
307 #ifdef _WIN32
308                 MEMORYSTATUS ms;
309                 GlobalMemoryStatus(&ms);
310                 return ms.dwAvailPhys;
311 #else
312         struct sysinfo info;
313         sysinfo(&info);                 // This function is Linux-specific
314         return info.freeram;
315 #endif
316 }
317
318 string GetApplicationPath()
319 {
320         char fullPath[FILENAME_MAX];
321
322 #ifdef _WIN32
323         GetModuleFileName(NULL, fullPath, FILENAME_MAX);
324 #else
325         char szTmp[32];
326         // XXX linux/proc file system dependent
327         sprintf(szTmp, "/proc/%d/exe", getpid());
328         int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
329
330         if( bytes >= 0 )
331                 fullPath[bytes] = '\0';
332 #endif
333
334         string sApplicationPath = fullPath;
335 #ifdef _WIN32
336         int nIndex = sApplicationPath.find_last_of('\\');
337 #else
338         int nIndex = sApplicationPath.find_last_of('/');
339 #endif
340
341         if ( nIndex != -1 )
342                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
343
344         return sApplicationPath;
345 }
346
347 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
348 {
349         uint32 i;
350         for (i = 0; i < sHash.size() / 2; i++)
351         {
352                 string sSub = sHash.substr(i * 2, 2);
353                 int nValue;
354                 sscanf(sSub.c_str(), "%02x", &nValue);
355                 pHash[i] = (unsigned char)nValue;
356         }
357
358         nHashLen = sHash.size() / 2;
359 }
360
361 void Logo()
362 {
363         printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
364         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
365         printf("http://www.freerainbowtables.com/\n");
366         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
367         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
368 }