Add --mangle switch for mangling password before hashing
[hashcat.git] / src / shared.c
index b59f0e8..8488463 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;
 
@@ -2653,7 +2658,7 @@ void lock_file (FILE *fp)
   {
     if (errno != EINTR)
     {
-      log_error ("ERROR: failed acquiring write lock: %s", strerror (errno));
+      log_error ("ERROR: Failed acquiring write lock: %s", strerror (errno));
 
       exit (-1);
     }
@@ -2751,13 +2756,13 @@ int hm_get_adapter_index_nvapi (HM_ADAPTER_NVAPI nvapiGPUHandle[DEVICES_MAX])
 {
   NvU32 pGpuCount;
 
-  if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nvapi, nvapiGPUHandle, &pGpuCount) != NVAPI_OK) return (0);
+  if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nvapi, nvapiGPUHandle, &pGpuCount) != NVAPI_OK) return 0;
 
   if (pGpuCount == 0)
   {
     log_info ("WARN: No NvAPI adapters found");
 
-    return (0);
+    return 0;
   }
 
   return (pGpuCount);
@@ -2782,7 +2787,7 @@ int hm_get_adapter_index_nvml (HM_ADAPTER_NVML nvmlGPUHandle[DEVICES_MAX])
   {
     log_info ("WARN: No NVML adapters found");
 
-    return (0);
+    return 0;
   }
 
   return (pGpuCount);
@@ -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;
+      }
     }
   }
 
@@ -3501,7 +3541,7 @@ void mp_css_to_uniq_tbl (uint css_cnt, cs_t *css, uint uniq_tbls[SP_PW_MAX][CHAR
 
   if (css_cnt > SP_PW_MAX)
   {
-    log_error ("ERROR: mask length is too long");
+    log_error ("ERROR: Mask length is too long");
 
     exit (-1);
   }
@@ -3613,7 +3653,7 @@ void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_
 
         if (in_pos == in_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", in_buf);
 
           exit (-1);
         }
@@ -3622,7 +3662,7 @@ void mp_expand (char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, int mp_
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask %s", in_buf);
+          log_error ("ERROR: Invalid hex character detected in mask %s", in_buf);
 
           exit (-1);
         }
@@ -3705,7 +3745,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
                   break;
         case '?': mp_add_cs_buf (&chr, 1, css, css_pos);
                   break;
-        default:  log_error ("ERROR: syntax error: %s", mask_buf);
+        default:  log_error ("ERROR: Syntax error: %s", mask_buf);
                   exit (-1);
       }
     }
@@ -3719,7 +3759,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
         if (mask_pos == mask_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -3730,7 +3770,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask %s", mask_buf);
+          log_error ("ERROR: Invalid hex character detected in mask %s", mask_buf);
 
           exit (-1);
         }
@@ -3753,7 +3793,7 @@ cs_t *mp_gen_css (char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, u
 
   if (css_pos == 0)
   {
-    log_error ("ERROR: invalid mask length (0)");
+    log_error ("ERROR: Invalid mask length (0)");
 
     exit (-1);
   }
@@ -3838,7 +3878,7 @@ void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, char *buf, uint index)
 
     if (len == 0)
     {
-      log_info ("WARNING: charset file corrupted");
+      log_info ("WARNING: Charset file corrupted");
 
       mp_expand (buf, strlen (buf), mp_sys, mp_usr, index, 1);
     }
