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