Replace LINUX macro with compiler predefined macro __linux__
[hashcat.git] / src / shared.c
index ef06013..f693122 100644 (file)
@@ -6,10 +6,15 @@
  * License.....: MIT
  */
 
-#ifdef OSX
+#ifdef __APPLE__
 #include <stdio.h>
 #endif
 
+#ifdef __FreeBSD__
+#include <stdio.h>
+#include <pthread_np.h>
+#endif
+
 #include <shared.h>
 #include <limits.h>
 
@@ -2333,15 +2338,15 @@ void drupal7_encode (u8 digest[64], u8 buf[43])
  * tty
  */
 
-#ifdef LINUX
-static struct termio savemodes;
+#ifdef __linux__
+static struct termios savemodes;
 static int havemodes = 0;
 
 int tty_break()
 {
-  struct termio modmodes;
+  struct termios modmodes;
 
-  if (ioctl (fileno (stdin), TCGETA, &savemodes) < 0) return -1;
+  if (tcgetattr (fileno (stdin), &savemodes) < 0) return -1;
 
   havemodes = 1;
 
@@ -2350,7 +2355,7 @@ int tty_break()
   modmodes.c_cc[VMIN] = 1;
   modmodes.c_cc[VTIME] = 0;
 
-  return ioctl (fileno (stdin), TCSETAW, &modmodes);
+  return tcsetattr (fileno (stdin), TCSANOW, &modmodes);
 }
 
 int tty_getchar()
@@ -2378,11 +2383,11 @@ int tty_fix()
 {
   if (!havemodes) return 0;
 
-  return ioctl (fileno (stdin), TCSETAW, &savemodes);
+  return tcsetattr (fileno (stdin), TCSADRAIN, &savemodes);
 }
 #endif
 
-#ifdef OSX
+#if defined(__APPLE__) || defined(__FreeBSD__)
 static struct termios savemodes;
 static int havemodes = 0;
 
@@ -3402,11 +3407,17 @@ int hm_get_throttle_with_device_id (const uint device_id)
     if (hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons   (data.hm_nvml, 1, data.hm_device[device_id].nvml, &clocksThrottleReasons)    != NVML_SUCCESS) return -1;
     if (hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (data.hm_nvml, 1, data.hm_device[device_id].nvml, &supportedThrottleReasons) != NVML_SUCCESS) return -1;
 
-    clocksThrottleReasons &= supportedThrottleReasons;
-
+    clocksThrottleReasons &=  supportedThrottleReasons;
+    clocksThrottleReasons &= ~nvmlClocksThrottleReasonGpuIdle;
+    clocksThrottleReasons &= ~nvmlClocksThrottleReasonApplicationsClocksSetting;
     clocksThrottleReasons &= ~nvmlClocksThrottleReasonUnknown;
 
-    return (clocksThrottleReasons > 0);
+    if (data.kernel_power_final)
+    {
+      clocksThrottleReasons &= ~nvmlClocksThrottleReasonHwSlowdown;
+    }
+
+    return (clocksThrottleReasons != nvmlClocksThrottleReasonNone);
   }
 
   return -1;
@@ -3418,33 +3429,51 @@ int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed
   {
     if (data.hm_adl)
     {
-      if (data.hm_device[device_id].od_version == 5)
+      if (fanpolicy == 1)
       {
-        ADLFanSpeedValue lpFanSpeedValue;
+        if (data.hm_device[device_id].od_version == 5)
+        {
+          ADLFanSpeedValue lpFanSpeedValue;
 
-        memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
+          memset (&lpFanSpeedValue, 0, sizeof (lpFanSpeedValue));
 
-        lpFanSpeedValue.iSize      = sizeof (lpFanSpeedValue);
-        lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
-        lpFanSpeedValue.iFlags     = (fanpolicy == 1) ? ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED : 0;
-        lpFanSpeedValue.iFanSpeed  = fanspeed;
+          lpFanSpeedValue.iSize      = sizeof (lpFanSpeedValue);
+          lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
+          lpFanSpeedValue.iFlags     = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
+          lpFanSpeedValue.iFanSpeed  = fanspeed;
 
-        if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1;
+          if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1;
 
-        return 0;
-      }
-      else // od_version == 6
-      {
-        ADLOD6FanSpeedValue fan_speed_value;
+          return 0;
+        }
+        else // od_version == 6
+        {
+          ADLOD6FanSpeedValue fan_speed_value;
 
-        memset (&fan_speed_value, 0, sizeof (fan_speed_value));
+          memset (&fan_speed_value, 0, sizeof (fan_speed_value));
 
-        fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT;
-        fan_speed_value.iFanSpeed  = fanspeed;
+          fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT;
+          fan_speed_value.iFanSpeed  = fanspeed;
 
-        if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, &fan_speed_value) != ADL_OK) return -1;
+          if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, &fan_speed_value) != ADL_OK) return -1;
 
