-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomm95c.c
480 lines (392 loc) · 12.6 KB
/
comm95c.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright 1995-2020 Mersenne Research, Inc. All rights reserved
*
* Common routines and variables used by Prime95 and NTPrime
*
* Comm95b contains information used only during execution
* Comm95c contains information used during setup and execution
*/
#include <wininet.h>
#include <process.h>
#include <sddl.h>
/* Common routines */
/* Load the PrimeNet DLL, make sure an internet connection is active */
int LoadPrimeNet (void)
{
DWORD flags;
/* If we're not using a dial-up connection, let primenet try to contact the server. */
if (!DIAL_UP) return (TRUE);
/* RAS calls, see below, is no longer the MS-prefered method of detecting */
/* an Internet connection. Starting in version 22.10 we offer a way for */
/* for users to use the prefered wininet.dll method. */
/* InternetGetConnectedState should return FALSE if the modem is not */
/* connected to the Internet. */
/* Starting in version 25.1, this became the default detection method. */
/* Starting with version 29.1 this became the only supported detection method */
if (InternetGetConnectedState (&flags, 0)) return (TRUE);
// Print error message if no there are no connections
OutputStr (COMM_THREAD_NUM, "Dial-up connection not active.\n");
return (FALSE);
}
/* Unload the PrimeNet DLL */
void UnloadPrimeNet (void)
{
}
/* Get Windows Serial Number */
void getWindowsSerialNumber (
char *output)
{
HKEY hkey = 0;
char buf[256];
DWORD type, disposition;
DWORD bufsize = sizeof (buf);
*output = 0;
if (RegCreateKeyEx (
HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion",
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkey,
&disposition) == ERROR_SUCCESS &&
RegQueryValueEx (hkey, "ProductId", NULL, &type,
(BYTE *) buf, &bufsize) == ERROR_SUCCESS &&
type == REG_SZ)
strcpy (output, buf);
if (hkey) RegCloseKey (hkey);
}
/* Get Windows Serial Number - our second attempt. This is more robust */
/* as it works under Vista too. */
void getWindowsSerialNumber_2 (
char *output)
{
HKEY hkey;
char buf[256];
DWORD type;
DWORD bufsize;
*output = 0;
hkey = 0;
bufsize = sizeof (buf);
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion", 0,
KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS &&
RegQueryValueEx (hkey, "ProductId", NULL, &type,
(BYTE *) buf, &bufsize) == ERROR_SUCCESS &&
type == REG_SZ)
strcpy (output, buf);
if (hkey) RegCloseKey (hkey);
if (*output) return;
hkey = 0;
bufsize = sizeof (buf);
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows NT\\CurrentVersion", 0,
KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS &&
RegQueryValueEx (hkey, "ProductId", NULL, &type,
(BYTE *) buf, &bufsize) == ERROR_SUCCESS &&
type == REG_SZ)
strcpy (output, buf);
if (hkey) RegCloseKey (hkey);
}
/* Get Window's SID. Hopefully this will combined with the Window's */
/* serial # will generate a unique computer ID. The SID code */
/* came courtesy of www.sysinternals.com with this copyright */
/* and restriction. */
// Copyright (c) 1997-2002 Mark Russinovich and Bryce Cogswell
//
// Changes the computer SID.
//
// This code is protected under copyright law. You do not have
// permission to use this code in a commercial SID-changing product.
PSECURITY_DESCRIPTOR GetRegSecDesc (HKEY Root, TCHAR *Path,
SECURITY_INFORMATION Information)
{
HKEY hKey;
LONG Status;
DWORD nb = 0;
PSECURITY_DESCRIPTOR SecDesc;
//
// Open the key with no access requests, since we don't need
// any.
// SECURITY_DESCRIPTOR
if (RegOpenKeyEx (Root, Path, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return NULL;
//
// Grab a copy of the security for key
//
if (RegGetKeySecurity (hKey, Information, NULL, &nb)
!= ERROR_INSUFFICIENT_BUFFER)
return NULL;
SecDesc = malloc (nb);
Status = RegGetKeySecurity (hKey, Information, SecDesc, &nb);
//
// Close the key anyway
//
RegCloseKey (hKey);
if (Status != ERROR_SUCCESS) {
free (SecDesc);
return NULL;
}
return SecDesc;
}
PSECURITY_DESCRIPTOR GetRegAccess (HKEY hKey)
{
DWORD nb = 0;
PSECURITY_DESCRIPTOR SecDesc;
//
// Get access
//
if (RegGetKeySecurity (hKey, DACL_SECURITY_INFORMATION, NULL, &nb) != ERROR_INSUFFICIENT_BUFFER)
return NULL;
SecDesc = (PSECURITY_DESCRIPTOR) malloc (nb);
if (RegGetKeySecurity (hKey, DACL_SECURITY_INFORMATION, SecDesc, &nb) != ERROR_SUCCESS) {
free (SecDesc);
return (NULL);
}
return (SecDesc);
}
LONG SetRegAccess (HKEY hKey, LPCTSTR lpSubKey,
PSECURITY_DESCRIPTOR SecDesc, PHKEY phKey)
{
//
// Grant requested access
//
if (RegSetKeySecurity (*phKey, DACL_SECURITY_INFORMATION, SecDesc)
!= ERROR_SUCCESS)
return FALSE;
//
// Re-open the key if requested
//
if (! hKey) return TRUE;
RegCloseKey (*phKey);
return (RegOpenKey (hKey, lpSubKey, phKey) == ERROR_SUCCESS);
}
PBYTE IsSubAuthValid( PBYTE SidData, DWORD SidLength )
{
PBYTE sidPtr;
sidPtr = NULL;
if ( SidLength % sizeof(DWORD) == 0 ) {
for ( sidPtr = SidData + SidLength - 5*sizeof(DWORD); sidPtr >= SidData; sidPtr -= sizeof(DWORD) )
if ( ((PDWORD)sidPtr)[1] == 0x05000000 && ((PDWORD)sidPtr)[2] == 0x00000015 )
break;
if ( sidPtr < SidData )
sidPtr = NULL;
}
return sidPtr;
}
void GetTextualSid(
PSID pSid, // binary Sid
PTCHAR TextualSid) // buffer for Textual representation of Sid
{
PSID_IDENTIFIER_AUTHORITY psia;
DWORD dwSubAuthorities;
DWORD dwSidRev=SID_REVISION;
DWORD dwCounter;
DWORD dwSidSize;
// Validate the binary SID.
if(!IsValidSid(pSid)) return;
// Get the identifier authority value from the SID.
psia = GetSidIdentifierAuthority(pSid);
// Get the number of subauthorities in the SID.
dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
// Compute the buffer length.
// S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
// Add 'S' prefix and revision number to the string.
dwSidSize= _stprintf(TextualSid, _T("S-%lu-"), dwSidRev );
// Add SID identifier authority to the string.
if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) ) {
dwSidSize += _stprintf(TextualSid + lstrlen(TextualSid),
_T("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
(USHORT)psia->Value[0],
(USHORT)psia->Value[1],
(USHORT)psia->Value[2],
(USHORT)psia->Value[3],
(USHORT)psia->Value[4],
(USHORT)psia->Value[5]);
} else {
dwSidSize += _stprintf(TextualSid + lstrlen(TextualSid),
_T("%lu"),
(ULONG)(psia->Value[5] ) +
(ULONG)(psia->Value[4] << 8) +
(ULONG)(psia->Value[3] << 16) +
(ULONG)(psia->Value[2] << 24) );
}
// Add SID subauthorities to the string.
//
for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++) {
dwSidSize+= _stprintf(TextualSid + dwSidSize, _T("-%lu"),
*GetSidSubAuthority(pSid, dwCounter) );
}
}
void getWindowsSID (
char *output)
{
PSECURITY_DESCRIPTOR newSecDesc, oldSecDesc;
DWORD valType;
PBYTE vData;
HKEY hKey;
DWORD nb;
PBYTE sidPtr;
DWORD Status;
*output = 0;
//
// Now, get the descriptor of HKLM\SOFTWARE and apply this to SECURITY
//
newSecDesc = GetRegSecDesc( HKEY_LOCAL_MACHINE, "SOFTWARE",
DACL_SECURITY_INFORMATION );
//
// Read the last subauthority of the current computer SID
//
if( RegOpenKey( HKEY_LOCAL_MACHINE, "SECURITY\\SAM\\Domains\\Account",
&hKey) != ERROR_SUCCESS ) {
free (newSecDesc);
return;
}
oldSecDesc = GetRegAccess( hKey );
SetRegAccess( HKEY_LOCAL_MACHINE, "SECURITY\\SAM\\Domains\\Account",
newSecDesc, &hKey );
nb = 0;
vData = NULL;
RegQueryValueEx( hKey, "V", NULL, &valType, vData, &nb );
vData = (PBYTE) malloc( nb );
Status = RegQueryValueEx( hKey, "V", NULL, &valType, vData, &nb );
if( Status != ERROR_SUCCESS ) {
SetRegAccess( HKEY_LOCAL_MACHINE, "SECURITY\\SAM\\Domains\\Account",
oldSecDesc, &hKey );
free (vData);
free (oldSecDesc);
free (newSecDesc);
return;
}
SetRegAccess( NULL, NULL, oldSecDesc, &hKey );
RegCloseKey( hKey );
free (oldSecDesc);
free (newSecDesc);
//
// Make sure that we're dealing with a SID we understand
//
if( !(sidPtr = IsSubAuthValid( vData, nb ))) {
free (vData);
return;
}
GetTextualSid( sidPtr, output );
free (vData);
}
/* Version 2. My simpler attempt at getting the Windows SID. */
/* Version 1 failed on RegOpenKey when I switch users on Vista. */
void getWindowsSID_2 (
char *output)
{
char computer_name[256];
SID *sid;
SID_NAME_USE snu;
char *domain;
char *stringsid;
unsigned long size, domainsize;
*output = 0;
/* Get the computer name */
size = sizeof (computer_name);
if (! GetComputerName ((LPSTR) computer_name, &size)) return;
/* First find the size of buffers required for the SID and domain name */
sid = 0;
domain = 0;
size = domainsize = 0;
LookupAccountName(0, (LPCSTR) computer_name, sid, &size, domain, &domainsize, &snu);
/* Should have failed with ERROR_INSUFFICIENT_BUFFER */
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
/* Allocate memory */
sid = (SID *) malloc (size);
domain = (char *) malloc (domainsize);
/* Get the SID */
if (sid != NULL && domain != NULL &&
LookupAccountName (0, (LPCSTR) computer_name, sid, &size, domain, &domainsize, &snu) &&
ConvertSidToStringSid(sid, &stringsid)) {
strcpy (output, stringsid);
LocalFree (stringsid);
}
/* Cleanup */
free (sid);
free (domain);
}
/* Return the number of MB of physical memory. */
unsigned long physical_memory (void)
{
MEMORYSTATUSEX mem;
mem.dwLength = sizeof (mem);
GlobalMemoryStatusEx (&mem);
return ((unsigned long) ((mem.ullTotalPhys + 1000000) >> 20));
}
/* Return a better guess for amount of memory to use in a torture test. */
/* Caller passes in its guess for amount of memory to use, but this routine */
/* can reduce that guess based on OS-specific code that looks at amount */
/* of available physical memory. */
/* This code was written by an anonymous GIMPS user. */
unsigned long GetSuggestedMemory (unsigned long nDesiredMemory)
{
MEMORYSTATUSEX ms = {0};
DWORDLONG ullUsedMem; // In-use Physical RAM in bytes
DWORDLONG ullDesiredMem = (DWORDLONG) nDesiredMemory << 20; // Desired memory in bytes
DWORDLONG ullDesiredMemNew = ullDesiredMem;
ms.dwLength = sizeof (ms);
GlobalMemoryStatusEx (&ms);
ullUsedMem = ms.ullTotalPhys - ms.ullAvailPhys;
// if very small/no page-file (pagefile <= total RAM) and
// in-use memory + desired memory > total RAM, then
// we have to set desired memory to free RAM,
// because the OS can't page out other apps to
// reclaim enough free memory since
// there's not enough space in the pagefile
// to store the paged-out apps
if ((ms.ullTotalPageFile <= ms.ullTotalPhys) &&
(ullUsedMem + ullDesiredMem >= ms.ullTotalPhys)) {
ullDesiredMemNew = ms.ullAvailPhys;
}
return ((unsigned long) (ullDesiredMemNew >> 20));
}
/* Return 1 to print time in AM/PM format. Return 2 to print */
/* times using a 24-hour clock. */
int getDefaultTimeFormat (void)
{
char buf[10];
GetLocaleInfo (
LOCALE_USER_DEFAULT, LOCALE_ITIME, (LPTSTR) buf, sizeof (buf));
return (buf[0] == '0' ? 1 : 2);
}
/* Return TRUE if we are on battery power. */
int OnBattery (void)
{
SYSTEM_POWER_STATUS power;
// We might be able to optimize this by caching the system power status
// and only regetting it after a PBT_APMPOWERSTATUSCHANGE message.
if (GetSystemPowerStatus (&power) &&
(power.ACLineStatus != 1 ||
(power.ACLineStatus == 1 &&
power.BatteryLifePercent < BATTERY_PERCENT)))
return (TRUE);
// Return FALSE, were on AC power */
return (FALSE);
}
/* Get list of file names from the current working directory ending in .proof */
int ProofFileNames (char filenames[50][255]) // Returns number of matching filenames
{
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
int num_files;
num_files = 0;
if ((hFind = FindFirstFile ("*.proof", &FindFileData)) != INVALID_HANDLE_VALUE) do {
// Double-check the pattern matching (to match Linux code)
int len = (int) strlen (FindFileData.cFileName);
if (len > 6 && strcmp (FindFileData.cFileName + len - 6, ".proof") == 0) {
if (num_files < 50) strcpy (filenames[num_files++], FindFileData.cFileName);
}
} while (FindNextFile (hFind, &FindFileData));
FindClose (hFind);
return (num_files);
}
/* Get the character that separates directories in a pathname */
char getDirectorySeparator ()
{
return ('\\');
}