diff --git a/include/types.h b/include/types.h index 7dbc41503..cda30cefb 100644 --- a/include/types.h +++ b/include/types.h @@ -1073,8 +1073,6 @@ typedef struct hc_fp gzFile gfp; // gzip fp unzFile ufp; // zip fp - bool is_gzip; - bool is_zip; int bom_size; const char *mode; diff --git a/src/debugfile.c b/src/debugfile.c index aaaed1822..b23fad791 100644 --- a/src/debugfile.c +++ b/src/debugfile.c @@ -133,9 +133,15 @@ int debugfile_init (hashcat_ctx_t *hashcat_ctx) } else { - debugfile_ctx->fp.is_gzip = false; - debugfile_ctx->fp.pfp = stdout; - debugfile_ctx->fp.fd = fileno (stdout); + HCFILE *fp = &debugfile_ctx->fp; + + 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; diff --git a/src/filehandling.c b/src/filehandling.c index 8ba626aff..f23c13024 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -23,7 +23,16 @@ int _wopen (const char *path, int oflag, ...) 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; @@ -60,13 +69,11 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) return false; } - fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; - fp->bom_size = 0; - unsigned char check[8] = { 0 }; + bool is_gzip = false; + bool is_zip = false; + int fd_tmp = open (path, O_RDONLY); 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 (check[0] == 0x1f && check[1] == 0x8b && check[2] == 0x08) fp->is_gzip = true; - if (check[0] == 0x50 && check[1] == 0x4b && check[2] == 0x03 && check[3] == 0x04) fp->is_zip = 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) is_zip = true; // 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); } @@ -89,39 +96,42 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) close (fd_tmp); } - if (fmode == -1) - { - fp->fd = open (path, oflag); - } - else + if (is_zip == false) { - fp->fd = open (path, oflag, fmode); - } + if (fmode == -1) + { + fp->fd = open (path, oflag); + } + else + { + 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 ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; - } - else if (fp->is_zip) - { - if ((fp->ufp = unzOpen64 (path)) == NULL) return false; + if (is_gzip) + { + if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; + } + else + { + if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; - if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false; + if (fp->bom_size) + { + // atm just skip bom + + const int nread = fread (check, sizeof (char), fp->bom_size, fp->pfp); + + if (nread != fp->bom_size) return false; + } + } } else { - if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; - - if (fp->bom_size) - { - // atm just skip bom - - const int nread = fread (check, sizeof (char), fp->bom_size, fp->pfp); + if ((fp->ufp = unzOpen64 (path)) == NULL) return false; - if (nread != fp->bom_size) return false; - } + if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false; } fp->path = path; @@ -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) { - 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; @@ -169,11 +188,6 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) return false; } - fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; - fp->bom_size = 0; - if (fmode == -1) { fp->fd = open (path, oflag); @@ -183,9 +197,9 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) 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; fp->path = path; fp->mode = mode; @@ -597,8 +611,8 @@ void hc_fclose (HCFILE *fp) fp->fd = -1; fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; + fp->gfp = NULL; + fp->ufp = NULL; fp->path = NULL; fp->mode = NULL; diff --git a/src/stdout.c b/src/stdout.c index 1fd7542bc..1d52881cd 100644 --- a/src/stdout.c +++ b/src/stdout.c @@ -83,9 +83,15 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, } else { - out.fp.is_gzip = false; - out.fp.pfp = stdout; - out.fp.fd = fileno (stdout); + HCFILE *fp = &out.fp; + + 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;