@@ -3888,7 +3928,7 @@ char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
 
         if (mask_pos == mask_len)
         {
-          log_error ("ERROR: the hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
+          log_error ("ERROR: The hex-charset option always expects couples of exactly 2 hexadecimal chars, failed mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -3899,7 +3939,7 @@ char *mp_get_truncated_mask (char *mask_buf, size_t mask_len, uint len)
 
         if ((is_valid_hex_char (p0) == 0) || (is_valid_hex_char (p1) == 0))
         {
-          log_error ("ERROR: invalid hex character detected in mask: %s", mask_buf);
+          log_error ("ERROR: Invalid hex character detected in mask: %s", mask_buf);
 
           exit (-1);
         }
@@ -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);
@@ -4518,13 +4578,13 @@ void set_cpu_affinity (char *cpu_affinity)
 
       if (cpu_id > 32)
       {
-        log_error ("ERROR: invalid cpu_id %u specified", cpu_id);
+        log_error ("ERROR: Invalid cpu_id %u specified", cpu_id);
 
         exit (-1);
       }
 
       #ifdef _WIN
-      aff_mask |= 1 << (cpu_id - 1);
+      aff_mask |= 1u << (cpu_id - 1);
       #elif _POSIX
       CPU_SET ((cpu_id - 1), &cpuset);
       #endif
@@ -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);
@@ -4584,7 +4647,7 @@ int sort_by_salt (const void *v1, const void *v2)
   while (n--)
   {
     if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
-    if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
+    if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   }
 
   n = 8;
@@ -4592,10 +4655,10 @@ int sort_by_salt (const void *v1, const void *v2)
   while (n--)
   {
     if (s1->salt_buf_pc[n] > s2->salt_buf_pc[n]) return ( 1);
-    if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return (-1);
+    if (s1->salt_buf_pc[n] < s2->salt_buf_pc[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_salt_buf (const void *v1, const void *v2)
@@ -4614,7 +4677,7 @@ int sort_by_salt_buf (const void *v1, const void *v2)
   while (n--)
   {
     if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
-    if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
+    if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   }
 
   return 0;
@@ -4634,20 +4697,20 @@ int sort_by_hash_t_salt (const void *v1, const void *v2)
   while (n--)
   {
     if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
-    if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
+    if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   }
 
   /* original code, seems buggy since salt_len can be very big (had a case with 131 len)
      also it thinks salt_buf[x] is a char but its a uint so salt_len should be / 4
   if (s1->salt_len > s2->salt_len) return ( 1);
-  if (s1->salt_len < s2->salt_len) return (-1);
+  if (s1->salt_len < s2->salt_len) return -1;
 
   uint n = s1->salt_len;
 
   while (n--)
   {
     if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
-    if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
+    if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   }
   */
 
@@ -4662,13 +4725,14 @@ 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--)
   {
     if (s1->salt_buf[n] > s2->salt_buf[n]) return ( 1);
-    if (s1->salt_buf[n] < s2->salt_buf[n]) return (-1);
+    if (s1->salt_buf[n] < s2->salt_buf[n]) return -1;
   }
 
   return 0;
@@ -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;
@@ -4784,10 +4848,10 @@ int sort_by_digest_4_2 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_4 (const void *v1, const void *v2)
@@ -4800,10 +4864,10 @@ int sort_by_digest_4_4 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_5 (const void *v1, const void *v2)
@@ -4816,10 +4880,10 @@ int sort_by_digest_4_5 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_6 (const void *v1, const void *v2)
@@ -4832,10 +4896,10 @@ int sort_by_digest_4_6 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_8 (const void *v1, const void *v2)
@@ -4848,10 +4912,10 @@ int sort_by_digest_4_8 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_16 (const void *v1, const void *v2)
@@ -4864,10 +4928,10 @@ int sort_by_digest_4_16 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_32 (const void *v1, const void *v2)
@@ -4880,10 +4944,10 @@ int sort_by_digest_4_32 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_4_64 (const void *v1, const void *v2)
@@ -4896,10 +4960,10 @@ int sort_by_digest_4_64 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_8_8 (const void *v1, const void *v2)
@@ -4912,10 +4976,10 @@ int sort_by_digest_8_8 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_8_16 (const void *v1, const void *v2)
@@ -4928,10 +4992,10 @@ int sort_by_digest_8_16 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_8_25 (const void *v1, const void *v2)
@@ -4944,10 +5008,10 @@ int sort_by_digest_8_25 (const void *v1, const void *v2)
   while (n--)
   {
     if (d1[n] > d2[n]) return ( 1);
-    if (d1[n] < d2[n]) return (-1);
+    if (d1[n] < d2[n]) return -1;
   }
 
-  return (0);
+  return 0;
 }
 
 int sort_by_digest_p0p1 (const void *v1, const void *v2)
@@ -4961,15 +5025,15 @@ int sort_by_digest_p0p1 (const void *v1, const void *v2)
   const uint dgst_pos3 = data.dgst_pos3;
 
   if (d1[dgst_pos3] > d2[dgst_pos3]) return ( 1);
-  if (d1[dgst_pos3] < d2[dgst_pos3]) return (-1);
+  if (d1[dgst_pos3] < d2[dgst_pos3]) return -1;
   if (d1[dgst_pos2] > d2[dgst_pos2]) return ( 1);
-  if (d1[dgst_pos2] < d2[dgst_pos2]) return (-1);
+  if (d1[dgst_pos2] < d2[dgst_pos2]) return -1;
   if (d1[dgst_pos1] > d2[dgst_pos1]) return ( 1);
-  if (d1[dgst_pos1] < d2[dgst_pos1]) return (-1);
+  if (d1[dgst_pos1] < d2[dgst_pos1]) return -1;
   if (d1[dgst_pos0] > d2[dgst_pos0]) return ( 1);
-  if (d1[dgst_pos0] < d2[dgst_pos0]) return (-1);
+  if (d1[dgst_pos0] < d2[dgst_pos0]) return -1;
 
-  return (0);
+  return 0;
 }
 
 int sort_by_tuning_db_alias (const void *v1, const void *v2)
@@ -5163,7 +5227,7 @@ void format_output (FILE *out_fp, char *out_buf, unsigned char *plain_ptr, const
     #endif
   }
 
-  fputc ('\n', out_fp);
+  fputs (EOL, out_fp);
 }
 
 void handle_show_request (pot_t *pot, uint pot_cnt, char *input_buf, int input_len, hash_t *hashes_buf, int (*sort_by_pot) (const void *, const void *), FILE *out_fp)
