Avoid directly accessing file handle

pull/2943/head
Jukka Ojanen 3 years ago
parent 85854236d1
commit 3bc7b6af90

@ -22,11 +22,12 @@ int hc_fprintf (HCFILE *fp, const char *format, ...);
int hc_vfprintf (HCFILE *fp, const char *format, va_list ap); int hc_vfprintf (HCFILE *fp, const char *format, va_list ap);
int hc_fseek (HCFILE *fp, off_t offset, int whence); int hc_fseek (HCFILE *fp, off_t offset, int whence);
void hc_rewind (HCFILE *fp); void hc_rewind (HCFILE *fp);
int hc_fstat (HCFILE *fp, struct stat *buf);
off_t hc_ftell (HCFILE *fp); off_t hc_ftell (HCFILE *fp);
int hc_fgetc (HCFILE *fp); int hc_fgetc (HCFILE *fp);
int hc_fileno (HCFILE *fp);
int hc_feof (HCFILE *fp); int hc_feof (HCFILE *fp);
void hc_fflush (HCFILE *fp); void hc_fflush (HCFILE *fp);
void hc_fsync (HCFILE *fp);
void hc_fclose (HCFILE *fp); void hc_fclose (HCFILE *fp);
int hc_fputc (int c, HCFILE *fp); int hc_fputc (int c, HCFILE *fp);
char *hc_fgets (char *buf, int len, HCFILE *fp); char *hc_fgets (char *buf, int len, HCFILE *fp);

@ -96,19 +96,19 @@ 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); else
} {
else fp->fd = open (path, oflag, fmode);
{ }
fp->fd = open (path, oflag, fmode);
}
if (fp->fd == -1) return false; if (fp->fd == -1) return false;
if (is_zip == false)
{
if (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;
@ -389,6 +389,13 @@ void hc_rewind (HCFILE *fp)
} }
} }
int hc_fstat (HCFILE *fp, struct stat *buf)
{
if (fp == NULL || buf == NULL || fp->fd == -1) return -1;
return fstat (fp->fd, buf);
}
off_t hc_ftell (HCFILE *fp) off_t hc_ftell (HCFILE *fp)
{ {
off_t n = 0; off_t n = 0;
@ -544,13 +551,6 @@ int hc_fscanf (HCFILE *fp, const char *format, void *ptr)
return 1; return 1;
} }
int hc_fileno (HCFILE *fp)
{
if (fp == NULL) return 1;
return fp->fd;
}
int hc_feof (HCFILE *fp) int hc_feof (HCFILE *fp)
{ {
int r = -1; int r = -1;
@ -590,6 +590,22 @@ void hc_fflush (HCFILE *fp)
} }
} }
void hc_fsync (HCFILE *fp)
{
if (fp == NULL) return;
if (fp->pfp)
{
#if defined (_WIN)
HANDLE h = (HANDLE) _get_osfhandle (fp->fd);
FlushFileBuffers (h);
#else
fsync (fp->fd);
#endif
}
}
void hc_fclose (HCFILE *fp) void hc_fclose (HCFILE *fp)
{ {
if (fp == NULL) return; if (fp == NULL) return;
@ -603,6 +619,8 @@ void hc_fclose (HCFILE *fp)
unzCloseCurrentFile (fp->ufp); unzCloseCurrentFile (fp->ufp);
unzClose (fp->ufp); unzClose (fp->ufp);
close (fp->fd);
} }
else if (fp->pfp) else if (fp->pfp)
{ {

@ -21,7 +21,7 @@ int hc_lockfile (HCFILE *fp)
lock.l_type = F_WRLCK; lock.l_type = F_WRLCK;
/* Needs this loop because a signal may interrupt a wait for lock */ /* Needs this loop because a signal may interrupt a wait for lock */
while (fcntl (hc_fileno (fp), F_SETLKW, &lock)) while (fcntl (fp->fd, F_SETLKW, &lock))
{ {
if (errno != EINTR) return -1; if (errno != EINTR) return -1;
} }
@ -39,7 +39,7 @@ int hc_unlockfile (HCFILE *fp)
lock.l_type = F_UNLCK; lock.l_type = F_UNLCK;
if (fcntl (hc_fileno (fp), F_SETLK, &lock)) return -1; if (fcntl (fp->fd, F_SETLK, &lock)) return -1;
return 0; return 0;
} }

@ -163,7 +163,7 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx)
struct stat outfile_stat; struct stat outfile_stat;
if (fstat (hc_fileno (&fp), &outfile_stat)) if (hc_fstat (&fp, &outfile_stat))
{ {
hc_fclose (&fp); hc_fclose (&fp);

@ -13,15 +13,6 @@
#include "folder.h" #include "folder.h"
#include "restore.h" #include "restore.h"
#if defined (_WIN)
static void fsync (int fd)
{
HANDLE h = (HANDLE) _get_osfhandle (fd);
FlushFileBuffers (h);
}
#endif
static int init_restore (hashcat_ctx_t *hashcat_ctx) static int init_restore (hashcat_ctx_t *hashcat_ctx)
{ {
restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx; restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
@ -232,7 +223,7 @@ static int write_restore (hashcat_ctx_t *hashcat_ctx)
hc_fflush (&fp); hc_fflush (&fp);
fsync (hc_fileno (&fp)); hc_fsync (&fp);
hc_fclose (&fp); hc_fclose (&fp);

@ -693,7 +693,7 @@ bool hc_same_files (char *file1, char *file2)
if (hc_fopen (&fp, file1, "r") == true) if (hc_fopen (&fp, file1, "r") == true)
{ {
if (fstat (hc_fileno (&fp), &tmpstat_file1)) if (hc_fstat (&fp, &tmpstat_file1))
{ {
hc_fclose (&fp); hc_fclose (&fp);
@ -707,7 +707,7 @@ bool hc_same_files (char *file1, char *file2)
if (hc_fopen (&fp, file2, "r") == true) if (hc_fopen (&fp, file2, "r") == true)
{ {
if (fstat (hc_fileno (&fp), &tmpstat_file2)) if (hc_fstat (&fp, &tmpstat_file2))
{ {
hc_fclose (&fp); hc_fclose (&fp);

@ -343,7 +343,7 @@ int count_words (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, const char *dictfile, u
memset (&d, 0, sizeof (d)); memset (&d, 0, sizeof (d));
if (fstat (hc_fileno (fp), &d.stat)) if (hc_fstat (fp, &d.stat))
{ {
*result = 0; *result = 0;

Loading…
Cancel
Save