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