Increase --stdout output performance
[hashcat.git] / src / hashcat.c
index ecd2e99..1c17cd2 100644 (file)
@@ -2337,9 +2337,46 @@ static void check_cracked (hc_device_param_t *device_param, const uint salt_pos)
   }
 }
 
+// stolen from princeprocessor ;)
+
+typedef struct
+{
+  FILE *fp;
+
+  char  buf[BUFSIZ];
+  int   len;
+
+} out_t;
+
+static void out_flush (out_t *out)
+{
+  fwrite (out->buf, 1, out->len, out->fp);
+
+  out->len = 0;
+}
+
+static void out_push (out_t *out, const u8 *pw_buf, const int pw_len)
+{
+  char *ptr = out->buf + out->len;
+
+  memcpy (ptr, pw_buf, pw_len);
+
+  ptr[pw_len] = '\n';
+
+  out->len += pw_len + 1;
+
+  if (out->len >= BUFSIZ - 100)
+  {
+    out_flush (out);
+  }
+}
+
 static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
 {
-  char out_buf[HCBUFSIZ] = { 0 };
+  out_t out;
+
+  out.fp  = stdout;
+  out.len = 0;
 
   uint plain_buf[16] = { 0 };
 
@@ -2372,7 +2409,7 @@ static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
 
         if (plain_len > data.pw_max) plain_len = data.pw_max;
 
-        format_output (stdout, out_buf, plain_ptr, plain_len, 0, NULL, 0);
+        out_push (&out, plain_ptr, plain_len);
       }
     }
   }
@@ -2414,7 +2451,7 @@ static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
           if (plain_len > data.pw_max) plain_len = data.pw_max;
         }
 
-        format_output (stdout, out_buf, plain_ptr, plain_len, 0, NULL, 0);
+        out_push (&out, plain_ptr, plain_len);
       }
     }
   }
@@ -2438,7 +2475,7 @@ static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
 
         plain_len = data.css_cnt;
 
-        format_output (stdout, out_buf, plain_ptr, plain_len, 0, NULL, 0);
+        out_push (&out, plain_ptr, plain_len);
       }
     }
   }
@@ -2468,7 +2505,7 @@ static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
 
         plain_len += start + stop;
 
-        format_output (stdout, out_buf, plain_ptr, plain_len, 0, NULL, 0);
+        out_push (&out, plain_ptr, plain_len);
       }
     }
   }
@@ -2500,10 +2537,12 @@ static void process_stdout (hc_device_param_t *device_param, const uint pws_cnt)
 
         plain_len += start + stop;
 
-        format_output (stdout, out_buf, plain_ptr, plain_len, 0, NULL, 0);
+        out_push (&out, plain_ptr, plain_len);
       }
     }
   }
+
+  out_flush (&out);
 }
 
 static void save_hash ()
@@ -6773,6 +6812,8 @@ int main (int argc, char **argv)
     kernel_loops          = 1024;
     force                 = 1;
     outfile_check_timer   = 0;
+    session               = "stdout";
+    opencl_vector_width   = 1;
   }
 
   if (remove_timer_chgd == 1)
@@ -8706,7 +8747,7 @@ int main (int argc, char **argv)
                    salt_type   = SALT_TYPE_NONE;
                    attack_exec = ATTACK_EXEC_INSIDE_KERNEL;
                    opts_type   = OPTS_TYPE_PT_GENERATE_LE;
-                   kern_type   = 0;
+                   kern_type   = KERN_TYPE_STDOUT;
                    dgst_size   = DGST_SIZE_4_4;
                    parse_func  = NULL;
                    sort_by_digest = NULL;
@@ -13637,14 +13678,13 @@ int main (int argc, char **argv)
       }
     }
 
-    /**
-     * OpenCL device types:
-     *   In case the user did not specify --opencl-device-types and the user runs hashcat in a system with only a CPU only he probably want to use that CPU.
-     *   In such a case, automatically enable CPU device type support, since it's disabled by default.
-     */
-
     if (opencl_device_types == NULL)
     {
+      /**
+       * OpenCL device types:
+       *   In case the user did not specify --opencl-device-types and the user runs hashcat in a system with only a CPU only he probably want to use that CPU.
+       */
+
       cl_device_type device_types_all = 0;
 
       for (uint platform_id = 0; platform_id < platforms_cnt; platform_id++)
@@ -13667,10 +13707,23 @@ int main (int argc, char **argv)
         }
       }
 
+      // In such a case, automatically enable CPU device type support, since it's disabled by default.
+
       if ((device_types_all & (CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR)) == 0)
       {
         device_types_filter |= CL_DEVICE_TYPE_CPU;
       }
+
+      // In another case, when the user uses --stdout, using CPU devices is much faster to setup
+      // If we have a CPU device, force it to be used
+
+      if (stdout_flag == 1)
+      {
+        if (device_types_all & CL_DEVICE_TYPE_CPU)
+        {
+          device_types_filter = CL_DEVICE_TYPE_CPU;
+        }
+      }
     }
 
     /**
@@ -13749,22 +13802,21 @@ int main (int argc, char **argv)
       {
         if (machine_readable == 0)
         {
-          int len = 0;
-
           if (platform_skipped == 0)
           {
-            len = log_info ("OpenCL Platform #%u: %s", platform_id + 1, platform_vendor);
+            const int len = log_info ("OpenCL Platform #%u: %s", platform_id + 1, platform_vendor);
+
+            char line[256] = { 0 };
+
+            for (int i = 0; i < len; i++) line[i] = '=';
+
+            log_info (line);
           }
           else
           {
-            len = log_info ("OpenCL Platform #%u: %s, skipped", platform_id + 1, platform_vendor);
+            log_info ("OpenCL Platform #%u: %s, skipped", platform_id + 1, platform_vendor);
+            log_info ("");
           }
-
-          char line[256] = { 0 };
-
-          for (int i = 0; i < len; i++) line[i] = '=';
-
-          log_info (line);
         }
       }