Skip to content

Commit

Permalink
update LONG_FSEEK/TELL
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Jun 22, 2021
1 parent 0e05ce7 commit b4b9b4a
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,53 @@
#include "report.h"
#include <limits.h>

// Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
// Credits to ZSTD library
/* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW */
/* Credits to ZSTD library */
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define LONG_SEEK _fseeki64
#define LONG_TELL _ftelli64
typedef __int64 OFF_T;
#define OFF_T_MAX +9223372036854775807 /* this is a guess */
#define OFF_T_MIN -9223372036854775807 /* this is a guess */
#elif !defined(__64BIT__) && \
(PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
#define LONG_SEEK fseeko
#define LONG_TELL ftello
typedef off_t OFF_T;
#define OFF_T_MAX +9223372036854775807 /* this is a guess */
#define OFF_T_MIN -9223372036854775807 /* this is a guess */
#elif defined(__MINGW32__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) && \
defined(__MSVCRT__)
#define LONG_SEEK fseeko64
#define LONG_TELL ftello64
typedef off64_t OFF_T;
#define OFF_T_MAX +9223372036854775807 /* this is a guess */
#define OFF_T_MIN -9223372036854775807 /* this is a guess */
#elif defined(_WIN32) && !defined(__DJGPP__)
static_assert(0, "Code does not work on the DJGPP compiler.");
#include <windows.h>
static int LONG_SEEK(FILE* file, __int64 offset, int origin)
{
LARGE_INTEGER off;
DWORD method;
off.QuadPart = offset;
if (origin == SEEK_END)
method = FILE_END;
else if (origin == SEEK_CUR)
method = FILE_CURRENT;
else
method = FILE_BEGIN;

if (SetFilePointerEx((HANDLE)_get_osfhandle(_fileno(file)), off, NULL, method))
return 0;
else
return -1;
}
static __int64 LONG_TELL(FILE* file)
{
LARGE_INTEGER off, newOff;
off.QuadPart = 0;
newOff.QuadPart = 0;
SetFilePointerEx((HANDLE)_get_osfhandle(_fileno(file)), off, &newOff, FILE_CURRENT);
return newOff.QuadPart;
}
#else
#define LONG_SEEK fseek
#define LONG_TELL ftell
typedef long OFF_T;
#define OFF_T_MAX LONG_MAX
#define OFF_T_MIN LONG_MIN
#endif

int bgen_fseek(FILE* stream, int64_t offset, int origin)
{
if (offset > OFF_T_MAX)
bgen_die("fseek overflow");

if (offset < OFF_T_MIN)
bgen_die("fseek underflow");

return LONG_SEEK(stream, offset, origin);
}

Expand Down

0 comments on commit b4b9b4a

Please sign in to comment.