2016-09-11 09:42:19 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-11 09:42:19 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-12 12:58:25 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "shared.h"
|
2016-09-11 09:42:19 +00:00
|
|
|
#include "loopback.h"
|
2016-09-12 12:58:25 +00:00
|
|
|
|
2016-09-30 10:07:49 +00:00
|
|
|
void loopback_init (loopback_ctx_t *loopback_ctx, const user_options_t *user_options)
|
2016-09-12 12:58:25 +00:00
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
loopback_ctx->enabled = false;
|
|
|
|
|
|
|
|
if (user_options->benchmark == true) return;
|
|
|
|
if (user_options->keyspace == true) return;
|
|
|
|
if (user_options->left == true) return;
|
2016-09-30 11:14:11 +00:00
|
|
|
if (user_options->opencl_info == true) return;
|
2016-09-30 10:07:49 +00:00
|
|
|
if (user_options->show == true) return;
|
|
|
|
if (user_options->stdout_flag == true) return;
|
|
|
|
if (user_options->usage == true) return;
|
|
|
|
if (user_options->version == true) return;
|
|
|
|
|
|
|
|
loopback_ctx->enabled = true;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
loopback_ctx->fp = NULL;
|
|
|
|
|
|
|
|
loopback_ctx->filename = (char *) mymalloc (HCBUFSIZ_TINY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopback_destroy (loopback_ctx_t *loopback_ctx)
|
|
|
|
{
|
2016-10-01 22:00:21 +00:00
|
|
|
if (loopback_ctx->enabled == false) return;
|
2016-09-30 10:07:49 +00:00
|
|
|
|
2016-10-01 22:00:21 +00:00
|
|
|
memset (loopback_ctx, 0, sizeof (loopback_ctx_t));
|
2016-09-12 12:58:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 10:07:49 +00:00
|
|
|
int loopback_write_open (loopback_ctx_t *loopback_ctx, const induct_ctx_t *induct_ctx)
|
2016-09-12 12:58:25 +00:00
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
if (loopback_ctx->enabled == false) return 0;
|
|
|
|
|
|
|
|
if (induct_ctx->enabled == false) return 0;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time (&now);
|
|
|
|
|
|
|
|
const uint random_num = get_random_num (0, 9999);
|
|
|
|
|
2016-09-30 10:07:49 +00:00
|
|
|
snprintf (loopback_ctx->filename, HCBUFSIZ_TINY - 1, "%s/%s.%d_%u", induct_ctx->root_directory, LOOPBACK_FILE, (int) now, random_num);
|
2016-09-12 12:58:25 +00:00
|
|
|
|
|
|
|
loopback_ctx->fp = fopen (loopback_ctx->filename, "ab");
|
|
|
|
|
|
|
|
if (loopback_ctx->fp == NULL)
|
|
|
|
{
|
|
|
|
log_error ("ERROR: %s: %s", loopback_ctx->filename, strerror (errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopback_write_unlink (loopback_ctx_t *loopback_ctx)
|
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
if (loopback_ctx->enabled == false) return;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
if (loopback_ctx->filename == NULL) return;
|
|
|
|
|
|
|
|
unlink (loopback_ctx->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopback_write_close (loopback_ctx_t *loopback_ctx)
|
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
if (loopback_ctx->enabled == false) return;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
if (loopback_ctx->fp == NULL) return;
|
|
|
|
|
|
|
|
fclose (loopback_ctx->fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopback_format_plain (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len)
|
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
if (loopback_ctx->enabled == false) return;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
int needs_hexify = 0;
|
|
|
|
|
|
|
|
for (uint i = 0; i < plain_len; i++)
|
|
|
|
{
|
|
|
|
if (plain_ptr[i] < 0x20)
|
|
|
|
{
|
|
|
|
needs_hexify = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plain_ptr[i] > 0x7f)
|
|
|
|
{
|
|
|
|
needs_hexify = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needs_hexify == 1)
|
|
|
|
{
|
|
|
|
fprintf (loopback_ctx->fp, "$HEX[");
|
|
|
|
|
|
|
|
for (uint i = 0; i < plain_len; i++)
|
|
|
|
{
|
|
|
|
fprintf (loopback_ctx->fp, "%02x", plain_ptr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (loopback_ctx->fp, "]");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fwrite (plain_ptr, plain_len, 1, loopback_ctx->fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loopback_write_append (loopback_ctx_t *loopback_ctx, const u8 *plain_ptr, const unsigned int plain_len)
|
|
|
|
{
|
2016-09-30 10:07:49 +00:00
|
|
|
if (loopback_ctx->enabled == false) return;
|
|
|
|
|
2016-09-12 12:58:25 +00:00
|
|
|
FILE *fp = loopback_ctx->fp;
|
|
|
|
|
|
|
|
loopback_format_plain (loopback_ctx, plain_ptr, plain_len);
|
|
|
|
|
|
|
|
fputc ('\n', fp);
|
|
|
|
|
|
|
|
fflush (fp);
|
|
|
|
}
|