From 739928df7baeefad9cc6c46cfcdbdcc14f557c1a Mon Sep 17 00:00:00 2001 From: "R. Bernstein" Date: Sat, 3 Mar 2012 12:10:53 -0500 Subject: [PATCH] Pete Batard's patches offset types especially for MinGW and MS Visual C. --- example/isofile.c | 2 +- example/isofile2.c | 2 +- example/isofuzzy.c | 2 +- include/cdio/udf.h | 2 +- include/cdio/udf_time.h | 7 ++-- include/cdio/util.h | 23 +++++++++--- lib/driver/Makefile.am | 2 +- lib/driver/_cdio_generic.c | 24 ++++++++++--- lib/driver/_cdio_stdio.c | 73 ++++++++++++++++++++++++++++---------- lib/driver/_cdio_stream.c | 25 ++++++++----- lib/driver/_cdio_stream.h | 25 +++++++------ lib/driver/abs_path.c | 25 +++++++------ lib/driver/image/bincue.c | 2 +- lib/driver/image/cdrdao.c | 2 +- lib/driver/logging.c | 2 +- src/iso-read.c | 2 +- test/testisocd.c | 2 +- test/testisocd2.c.in | 8 ++--- 18 files changed, 151 insertions(+), 79 deletions(-) diff --git a/example/isofile.c b/example/isofile.c index bcfe06a5..559aac12 100644 --- a/example/isofile.c +++ b/example/isofile.c @@ -31,7 +31,7 @@ /* portable.h has to come first else _FILE_OFFSET_BITS are redefined in say opensolaris. */ -#include "portable.h" +#include "filemode.h" #include #include diff --git a/example/isofile2.c b/example/isofile2.c index 474f0c1f..09ff4bed 100644 --- a/example/isofile2.c +++ b/example/isofile2.c @@ -37,7 +37,7 @@ /* portable.h has to come first else _FILE_OFFSET_BITS are redefined in say opensolaris. */ -#include "portable.h" +#include "filemode.h" #ifdef HAVE_SYS_TYPES_H #include diff --git a/example/isofuzzy.c b/example/isofuzzy.c index 138e1fcf..9ca23baa 100644 --- a/example/isofuzzy.c +++ b/example/isofuzzy.c @@ -28,7 +28,7 @@ /* portable.h has to come first else _FILE_OFFSET_BITS are redefined in say opensolaris. */ -#include "portable.h" +#include "filemode.h" #ifdef HAVE_STDIO_H #include diff --git a/include/cdio/udf.h b/include/cdio/udf.h index 10f90116..2d87e9e3 100644 --- a/include/cdio/udf.h +++ b/include/cdio/udf.h @@ -51,7 +51,7 @@ typedef struct udf_dirent_s { /* This field has to come last because it is variable in length. */ udf_file_entry_t fe; -} udf_dirent_t;; +} udf_dirent_t; diff --git a/include/cdio/udf_time.h b/include/cdio/udf_time.h index e93dbadb..33b2f8c4 100644 --- a/include/cdio/udf_time.h +++ b/include/cdio/udf_time.h @@ -27,11 +27,10 @@ #include -#ifndef HAVE_STRUCT_TIMESPEC -#define HAVE_STRUCT_TIMESPEC +#if defined(__MINGW32__) && !defined(__MINGW64__) struct timespec { - time_t tv_sec; /* Seconds */ - long tv_nsec; /* Nanoseconds */ + time_t tv_sec; /* Seconds */ + long tv_nsec; /* Nanoseconds */ }; #endif diff --git a/include/cdio/util.h b/include/cdio/util.h index 1e653dfe..6d40de85 100644 --- a/include/cdio/util.h +++ b/include/cdio/util.h @@ -1,5 +1,6 @@ /* - Copyright (C) 2004, 2005, 2006, 2008, 2010 Rocky Bernstein + Copyright (C) 2004, 2005, 2006, 2008, 2010, 2012 + Rocky Bernstein Copyright (C) 2000 Herbert Valerio Riedel This program is free software: you can redistribute it and/or modify @@ -28,6 +29,18 @@ #include #include +#if !defined CDIO_INLINE +#if defined(__cplusplus) || defined(inline) +#define CDIO_INLINE inline +#elif defined(__GNUC__) +#define CDIO_INLINE __inline__ +#elif defined(_MSC_VER) +#define CDIO_INLINE __inline +#else +#define CDIO_INLINE +#endif +#endif /* CDIO_INLINE */ + #undef MAX #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -40,7 +53,7 @@ #undef CLAMP #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) -static inline uint32_t +static CDIO_INLINE uint32_t _cdio_len2blocks (uint32_t i_len, uint16_t i_blocksize) { uint32_t i_blocks; @@ -53,13 +66,13 @@ _cdio_len2blocks (uint32_t i_len, uint16_t i_blocksize) } /* round up to next block boundary */ -static inline unsigned +static CDIO_INLINE unsigned _cdio_ceil2block (unsigned offset, uint16_t i_blocksize) { return _cdio_len2blocks (offset, i_blocksize) * i_blocksize; } -static inline unsigned int +static CDIO_INLINE unsigned int _cdio_ofs_add (unsigned offset, unsigned length, uint16_t i_blocksize) { if (i_blocksize - (offset % i_blocksize) < length) @@ -70,7 +83,7 @@ _cdio_ofs_add (unsigned offset, unsigned length, uint16_t i_blocksize) return offset; } -static inline const char * +static CDIO_INLINE const char * _cdio_bool_str (bool b) { return b ? "yes" : "no"; diff --git a/lib/driver/Makefile.am b/lib/driver/Makefile.am index beee0a6c..d19bfcb7 100644 --- a/lib/driver/Makefile.am +++ b/lib/driver/Makefile.am @@ -49,7 +49,7 @@ EXTRA_DIST = image/Makefile \ FreeBSD/Makefile MSWindows/Makefile \ libcdio.sym -noinst_HEADERS = cdio_assert.h cdio_private.h portable.h +noinst_HEADERS = cdio_assert.h cdio_private.h filemode.h libcdio_sources = \ _cdio_generic.c \ diff --git a/lib/driver/_cdio_generic.c b/lib/driver/_cdio_generic.c index 0a5c726b..36352686 100644 --- a/lib/driver/_cdio_generic.c +++ b/lib/driver/_cdio_generic.c @@ -30,14 +30,18 @@ #include #ifdef HAVE_UNISTD_H -#include -#endif /*HAVE_UNISTD_H*/ +#include +#endif #include #include +#ifdef HAVE_SYS_STAT_H #include +#endif +#ifdef HAVE_SYS_TYPES_H #include +#endif #include #include @@ -45,12 +49,22 @@ #include "cdio_assert.h" #include "cdio_private.h" #include "_cdio_stdio.h" -#include "portable.h" +#include "filemode.h" #ifndef PATH_MAX #define PATH_MAX 4096 #endif +/* If available and LFS is enabled, try to use lseek64 */ +#if defined(HAVE_LSEEK64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64) +#if defined(_MSC_VER) +#include +#endif +#define CDIO_LSEEK lseek64 +#else +#define CDIO_LSEEK lseek +#endif + /*! Eject media -- there's nothing to do here. We always return -2. Should we also free resources? @@ -150,13 +164,13 @@ cdio_generic_read_form1_sector (void * user_data, void *data, lsn_t lsn) /*! Reads into buf the next size bytes. Returns -1 on error. - Is in fact libc's lseek(). + Is in fact libc's lseek()/lseek64(). */ off_t cdio_generic_lseek (void *user_data, off_t offset, int whence) { generic_img_private_t *p_env = user_data; - return lseek(p_env->fd, offset, whence); + return CDIO_LSEEK(p_env->fd, offset, whence); } /*! diff --git a/lib/driver/_cdio_stdio.c b/lib/driver/_cdio_stdio.c index 8d692d3e..d89625af 100644 --- a/lib/driver/_cdio_stdio.c +++ b/lib/driver/_cdio_stdio.c @@ -21,22 +21,50 @@ # define __CDIO_CONFIG_H__ 1 #endif +#ifdef HAVE_STDIO_H #include +#endif +#ifdef HAVE_STDLIB_H #include +#endif +#ifdef HAVE_STRING_H #include - +#endif #ifdef HAVE_UNISTD_H #include -#endif /*HAVE_UNISTD_H*/ - +#endif +#ifdef HAVE_SYS_STAT_H #include +#endif +#ifdef HAVE_ERRNO_H #include +#endif #include #include #include "_cdio_stream.h" #include "_cdio_stdio.h" +/* On 32 bit platforms, fseek can only access streams of 2 GB or less. + Prefer fseeko/fseeko64, that take a 64 bit offset when LFS is enabled */ +#if defined(HAVE_FSEEKO64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64) +#define CDIO_FSEEK fseeko64 +#elif defined(HAVE_FSEEKO) +#define CDIO_FSEEK fseeko +#else +#define CDIO_FSEEK fseek +#endif + +/* Use _stati64 if needed, on platforms that don't have transparent LFS support */ +#if defined(HAVE__STATI64) && defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64) +#define CDIO_STAT _stati64 +#else +#define CDIO_STAT stat +#endif + +#define _STRINGIFY(a) #a +#define STRINGIFY(a) _STRINGIFY(a) + static const char _rcsid[] = "$Id: _cdio_stdio.c,v 1.6 2008/04/22 15:29:11 karl Exp $"; #define CDIO_STDIO_BUFSIZE (128*1024) @@ -93,7 +121,7 @@ _stdio_free(void *user_data) } /*! - Like fseek(3) and in fact may be the same. + Like fseek/fseeko(3) and in fact may be the same. This function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained @@ -108,19 +136,28 @@ _stdio_free(void *user_data) DRIVER_OP_ERROR is returned and the global variable errno is set to indicate the error. */ -static driver_return_code_t -_stdio_seek(void *p_user_data, long i_offset, int whence) +static int +_stdio_seek(void *p_user_data, off_t i_offset, int whence) { _UserData *const ud = p_user_data; + int ret; +#if !defined(HAVE_FSEEKO) && !defined(HAVE_FSEEKO64) + /* Detect if off_t is lossy-truncated to long to avoid data corruption */ + if ( (sizeof(off_t) > sizeof(long)) && (i_offset != (off_t)((long)i_offset)) ) { + cdio_error ( STRINGIFY(CDIO_FSEEK) " (): lossy truncation detected!"); + errno = EFBIG; + return DRIVER_OP_ERROR; + } +#endif - if ( (i_offset=fseek (ud->fd, i_offset, whence)) ) { - cdio_error ("fseek (): %s", strerror (errno)); + if ( (ret=CDIO_FSEEK (ud->fd, i_offset, whence)) ) { + cdio_error ( STRINGIFY(CDIO_FSEEK) " (): %s", strerror (errno)); } - return i_offset; + return ret; } -static long int +static off_t _stdio_stat(void *p_user_data) { const _UserData *const ud = p_user_data; @@ -145,15 +182,15 @@ _stdio_stat(void *p_user_data) We do not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. */ -static long -_stdio_read(void *user_data, void *buf, long int count) +static ssize_t +_stdio_read(void *user_data, void *buf, size_t count) { _UserData *const ud = user_data; - long read; + long read_count; - read = fread(buf, 1, count, ud->fd); + read_count = fread(buf, 1, count, ud->fd); - if (read != count) + if (read_count != count) { /* fixme -- ferror/feof */ if (feof (ud->fd)) { @@ -169,7 +206,7 @@ _stdio_read(void *user_data, void *buf, long int count) cdio_debug ("fread (): short read and no EOF?!?"); } - return read; + return read_count; } /*! @@ -187,9 +224,9 @@ cdio_stdio_new(const char pathname[]) CdioDataSource_t *new_obj = NULL; cdio_stream_io_functions funcs = { NULL, NULL, NULL, NULL, NULL, NULL }; _UserData *ud = NULL; - struct stat statbuf; + struct CDIO_STAT statbuf; - if (stat (pathname, &statbuf) == -1) + if (CDIO_STAT (pathname, &statbuf) == -1) { cdio_warn ("could not retrieve file info for `%s': %s", pathname, strerror (errno)); diff --git a/lib/driver/_cdio_stream.c b/lib/driver/_cdio_stream.c index c1d04892..f3c453c2 100644 --- a/lib/driver/_cdio_stream.c +++ b/lib/driver/_cdio_stream.c @@ -21,10 +21,18 @@ # define __CDIO_CONFIG_H__ 1 #endif +#ifdef HAVE_STDIO_H #include +#endif +#ifdef HAVE_STDLIB_H #include +#endif +#ifdef HAVE_STRING_H #include +#endif +#ifdef HAVE_STDARG_H #include +#endif #include "cdio_assert.h" /* #define STREAM_DEBUG */ @@ -43,7 +51,7 @@ struct _CdioDataSource { void* user_data; cdio_stream_io_functions op; int is_open; - long position; + off_t position; }; void @@ -80,8 +88,8 @@ cdio_stream_destroy(CdioDataSource_t *p_obj) @return unpon successful completion, return value is positive, else, the global variable errno is set to indicate the error. */ -ssize_t -cdio_stream_getpos(CdioDataSource_t* p_obj, /*out*/ ssize_t *i_offset) +off_t +cdio_stream_getpos(CdioDataSource_t* p_obj, /*out*/ off_t *i_offset) { if (!p_obj || !p_obj->is_open) return DRIVER_OP_UNINIT; return *i_offset = p_obj->position; @@ -140,7 +148,7 @@ _cdio_stream_open_if_necessary(CdioDataSource_t *p_obj) must use feof(3) and ferror(3) to determine which occurred. */ ssize_t -cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long size, long nmemb) +cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, size_t size, size_t nmemb) { long read_bytes; @@ -168,8 +176,8 @@ cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long size, long nmemb) @return unpon successful completion, return value is positive, else, the global variable errno is set to indicate the error. */ -ssize_t -cdio_stream_seek(CdioDataSource_t* p_obj, ssize_t offset, int whence) +int +cdio_stream_seek(CdioDataSource_t* p_obj, off_t offset, int whence) { if (!p_obj) return DRIVER_OP_UNINIT; @@ -178,10 +186,11 @@ cdio_stream_seek(CdioDataSource_t* p_obj, ssize_t offset, int whence) return DRIVER_OP_ERROR; if (offset < 0) return DRIVER_OP_ERROR; + if (p_obj->position < 0) return DRIVER_OP_ERROR; if (p_obj->position != offset) { #ifdef STREAM_DEBUG - cdio_warn("had to reposition DataSource from %ld to %ld!", obj->position, offset); + cdio_warn("had to reposition DataSource from %ld to %ld!", p_obj->position, offset); #endif p_obj->position = offset; return p_obj->op.seek(p_obj->user_data, offset, whence); @@ -194,7 +203,7 @@ cdio_stream_seek(CdioDataSource_t* p_obj, ssize_t offset, int whence) Return whatever size of stream reports, I guess unit size is bytes. On error return -1; */ -ssize_t +off_t cdio_stream_stat(CdioDataSource_t *p_obj) { if (!p_obj) return -1; diff --git a/lib/driver/_cdio_stream.h b/lib/driver/_cdio_stream.h index 7147a0b9..313bc31f 100644 --- a/lib/driver/_cdio_stream.h +++ b/lib/driver/_cdio_stream.h @@ -1,7 +1,6 @@ /* - $Id: _cdio_stream.h,v 1.5 2008/04/22 15:29:11 karl Exp $ - - Copyright (C) 2003, 2004, 2005, 2006, 2008 Rocky Bernstein + Copyright (C) 2003, 2004, 2005, 2006, 2008, 2012 + Rocky Bernstein Copyright (C) 2000 Herbert Valerio Riedel This program is free software: you can redistribute it and/or modify @@ -33,12 +32,12 @@ extern "C" { typedef int(*cdio_data_open_t)(void *user_data); - typedef long(*cdio_data_read_t)(void *user_data, void *buf, long count); + typedef ssize_t(*cdio_data_read_t)(void *user_data, void *buf, size_t count); - typedef driver_return_code_t(*cdio_data_seek_t)(void *user_data, long offset, + typedef int(*cdio_data_seek_t)(void *user_data, off_t offset, int whence); - typedef long(*cdio_data_stat_t)(void *user_data); + typedef off_t(*cdio_data_stat_t)(void *user_data); typedef int(*cdio_data_close_t)(void *user_data); @@ -65,8 +64,8 @@ extern "C" { @return unpon successful completion, return value is positive, else, the global variable errno is set to indicate the error. */ - ssize_t cdio_stream_getpos(CdioDataSource_t* p_obj, - /*out*/ ssize_t *i_offset); + off_t cdio_stream_getpos(CdioDataSource_t* p_obj, + /*out*/ off_t *i_offset); CdioDataSource_t * cdio_stream_new(void *user_data, const cdio_stream_io_functions *funcs); @@ -88,11 +87,11 @@ extern "C" { We do not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. */ - ssize_t cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, long i_size, - long nmemb); + ssize_t cdio_stream_read(CdioDataSource_t* p_obj, void *ptr, size_t i_size, + size_t nmemb); /** - Like fseek(3) and in fact may be the same. + Like fseek(3)/fseeko(3) and in fact may be the same. This function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained @@ -107,14 +106,14 @@ extern "C" { DRIVER_OP_ERROR is returned and the global variable errno is set to indicate the error. */ - ssize_t cdio_stream_seek(CdioDataSource_t *p_obj, ssize_t i_offset, + int cdio_stream_seek(CdioDataSource_t *p_obj, off_t i_offset, int whence); /** Return whatever size of stream reports, I guess unit size is bytes. On error return -1; */ - ssize_t cdio_stream_stat(CdioDataSource_t *p_obj); + off_t cdio_stream_stat(CdioDataSource_t *p_obj); /** Deallocate resources associated with p_obj. After this p_obj is unusable. diff --git a/lib/driver/abs_path.c b/lib/driver/abs_path.c index 9b01489b..e21bb106 100644 --- a/lib/driver/abs_path.c +++ b/lib/driver/abs_path.c @@ -20,23 +20,25 @@ # include #endif -#include +#ifdef HAVE_STDIO_H +# include +#endif #ifndef PATH_MAX -#define PATH_MAX 4096 +# define PATH_MAX 4096 #endif #ifndef NULL -# define NULL 0 +# define NULL 0 #endif #ifdef __CYGWIN__ -#undef DOSISH +# undef DOSISH #endif #if defined __CYGWIN__ || defined DOSISH -#define DOSISH_UNC -#define DOSISH_DRIVE_LETTER -#define FILE_ALT_SEPARATOR '\\' +# define DOSISH_UNC +# define DOSISH_DRIVE_LETTER +# define FILE_ALT_SEPARATOR '\\' #endif #ifndef CDIO_FILE_SEPARATOR @@ -44,18 +46,19 @@ #endif #if defined __CYGWIN__ || defined DOSISH -# define FILE_ALT_SEPARATOR '\\' +# define FILE_ALT_SEPARATOR '\\' #endif #ifdef CDIO_FILE_ALT_SEPARATOR -# define isdirsep(x) ((x) == CDIO_FILE_SEPARATOR || (x) == CDIO_FILE_ALT_SEPARATOR) +# define isdirsep(x) ((x) == CDIO_FILE_SEPARATOR || (x) == CDIO_FILE_ALT_SEPARATOR) #else -# define isdirsep(x) ((x) == CDIO_FILE_SEPARATOR) +# define isdirsep(x) ((x) == CDIO_FILE_SEPARATOR) #endif #define skipprefix(path) (path) -#ifndef CharNext /* defined as CharNext[AW] on Windows. */ +#if !defined(CharNext) || defined(_MSC_VER) /* defined as CharNext[AW] on Windows. */ +# undef CharNext # define CharNext(p) ((p) + 1) #endif diff --git a/lib/driver/image/bincue.c b/lib/driver/image/bincue.c index fb314a73..14ca127c 100644 --- a/lib/driver/image/bincue.c +++ b/lib/driver/image/bincue.c @@ -53,7 +53,7 @@ #endif #include -#include "portable.h" +#include "filemode.h" /* reader */ #define DEFAULT_CDIO_DEVICE "videocd.bin" diff --git a/lib/driver/image/cdrdao.c b/lib/driver/image/cdrdao.c index 7aab546c..345eee65 100644 --- a/lib/driver/image/cdrdao.c +++ b/lib/driver/image/cdrdao.c @@ -57,7 +57,7 @@ #include -#include "portable.h" +#include "filemode.h" /* reader */ diff --git a/lib/driver/logging.c b/lib/driver/logging.c index bac7b211..accb589f 100644 --- a/lib/driver/logging.c +++ b/lib/driver/logging.c @@ -27,7 +27,7 @@ #include #include "cdio_assert.h" -#include "portable.h" +#include "filemode.h" static const char _rcsid[] = "$Id: logging.c,v 1.2 2008/04/22 15:29:12 karl Exp $"; diff --git a/src/iso-read.c b/src/iso-read.c index b6140112..cb59b166 100644 --- a/src/iso-read.c +++ b/src/iso-read.c @@ -20,7 +20,7 @@ /* Program to read ISO-9660 images. */ #include "util.h" -#include "portable.h" +#include "filemode.h" #ifdef HAVE_CONFIG_H # include "config.h" diff --git a/test/testisocd.c b/test/testisocd.c index 229daacb..e4c09264 100644 --- a/test/testisocd.c +++ b/test/testisocd.c @@ -23,7 +23,7 @@ # define __CDIO_CONFIG_H__ 1 #endif -#include "portable.h" +#include "filemode.h" #include #include diff --git a/test/testisocd2.c.in b/test/testisocd2.c.in index 35e0cbed..0664c01b 100644 --- a/test/testisocd2.c.in +++ b/test/testisocd2.c.in @@ -1,6 +1,5 @@ -/* $Id: testisocd2.c.in,v 1.2 2008/03/22 18:08:25 karl Exp $ - - Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Rocky Bernstein +/* + Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2012 Rocky Bernstein This program is free software: you can redistribute it and/or modify @@ -23,8 +22,7 @@ # include "config.h" #endif -#include "portable.h" - +#include "filemode.h" #include #include #include