diff --git a/CMake/TestFileOffsetBits.c b/CMake/TestFileOffsetBits.c new file mode 100644 index 00000000..993b2da4 --- /dev/null +++ b/CMake/TestFileOffsetBits.c @@ -0,0 +1,10 @@ +#include + +int main(int argc, char **argv) +{ + /* Cause a compile-time error if off_t is smaller than 64 bits */ +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) + int off_t_is_large[ (LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1 ]; + return 0; +} + diff --git a/CMake/TestLargeFiles.c.cmake.in b/CMake/TestLargeFiles.c.cmake.in new file mode 100644 index 00000000..82691032 --- /dev/null +++ b/CMake/TestLargeFiles.c.cmake.in @@ -0,0 +1,24 @@ +#cmakedefine _LARGEFILE_SOURCE +#cmakedefine _LARGEFILE64_SOURCE +#cmakedefine _LARGE_FILES +#cmakedefine _FILE_OFFSET_BITS + +#include +#include +#include + +int main(int argc, char **argv) +{ + /* Cause a compile-time error if off_t is smaller than 64 bits, + * and make sure we have ftello / fseeko. + */ +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) + int off_t_is_large[ (LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1 ]; + FILE *fp = fopen(argv[0],"r"); + off_t offset = ftello( fp ); + + fseeko( fp, offset, SEEK_CUR ); + fclose(fp); + return 0; +} + diff --git a/CMake/TestLargeFiles.cmake b/CMake/TestLargeFiles.cmake new file mode 100644 index 00000000..96159d69 --- /dev/null +++ b/CMake/TestLargeFiles.cmake @@ -0,0 +1,120 @@ +# - Define macro to check large file support +# +# OPJ_TEST_LARGE_FILES(VARIABLE) +# +# VARIABLE will be set to true if off_t is 64 bits, and fseeko/ftello present. +# This macro will also set defines necessary enable large file support, for instance +# _LARGE_FILES +# _LARGEFILE_SOURCE +# _FILE_OFFSET_BITS 64 +# HAVE_FSEEKO +# +# However, it is YOUR job to make sure these defines are set in a CMakedefine so they +# end up in a config.h file that is included in your source if necessary! + +MACRO(OPJ_TEST_LARGE_FILES VARIABLE) + IF(NOT DEFINED ${VARIABLE}) + + # On most platforms it is probably overkill to first test the flags for 64-bit off_t, + # and then separately fseeko. However, in the future we might have 128-bit filesystems + # (ZFS), so it might be dangerous to indiscriminately set e.g. _FILE_OFFSET_BITS=64. + + MESSAGE(STATUS "Checking for 64-bit off_t") + + # First check without any special flags + TRY_COMPILE(FILE64_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/CMake/TestFileOffsetBits.c") + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present") + endif(FILE64_OK) + + if(NOT FILE64_OK) + # Test with _FILE_OFFSET_BITS=64 + TRY_COMPILE(FILE64_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/CMake/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_FILE_OFFSET_BITS=64" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _FILE_OFFSET_BITS=64") + set(_FILE_OFFSET_BITS 64) + endif(FILE64_OK) + endif(NOT FILE64_OK) + + if(NOT FILE64_OK) + # Test with _LARGE_FILES + TRY_COMPILE(FILE64_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/CMake/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_LARGE_FILES" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGE_FILES") + set(_LARGE_FILES 1) + endif(FILE64_OK) + endif(NOT FILE64_OK) + + if(NOT FILE64_OK) + # Test with _LARGEFILE_SOURCE + TRY_COMPILE(FILE64_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/CMake/TestFileOffsetBits.c" + COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" ) + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _LARGEFILE_SOURCE") + set(_LARGEFILE_SOURCE 1) + endif(FILE64_OK) + endif(NOT FILE64_OK) + + if(NOT FILE64_OK) + # now check for Windows stuff + TRY_COMPILE(FILE64_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_SOURCE_DIR}/CMake/TestWindowsFSeek.c") + if(FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - present with _fseeki64") + set(HAVE__FSEEKI64 1) + endif(FILE64_OK) + endif(NOT FILE64_OK) + + if(NOT FILE64_OK) + MESSAGE(STATUS "Checking for 64-bit off_t - not present") + else(NOT FILE64_OK) + + # Set the flags we might have determined to be required above + configure_file("${PROJECT_SOURCE_DIR}/CMake/TestLargeFiles.c.cmake.in" + "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c") + + MESSAGE(STATUS "Checking for fseeko/ftello") + # Test if ftello/fseeko are available + TRY_COMPILE(FSEEKO_COMPILE_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c") + if(FSEEKO_COMPILE_OK) + MESSAGE(STATUS "Checking for fseeko/ftello - present") + endif(FSEEKO_COMPILE_OK) + + if(NOT FSEEKO_COMPILE_OK) + # glibc 2.2 neds _LARGEFILE_SOURCE for fseeko (but not 64-bit off_t...) + TRY_COMPILE(FSEEKO_COMPILE_OK "${PROJECT_BINARY_DIR}" + "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/TestLargeFiles.c" + COMPILE_DEFINITIONS "-D_LARGEFILE_SOURCE" ) + if(FSEEKO_COMPILE_OK) + MESSAGE(STATUS "Checking for fseeko/ftello - present with _LARGEFILE_SOURCE") + set(_LARGEFILE_SOURCE 1) + endif(FSEEKO_COMPILE_OK) + endif(NOT FSEEKO_COMPILE_OK) + + endif(NOT FILE64_OK) + + if(FSEEKO_COMPILE_OK) + SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE) + set(HAVE_FSEEKO 1) + else(FSEEKO_COMPILE_OK) + if (HAVE__FSEEKI64) + SET(${VARIABLE} 1 CACHE INTERNAL "Result of test for large file support" FORCE) + SET(HAVE__FSEEKI64 1 CACHE INTERNAL "Windows 64-bit fseek" FORCE) + else (HAVE__FSEEKI64) + MESSAGE(STATUS "Checking for fseeko/ftello - not found") + SET(${VARIABLE} 0 CACHE INTERNAL "Result of test for large file support" FORCE) + endif (HAVE__FSEEKI64) + endif(FSEEKO_COMPILE_OK) + + ENDIF(NOT DEFINED ${VARIABLE}) +ENDMACRO(OPJ_TEST_LARGE_FILES VARIABLE) + + + diff --git a/CMake/TestWindowsFSeek.c b/CMake/TestWindowsFSeek.c new file mode 100644 index 00000000..ad9f0be1 --- /dev/null +++ b/CMake/TestWindowsFSeek.c @@ -0,0 +1,11 @@ + +#include + +int main() +{ + __int64 off=0; + + _fseeki64(NULL, off, SEEK_SET); + + return 0; +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 3145fc13..09a0a100 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,6 +168,10 @@ CHECK_INCLUDE_FILE("sys/stat.h" HAVE_SYS_STAT_H) CHECK_INCLUDE_FILE("sys/types.h" HAVE_SYS_TYPES_H) CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H) +# Enable Large file support +INCLUDE(TestLargeFiles) +OPJ_TEST_LARGE_FILES(OPJ_HAVE_LARGEFILES) + #----------------------------------------------------------------------------- # Build Library INCLUDE_DIRECTORIES(BEFORE ${OPENJPEG_BINARY_DIR}) diff --git a/libopenjpeg/opj_includes.h b/libopenjpeg/opj_includes.h index d94237e7..4801ef69 100644 --- a/libopenjpeg/opj_includes.h +++ b/libopenjpeg/opj_includes.h @@ -26,6 +26,12 @@ #ifndef OPJ_INCLUDES_H #define OPJ_INCLUDES_H +/* + * This must be included before any system headers, + * since they can react to macro defined there + */ +#include "opj_config.h" + /* ========================================================== Standard includes used by the library @@ -42,6 +48,40 @@ #include #include +/* + Use fseeko() and ftello() if they are available since they use + 'off_t' rather than 'long'. It is wrong to use fseeko() and + ftello() only on systems with special LFS support since some systems + (e.g. FreeBSD) support a 64-bit off_t by default. +*/ +#if defined(HAVE_FSEEKO) +# define fseek fseeko +# define ftell ftello +#endif + + +#if defined(MSWINDOWS) && !defined(Windows95) && !defined(__BORLANDC__) && \ + !(defined(_MSC_VER) && _MSC_VER < 1400) && \ + !(defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x800) + /* + Windows '95 and Borland C do not support _lseeki64 + Visual Studio does not support _fseeki64 and _ftelli64 until the 2005 release. + Without these interfaces, files over 2GB in size are not supported for Windows. + */ +# define OPJ_FSEEK(stream,offset,whence) _fseeki64(stream,/* __int64 */ offset,whence) +# define OPJ_FSTAT(fildes,stat_buff) _fstati64(fildes,/* struct _stati64 */ stat_buff) +# define OPJ_FTELL(stream) /* __int64 */ _ftelli64(stream) +# define OPJ_STAT_STRUCT_T struct _stati64 +# define OPJ_STAT(path,stat_buff) _stati64(path,/* struct _stati64 */ stat_buff) +#else +# define OPJ_FSEEK(stream,offset,whence) fseek(stream,offset,whence) +# define OPJ_FSTAT(fildes,stat_buff) fstat(fildes,stat_buff) +# define OPJ_FTELL(stream) ftell(stream) +# define OPJ_STAT_STRUCT_T struct stat +# define OPJ_STAT(path,stat_buff) stat(path,stat_buff) +#endif + + /* ========================================================== OpenJPEG interface diff --git a/opj_config.h.cmake.in b/opj_config.h.cmake.in index f83728b2..ce12dede 100644 --- a/opj_config.h.cmake.in +++ b/opj_config.h.cmake.in @@ -13,7 +13,15 @@ #cmakedefine HAVE_LIBPNG #cmakedefine HAVE_PNG_H #cmakedefine HAVE_LIBTIFF -#cmakedefine HAVE_TIFF_H +#cmakedefine HAVE_TIFF_H + +#cmakedefine _LARGEFILE_SOURCE +#cmakedefine _LARGEFILE64_SOURCE +#cmakedefine _LARGE_FILES +#cmakedefine _FILE_OFFSET_BITS +#cmakedefine HAVE_FSEEKO +#cmakedefine HAVE__FSEEKI64 + #cmakedefine HAVE_LIBLCMS1 #cmakedefine HAVE_LIBLCMS2