-        return 0;
+          return 0;
+        }
+      }
+      else
+      {
+        if (data.hm_device[device_id].od_version == 5)
+        {
+          if (hm_ADL_Overdrive5_FanSpeedToDefault_Set (data.hm_adl, data.hm_device[device_id].adl, 0) != ADL_OK) return -1;
+
+          return 0;
+        }
+        else // od_version == 6
+        {
+          if (hm_ADL_Overdrive6_FanSpeed_Reset (data.hm_adl, data.hm_device[device_id].adl) != ADL_OK) return -1;
+
+          return 0;
+        }
       }
     }
   }
@@ -3458,16 +3487,27 @@ int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspe
   {
     if (data.hm_nvapi)
     {
-      NV_GPU_COOLER_LEVELS CoolerLevels = { 0 };
+      if (fanpolicy == 1)
+      {
+        NV_GPU_COOLER_LEVELS CoolerLevels;
 
-      CoolerLevels.Version = GPU_COOLER_LEVELS_VER | sizeof (NV_GPU_COOLER_LEVELS);
+        memset (&CoolerLevels, 0, sizeof (NV_GPU_COOLER_LEVELS));
 
-      CoolerLevels.Levels[0].Level  = fanspeed;
-      CoolerLevels.Levels[0].Policy = fanpolicy;
+        CoolerLevels.Version = GPU_COOLER_LEVELS_VER | sizeof (NV_GPU_COOLER_LEVELS);
 
-      if (hm_NvAPI_GPU_SetCoolerLevels (data.hm_nvapi, data.hm_device[device_id].nvapi, 0, &CoolerLevels) != NVAPI_OK) return -1;
+        CoolerLevels.Levels[0].Level  = fanspeed;
+        CoolerLevels.Levels[0].Policy = 1;
 
-      return 0;
+        if (hm_NvAPI_GPU_SetCoolerLevels (data.hm_nvapi, data.hm_device[device_id].nvapi, 0, &CoolerLevels) != NVAPI_OK) return -1;
+
+        return 0;
+      }
+      else
+      {
+        if (hm_NvAPI_GPU_RestoreCoolerSettings (data.hm_nvapi, data.hm_device[device_id].nvapi, 0) != NVAPI_OK) return -1;
+
+        return 0;
+      }
     }
   }
 
@@ -4313,7 +4353,7 @@ char *get_exec_path ()
 
   char *exec_path = (char *) mymalloc (exec_path_len);
 
-  #ifdef LINUX
+  #ifdef __linux__
 
   char tmp[32] = { 0 };
 
@@ -4325,7 +4365,7 @@ char *get_exec_path ()
 
   const int len = GetModuleFileName (NULL, exec_path, exec_path_len - 1);
 
-  #elif OSX
+  #elif __APPLE__
 
   uint size = exec_path_len;
 
@@ -4338,6 +4378,23 @@ char *get_exec_path ()
 
   const int len = strlen (exec_path);
 
+  #elif __FreeBSD__
+
+  #include <sys/sysctl.h>
+
+  int mib[4];
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC;
+  mib[2] = KERN_PROC_PATHNAME;
+  mib[3] = -1;
+
+  char tmp[32] = { 0 };
+
+  size_t size = exec_path_len;
+  sysctl(mib, 4, exec_path, &size, NULL, 0);
+
+  const int len = readlink (tmp, exec_path, exec_path_len - 1);
+
   #else
   #error Your Operating System is not supported or detected
   #endif
@@ -4462,7 +4519,7 @@ void truecrypt_crc32 (const char *filename, u8 keytab[64])
   myfree (buf);
 }
 
