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