]> git.sesse.net Git - freerainbowtables/blob - Client Applications/converti2/Public.cpp
actually commit everything
[freerainbowtables] / Client Applications / converti2 / 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
32 #else
33 #include <cstdio>
34 #include <cctype>
35 #include <ctime>
36 #include <cstring>
37 #include <cstdlib>
38 #include <csignal>
39 #include <unistd.h>
40
41 #endif
42
43 #include "Public.h"
44
45 #ifdef _WIN32
46         #include <windows.h>
47 #elif defined(__APPLE__) || \
48         ((defined(__unix__) || defined(unix)) && !defined(USG))
49
50         #include <sys/param.h>
51
52         #if defined(BSD)
53                 #include <sys/sysctl.h>
54         #elif defined(__linux__)
55                 #include <sys/sysinfo.h>
56         #else
57                 #error Unsupported Operating system
58         #endif
59 #endif
60
61 //////////////////////////////////////////////////////////////////////
62
63 unsigned int GetFileLen(FILE* file)
64 {
65         unsigned int pos = ftell(file);
66         fseek(file, 0, SEEK_END);
67         unsigned int len = ftell(file);
68         fseek(file, pos, SEEK_SET);
69
70         return len;
71 }
72
73 string TrimString(string s)
74 {
75         while (s.size() > 0)
76         {
77                 if (s[0] == ' ' || s[0] == '\t')
78                         s = s.substr(1);
79                 else
80                         break;
81         }
82
83         while (s.size() > 0)
84         {
85                 if (s[s.size() - 1] == ' ' || s[s.size() - 1] == '\t')
86                         s = s.substr(0, s.size() - 1);
87                 else
88                         break;
89         }
90
91         return s;
92 }
93 bool GetHybridCharsets(string sCharset, vector<tCharset>& vCharset)
94 {
95         // Example: hybrid(mixalpha-numeric-all-space#1-6,numeric#1-4)
96         if(sCharset.substr(0, 6) != "hybrid") // Not hybrid charset
97                 return false;
98
99         UINT4 nEnd = (int) sCharset.rfind(')');
100         UINT4 nStart = (int) sCharset.rfind('(');
101         string sChar = sCharset.substr(nStart + 1, nEnd - nStart - 1);
102         vector<string> vParts;
103         SeperateString(sChar, ",", vParts);
104         for(UINT4 i = 0; i < vParts.size(); i++)
105         {
106                 tCharset stCharset;
107                 vector<string> vParts2;
108                 SeperateString(vParts[i], "#", vParts2);
109                 stCharset.sName = vParts2[0];
110                 vector<string> vParts3;
111                 SeperateString(vParts2[1], "-", vParts3);
112                 stCharset.nPlainLenMin = atoi(vParts3[0].c_str());
113                 stCharset.nPlainLenMax = atoi(vParts3[1].c_str());
114                 vCharset.push_back(stCharset);
115         }
116         return true;
117 }
118 bool ReadLinesFromFile(string sPathName, vector<string>& vLine)
119 {
120         vLine.clear();
121     FILE *file = fopen(sPathName.c_str(), "rb");
122         if (file != NULL)
123         {
124                 unsigned int len = GetFileLen(file);
125                 char* data = new char[len + 1];
126                 fread(data, 1, len, file);
127                 data[len] = '\0';
128                 string content = data;
129                 content += "\n";
130                 delete [] data;
131
132                 unsigned int i;
133                 for (i = 0; i < content.size(); i++)
134                 {
135                         if (content[i] == '\r')
136                                 content[i] = '\n';
137                 }
138
139                 int n;
140                 while ((n = content.find("\n", 0)) != -1)
141                 {
142                         string line = content.substr(0, n);
143                         line = TrimString(line);
144                         if (line != "")
145                                 vLine.push_back(line);
146                         content = content.substr(n + 1);
147                 }
148
149                 fclose(file);
150         }
151         else
152                 return false;
153
154         return true;
155 }
156
157 bool SeperateString(string s, string sSeperator, vector<string>& vPart)
158 {
159         vPart.clear();
160
161         unsigned int i;
162         for (i = 0; i < sSeperator.size(); i++)
163         {
164                 int n = s.find(sSeperator[i]);
165                 if (n != -1)
166                 {
167                         vPart.push_back(s.substr(0, n));
168                         s = s.substr(n + 1);
169                 }
170                 else
171                         return false;
172         }
173         vPart.push_back(s);
174
175         return true;
176 }
177
178 string uint64tostr(uint64 n)
179 {
180         char str[32];
181
182 #ifdef _WIN32
183         sprintf(str, "%I64u", n);
184 #else
185         sprintf(str, "%llu", n);
186 #endif
187
188         return str;
189 }
190
191 string uint64tohexstr(uint64 n)
192 {
193         char str[32];
194
195 #ifdef _WIN32
196         sprintf(str, "%016I64x", n);
197 #else
198         sprintf(str, "%016llx", n);
199 #endif
200
201         return str;
202 }
203
204 string HexToStr(const unsigned char* pData, int nLen)
205 {
206         string sRet;
207         int i;
208         for (i = 0; i < nLen; i++)
209         {
210                 char szByte[3];
211                 sprintf(szByte, "%02x", pData[i]);
212                 sRet += szByte;
213         }
214
215         return sRet;
216 }
217
218 uint64 GetAvailPhysMemorySize()
219 {
220 #ifdef _WIN32
221         MEMORYSTATUS ms;
222         GlobalMemoryStatus(&ms);
223         return ms.dwAvailPhys;
224 #elif defined(BSD)
225         int mib[2] = { CTL_HW, HW_PHYSMEM };
226         uint64 physMem;
227         //XXX warning size_t isn't portable
228         size_t len;
229         len = sizeof(physMem);
230         sysctl(mib, 2, &physMem, &len, NULL, 0);
231         return physMem;
232 #elif defined(__linux__)
233         struct sysinfo info;
234         sysinfo(&info);
235         return ( info.freeram + info.bufferram ) * (unsigned long) info.mem_unit;
236 #else
237         return 0;
238         #error Unsupported Operating System
239 #endif
240 }
241
242 void ParseHash(string sHash, unsigned char* pHash, int& nHashLen)
243 {
244         UINT4 i;
245         for (i = 0; i < sHash.size() / 2; i++)
246         {
247                 string sSub = sHash.substr(i * 2, 2);
248                 int nValue;
249                 sscanf(sSub.c_str(), "%02x", &nValue);
250                 pHash[i] = (unsigned char)nValue;
251         }
252
253         nHashLen = sHash.size() / 2;
254 }
255
256 void Logo()
257 {
258         printf("RainbowCrack (improved) 2.0 - Making a Faster Cryptanalytic Time-Memory Trade-Off\n");
259         printf("by Martin Westergaard <martinwj2005@gmail.com>\n");
260         printf("http://www.freerainbowtables.com/\n");
261         printf("original code by Zhu Shuanglei <shuanglei@hotmail.com>\n");
262         printf("http://www.antsight.com/zsl/rainbowcrack/\n\n");
263 }
264
265 // XXX nmap is GPL2, will check newer releases regarding license
266 // Code comes from nmap, used for the linux implementation of kbhit()
267 #ifndef _WIN32
268
269 static int tty_fd = 0;
270 struct termios saved_ti;
271
272 int tty_getchar()
273 {
274         int c, numChars;
275
276         if (tty_fd && tcgetpgrp(tty_fd) == getpid()) {
277                 c = 0;
278                 numChars = read(tty_fd, &c, 1);
279                 if (numChars > 0) return c;
280         }
281
282         return -1;
283 }
284
285 void tty_done()
286 {
287         if (!tty_fd) return;
288
289         tcsetattr(tty_fd, TCSANOW, &saved_ti);
290
291         close(tty_fd);
292         tty_fd = 0;
293 }
294
295 void tty_init()
296 {
297         struct termios ti;
298
299         if (tty_fd)
300                 return;
301
302         if ((tty_fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;
303
304         tcgetattr(tty_fd, &ti);
305         saved_ti = ti;
306         ti.c_lflag &= ~(ICANON | ECHO);
307         ti.c_cc[VMIN] = 1;
308         ti.c_cc[VTIME] = 0;
309         tcsetattr(tty_fd, TCSANOW, &ti);
310
311         atexit(tty_done);
312 }
313
314 void tty_flush(void)
315 {
316         tcflush(tty_fd, TCIFLUSH);
317 }
318 // end nmap code
319 #endif