2016-09-06 17:44:27 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-06 17:44:27 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-09-16 15:01:18 +00:00
|
|
|
#include "types.h"
|
2016-09-06 17:44:27 +00:00
|
|
|
#include "memory.h"
|
2017-07-06 08:35:25 +00:00
|
|
|
#include "shared.h"
|
2016-09-06 17:44:27 +00:00
|
|
|
#include "filehandling.h"
|
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
#if defined (__CYGWIN__)
|
|
|
|
// workaround for zlib with cygwin build
|
2019-07-29 16:42:50 +00:00
|
|
|
int _wopen (const char *path, int oflag, ...)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2019-07-12 22:20:41 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start (ap, oflag);
|
|
|
|
int r = open (path, oflag, ap);
|
|
|
|
va_end (ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2021-07-18 19:27:21 +00:00
|
|
|
bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
|
2019-07-12 22:20:41 +00:00
|
|
|
{
|
|
|
|
if (path == NULL || mode == NULL) return false;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
int oflag = -1;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
int fmode = S_IRUSR|S_IWUSR;
|
|
|
|
|
|
|
|
if (strncmp (mode, "a", 1) == 0 || strncmp (mode, "ab", 2) == 0)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2019-07-12 22:20:41 +00:00
|
|
|
oflag = O_WRONLY | O_CREAT | O_APPEND;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "ab", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (strncmp (mode, "r", 1) == 0 || strncmp (mode, "rb", 2) == 0)
|
|
|
|
{
|
|
|
|
oflag = O_RDONLY;
|
|
|
|
fmode = -1;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "rb", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (strncmp (mode, "w", 1) == 0 || strncmp (mode, "wb", 2) == 0)
|
|
|
|
{
|
|
|
|
oflag = O_WRONLY | O_CREAT | O_TRUNC;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "wb", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ADD more strncmp to handle more "mode"
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2021-06-29 18:42:22 +00:00
|
|
|
fp->pfp = NULL;
|
|
|
|
fp->is_gzip = false;
|
|
|
|
fp->is_zip = false;
|
|
|
|
fp->bom_size = 0;
|
2019-07-12 22:20:41 +00:00
|
|
|
|
2021-06-29 18:42:22 +00:00
|
|
|
unsigned char check[8] = { 0 };
|
2019-07-12 22:20:41 +00:00
|
|
|
|
|
|
|
int fd_tmp = open (path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd_tmp != -1)
|
|
|
|
{
|
|
|
|
lseek (fd_tmp, 0, SEEK_SET);
|
|
|
|
|
2020-06-02 07:52:10 +00:00
|
|
|
if (read (fd_tmp, check, sizeof (check)) > 0)
|
2019-07-12 22:20:41 +00:00
|
|
|
{
|
2021-06-29 18:42:22 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
// compressed files with BOM will be undetected!
|
|
|
|
|
|
|
|
if ((fp->is_gzip == false) && (fp->is_zip == false))
|
|
|
|
{
|
|
|
|
fp->bom_size = hc_string_bom_size (check);
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
|
|
|
|
close (fd_tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmode == -1)
|
|
|
|
{
|
|
|
|
fp->fd = open (path, oflag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fp->fd = open (path, oflag, fmode);
|
|
|
|
}
|
|
|
|
|
2019-07-29 16:42:50 +00:00
|
|
|
if (fp->fd == -1 && fp->is_zip == false) return false;
|
2019-07-12 22:20:41 +00:00
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false;
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
if ((fp->ufp = unzOpen64 (path)) == NULL) return false;
|
|
|
|
|
|
|
|
if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false;
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
2021-06-29 18:42:22 +00:00
|
|
|
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 (nread != fp->bom_size) return false;
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fp->path = path;
|
|
|
|
fp->mode = mode;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-18 19:27:21 +00:00
|
|
|
bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode)
|
2021-05-29 11:27:05 +00:00
|
|
|
{
|
|
|
|
if (path == NULL || mode == NULL) return false;
|
|
|
|
|
|
|
|
int oflag = -1;
|
|
|
|
|
|
|
|
int fmode = S_IRUSR|S_IWUSR;
|
|
|
|
|
|
|
|
if (strncmp (mode, "a", 1) == 0 || strncmp (mode, "ab", 2) == 0)
|
|
|
|
{
|
|
|
|
oflag = O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "ab", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (strncmp (mode, "r", 1) == 0 || strncmp (mode, "rb", 2) == 0)
|
|
|
|
{
|
|
|
|
oflag = O_RDONLY;
|
|
|
|
fmode = -1;
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "rb", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (strncmp (mode, "w", 1) == 0 || strncmp (mode, "wb", 2) == 0)
|
|
|
|
{
|
|
|
|
oflag = O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
if (strncmp (mode, "wb", 2) == 0) oflag |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ADD more strncmp to handle more "mode"
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:42:22 +00:00
|
|
|
fp->pfp = NULL;
|
|
|
|
fp->is_gzip = false;
|
|
|
|
fp->is_zip = false;
|
|
|
|
fp->bom_size = 0;
|
2021-05-29 11:27:05 +00:00
|
|
|
|
|
|
|
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->pfp = fdopen (fp->fd, mode)) == NULL) return false;
|
|
|
|
|
|
|
|
fp->path = path;
|
|
|
|
fp->mode = mode;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
size_t hc_fread (void *ptr, size_t size, size_t nmemb, HCFILE *fp)
|
|
|
|
{
|
|
|
|
size_t n = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return n;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
n = gzfread (ptr, size, nmemb, fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
unsigned s = size * nmemb;
|
|
|
|
|
|
|
|
n = unzReadCurrentFile (fp->ufp, ptr, s);
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-02 07:52:10 +00:00
|
|
|
#if defined (_WIN)
|
|
|
|
|
|
|
|
// 4 GB fread () limit for windows systems ?
|
|
|
|
// see: https://social.msdn.microsoft.com/Forums/vstudio/en-US/7c913001-227e-439b-bf07-54369ba07994/fwrite-issues-with-large-data-write
|
|
|
|
|
|
|
|
#define GIGABYTE (1024u * 1024u * 1024u)
|
|
|
|
|
|
|
|
if (((size * nmemb) / GIGABYTE) < 4)
|
|
|
|
{
|
|
|
|
n = fread (ptr, size, nmemb, fp->pfp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((size / GIGABYTE) > 3) return -1;
|
|
|
|
|
|
|
|
size_t elements_max = (3u * GIGABYTE) / size;
|
|
|
|
size_t elements_left = nmemb;
|
|
|
|
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
while (elements_left > 0)
|
|
|
|
{
|
|
|
|
size_t elements_cur = elements_max;
|
|
|
|
|
|
|
|
if (elements_left < elements_max) elements_cur = elements_left;
|
|
|
|
|
|
|
|
size_t ret = fread (ptr + off, size, elements_cur, fp->pfp);
|
|
|
|
|
|
|
|
if (ret != elements_cur) return -1;
|
|
|
|
|
|
|
|
n += ret;
|
|
|
|
off += ret * size;
|
|
|
|
|
|
|
|
elements_left -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2019-07-12 22:20:41 +00:00
|
|
|
n = fread (ptr, size, nmemb, fp->pfp);
|
2020-06-02 07:52:10 +00:00
|
|
|
#endif
|
2019-07-12 22:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-09-12 01:05:01 +00:00
|
|
|
size_t hc_fwrite (const void *ptr, size_t size, size_t nmemb, HCFILE *fp)
|
2019-07-12 22:20:41 +00:00
|
|
|
{
|
|
|
|
size_t n = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return n;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
n = gzfwrite (ptr, size, nmemb, fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-02 07:52:10 +00:00
|
|
|
#if defined (_WIN)
|
|
|
|
|
|
|
|
// 4 GB fwrite () limit for windows systems ?
|
|
|
|
// see: https://social.msdn.microsoft.com/Forums/vstudio/en-US/7c913001-227e-439b-bf07-54369ba07994/fwrite-issues-with-large-data-write
|
|
|
|
|
|
|
|
#define GIGABYTE (1024u * 1024u * 1024u)
|
|
|
|
|
|
|
|
if (((size * nmemb) / GIGABYTE) < 4)
|
|
|
|
{
|
|
|
|
n = fwrite (ptr, size, nmemb, fp->pfp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((size / GIGABYTE) > 3) return -1;
|
|
|
|
|
|
|
|
size_t elements_max = (3u * GIGABYTE) / size;
|
|
|
|
size_t elements_left = nmemb;
|
|
|
|
|
|
|
|
size_t off = 0;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
while (elements_left > 0)
|
|
|
|
{
|
|
|
|
size_t elements_cur = elements_max;
|
|
|
|
|
|
|
|
if (elements_left < elements_max) elements_cur = elements_left;
|
|
|
|
|
|
|
|
size_t ret = fwrite (ptr + off, size, elements_cur, fp->pfp);
|
|
|
|
|
|
|
|
if (ret != elements_cur) return -1;
|
|
|
|
|
|
|
|
n += ret;
|
|
|
|
off += ret * size;
|
|
|
|
|
|
|
|
elements_left -= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2019-07-12 22:20:41 +00:00
|
|
|
n = fwrite (ptr, size, nmemb, fp->pfp);
|
2020-06-02 07:52:10 +00:00
|
|
|
#endif
|
2019-07-12 22:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fseek (HCFILE *fp, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
2019-08-04 01:34:38 +00:00
|
|
|
r = gzseek (fp->gfp, offset, whence);
|
2019-07-12 22:20:41 +00:00
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
// untested and not used in wordlist engine
|
|
|
|
zlib_filefunc64_32_def *d = NULL;
|
|
|
|
if (whence == SEEK_SET)
|
|
|
|
{
|
2020-06-13 09:19:00 +00:00
|
|
|
r = ZSEEK64 (*d, fp->ufp, offset, ZLIB_FILEFUNC_SEEK_SET);
|
2019-07-29 16:42:50 +00:00
|
|
|
}
|
|
|
|
else if (whence == SEEK_CUR)
|
|
|
|
{
|
2020-06-13 09:19:00 +00:00
|
|
|
r = ZSEEK64 (*d, fp->ufp, offset, ZLIB_FILEFUNC_SEEK_CUR);
|
2019-07-29 16:42:50 +00:00
|
|
|
}
|
|
|
|
else if (whence == SEEK_END)
|
|
|
|
{
|
2020-06-13 09:19:00 +00:00
|
|
|
r = ZSEEK64 (*d, fp->ufp, offset, ZLIB_FILEFUNC_SEEK_END);
|
2019-07-29 16:42:50 +00:00
|
|
|
}
|
|
|
|
// or
|
|
|
|
// r = unzSetOffset (fp->ufp, offset);
|
|
|
|
*/
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = fseeko (fp->pfp, offset, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hc_rewind (HCFILE *fp)
|
|
|
|
{
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
gzrewind (fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
unzGoToFirstFile (fp->ufp);
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rewind (fp->pfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t hc_ftell (HCFILE *fp)
|
|
|
|
{
|
|
|
|
off_t n = 0;
|
|
|
|
|
|
|
|
if (fp == NULL) return -1;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
n = (off_t) gztell (fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
n = unztell (fp->ufp);
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
n = ftello (fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fputc (int c, HCFILE *fp)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzputc (fp->gfp, c);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = fputc (c, fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fgetc (HCFILE *fp)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzgetc (fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
unsigned char c = 0;
|
|
|
|
|
|
|
|
if (unzReadCurrentFile (fp->ufp, &c, 1) == 1) r = (int) c;
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = fgetc (fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *hc_fgets (char *buf, int len, HCFILE *fp)
|
|
|
|
{
|
|
|
|
char *r = NULL;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzgets (fp->gfp, buf, len);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
if (unzReadCurrentFile (fp->ufp, buf, len) > 0) r = buf;
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = fgets (buf, len, fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_vfprintf (HCFILE *fp, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzvprintf (fp->gfp, format, ap);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = vfprintf (fp->pfp, format, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fprintf (HCFILE *fp, const char *format, ...)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, format);
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzvprintf (fp->gfp, format, ap);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = vfprintf (fp->pfp, format, ap);
|
2016-09-06 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fscanf (HCFILE *fp, const char *format, void *ptr)
|
|
|
|
{
|
|
|
|
if (fp == NULL) return -1;
|
|
|
|
|
|
|
|
char *buf = (char *) hcmalloc (HCBUFSIZ_TINY);
|
|
|
|
|
|
|
|
if (buf == NULL) return -1;
|
|
|
|
|
|
|
|
char *b = hc_fgets (buf, HCBUFSIZ_TINY - 1, fp);
|
|
|
|
|
|
|
|
if (b == NULL)
|
|
|
|
{
|
|
|
|
hcfree (buf);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-04 01:34:38 +00:00
|
|
|
sscanf (b, format, ptr);
|
2019-07-12 22:20:41 +00:00
|
|
|
|
2016-10-10 09:03:11 +00:00
|
|
|
hcfree (buf);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_fileno (HCFILE *fp)
|
|
|
|
{
|
|
|
|
if (fp == NULL) return 1;
|
|
|
|
|
|
|
|
return fp->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hc_feof (HCFILE *fp)
|
|
|
|
{
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
if (fp == NULL) return r;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
r = gzeof (fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
r = unzeof (fp->ufp);
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = feof (fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hc_fflush (HCFILE *fp)
|
|
|
|
{
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
gzflush (fp->gfp, Z_SYNC_FLUSH);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fflush (fp->pfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hc_fclose (HCFILE *fp)
|
|
|
|
{
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
if (fp->is_gzip)
|
|
|
|
{
|
|
|
|
gzclose (fp->gfp);
|
|
|
|
}
|
2019-07-29 16:42:50 +00:00
|
|
|
else if (fp->is_zip)
|
|
|
|
{
|
|
|
|
unzCloseCurrentFile (fp->ufp);
|
|
|
|
|
|
|
|
unzClose (fp->ufp);
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose (fp->pfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp->fd = -1;
|
|
|
|
fp->pfp = NULL;
|
|
|
|
fp->is_gzip = false;
|
2019-07-29 16:42:50 +00:00
|
|
|
fp->is_zip = false;
|
2019-07-12 22:20:41 +00:00
|
|
|
|
|
|
|
fp->path = NULL;
|
|
|
|
fp->mode = NULL;
|
2016-09-06 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 10:22:24 +00:00
|
|
|
size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2019-08-06 10:22:24 +00:00
|
|
|
size_t line_truncated = 0;
|
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
size_t line_len = 0;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2019-06-26 17:06:46 +00:00
|
|
|
while (!hc_feof (fp))
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2019-06-26 17:06:46 +00:00
|
|
|
const int c = hc_fgetc (fp);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
if (c == EOF) break;
|
|
|
|
|
2019-08-06 10:22:24 +00:00
|
|
|
if (line_len == line_sz)
|
|
|
|
{
|
|
|
|
line_truncated++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
line_buf[line_len] = (char) c;
|
|
|
|
|
|
|
|
line_len++;
|
|
|
|
|
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
|
2019-08-06 10:22:24 +00:00
|
|
|
if (line_truncated > 0)
|
|
|
|
{
|
2020-01-14 12:29:02 +00:00
|
|
|
fprintf (stderr, "\nOversized line detected! Truncated %" PRIu64 " bytes\n", (u64) line_truncated);
|
2019-08-06 10:22:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
if (line_len == 0) return 0;
|
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
while (line_len)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2017-01-05 20:58:24 +00:00
|
|
|
if (line_buf[line_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
line_len--;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
if (line_buf[line_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
line_len--;
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-06 17:44:27 +00:00
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
break;
|
2016-09-06 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 20:58:24 +00:00
|
|
|
line_buf[line_len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
return (line_len);
|
|
|
|
}
|
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
u64 count_lines (HCFILE *fp)
|
2017-02-14 10:14:32 +00:00
|
|
|
{
|
2019-07-12 22:20:41 +00:00
|
|
|
u64 cnt = 0;
|
2017-02-14 10:14:32 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
char *buf = (char *) hcmalloc (HCBUFSIZ_LARGE + 1);
|
|
|
|
|
|
|
|
char prev = '\n';
|
|
|
|
|
|
|
|
while (!hc_feof (fp))
|
2017-02-14 10:14:32 +00:00
|
|
|
{
|
2019-07-12 22:20:41 +00:00
|
|
|
size_t nread = hc_fread (buf, sizeof (char), HCBUFSIZ_LARGE, fp);
|
2017-02-14 10:14:32 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
if (nread < 1) continue;
|
2017-02-14 10:14:32 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
size_t i;
|
2017-02-14 10:14:32 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
for (i = 0; i < nread; i++)
|
2017-02-14 10:14:32 +00:00
|
|
|
{
|
2019-07-12 22:20:41 +00:00
|
|
|
if (prev == '\n') cnt++;
|
2017-02-14 10:14:32 +00:00
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
prev = buf[i];
|
2017-02-14 10:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 22:20:41 +00:00
|
|
|
hcfree (buf);
|
|
|
|
|
|
|
|
return cnt;
|
2017-02-14 10:14:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:13:29 +00:00
|
|
|
size_t in_superchop (char *buf)
|
2016-09-06 17:44:27 +00:00
|
|
|
{
|
2016-11-17 05:17:28 +00:00
|
|
|
size_t len = strlen (buf);
|
2016-09-06 17:44:27 +00:00
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
2017-02-14 10:14:32 +00:00
|
|
|
buf[len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[len - 1] == '\r')
|
|
|
|
{
|
|
|
|
len--;
|
|
|
|
|
2017-02-14 10:14:32 +00:00
|
|
|
buf[len] = 0;
|
|
|
|
|
2016-09-06 17:44:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2019-07-12 22:20:41 +00:00
|
|
|
|
|
|
|
size_t superchop_with_length (char *buf, const size_t len)
|
|
|
|
{
|
|
|
|
size_t new_len = len;
|
|
|
|
|
|
|
|
while (new_len)
|
|
|
|
{
|
|
|
|
if (buf[new_len - 1] == '\n')
|
|
|
|
{
|
|
|
|
new_len--;
|
|
|
|
|
|
|
|
buf[new_len] = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[new_len - 1] == '\r')
|
|
|
|
{
|
|
|
|
new_len--;
|
|
|
|
|
|
|
|
buf[new_len] = 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_len;
|
|
|
|
}
|