You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hashcat/src/shared.c

119 lines
1.8 KiB

9 years ago
/**
* Author......: See docs/credits.txt
9 years ago
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "shared.h"
9 years ago
bool is_power_of_2 (const u32 v)
{
return (v && !(v & (v - 1)));
}
u32 get_random_num (const u32 min, const u32 max)
{
if (min == max) return (min);
return ((rand () % (max - min)) + min);
}
u32 mydivc32 (const u32 dividend, const u32 divisor)
{
u32 quotient = dividend / divisor;
if (dividend % divisor) quotient++;
return quotient;
}
u64 mydivc64 (const u64 dividend, const u64 divisor)
{
u64 quotient = dividend / divisor;
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;
}
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;
9 years ago
out++;
}
9 years ago
if (out == s_escaped_max - 2) break;
9 years ago
s_escaped[out] = c;
}
strncpy (s, s_escaped, s_max - 1);
}
9 years ago
void hc_sleep_ms (const int msec)
{
#if defined (_WIN)
Sleep (msec);
#else
usleep (msec * 1000);
#endif
}
void hc_sleep (const int sec)
{
#if defined (_WIN)
Sleep (sec * 1000);
#else
sleep (sec);
#endif
}