1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-17 04:08:34 +00:00
hashcat/src/shared.c

173 lines
2.8 KiB
C
Raw Normal View History

2015-12-04 14:47:52 +00:00
/**
* Author......: See docs/credits.txt
2015-12-04 14:47:52 +00:00
* License.....: MIT
*/
#include "common.h"
2016-09-16 15:01:18 +00:00
#include "types.h"
#include "shared.h"
2015-12-04 14:47:52 +00:00
2016-09-20 15:04:31 +00:00
bool is_power_of_2 (const u32 v)
{
return (v && !(v & (v - 1)));
}
2016-09-08 08:01:49 +00:00
u32 get_random_num (const u32 min, const u32 max)
{
if (min == max) return (min);
2016-09-30 15:41:40 +00:00
return (((u32) rand () % (max - min)) + min);
2016-09-08 08:01:49 +00:00
}
u32 mydivc32 (const u32 dividend, const u32 divisor)
{
2016-09-08 08:01:49 +00:00
u32 quotient = dividend / divisor;
2016-09-08 08:01:49 +00:00
if (dividend % divisor) quotient++;
2016-09-08 08:01:49 +00:00
return quotient;
}
2016-09-08 08:01:49 +00:00
u64 mydivc64 (const u64 dividend, const u64 divisor)
{
u64 quotient = dividend / divisor;
2016-09-08 08:01:49 +00:00
if (dividend % divisor) quotient++;
return quotient;
}
char *filename_from_filepath (char *filepath)
{
char *ptr = NULL;
if ((ptr = strrchr (filepath, '/')) != NULL)
{
ptr++;
}
else if ((ptr = strrchr (filepath, '\\')) != NULL)
{
ptr++;
}
else
{
ptr = filepath;
}
return ptr;
}
2016-09-30 15:41:40 +00:00
void naive_replace (char *s, const char key_char, const char replace_char)
2016-09-08 08:01:49 +00:00
{
const size_t len = strlen (s);
for (size_t in = 0; in < len; in++)
{
2016-09-30 15:41:40 +00:00
const char c = s[in];
2016-09-08 08:01:49 +00:00
if (c == key_char)
{
2016-09-08 08:01:49 +00:00
s[in] = replace_char;
}
}
2016-09-08 08:01:49 +00:00
}
2016-09-30 15:41:40 +00:00
void naive_escape (char *s, size_t s_max, const char key_char, const char escape_char)
2016-09-08 08:01:49 +00:00
{
char s_escaped[1024] = { 0 };
2016-09-08 08:01:49 +00:00
size_t s_escaped_max = sizeof (s_escaped);
2016-09-08 08:01:49 +00:00
const size_t len = strlen (s);
2016-09-08 08:01:49 +00:00
for (size_t in = 0, out = 0; in < len; in++, out++)
{
2016-09-30 15:41:40 +00:00
const char c = s[in];
2016-09-08 08:01:49 +00:00
if (c == key_char)
{
s_escaped[out] = escape_char;
2015-12-04 14:47:52 +00:00
2016-09-08 08:01:49 +00:00
out++;
}
2015-12-04 14:47:52 +00:00
2016-09-08 08:01:49 +00:00
if (out == s_escaped_max - 2) break;
2015-12-04 14:47:52 +00:00
2016-09-08 08:01:49 +00:00
s_escaped[out] = c;
}
strncpy (s, s_escaped, s_max - 1);
}
2015-12-04 14:47:52 +00:00
void hc_sleep_msec (const u32 msec)
2016-09-08 08:48:38 +00:00
{
#if defined (_WIN)
Sleep (msec);
#else
usleep (msec * 1000);
2016-09-08 08:48:38 +00:00
#endif
}
2016-09-08 08:01:49 +00:00
2016-09-30 15:41:40 +00:00
void hc_sleep (const u32 sec)
{
#if defined (_WIN)
Sleep (sec * 1000);
#else
2016-09-08 08:48:38 +00:00
sleep (sec);
#endif
2016-09-08 08:48:38 +00:00
}
2016-09-28 21:53:46 +00:00
void setup_environment_variables ()
{
char *compute = getenv ("COMPUTE");
if (compute)
{
static char display[100];
snprintf (display, sizeof (display) - 1, "DISPLAY=%s", compute);
putenv (display);
}
else
{
if (getenv ("DISPLAY") == NULL)
putenv ((char *) "DISPLAY=:0");
}
if (getenv ("GPU_MAX_ALLOC_PERCENT") == NULL)
putenv ((char *) "GPU_MAX_ALLOC_PERCENT=100");
if (getenv ("CPU_MAX_ALLOC_PERCENT") == NULL)
putenv ((char *) "CPU_MAX_ALLOC_PERCENT=100");
if (getenv ("GPU_USE_SYNC_OBJECTS") == NULL)
putenv ((char *) "GPU_USE_SYNC_OBJECTS=1");
if (getenv ("CUDA_CACHE_DISABLE") == NULL)
putenv ((char *) "CUDA_CACHE_DISABLE=1");
if (getenv ("POCL_KERNEL_CACHE") == NULL)
putenv ((char *) "POCL_KERNEL_CACHE=0");
}
void setup_umask ()
{
umask (077);
}
void setup_seeding (const bool rp_gen_seed_chgd, const u32 rp_gen_seed)
{
if (rp_gen_seed_chgd == true)
{
srand (rp_gen_seed);
}
else
{
time_t ts;
time (&ts);
srand (ts);
}
}