@@ -5439,12 +5503,12 @@ uint setup_opencl_platforms_filter (char *opencl_platforms)
 
       if (platform < 1 || platform > 32)
       {
-        log_error ("ERROR: invalid OpenCL platform %u specified", platform);
+        log_error ("ERROR: Invalid OpenCL platform %u specified", platform);
 
         exit (-1);
       }
 
-      opencl_platforms_filter |= 1 << (platform - 1);
+      opencl_platforms_filter |= 1u << (platform - 1);
 
     } while ((next = strtok (NULL, ",")) != NULL);
 
@@ -5474,12 +5538,12 @@ u32 setup_devices_filter (char *opencl_devices)
 
       if (device_id < 1 || device_id > 32)
       {
-        log_error ("ERROR: invalid device_id %u specified", device_id);
+        log_error ("ERROR: Invalid device_id %u specified", device_id);
 
         exit (-1);
       }
 
-      devices_filter |= 1 << (device_id - 1);
+      devices_filter |= 1u << (device_id - 1);
 
     } while ((next = strtok (NULL, ",")) != NULL);
 
@@ -5509,12 +5573,12 @@ cl_device_type setup_device_types_filter (char *opencl_device_types)
 
       if (device_type < 1 || device_type > 3)
       {
-        log_error ("ERROR: invalid device_type %u specified", device_type);
+        log_error ("ERROR: Invalid device_type %u specified", device_type);
 
         exit (-1);
       }
 
-      device_types_filter |= 1 << device_type;
+      device_types_filter |= 1u << device_type;
 
     } while ((next = strtok (NULL, ",")) != NULL);
 
@@ -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);
@@ -5903,7 +5974,6 @@ char *strhashtype (const uint hash_mode)
     case   141: return ((char *) HT_00141); break;
     case   150: return ((char *) HT_00150); break;
     case   160: return ((char *) HT_00160); break;
-    case   190: return ((char *) HT_00190); break;
     case   200: return ((char *) HT_00200); break;
     case   300: return ((char *) HT_00300); break;
     case   400: return ((char *) HT_00400); break;
@@ -6115,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;
 
@@ -9130,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");
@@ -9176,7 +9243,7 @@ void stop_at_checkpoint ()
 
   if (data.restore_disable == 1)
   {
-    log_info ("WARNING: this feature is disabled when --restore-disable was specified");
+    log_info ("WARNING: This feature is disabled when --restore-disable is specified");
 
     return;
   }
