1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-27 08:58:10 +00:00

Always cleanup and initialize all parts of HC_FILE structure

This commit is contained in:
Jukka Ojanen 2021-08-18 19:28:00 +03:00
parent f3cd32bffd
commit fed32a58c7
4 changed files with 77 additions and 53 deletions

View File

@ -1073,8 +1073,6 @@ typedef struct hc_fp
gzFile gfp; // gzip fp gzFile gfp; // gzip fp
unzFile ufp; // zip fp unzFile ufp; // zip fp
bool is_gzip;
bool is_zip;
int bom_size; int bom_size;
const char *mode; const char *mode;

View File

@ -133,9 +133,15 @@ int debugfile_init (hashcat_ctx_t *hashcat_ctx)
} }
else else
{ {
debugfile_ctx->fp.is_gzip = false; HCFILE *fp = &debugfile_ctx->fp;
debugfile_ctx->fp.pfp = stdout;
debugfile_ctx->fp.fd = fileno (stdout); fp->fd = fileno (stdout);
fp->pfp = stdout;
fp->gfp = NULL;
fp->ufp = NULL;
fp->bom_size = 0;
fp->path = NULL;
fp->mode = NULL;
} }
return 0; return 0;

View File

@ -23,7 +23,16 @@ int _wopen (const char *path, int oflag, ...)
bool hc_fopen (HCFILE *fp, const char *path, const char *mode) bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
{ {
if (path == NULL || mode == NULL) return false; if (fp == NULL || path == NULL || mode == NULL) return false;
/* cleanup */
fp->fd = -1;
fp->pfp = NULL;
fp->gfp = NULL;
fp->ufp = NULL;
fp->bom_size = 0;
fp->path = NULL;
fp->mode = NULL;
int oflag = -1; int oflag = -1;
@ -60,13 +69,11 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
return false; return false;
} }
fp->pfp = NULL;
fp->is_gzip = false;
fp->is_zip = false;
fp->bom_size = 0;
unsigned char check[8] = { 0 }; unsigned char check[8] = { 0 };
bool is_gzip = false;
bool is_zip = false;
int fd_tmp = open (path, O_RDONLY); int fd_tmp = open (path, O_RDONLY);
if (fd_tmp != -1) if (fd_tmp != -1)
@ -75,12 +82,12 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
if (read (fd_tmp, check, sizeof (check)) > 0) if (read (fd_tmp, check, sizeof (check)) > 0)
{ {
if (check[0] == 0x1f && check[1] == 0x8b && check[2] == 0x08) fp->is_gzip = true; if (check[0] == 0x1f && check[1] == 0x8b && check[2] == 0x08) is_gzip = true;
if (check[0] == 0x50 && check[1] == 0x4b && check[2] == 0x03 && check[3] == 0x04) fp->is_zip = true; if (check[0] == 0x50 && check[1] == 0x4b && check[2] == 0x03 && check[3] == 0x04) is_zip = true;
// compressed files with BOM will be undetected! // compressed files with BOM will be undetected!
if (fp->is_gzip == false && fp->is_zip == false) if (is_gzip == false && is_zip == false)
{ {
fp->bom_size = hc_string_bom_size (check); fp->bom_size = hc_string_bom_size (check);
} }
@ -89,6 +96,8 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
close (fd_tmp); close (fd_tmp);
} }
if (is_zip == false)
{
if (fmode == -1) if (fmode == -1)
{ {
fp->fd = open (path, oflag); fp->fd = open (path, oflag);
@ -98,18 +107,12 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
fp->fd = open (path, oflag, fmode); fp->fd = open (path, oflag, fmode);
} }
if (fp->fd == -1 && fp->is_zip == false) return false; if (fp->fd == -1) return false;
if (fp->is_gzip) if (is_gzip)
{ {
if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false;
} }
else if (fp->is_zip)
{
if ((fp->ufp = unzOpen64 (path)) == NULL) return false;
if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false;
}
else else
{ {
if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false;
@ -123,6 +126,13 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
if (nread != fp->bom_size) return false; if (nread != fp->bom_size) return false;
} }
} }
}
else
{
if ((fp->ufp = unzOpen64 (path)) == NULL) return false;
if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false;
}
fp->path = path; fp->path = path;
fp->mode = mode; fp->mode = mode;
@ -132,7 +142,16 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode)
{ {
if (path == NULL || mode == NULL) return false; if (fp == NULL || path == NULL || mode == NULL) return false;
/* cleanup */
fp->fd = -1;
fp->pfp = NULL;
fp->gfp = NULL;
fp->ufp = NULL;
fp->bom_size = 0;
fp->path = NULL;
fp->mode = NULL;
int oflag = -1; int oflag = -1;
@ -169,11 +188,6 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode)
return false; return false;
} }
fp->pfp = NULL;
fp->is_gzip = false;
fp->is_zip = false;
fp->bom_size = 0;
if (fmode == -1) if (fmode == -1)
{ {
fp->fd = open (path, oflag); fp->fd = open (path, oflag);
@ -183,7 +197,7 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode)
fp->fd = open (path, oflag, fmode); fp->fd = open (path, oflag, fmode);
} }
if (fp->fd == -1 && fp->is_zip == false) return false; if (fp->fd == -1) return false;
if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false;
@ -597,8 +611,8 @@ void hc_fclose (HCFILE *fp)
fp->fd = -1; fp->fd = -1;
fp->pfp = NULL; fp->pfp = NULL;
fp->is_gzip = false; fp->gfp = NULL;
fp->is_zip = false; fp->ufp = NULL;
fp->path = NULL; fp->path = NULL;
fp->mode = NULL; fp->mode = NULL;

View File

@ -83,9 +83,15 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param,
} }
else else
{ {
out.fp.is_gzip = false; HCFILE *fp = &out.fp;
out.fp.pfp = stdout;
out.fp.fd = fileno (stdout); fp->fd = fileno (stdout);
fp->pfp = stdout;
fp->gfp = NULL;
fp->ufp = NULL;
fp->bom_size = 0;
fp->path = NULL;
fp->mode = NULL;
} }
out.len = 0; out.len = 0;