1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-11-26 09:58:16 +00:00

Move tty_*() specific functions into their own source file

This commit is contained in:
jsteube 2016-09-06 13:16:38 +02:00
parent eb5e192df5
commit 5dd5c15966
7 changed files with 305 additions and 171 deletions

16
include/logfile.h Normal file
View File

@ -0,0 +1,16 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
char *logfile_generate_topid (void);
char *logfile_generate_subid (void);
void logfile_append (const char *fmt, ...);

View File

@ -24,7 +24,6 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <pwd.h> #include <pwd.h>
#include <limits.h> #include <limits.h>
#include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif // _POSIX #endif // _POSIX

23
include/terminal.h Normal file
View File

@ -0,0 +1,23 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#pragma once
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef _POSIX
#include <termios.h>
#endif // _POSIX
#ifdef _WIN
#include <windows.h>
#endif // _WIN
int tty_break();
int tty_getchar();
int tty_fix();

View File

@ -165,7 +165,7 @@ LFLAGS_CROSS_WIN := -lpsapi
## Objects ## Objects
## ##
NATIVE_OBJS := obj/logfile.NATIVE.o obj/data.NATIVE.o obj/convert.NATIVE.o obj/memory.NATIVE.o obj/logging.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/bitops.NATIVE.o obj/bitops.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/shared.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/interface.NATIVE.o NATIVE_OBJS := obj/terminal.NATIVE.o obj/logfile.NATIVE.o obj/data.NATIVE.o obj/convert.NATIVE.o obj/memory.NATIVE.o obj/logging.NATIVE.o obj/cpu_aes.NATIVE.o obj/cpu_des.NATIVE.o obj/cpu_md5.NATIVE.o obj/cpu_sha1.NATIVE.o obj/cpu_sha256.NATIVE.o obj/bitops.NATIVE.o obj/bitops.NATIVE.o obj/ext_OpenCL.NATIVE.o obj/shared.NATIVE.o obj/rp_kernel_on_cpu.NATIVE.o obj/interface.NATIVE.o
ifeq ($(UNAME),Linux) ifeq ($(UNAME),Linux)
NATIVE_OBJS += obj/ext_ADL.NATIVE.o NATIVE_OBJS += obj/ext_ADL.NATIVE.o
@ -174,8 +174,8 @@ NATIVE_OBJS += obj/ext_nvml.NATIVE.o
NATIVE_OBJS += obj/ext_xnvctrl.NATIVE.o NATIVE_OBJS += obj/ext_xnvctrl.NATIVE.o
endif endif
LINUX_32_OBJS := obj/logfile.LINUX.32.o obj/data.LINUX.32.o obj/convert.LINUX.32.o obj/memory.LINUX.32.o obj/logging.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/bitops.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/shared.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/interface.LINUX.32.o LINUX_32_OBJS := obj/terminal.LINUX.32.o obj/logfile.LINUX.32.o obj/data.LINUX.32.o obj/convert.LINUX.32.o obj/memory.LINUX.32.o obj/logging.LINUX.32.o obj/cpu_aes.LINUX.32.o obj/cpu_des.LINUX.32.o obj/cpu_md5.LINUX.32.o obj/cpu_sha1.LINUX.32.o obj/cpu_sha256.LINUX.32.o obj/bitops.LINUX.32.o obj/ext_OpenCL.LINUX.32.o obj/shared.LINUX.32.o obj/rp_kernel_on_cpu.LINUX.32.o obj/ext_ADL.LINUX.32.o obj/ext_nvml.LINUX.32.o obj/ext_nvapi.LINUX.32.o obj/ext_xnvctrl.LINUX.32.o obj/interface.LINUX.32.o
LINUX_64_OBJS := obj/logfile.LINUX.64.o obj/data.LINUX.64.o obj/convert.LINUX.64.o obj/memory.LINUX.64.o obj/logging.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/bitops.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/shared.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/interface.LINUX.64.o LINUX_64_OBJS := obj/terminal.LINUX.64.o obj/logfile.LINUX.64.o obj/data.LINUX.64.o obj/convert.LINUX.64.o obj/memory.LINUX.64.o obj/logging.LINUX.64.o obj/cpu_aes.LINUX.64.o obj/cpu_des.LINUX.64.o obj/cpu_md5.LINUX.64.o obj/cpu_sha1.LINUX.64.o obj/cpu_sha256.LINUX.64.o obj/bitops.LINUX.64.o obj/ext_OpenCL.LINUX.64.o obj/shared.LINUX.64.o obj/rp_kernel_on_cpu.LINUX.64.o obj/ext_ADL.LINUX.64.o obj/ext_nvml.LINUX.64.o obj/ext_nvapi.LINUX.64.o obj/ext_xnvctrl.LINUX.64.o obj/interface.LINUX.64.o
# Windows CRT file globbing: # Windows CRT file globbing:
@ -185,8 +185,8 @@ CRT_GLOB_INCLUDE_FOLDER := $(dir $(lastword $(MAKEFILE_LIST)))
include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk
WIN_32_OBJS := obj/logfile.WIN.32.o obj/data.WIN.32.o obj/convert.WIN.32.o obj/memory.WIN.32.o obj/logging.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/bitops.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/shared.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/interface.WIN.32.o $(CRT_GLOB_32) WIN_32_OBJS := obj/terminal.WIN.32.o obj/logfile.WIN.32.o obj/data.WIN.32.o obj/convert.WIN.32.o obj/memory.WIN.32.o obj/logging.WIN.32.o obj/cpu_aes.WIN.32.o obj/cpu_des.WIN.32.o obj/cpu_md5.WIN.32.o obj/cpu_sha1.WIN.32.o obj/cpu_sha256.WIN.32.o obj/bitops.WIN.32.o obj/ext_OpenCL.WIN.32.o obj/shared.WIN.32.o obj/rp_kernel_on_cpu.WIN.32.o obj/ext_ADL.WIN.32.o obj/ext_nvml.WIN.32.o obj/ext_nvapi.WIN.32.o obj/ext_xnvctrl.WIN.32.o obj/interface.WIN.32.o $(CRT_GLOB_32)
WIN_64_OBJS := obj/logfile.WIN.64.o obj/data.WIN.64.o obj/convert.WIN.64.o obj/memory.WIN.64.o obj/logging.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/bitops.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/shared.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/interface.WIN.64.o $(CRT_GLOB_64) WIN_64_OBJS := obj/terminal.WIN.64.o obj/logfile.WIN.64.o obj/data.WIN.64.o obj/convert.WIN.64.o obj/memory.WIN.64.o obj/logging.WIN.64.o obj/cpu_aes.WIN.64.o obj/cpu_des.WIN.64.o obj/cpu_md5.WIN.64.o obj/cpu_sha1.WIN.64.o obj/cpu_sha256.WIN.64.o obj/bitops.WIN.64.o obj/ext_OpenCL.WIN.64.o obj/shared.WIN.64.o obj/rp_kernel_on_cpu.WIN.64.o obj/ext_ADL.WIN.64.o obj/ext_nvml.WIN.64.o obj/ext_nvapi.WIN.64.o obj/ext_xnvctrl.WIN.64.o obj/interface.WIN.64.o $(CRT_GLOB_64)
## ##
## Targets: Global ## Targets: Global

