added more keys (@equipter)
[RRG-proxmark3.git] / tools / mf_nonce_brute / util_posix.c
blob050040a32e1d593e4301008fafbbd0cf60fc132d
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // utilities requiring Posix library functions
9 //-----------------------------------------------------------------------------
11 // ensure availability even with -std=c99; must be included before
12 #if !defined(_WIN32)
13 //#define _POSIX_C_SOURCE 199309L // need nanosleep()
14 #define _POSIX_C_SOURCE 200112L // need localtime_r()
15 #else
16 #include <windows.h>
17 #endif
19 #include "util_posix.h"
20 #include <stdint.h>
21 #include <time.h>
24 // Timer functions
25 #if !defined (_WIN32)
26 #include <errno.h>
28 static void nsleep(uint64_t n) {
29 struct timespec timeout;
30 timeout.tv_sec = n / 1000000000;
31 timeout.tv_nsec = n % 1000000000;
32 while (nanosleep(&timeout, &timeout) && errno == EINTR);
35 void msleep(uint32_t n) {
36 nsleep(1000000 * (uint64_t)n);
38 #endif // _WIN32
40 #ifdef __APPLE__
42 #ifndef CLOCK_MONOTONIC
43 #define CLOCK_MONOTONIC (1)
44 #endif
45 #ifndef CLOCK_REALTIME
46 #define CLOCK_REALTIME (2)
47 #endif
49 #include <sys/time.h>
50 #include <mach/clock.h>
51 #include <mach/mach.h>
52 #include <mach/mach_time.h>
54 /* clock_gettime is not implemented on OSX prior to 10.12 */
55 int _civet_clock_gettime(int clk_id, struct timespec *t);
57 int _civet_clock_gettime(int clk_id, struct timespec *t) {
58 memset(t, 0, sizeof(*t));
59 if (clk_id == CLOCK_REALTIME) {
60 struct timeval now;
61 int rv = gettimeofday(&now, NULL);
62 if (rv) {
63 return rv;
65 t->tv_sec = now.tv_sec;
66 t->tv_nsec = now.tv_usec * 1000;
67 return 0;
69 } else if (clk_id == CLOCK_MONOTONIC) {
70 static uint64_t clock_start_time = 0;
71 static mach_timebase_info_data_t timebase_info = {0, 0};
73 uint64_t now = mach_absolute_time();
75 if (clock_start_time == 0) {
77 mach_timebase_info(&timebase_info);
78 clock_start_time = now;
81 now = (uint64_t)((double)(now - clock_start_time)
82 * (double)timebase_info.numer
83 / (double)timebase_info.denom);
85 t->tv_sec = now / 1000000000;
86 t->tv_nsec = now % 1000000000;
87 return 0;
89 return -1; // EINVAL - Clock ID is unknown
92 /* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */
93 #ifdef __CLOCK_AVAILABILITY
94 /* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared
95 * but it may be NULL at runtime. So we need to check before using it. */
96 int _civet_safe_clock_gettime(int clk_id, struct timespec *t);
98 int _civet_safe_clock_gettime(int clk_id, struct timespec *t) {
99 if (clock_gettime) {
100 return clock_gettime(clk_id, t);
102 return _civet_clock_gettime(clk_id, t);
104 #define clock_gettime _civet_safe_clock_gettime
105 #else
106 #define clock_gettime _civet_clock_gettime
107 #endif
109 #endif
112 // a milliseconds timer for performance measurement
113 uint64_t msclock(void) {
114 #if defined(_WIN32)
115 #include <sys/types.h>
117 // WORKAROUND FOR MinGW (some versions - use if normal code does not compile)
118 // It has no _ftime_s and needs explicit inclusion of timeb.h
119 #include <sys/timeb.h>
120 struct _timeb t;
121 _ftime(&t);
122 return 1000 * (uint64_t)t.time + t.millitm;
124 // NORMAL CODE (use _ftime_s)
125 //struct _timeb t;
126 //if (_ftime_s(&t)) {
127 // return 0;
128 //} else {
129 // return 1000 * t.time + t.millitm;
131 #else
132 struct timespec t;
133 clock_gettime(CLOCK_MONOTONIC, &t);
134 return (1000 * (uint64_t)t.tv_sec + t.tv_nsec / 1000000);
135 #endif