-#ifdef OSX
+#ifdef __APPLE__
 int pthread_setaffinity_np (pthread_t thread, size_t cpu_size, cpu_set_t *cpu_set)
 {
   int core;
@@ -4490,6 +4547,9 @@ void set_cpu_affinity (char *cpu_affinity)
 {
   #ifdef _WIN
   DWORD_PTR aff_mask = 0;
+  #elif __FreeBSD__
+  cpuset_t cpuset;
+  CPU_ZERO (&cpuset);
   #elif _POSIX
   cpu_set_t cpuset;
   CPU_ZERO (&cpuset);
@@ -4537,6 +4597,9 @@ void set_cpu_affinity (char *cpu_affinity)
   #ifdef _WIN
   SetProcessAffinityMask (GetCurrentProcess (), aff_mask);
   SetThreadAffinityMask (GetCurrentThread (), aff_mask);
+  #elif __FreeBSD__
+  pthread_t thread = pthread_self ();
+  pthread_setaffinity_np (thread, sizeof (cpuset_t), &cpuset);
   #elif _POSIX
   pthread_t thread = pthread_self ();
   pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset);
@@ -4662,8 +4725,9 @@ int sort_by_hash_t_salt_hccap (const void *v1, const void *v2)
   const salt_t *s1 = h1->salt;
   const salt_t *s2 = h2->salt;
 
-  // 16 - 2 (since last 2 uints contain the digest)
-  uint n = 14;
+  // last 2: salt_buf[10] and salt_buf[11] contain the digest (skip them)
+
+  uint n = 9; // 9 * 4 = 36 bytes (max length of ESSID)
 
   while (n--)
   {
@@ -4757,7 +4821,7 @@ int sort_by_dictstat (const void *s1, const void *s2)
   dictstat_t *d1 = (dictstat_t *) s1;
   dictstat_t *d2 = (dictstat_t *) s2;
 
-  #ifdef _LINUX
+  #ifdef __linux__
   d2->stat.st_atim = d1->stat.st_atim;
   #else
   d2->stat.st_atime = d1->stat.st_atime;
@@ -5730,11 +5794,14 @@ char **scan_directory (const char *path)
 
   if ((d = opendir (tmp_path)) != NULL)
   {
-    #ifdef OSX
+    #ifdef __APPLE__
+
     struct dirent e;
 
-    for (;;) {
+    for (;;)
+    {
       memset (&e, 0, sizeof (e));
+
       struct dirent *de = NULL;
 
       if (readdir_r (d, &e, &de) != 0)
@@ -5745,12 +5812,16 @@ char **scan_directory (const char *path)
       }
 
       if (de == NULL) break;
+
     #else
+
     struct dirent *de;
 
     while ((de = readdir (d)) != NULL)
     {
+
     #endif
+
       if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
 
       int path_size = strlen (tmp_path) + 1 + strlen (de->d_name);
@@ -6114,9 +6185,10 @@ void ascii_digest (char *out_buf, uint salt_pos, uint digest_pos)
 
   uint len = 4096;
 
-  uint digest_buf[64] = { 0 };
+  u8 datax[256] = { 0 };
 
-  u64 *digest_buf64 = (u64 *) digest_buf;
+  u64 *digest_buf64 = (u64 *) datax;
+  u32 *digest_buf   = (u32 *) datax;
 
   char *digests_buf_ptr = (char *) data.digests_buf;
 
@@ -9129,36 +9201,32 @@ void to_hccap_t (hccap_t *hccap, uint salt_pos, uint digest_pos)
 
 void SuspendThreads ()
 {
-  if (data.devices_status == STATUS_RUNNING)
-  {
-    hc_timer_set (&data.timer_paused);
+  if (data.devices_status != STATUS_RUNNING) return;
 
-    data.devices_status = STATUS_PAUSED;
+  hc_timer_set (&data.timer_paused);
 
-    log_info ("Paused");
-  }
+  data.devices_status = STATUS_PAUSED;
+
+  log_info ("Paused");
 }
 
 void ResumeThreads ()
 {
-  if (data.devices_status == STATUS_PAUSED)
-  {
-    double ms_paused;
+  if (data.devices_status != STATUS_PAUSED) return;
 
-    hc_timer_get (data.timer_paused, ms_paused);
+  double ms_paused;
 
-    data.ms_paused += ms_paused;
+  hc_timer_get (data.timer_paused, ms_paused);
 
-    data.devices_status = STATUS_RUNNING;
+  data.ms_paused += ms_paused;
 
-    log_info ("Resumed");
-  }
+  data.devices_status = STATUS_RUNNING;
+
+  log_info ("Resumed");
 }
 
 void bypass ()
 {
-  if (data.devices_status != STATUS_RUNNING) return;
-
   data.devices_status = STATUS_BYPASS;
 
   log_info ("Next dictionary / mask in queue selected, bypassing current one");
@@ -9206,17 +9274,11 @@ void stop_at_checkpoint ()
 
 void myabort ()
 {
-  if (data.devices_status == STATUS_INIT)     return;
-  if (data.devices_status == STATUS_STARTING) return;
-
   data.devices_status = STATUS_ABORTED;
 }
 
 void myquit ()
 {
-  if (data.devices_status == STATUS_INIT)     return;
-  if (data.devices_status == STATUS_STARTING) return;
-
   data.devices_status = STATUS_QUIT;
 }
 
@@ -20704,7 +20766,7 @@ BOOL WINAPI sigHandler_benchmark (DWORD sig)
   {
     case CTRL_CLOSE_EVENT:
 
-      myabort ();
+      myquit ();
 
       SetConsoleCtrlHandler (NULL, TRUE);
 
@@ -20773,7 +20835,7 @@ void *thread_keypress (void *p)
 
   tty_break();
 
-  while ((data.devices_status != STATUS_EXHAUSTED) && (data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT))
+  while (data.shutdown_outer == 0)
   {
     int ch = tty_getchar();