forked from samr7/vanitygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winglue.c
219 lines (187 loc) · 4.49 KB
/
winglue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Vanitygen, vanity bitcoin address generator
* Copyright (C) 2011 <samr7@cs.washington.edu>
*
* Vanitygen is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Vanitygen is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Vanitygen. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include "winglue.h"
int
count_processors(void)
{
typedef BOOL (WINAPI *LPFN_GLPI)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
LPFN_GLPI glpi;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL, ptr;
DWORD size = 0, count = 0, pos = 0, i, ret;
glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")),
"GetLogicalProcessorInformation");
if (!glpi)
return -1;
while (1) {
ret = glpi(buffer, &size);
if (ret)
break;
if (buffer)
free(buffer);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return -1;
buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION) malloc(size);
if (!buffer)
return -1;
}
for (ptr = buffer;
(pos + sizeof(*ptr)) <= size;
ptr++, pos += sizeof(*ptr)) {
switch (ptr->Relationship) {
case RelationProcessorCore:
for (i = ptr->ProcessorMask; i != 0; i >>= 1) {
if (i & 1)
count++;
}
break;
default:
break;
}
}
if (buffer)
free(buffer);
return count;
}
/*
* struct timeval compatibility for Win32
*/
#define TIMESPEC_TO_FILETIME_OFFSET \
( ((unsigned __int64) 27111902 << 32) + \
(unsigned __int64) 3577643008 )
int
gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
if (NULL != tv) {
GetSystemTimeAsFileTime(&ft);
tv->tv_sec = (int) ((*(unsigned __int64 *) &ft -
TIMESPEC_TO_FILETIME_OFFSET) /
10000000);
tv->tv_usec = (int) ((*(unsigned __int64 *) &ft -
TIMESPEC_TO_FILETIME_OFFSET -
((unsigned __int64) tv->tv_sec *
(unsigned __int64) 10000000)) / 10);
}
return 0;
}
void
timeradd(struct timeval *a, struct timeval *b, struct timeval *result)
{
result->tv_sec = a->tv_sec + b->tv_sec;
result->tv_usec = a->tv_usec + b->tv_usec;
if (result->tv_usec > 10000000) {
result->tv_sec++;
result->tv_usec -= 1000000;
}
}
void
timersub(struct timeval *a, struct timeval *b, struct timeval *result)
{
result->tv_sec = a->tv_sec - b->tv_sec;
result->tv_usec = a->tv_usec - b->tv_usec;
if (result->tv_usec < 0) {
result->tv_sec--;
result->tv_usec += 1000000;
}
}
/*
* getopt() for Win32 -- public domain ripped from codeproject.com
*/
TCHAR *optarg = NULL;
int optind = 0;
int getopt(int argc, TCHAR *argv[], TCHAR *optstring)
{
static TCHAR *next = NULL;
TCHAR c;
TCHAR *cp;
if (optind == 0)
next = NULL;
optarg = NULL;
if (next == NULL || *next == _T('\0'))
{
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != _T('-') || argv[optind][1] == _T('\0'))
{
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
if (_tcscmp(argv[optind], _T("--")) == 0)
{
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
next = argv[optind];
next++; // skip past -
optind++;
}
c = *next++;
cp = _tcschr(optstring, c);
if (cp == NULL || c == _T(':'))
return _T('?');
cp++;
if (*cp == _T(':'))
{
if (*next != _T('\0'))
{
optarg = next;
next = NULL;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return _T('?');
}
}
return c;
}
/*
* If ptw32 is being linked in as a static library, make sure that
* its process attach function gets called before main().
*/
#if defined(PTW32_STATIC_LIB)
int __cdecl __initptw32(void);
#if defined(_MSC_VER)
class __constructme { public: __constructme() { __initptw32(); } } __vg_pinit;
#define CONSTRUCTOR_TYPE __cdecl
#elif defined(__GNUC__)
#define CONSTRUCTOR_TYPE __cdecl __attribute__((constructor))
#else
#error "Unknown compiler -- can't mark constructor"
#endif
int CONSTRUCTOR_TYPE
__initptw32(void)
{
pthread_win32_process_attach_np();
return 0;
}
#endif /* defined(PTW32_STATIC_LIB) */