-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutil.cpp
More file actions
333 lines (266 loc) · 7.69 KB
/
Copy pathutil.cpp
File metadata and controls
333 lines (266 loc) · 7.69 KB
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
#include <Rcpp.h>
#if defined(_WIN32)
// See: http://stackoverflow.com/questions/11588765/using-rcpp-with-windows-specific-includes
#undef Realloc
#undef Free
#include <Windows.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
#endif
#include <sys/time.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
extern "C" {
#include "scrypt_platform.h"
#include "crypto/crypto_scrypt.h"
}
#include "util.hpp"
#ifdef HAVE_CLOCK_GETTIME
static clockid_t clocktouse;
static int getclockres(double *resd)
{
struct timespec res;
/*
* Try clocks in order of preference until we find one which works.
* (We assume that if clock_getres works, clock_gettime will, too.)
* The use of if/else/if/else/if/else rather than if/elif/elif/else
* is ugly but legal, and allows us to #ifdef things appropriately.
*/
#ifdef CLOCK_VIRTUAL
if (clock_getres(CLOCK_VIRTUAL, &res) == 0)
clocktouse = CLOCK_VIRTUAL;
else
#endif
#ifdef CLOCK_MONOTONIC
if (clock_getres(CLOCK_MONOTONIC, &res) == 0)
clocktouse = CLOCK_MONOTONIC;
else
#endif
if (clock_getres(CLOCK_REALTIME, &res) == 0)
clocktouse = CLOCK_REALTIME;
else
return (-1);
/* Convert clock resolution to a double. */
*resd = res.tv_sec + res.tv_nsec * 0.000000001;
return (0);
}
static int getclocktime(struct timespec *ts)
{
#ifdef DEBUG
REprintf("Using clock_gettime()\n");
#endif
if (clock_gettime(clocktouse, ts))
return (-1);
return (0);
}
#else
static int getclockres(double *resd)
{
#ifdef DEBUG
REprintf("Using gettimeofday()\n");
#endif
*resd = 1.0 / CLOCKS_PER_SEC;
return (0);
}
static int getclocktime(struct timespec *ts)
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
return (-1);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
return (0);
}
#endif
static int getclockdiff(struct timespec * st, double * diffd)
{
struct timespec en;
if (getclocktime(&en))
return (1);
*diffd = (en.tv_nsec - st->tv_nsec) * 0.000000001 +
(en.tv_sec - st->tv_sec);
return (0);
}
/*
* Get CPU performance
*
* This function is derived from Colin Percival's scrypt reference code
*/
int getcpuperf(double *opps)
{
struct timespec st;
double resd, diffd;
uint64_t i = 0;
uint8_t salt[32] = {0};
/* Get the clock resolution. */
if (getclockres(&resd))
return (2);
#ifdef DEBUG
REprintf("Clock resolution is %f\n", resd);
#endif
/* Loop until the clock ticks. */
if (getclocktime(&st))
return (2);
do {
/* Do an scrypt. */
if (crypto_scrypt(NULL, 0, salt, 0, 16, 1, 1, NULL, 0))
return (3);
/* Has the clock ticked? */
if (getclockdiff(&st, &diffd))
return (2);
if (diffd > 0)
break;
} while (1);
/* Could how many scryps we can do before the next tick. */
if (getclocktime(&st))
return (2);
do {
/* Do an scrypt. */
if (crypto_scrypt(NULL, 0, salt, 0, 128, 1, 1, NULL, 0))
return (3);
/* We invoked the salsa20/8 core 512 times. */
i += 512;
/* Check if we have looped for long enough. */
if (getclockdiff(&st, &diffd))
return (2);
if (diffd > resd)
break;
} while (1);
#ifdef DEBUG
REprintf("%ju salsa20/8 cores performed in %f seconds\n",
(uintmax_t)i, diffd);
#endif
/* We can do approximately i salsa20/8 cores per diffd seconds. */
*opps = i / diffd;
return (0);
}
/*
* Get available memory
*
* This function is derived from:
* http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system
*/
int getmemlimit(size_t *memlimit) {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(__MINGW__) || defined(__MINGW32__) )
/* Cygwin under Windows. ------------------------------------ */
/* New 64-bit MEMORYSTATUSEX isn't available. Use old 32.bit */
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
*memlimit = (size_t)status.dwTotalPhys;
return 0;
#elif defined(_WIN32)
/* Windows. ------------------------------------------------- */
/* Use new 64-bit MEMORYSTATUSEX, not old 32-bit MEMORYSTATUS */
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
*memlimit = (size_t)status.ullTotalPhys;
return 0;
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* UNIX variants. ------------------------------------------- */
#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_MEMSIZE)
mib[1] = HW_MEMSIZE; // OSX
#elif defined(HW_PHYSMEM64)
mib[1] = HW_PHYSMEM64; // NetBSD, OpenBSD
#endif
int64_t size = 0; // 64-bit
size_t len = sizeof( size );
if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
*memlimit = (size_t)size;
return 0;
}
return -1; // Failure
#elif defined(_SC_AIX_REALMEM)
/* AIX. ----------------------------------------------------- */
*memlimit = (size_t)sysconf( _SC_AIX_REALMEM ) * (size_t)1024L;
return 0;
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
/* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
*memlimit = (size_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGESIZE );
return 0;
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
/* Legacy. -------------------------------------------------- */
*memlimit = (size_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGE_SIZE );
return 0;
#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
/* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_REALMEM)
mib[1] = HW_REALMEM; // FreeBSD
#elif defined(HW_PYSMEM)
mib[1] = HW_PHYSMEM; // Others
#endif
unsigned int size = 0; // 32-bit
size_t len = sizeof( size );
if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) {
*memlimit = (size_t)size;
return 0;
}
return -1; // Failure
#endif
#else
return -2; // Unknown OS
#endif
}
/*
* Obtains salt for password hash.
* This function is derived from Colin Percival's scrypt reference code
*/
int getsalt(uint8_t salt[32]) {
uint8_t *buf = salt;
size_t buflen = 32;
#if defined(_WIN32)
HCRYPTPROV hCryptCtx;
if (CryptAcquireContext(&hCryptCtx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
if (!CryptGenRandom(hCryptCtx, buflen, buf))
goto err;
CryptReleaseContext(hCryptCtx, 0);
} else {
goto err;
}
/* Success! */
return (0);
#else
int fd;
ssize_t lenread;
/* Open /dev/urandom */
if ((fd = open("/dev/urandom", O_RDONLY)) == -1)
goto err;
/* Read bytes until we have filled the buffer */
while (buflen > 0) {
if ((lenread = read(fd, buf, buflen)) == -1)
goto close;
/* The random device should never EOF */
if (lenread == 0)
goto close;
/* We're partly done */
buf += lenread;
buflen -= lenread;
}
/* Close the device */
while (close(fd) == -1) {
if (errno != EINTR)
goto err;
}
/* Success! */
return (0);
close:
close(fd);
#endif
err:
/* Failure! */
return (4);
}