@@ -9191,7 +9258,7 @@ void stop_at_checkpoint ()
 
     data.checkpoint_cur_words = get_lowest_words_done ();
 
-    log_info ("Checkpoint enabled: will quit at next Restore Point update");
+    log_info ("Checkpoint enabled: Will quit at next Restore Point update");
   }
   else
   {
@@ -9207,20 +9274,56 @@ 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;
 }
 
+void naive_replace (char *s, const u8 key_char, const u8 replace_char)
+{
+  const size_t len = strlen (s);
+
+  for (size_t in = 0; in < len; in++)
+  {
+    const u8 c = s[in];
+
+    if (c == key_char)
+    {
+      s[in] = replace_char;
+    }
+  }
+}
+
+void naive_escape (char *s, size_t s_max, const u8 key_char, const u8 escape_char)
+{
+  char s_escaped[1024] = { 0 };
+
+  size_t s_escaped_max = sizeof (s_escaped);
+
+  const size_t len = strlen (s);
+
+  for (size_t in = 0, out = 0; in < len; in++, out++)
+  {
+    const u8 c = s[in];
+
+    if (c == key_char)
+    {
+      s_escaped[out] = escape_char;
+
+      out++;
+    }
+
+    if (out == s_escaped_max - 2) break;
+
+    s_escaped[out] = c;
+  }
+
+  strncpy (s, s_escaped, s_max - 1);
+}
+
 void load_kernel (const char *kernel_file, int num_devices, size_t *kernel_lengths, const u8 **kernel_sources)
 {
   FILE *fp = fopen (kernel_file, "rb");
@@ -9297,7 +9400,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
       if (nread != 1)
       {
-        log_error ("ERROR: cannot read %s", data.eff_restore_file);
+        log_error ("ERROR: Cannot read %s", data.eff_restore_file);
 
         exit (-1);
       }
@@ -9333,7 +9436,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
           if (strcmp (argv0_r, pidbin_r) == 0)
           {
-            log_error ("ERROR: already an instance %s running on pid %d", pidbin, rd->pid);
+            log_error ("ERROR: Already an instance %s running on pid %d", pidbin, rd->pid);
 
             exit (-1);
           }
@@ -9356,7 +9459,7 @@ restore_data_t *init_restore (int argc, char **argv)
         {
           if (strcmp (pidbin, pidbin2) == 0)
           {
-            log_error ("ERROR: already an instance %s running on pid %d", pidbin2, rd->pid);
+            log_error ("ERROR: Already an instance %s running on pid %d", pidbin2, rd->pid);
 
             exit (-1);
           }
@@ -9371,7 +9474,7 @@ restore_data_t *init_restore (int argc, char **argv)
 
       if (rd->version_bin < RESTORE_MIN)
       {
-        log_error ("ERROR: cannot use outdated %s. Please remove it.", data.eff_restore_file);
+        log_error ("ERROR: Cannot use outdated %s. Please remove it.", data.eff_restore_file);
 
         exit (-1);
       }
@@ -9407,14 +9510,14 @@ void read_restore (const char *eff_restore_file, restore_data_t *rd)
 
   if (fp == NULL)
   {
-    log_error ("ERROR: restore file '%s': %s", eff_restore_file, strerror (errno));
+    log_error ("ERROR: Restore file '%s': %s", eff_restore_file, strerror (errno));
 
     exit (-1);
   }
 
   if (fread (rd, sizeof (restore_data_t), 1, fp) != 1)
   {
-    log_error ("ERROR: cannot read %s", eff_restore_file);
+    log_error ("ERROR: Can't read %s", eff_restore_file);
 
     exit (-1);
   }
@@ -9427,7 +9530,7 @@ void read_restore (const char *eff_restore_file, restore_data_t *rd)
   {
     if (fgets (buf, HCBUFSIZ - 1, fp) == NULL)
     {
-      log_error ("ERROR: cannot read %s", eff_restore_file);
+      log_error ("ERROR: Can't read %s", eff_restore_file);
 
       exit (-1);
     }
@@ -9536,13 +9639,13 @@ void cycle_restore ()
   {
     if (unlink (eff_restore_file))
     {
-      log_info ("WARN: unlink file '%s': %s", eff_restore_file, strerror (errno));
+      log_info ("WARN: Unlink file '%s': %s", eff_restore_file, strerror (errno));
     }
   }
 
   if (rename (new_restore_file, eff_restore_file))
   {
-    log_info ("WARN: rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
+    log_info ("WARN: Rename file '%s' to '%s': %s", new_restore_file, eff_restore_file, strerror (errno));
   }
 }
 
@@ -9996,7 +10099,7 @@ int bcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   char *iter_pos = input_buf + 4;
 
-  salt->salt_iter = 1 << atoi (iter_pos);
+  salt->salt_iter = 1u << atoi (iter_pos);
 
   char *salt_pos = strchr (iter_pos, '$');
 
@@ -10505,7 +10608,7 @@ int wpa_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   if (salt_len > 36)
   {
-    log_info ("WARNING: the length of the ESSID is too long. The hccap file may be invalid or corrupted");
+    log_info ("WARNING: The ESSID length is too long, the hccap file may be invalid or corrupted");
 
     return (PARSER_SALT_LENGTH);
   }
@@ -10732,7 +10835,7 @@ int phpass_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   char *iter_pos = input_buf + 3;
 
-  uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
+  uint salt_iter = 1u << itoa64_to_int (iter_pos[0]);
 
   if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
 
@@ -12005,21 +12108,6 @@ int sha1_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
   return (PARSER_OK);
 }
 
-int sha1linkedin_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
-{
-  if ((input_len < DISPLAY_LEN_MIN_100) || (input_len > DISPLAY_LEN_MAX_100)) return (PARSER_GLOBAL_LENGTH);
-
-  u32 *digest = (u32 *) hash_buf->digest;
-
-  digest[0] = hex_to_u32 ((const u8 *) &input_buf[ 0]);
-  digest[1] = hex_to_u32 ((const u8 *) &input_buf[ 8]);
-  digest[2] = hex_to_u32 ((const u8 *) &input_buf[16]);
-  digest[3] = hex_to_u32 ((const u8 *) &input_buf[24]);
-  digest[4] = hex_to_u32 ((const u8 *) &input_buf[32]);
-
-  return (PARSER_OK);
-}
-
 int sha1axcrypt_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 {
   if ((input_len < DISPLAY_LEN_MIN_13300) || (input_len > DISPLAY_LEN_MAX_13300)) return (PARSER_GLOBAL_LENGTH);
@@ -13357,7 +13445,7 @@ int sha1aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   salt->salt_sign[0] = atoi (salt_iter);
 
-  salt->salt_iter = (1 << atoi (salt_iter)) - 1;
+  salt->salt_iter = (1u << atoi (salt_iter)) - 1;
 
   hash_pos++;
 
@@ -13406,7 +13494,7 @@ int sha256aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   salt->salt_sign[0] = atoi (salt_iter);
 
-  salt->salt_iter = (1 << atoi (salt_iter)) - 1;
+  salt->salt_iter = (1u << atoi (salt_iter)) - 1;
 
   hash_pos++;
 
@@ -13458,7 +13546,7 @@ int sha512aix_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   salt->salt_sign[0] = atoi (salt_iter);
 
-  salt->salt_iter = (1 << atoi (salt_iter)) - 1;
+  salt->salt_iter = (1u << atoi (salt_iter)) - 1;
 
   hash_pos++;
 
@@ -14391,7 +14479,7 @@ int drupal7_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   char *iter_pos = input_buf + 3;
 
-  uint salt_iter = 1 << itoa64_to_int (iter_pos[0]);
+  uint salt_iter = 1u << itoa64_to_int (iter_pos[0]);
 
   if (salt_iter > 0x80000000) return (PARSER_SALT_ITERATION);
 
@@ -17845,7 +17933,7 @@ int pdf17l8_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   int enc_md = atoi (enc_md_pos);
 
-  if (enc_md != 1) return (PARSER_SALT_VALUE);
+  if ((enc_md != 0) && (enc_md != 1)) return (PARSER_SALT_VALUE);
 
   const uint id_len = atoi (id_len_pos);
   const uint u_len  = atoi (u_len_pos);
@@ -18606,7 +18694,7 @@ int sip_auth_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   if (memcmp (directive_pos, "MD5", 3))
   {
-    log_info ("ERROR: only the MD5 directive is currently supported\n");
+    log_info ("ERROR: Only the MD5 directive is currently supported\n");
 
     myfree (temp_input_buf);
 
@@ -18972,7 +19060,7 @@ int seven_zip_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   salt->salt_sign[0] = iter;
 
-  salt->salt_iter = 1 << iter;
+  salt->salt_iter = 1u << iter;
 
   /**
    * digest
@@ -19591,7 +19679,7 @@ int rar5_parse_hash (char *input_buf, uint input_len, hash_t *hash_buf)
 
   salt->salt_sign[0] = iterations;
 
-  salt->salt_iter = ((1 << iterations) + 32) - 1;
+  salt->salt_iter = ((1u << iterations) + 32) - 1;
 
   /**
    * digest buf
@@ -20678,7 +20766,7 @@ BOOL WINAPI sigHandler_benchmark (DWORD sig)
   {
     case CTRL_CLOSE_EVENT:
 
-      myabort ();
+      myquit ();
 
       SetConsoleCtrlHandler (NULL, TRUE);
 
@@ -20743,13 +20831,11 @@ void status_display ();
 
 void *thread_keypress (void *p)
 {
-  int benchmark = *((int *) p);
-
   uint quiet = data.quiet;
 
   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();
 
@@ -20826,8 +20912,6 @@ void *thread_keypress (void *p)
 
         log_info ("");
 
-        if (benchmark == 1) break;
-
         stop_at_checkpoint ();
 
         log_info ("");
@@ -20841,14 +20925,7 @@ void *thread_keypress (void *p)
 
         log_info ("");
 
-        if (benchmark == 1)
-        {
-          myquit ();
-        }
-        else
-        {
-          myabort ();
-        }
+        myabort ();
 
         break;
     }
@@ -21062,11 +21139,12 @@ int cpu_rule_to_kernel_rule (char *rule_buf, uint rule_len, kernel_rule_t *rule)
         break;
 
       case RULE_OP_MANGLE_PURGECHAR:
-        return (-1);
+        SET_NAME (rule, rule_buf[rule_pos]);
+        SET_P0   (rule, rule_buf[rule_pos]);
         break;
 
       case RULE_OP_MANGLE_TOGGLECASE_REC:
-        return (-1);
+        return -1;
         break;
 
       case RULE_OP_MANGLE_DUPECHAR_FIRST:
@@ -21142,14 +21220,14 @@ int cpu_rule_to_kernel_rule (char *rule_buf, uint rule_len, kernel_rule_t *rule)
         break;
 
       default:
-        return (-1);
+        return -1;
         break;
     }
   }
 
-  if (rule_pos < rule_len) return (-1);
+  if (rule_pos < rule_len) return -1;
 
-  return (0);
+  return 0;
 }
 
 int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
@@ -21281,11 +21359,12 @@ int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
         break;
 
       case RULE_OP_MANGLE_PURGECHAR:
-        return (-1);
+        rule_buf[rule_pos] = rule_cmd;
+        GET_P0 (rule);
         break;
 
       case RULE_OP_MANGLE_TOGGLECASE_REC:
-        return (-1);
+        return -1;
         break;
 
       case RULE_OP_MANGLE_DUPECHAR_FIRST:
@@ -21365,7 +21444,7 @@ int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
         break;
 
       default:
-        return (-1);
+        return -1;
         break;
     }
   }
@@ -21375,7 +21454,7 @@ int kernel_rule_to_cpu_rule (char *rule_buf, kernel_rule_t *rule)
     return rule_pos;
   }
 
-  return (-1);
+  return -1;
 }
 
 /**