92
src/logfile.c Normal file
View File

@ -0,0 +1,92 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#include "common.h"
#include "types_int.h"
#include "ext_OpenCL.h"
#include "ext_ADL.h"
#include "ext_nvapi.h"
#include "ext_nvml.h"
#include "ext_xnvctrl.h"
#include "timer.h"
#include "types.h"
#include "memory.h"
#include "data.h"
#include "logfile.h"
extern hc_global_data_t data;
static FILE *logfile_open (char *logfile)
{
FILE *fp = fopen (logfile, "ab");
if (fp == NULL)
{
fp = stdout;
}
return fp;
}
static void logfile_close (FILE *fp)
{
if (fp == stdout) return;
fclose (fp);
}
void logfile_append (const char *fmt, ...)
{
if (data.logfile_disable == 1) return;
FILE *fp = logfile_open (data.logfile);
va_list ap;
va_start (ap, fmt);
vfprintf (fp, fmt, ap);
va_end (ap);
fputc ('\n', fp);
fflush (fp);
logfile_close (fp);
}
static int logfile_generate_id ()
{
const int n = rand ();
time_t t;
time (&t);
return t + n;
}
char *logfile_generate_topid ()
{
const int id = logfile_generate_id ();
char *topid = (char *) mymalloc (1 + 16 + 1);
snprintf (topid, 1 + 16, "TOP%08x", id);
return topid;
}
char *logfile_generate_subid ()
{
const int id = logfile_generate_id ();
char *subid = (char *) mymalloc (1 + 16 + 1);
snprintf (subid, 1 + 16, "SUB%08x", id);
return subid;
}

