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