From af82ec2938930db8d8708ee9bc65c092240dd28f Mon Sep 17 00:00:00 2001 From: thor Date: Mon, 28 Oct 2024 12:01:06 +0000 Subject: [PATCH] libmpg123: slightly rework the MSVCRT paths, using fallback functions This has the MSVC-specific code paths only in one place for seek and read. Also, I took the liberty to remove the && 0, enabling the 64 bit lseek again. git-svn-id: svn://scm.orgis.org/mpg123/trunk@5447 35dc7657-300d-0410-a2e5-dc2837fedb53 --- NEWS | 5 +++ src/libmpg123/lfs_wrap.c | 71 ++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/NEWS b/NEWS index 2f2a6041..6beef63b 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +1.32.9 +------ +- libmpg123: enable 64 bit offset path for MSVCRT and avoid warnings about + MS's game about POSIX API with and without underscores (bug 373). + 1.32.8 ------ - libmpg123: diff --git a/src/libmpg123/lfs_wrap.c b/src/libmpg123/lfs_wrap.c index 40f54acb..62cd6747 100644 --- a/src/libmpg123/lfs_wrap.c +++ b/src/libmpg123/lfs_wrap.c @@ -703,6 +703,35 @@ static int64_t wrap_lseek(void *handle, int64_t offset, int whence) return -1; } +// Defining a wrapper to the native read to be sure the prototype matches. +// There are platforms where it is read(int, void*, unsigned int). +// We know that we read small chunks where the difference does not matter. Could +// apply specific hackery, use a common compat_read() (INT123_unintr_read()?) with system +// specifics. +static mpg123_ssize_t fallback_read(int fd, void *buf, size_t count) +{ +#if defined(MPG123_COMPAT_MSVCRT_IO) + if(count > UINT_MAX) + { + errno = EOVERFLOW; + return -1; + } + return _read(fd, buf, (unsigned int)count); +#else + return read(fd, buf, count); +#endif +} + +static off_t fallback_lseek(int fd, off_t offset, int whence) +{ +#if defined(MPG123_COMPAT_MSVCRT_IO) + // Off_t is 32 bit and does fit into long. We know that. + return _lseek(fd, (long)offset, whence); +#else + return lseek(fd, offset, whence); +#endif +} + // This is assuming an internally opened file, which usually will be // using 64 bit offsets. It keeps reading on on trivial interruptions. // I guess any file descriptor that matches the libc should work fine. @@ -734,11 +763,7 @@ static int internal_read64(void *handle, void *buf, size_t bytes, size_t *got_by } #endif errno = 0; -#if defined(MPG123_COMPAT_MSVCRT_IO) - ptrdiff_t part = _read(fd, (char*)buf+got, bytes); -#else - ptrdiff_t part = read(fd, (char*)buf+got, bytes); -#endif + ptrdiff_t part = fallback_read(fd, (char*)buf+got, bytes); if(part > 0) // == 0 is end of file { SATURATE_SUB(bytes, part, 0) @@ -763,24 +788,15 @@ static int64_t internal_lseek64(void *handle, int64_t offset, int whence) struct wrap_data* ioh = handle; #ifdef LFS_LARGEFILE_64 return lseek64(ioh->fd, offset, whence); -#elif defined(MPG123_COMPAT_MSVCRT_IO_64) && 0 +#elif defined(MPG123_COMPAT_MSVCRT_IO_64) return _lseeki64(ioh->fd, offset, whence); -#else -#if defined(MPG123_COMPAT_MSVCRT_IO) - if(offset < LONG_MIN || offset > LONG_MAX) - { - errno = EOVERFLOW; - return -1; - } - return _lseek(ioh->fd, (long)offset, whence); #else if(offset < OFF_MIN || offset > OFF_MAX) { errno = EOVERFLOW; return -1; } - return lseek(ioh->fd, (off_t)offset, whence); -#endif + return fallback_lseek(ioh->fd, (off_t)offset, whence); #endif } @@ -880,29 +896,6 @@ int INT123_wrap_open(mpg123_handle *mh, void *handle, const char *path, int fd, // So, native off_t reader replacement. -// Defining a wrapper to the native read to be sure the prototype matches. -// There are platforms where it is read(int, void*, unsigned int). -// We know that we read small chunks where the difference does not matter. Could -// apply specific hackery, use a common compat_read() (INT123_unintr_read()?) with system -// specifics. -static mpg123_ssize_t fallback_read(int fd, void *buf, size_t count) -{ -#if defined(MPG123_COMPAT_MSVCRT_IO) - return _read(fd, buf, count); -#else - return read(fd, buf, count); -#endif -} - -static off_t fallback_lseek(int fd, off_t offset, int whence) -{ -#if defined(MPG123_COMPAT_MSVCRT_IO) - return _lseek(fd, offset, whence); -#else - return lseek(fd, offset, whence); -#endif -} - // In forced 64 bit offset mode, the only definitions of these are // the _64 ones. #ifdef FORCED_OFF_64