View File

@ -20,176 +20,12 @@
#include "timer.h" #include "timer.h"
#include "types.h" #include "types.h"
#include "rp_cpu.h" #include "rp_cpu.h"
#include "terminal.h"
#include "data.h" #include "data.h"
#include "shared.h" #include "shared.h"
extern hc_global_data_t data; extern hc_global_data_t data;
/**
* tty
*/
#ifdef __linux__
static struct termios savemodes;
static int havemodes = 0;
static int tty_break()
{
struct termios modmodes;
if (tcgetattr (fileno (stdin), &savemodes) < 0) return -1;
havemodes = 1;
modmodes = savemodes;
modmodes.c_lflag &= ~ICANON;
modmodes.c_cc[VMIN] = 1;
modmodes.c_cc[VTIME] = 0;
return tcsetattr (fileno (stdin), TCSANOW, &modmodes);
}
static int tty_getchar()
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fileno (stdin), &rfds);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int retval = select (1, &rfds, NULL, NULL, &tv);
if (retval == 0) return 0;
if (retval == -1) return -1;
return getchar();
}
static int tty_fix()
{
if (!havemodes) return 0;
return tcsetattr (fileno (stdin), TCSADRAIN, &savemodes);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
static struct termios savemodes;
static int havemodes = 0;
int tty_break()
{
struct termios modmodes;
if (ioctl (fileno (stdin), TIOCGETA, &savemodes) < 0) return -1;
havemodes = 1;
modmodes = savemodes;
modmodes.c_lflag &= ~ICANON;
modmodes.c_cc[VMIN] = 1;
modmodes.c_cc[VTIME] = 0;
return ioctl (fileno (stdin), TIOCSETAW, &modmodes);
}
int tty_getchar()
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fileno (stdin), &rfds);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int retval = select (1, &rfds, NULL, NULL, &tv);
if (retval == 0) return 0;
if (retval == -1) return -1;
return getchar();
}
int tty_fix()
{
if (!havemodes) return 0;
return ioctl (fileno (stdin), TIOCSETAW, &savemodes);
}
#endif
#ifdef WIN
static DWORD saveMode = 0;
static int tty_break()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
GetConsoleMode (stdinHandle, &saveMode);
SetConsoleMode (stdinHandle, ENABLE_PROCESSED_INPUT);
return 0;
}
static int tty_getchar()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
DWORD rc = WaitForSingleObject (stdinHandle, 1000);
if (rc == WAIT_TIMEOUT) return 0;
if (rc == WAIT_ABANDONED) return -1;
if (rc == WAIT_FAILED) return -1;
// The whole ReadConsoleInput () part is a workaround.
// For some unknown reason, maybe a mingw bug, a random signal
// is sent to stdin which unblocks WaitForSingleObject () and sets rc 0.
// Then it wants to read with getche () a keyboard input
// which has never been made.
INPUT_RECORD buf[100];
DWORD num = 0;
memset (buf, 0, sizeof (buf));
ReadConsoleInput (stdinHandle, buf, 100, &num);
FlushConsoleInputBuffer (stdinHandle);
for (uint i = 0; i < num; i++)
{
if (buf[i].EventType != KEY_EVENT) continue;
KEY_EVENT_RECORD KeyEvent = buf[i].Event.KeyEvent;
if (KeyEvent.bKeyDown != TRUE) continue;
return KeyEvent.uChar.AsciiChar;
}
return 0;
}
static int tty_fix()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
SetConsoleMode (stdinHandle, saveMode);
return 0;
}
#endif
/** /**
* system * system
*/ */

168
src/terminal.c Normal file
View File

@ -0,0 +1,168 @@
/**
* Author......: Jens Steube <jens.steube@gmail.com>
* License.....: MIT
*/
#include "common.h"
#include "terminal.h"
#ifdef __linux__
static struct termios savemodes;
static int havemodes = 0;
int tty_break()
{
struct termios modmodes;
if (tcgetattr (fileno (stdin), &savemodes) < 0) return -1;
havemodes = 1;
modmodes = savemodes;
modmodes.c_lflag &= ~ICANON;
modmodes.c_cc[VMIN] = 1;
modmodes.c_cc[VTIME] = 0;
return tcsetattr (fileno (stdin), TCSANOW, &modmodes);
}
int tty_getchar()
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fileno (stdin), &rfds);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int retval = select (1, &rfds, NULL, NULL, &tv);
if (retval == 0) return 0;
if (retval == -1) return -1;
return getchar();
}
int tty_fix()
{
if (!havemodes) return 0;
return tcsetattr (fileno (stdin), TCSADRAIN, &savemodes);
}
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
static struct termios savemodes;
static int havemodes = 0;
int tty_break()
{
struct termios modmodes;
if (ioctl (fileno (stdin), TIOCGETA, &savemodes) < 0) return -1;
havemodes = 1;
modmodes = savemodes;
modmodes.c_lflag &= ~ICANON;
modmodes.c_cc[VMIN] = 1;
modmodes.c_cc[VTIME] = 0;
return ioctl (fileno (stdin), TIOCSETAW, &modmodes);
}
int tty_getchar()
{
fd_set rfds;
FD_ZERO (&rfds);
FD_SET (fileno (stdin), &rfds);
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int retval = select (1, &rfds, NULL, NULL, &tv);
if (retval == 0) return 0;
if (retval == -1) return -1;
return getchar();
}
int tty_fix()
{
if (!havemodes) return 0;
return ioctl (fileno (stdin), TIOCSETAW, &savemodes);
}
#endif
#ifdef WIN
static DWORD saveMode = 0;
int tty_break()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
GetConsoleMode (stdinHandle, &saveMode);
SetConsoleMode (stdinHandle, ENABLE_PROCESSED_INPUT);
return 0;
}
int tty_getchar()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
DWORD rc = WaitForSingleObject (stdinHandle, 1000);
if (rc == WAIT_TIMEOUT) return 0;
if (rc == WAIT_ABANDONED) return -1;
if (rc == WAIT_FAILED) return -1;
// The whole ReadConsoleInput () part is a workaround.
// For some unknown reason, maybe a mingw bug, a random signal
// is sent to stdin which unblocks WaitForSingleObject () and sets rc 0.
// Then it wants to read with getche () a keyboard input
// which has never been made.
INPUT_RECORD buf[100];
DWORD num = 0;
memset (buf, 0, sizeof (buf));
ReadConsoleInput (stdinHandle, buf, 100, &num);
FlushConsoleInputBuffer (stdinHandle);
for (DWORD i = 0; i < num; i++)
{
if (buf[i].EventType != KEY_EVENT) continue;
KEY_EVENT_RECORD KeyEvent = buf[i].Event.KeyEvent;
if (KeyEvent.bKeyDown != TRUE) continue;
return KeyEvent.uChar.AsciiChar;
}
return 0;
}
int tty_fix()
{
HANDLE stdinHandle = GetStdHandle (STD_INPUT_HANDLE);
SetConsoleMode (stdinHandle, saveMode);
return 0;
}
#endif