]> git.sesse.net Git - freerainbowtables/blob - Common/rt api/Public.cpp
distrrtgen fixes
[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("loweralpha-space            = [abcdefghijklmnopqrstuvwxyz ]");
129         vLine.push_back("loweralpha                  = [abcdefghijklmnopqrstuvwxyz]");
130 /*
131         char input_path[512];
132         boinc_resolve_filename(sPathName.c_str(), input_path, sizeof(input_path));
133         FILE *file = boinc_fopen(input_path, "rb");
134         if (!file) {
135                 fprintf(stderr,
136                         "Couldn't find input file, resolved name %s.\n", input_path
137                 );
138                 exit(-1);
139         }
140
141         if (file != NULL)
142         {
143                 unsigned int len = GetFileLen(file);
144                 char* data = new char[len + 1];
145                 fread(data, 1, len, file);
146                 data[len] = '\0';
147                 string content = data;
148                 content += "\n";
149                 delete [] data;
150
151                 unsigned int i;
152                 for (i = 0; i < content.size(); i++)
153                 {
154                         if (content[i] == '\r')
155                                 content[i] = '\n';
156                 }
157
158                 int n;
159                 while ((n = content.find("\n", 0)) != -1)
160                 {
161                         string line = content.substr(0, n);
162                         line = TrimString(line);
163                         if (line != "")
164                                 vLine.push_back(line);
165                         content = content.substr(n + 1);
166                 }
167
168                 fclose(file);
169         }
170         else
171                 return false;
172                 */
173         return true;
174 }
175 #endif 
176 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
177 {
178         vLine.clear();
179     FILE *file = fopen(sPathName.c_str(), "rb");
180         if (file != NULL)
181         {
182                 unsigned int len = GetFileLen(file);
183                 char* data = new char[len + 1];
184                 fread(data, 1, len, file);
185                 data[len] = '\0';
186                 string content = data;
187                 content += "\n";
188                 delete data;
189
190                 unsigned int i;
191                 for (i = 0; i < content.size(); i++)
192                 {
193                         if (content[i] == '\r')
194                                 content[i] = '\n';
195                 }
196
197                 int n;
198                 while ((n = content.find("\n", 0)) != -1)
199                 {
200                         string line = content.substr(0, n);
201                         line = TrimString(line);
202                         if (line != "")
203                                 vLine.push_back(line);
204                         content = content.substr(n + 1);
205                 }
206
207                 fclose(file);
208         }
209         else
210                 return false;
211
212         return true;
213 }
214
215
216 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
217 {
218         vPart.clear();
219
220         unsigned int i;
221         for (i = 0; i < sSeperator.size(); i++)
222         {
223                 int n = s.find(sSeperator[i]);
224                 if (n != -1)
225                 {
226                         vPart.push_back(s.substr(0, n));
227                         s = s.substr(n + 1);
228                 }
229                 else
230                         return false;
231         }
232         vPart.push_back(s);
233
234         return true;
235 }
236
237 string uint64tostr(uint64 n)
238 {
239         char str[32];
240
241 #ifdef _WIN32
242         sprintf(str, "%I64u", n);
243 #else
244         sprintf(str, "%llu", n);
245 #endif
246
247         return str;
248 }
249
250 string uint64tohexstr(uint64 n)
251 {
252         char str[32];
253
254 #ifdef _WIN32
255         sprintf(str, "%016I64x", n);
256 #else
257         sprintf(str, "%016llx", n);
258 #endif
259
260         return str;
261 }
262
263 string HexToStr(const unsigned char* pData, int nLen)
264 {
265         string sRet;
266         int i;
267         for (i = 0; i < nLen; i++)
268         {
269                 char szByte[3];
270                 sprintf(szByte, "%02x", pData[i]);
271                 sRet += szByte;
272         }
273
274         return sRet;
275 }
276
277 unsigned int GetAvailPhysMemorySize()
278 {
279 #ifdef _WIN32
280                 MEMORYSTATUS ms;
281                 GlobalMemoryStatus(&ms);
282                 return ms.dwAvailPhys;
283 #else
284         struct sysinfo info;
285         sysinfo(&info);                 // This function is Linux-specific
286         return info.freeram;
287 #endif
288 }
289
290 string GetApplicationPath()
291 {
292         char fullPath[FILENAME_MAX];
293
294 #ifdef _WIN32
295         GetModuleFileName(NULL, fullPath, FILENAME_MAX);
296 #else
297         char szTmp[32];
298         // XXX linux/proc file system dependent
299         sprintf(szTmp, "/proc/%d/exe", getpid());
300         int bytes = readlink(szTmp, fullPath, FILENAME_MAX);
301
302         if( bytes >= 0 )
303                 fullPath[bytes] = '\0';
304 #endif
305
306         string sApplicationPath = fullPath;
307 #ifdef _WIN32
308         int nIndex = sApplicationPath.find_last_of('\\');
309 #else
310         int nIndex = sApplicationPath.find_last_of('/');
311 #endif
312
313         if ( nIndex != -1 )
314                 sApplicationPath = sApplicationPath.substr(0, nIndex+1);
315
316         return sApplicationPath;
317 }
318
319 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
320 {
321         uint32 i;
322         for (i = 0; i < sHash.size() / 2; i++)
323         {
324                 string sSub = sHash.substr(i * 2, 2);
325                 int nValue;
326                 sscanf(sSub.c_str(), "%02x", &nValue);
327                 pHash[i] = (unsigned char)nValue;
328         }
329
330         nHashLen = sHash.size() / 2;
331 }
332
333 void Logo()
334 {
335         printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
336         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
337         printf("http://www.freerainbowtables.com/\n");
338         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
339         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
340 }