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