Commit 48ed05d9 authored by Adrian Johnson's avatar Adrian Johnson

Make Goffset long long and factor out all fseek/ftell into gfile.cc

This fixes build problems on 32-bit machines due to off_t being
defined differently depending on whether source files included
condig.h.

Bug 60095
parent fbcd6438
......@@ -49,6 +49,8 @@
# include <unixlib.h>
# endif
#endif // _WIN32
#include <stdio.h>
#include <limits>
#include "GooString.h"
#include "gfile.h"
......@@ -546,6 +548,42 @@ char *getLine(char *buf, int size, FILE *f) {
return buf;
}
int Gfseek(FILE *f, Goffset offset, int whence) {
#if HAVE_FSEEKO
return fseeko(f, offset, whence);
#elif HAVE_FSEEK64
return fseek64(f, offset, whence);
#elif _WIN32
return _fseeki64(f, offset, whence);
#else
return fseek(f, offset, whence);
#endif
}
Goffset Gftell(FILE *f) {
#if HAVE_FSEEKO
return ftello(f);
#elif HAVE_FSEEK64
return ftell64(f);
#elif _WIN32
return _ftelli64(f);
#else
return ftell(f);
#endif
}
Goffset GoffsetMax() {
#if HAVE_FSEEKO
return (std::numeric_limits<off_t>::max)();
#elif HAVE_FSEEK64
return (std::numeric_limits<off64_t>::max)();
#elif _WIN32
return (std::numeric_limits<__int64>::max)();
#else
return (std::numeric_limits<long>::max)();
#endif
}
//------------------------------------------------------------------------
// GDir and GDirEntry
//------------------------------------------------------------------------
......
......@@ -114,6 +114,13 @@ extern FILE *openFile(const char *path, const char *mode);
// conventions.
extern char *getLine(char *buf, int size, FILE *f);
// Like fseek/ftell but uses platform specific variants that support large files
extern int Gfseek(FILE *f, Goffset offset, int whence);
extern Goffset Gftell(FILE *f);
// Largest offset supported by Gfseek/Gftell
extern Goffset GoffsetMax();
//------------------------------------------------------------------------
// GDir and GDirEntry
//------------------------------------------------------------------------
......
......@@ -27,10 +27,6 @@
#include "poppler-config.h"
#ifndef _WIN32
#include <sys/types.h> // for off_t
#endif
/*
* These have stupid names to avoid conflicts with some (but not all)
* C++ compilers which define them.
......@@ -51,19 +47,6 @@ typedef unsigned char Guchar;
typedef unsigned short Gushort;
typedef unsigned int Guint;
typedef unsigned long Gulong;
/* Define Goffset to be the offset type used by the version of
* fseek we are using.
*/
#if HAVE_FSEEKO
typedef off_t Goffset;
#elif HAVE_FSEEK64
typedef off64_t Goffset;
#elif _WIN32
typedef __int64 Goffset;
#else
typedef long Goffset;
#endif
typedef long long Goffset;
#endif
......@@ -385,7 +385,7 @@ void FileOutStream::close ()
Goffset FileOutStream::getPos ()
{
return ftell(f);
return Gftell(f);
}
void FileOutStream::put (char c)
......@@ -800,19 +800,8 @@ Stream *FileStream::makeSubStream(Goffset startA, GBool limitedA,
}
void FileStream::reset() {
#if HAVE_FSEEKO
savePos = ftello(f);
fseeko(f, start, SEEK_SET);
#elif HAVE_FSEEK64
savePos = ftell64(f);
fseek64(f, start, SEEK_SET);
#elif _WIN32
savePos = _ftelli64(f);
_fseeki64(f, start, SEEK_SET);
#else
savePos = ftell(f);
fseek(f, start, SEEK_SET);
#endif
savePos = Gftell(f);
Gfseek(f, start, SEEK_SET);
saved = gTrue;
bufPtr = bufEnd = buf;
bufPos = start;
......@@ -820,15 +809,7 @@ void FileStream::reset() {
void FileStream::close() {
if (saved) {
#if HAVE_FSEEKO
fseeko(f, savePos, SEEK_SET);
#elif HAVE_FSEEK64
fseek64(f, savePos, SEEK_SET);
#elif _WIN32
_fseeki64(f, savePos, SEEK_SET);
#else
fseek(f, savePos, SEEK_SET);
#endif
Gfseek(f, savePos, SEEK_SET);
saved = gFalse;
}
}
......@@ -858,45 +839,15 @@ void FileStream::setPos(Goffset pos, int dir) {
Goffset size;
if (dir >= 0) {
#if HAVE_FSEEKO
fseeko(f, pos, SEEK_SET);
#elif HAVE_FSEEK64
fseek64(f, pos, SEEK_SET);
#elif _WIN32
_fseeki64(f, pos, SEEK_SET);
#else
fseek(f, pos, SEEK_SET);
#endif
Gfseek(f, pos, SEEK_SET);
bufPos = pos;
} else {
#if HAVE_FSEEKO
fseeko(f, 0, SEEK_END);
size = ftello(f);
#elif HAVE_FSEEK64
fseek64(f, 0, SEEK_END);
size = ftell64(f);
#elif _WIN32
_fseeki64(f, 0, SEEK_END);
size = _ftelli64(f);
#else
fseek(f, 0, SEEK_END);
size = ftell(f);
#endif
Gfseek(f, 0, SEEK_END);
size = Gftell(f);
if (pos > size)
pos = size;
#if HAVE_FSEEKO
fseeko(f, -pos, SEEK_END);
bufPos = ftello(f);
#elif HAVE_FSEEK64
fseek64(f, -pos, SEEK_END);
bufPos = ftell64(f);
#elif _WIN32
_fseeki64(f, -pos, SEEK_END);
bufPos = _ftelli64(f);
#else
fseek(f, -pos, SEEK_END);
bufPos = ftell(f);
#endif
Gfseek(f, -pos, SEEK_END);
bufPos = Gftell(f);
}
bufPtr = bufEnd = buf;
}
......
......@@ -41,8 +41,8 @@
#include <math.h>
#include <ctype.h>
#include <limits.h>
#include <limits>
#include <float.h>
#include "goo/gfile.h"
#include "goo/gmem.h"
#include "Object.h"
#include "Stream.h"
......@@ -852,7 +852,7 @@ GBool XRef::readXRefStreamSection(Stream *xrefStr, int *w, int first, int n) {
}
offset = (offset << 8) + c;
}
if (offset > (unsigned long long)(std::numeric_limits<Goffset>::max)()) {
if (offset > (unsigned long long)GoffsetMax()) {
error(errSyntaxError, -1, "Offset inside xref table too large for fseek");
return gFalse;
}
......
......@@ -107,12 +107,6 @@
#cmakedefine USE_CMS 1
#endif
/* Define to 1 if you have the `fseek64' function. */
#cmakedefine HAVE_FSEEK64 1
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
#cmakedefine HAVE_FSEEKO 1
// Also, there are preprocessor symbols in the header files
// that are used but never defined when building poppler using configure
// or cmake: DISABLE_OUTLINE, DEBUG_MEM, SPLASH_CMYK, HAVE_T1LIB_H,
......
......@@ -107,12 +107,6 @@
#undef USE_CMS
#endif
/* Define to 1 if you have the `fseek64' function. */
#undef HAVE_FSEEK64
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
#undef HAVE_FSEEKO
// Also, there are preprocessor symbols in the header files
// that are used but never defined when building poppler using configure
// or cmake: DISABLE_OUTLINE, DEBUG_MEM, SPLASH_CMYK, HAVE_T1LIB_H,
......
......@@ -36,6 +36,7 @@
#include "parseargs.h"
#include "printencodings.h"
#include "goo/GooString.h"
#include "goo/gfile.h"
#include "goo/gmem.h"
#include "GlobalParams.h"
#include "Object.h"
......@@ -351,19 +352,8 @@ int main(int argc, char *argv[]) {
f = fopen(fileName->getCString(), "rb");
#endif
if (f) {
#if HAVE_FSEEKO
fseeko(f, 0, SEEK_END);
printf("File size: %lld bytes\n", (long long)ftello(f));
#elif HAVE_FSEEK64
fseek64(f, 0, SEEK_END);
printf("File size: %lld bytes\n", (long long)ftell64(f));
#elif _WIN32
_fseeki64(f, 0, SEEK_END);
printf("File size: %lld bytes\n", (long long)_ftelli64(f));
#else
fseek(f, 0, SEEK_END);
printf("File size: %lld bytes\n", (long long)ftell(f));
#endif
Gfseek(f, 0, SEEK_END);
printf("File size: %lld bytes\n", (long long)Gftell(f));
fclose(f);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment