From aa20020177bb839555c3d0fb2c4958aa4272ebae Mon Sep 17 00:00:00 2001 From: thor Date: Mon, 28 Oct 2024 11:43:53 +0000 Subject: [PATCH] libmpg123: use _read/_lseek/_open on MSVCRT (bug 373), patch by manx git-svn-id: svn://scm.orgis.org/mpg123/trunk@5446 35dc7657-300d-0410-a2e5-dc2837fedb53 --- src/compat/compat.c | 6 +++++- src/compat/compat.h | 28 ++++++++++++++++++++++++++++ src/libmpg123/lfs_wrap.c | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/compat/compat.c b/src/compat/compat.c index b66fe7ea..05b30faf 100644 --- a/src/compat/compat.c +++ b/src/compat/compat.c @@ -88,7 +88,7 @@ int INT123_compat_open(const char *filename, int flags) open_fallback: #endif -#if (defined(WIN32) && !defined (__CYGWIN__)) +#if defined(MPG123_COMPAT_MSVCRT_IO) /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */ /* Try plain old _open(), if it fails, do nothing */ ret = _open(filename, flags|_O_BINARY, _S_IREAD | _S_IWRITE); @@ -138,7 +138,11 @@ FILE* INT123_compat_fopen(const char *filename, const char *mode) FILE* INT123_compat_fdopen(int fd, const char *mode) { +#if defined(MPG123_COMPAT_MSVCRT_IO) + return _fdopen(fd, mode); +#else return fdopen(fd, mode); +#endif } int INT123_compat_close(int infd) diff --git a/src/compat/compat.h b/src/compat/compat.h index dee811f7..0657a667 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -110,7 +110,35 @@ typedef unsigned char byte; +#if (defined(_UCRT) || defined(_MSC_VER) || (defined(__MINGW32__) || defined(__MINGW64__))) && !defined(__CYGWIN__) +#define MPG123_COMPAT_MSVCRT_IO +#endif + +#if defined(MPG123_COMPAT_MSVCRT_IO) +#if defined(_UCRT) +// needs to get checked separately from MSVC and MinGW becuase it is also used by native Clang on Windows +#ifndef MPG123_COMPAT_MSVCRT_IO_64 +#define MPG123_COMPAT_MSVCRT_IO_64 +#endif +#endif #if defined(_MSC_VER) +#if (_MSC_VER >= 1200) +// >= VC6 +#ifndef MPG123_COMPAT_MSVCRT_IO_64 +#define MPG123_COMPAT_MSVCRT_IO_64 +#endif +#endif +#endif +#if defined(__MINGW32__) || defined(__MINGW64__) +#if (defined(__MSVCRT__) || defined(_UCRT)) && !defined(__CRTDLL__) +#ifndef MPG123_COMPAT_MSVCRT_IO_64 +#define MPG123_COMPAT_MSVCRT_IO_64 +#endif +#endif +#endif +#endif + +#if defined(HAVE__SETMODE) || defined(HAVE_SETMODE) || defined(MPG123_COMPAT_MSVCRT_IO) // For _setmode(), at least. #include #endif diff --git a/src/libmpg123/lfs_wrap.c b/src/libmpg123/lfs_wrap.c index 4a7b8b67..40f54acb 100644 --- a/src/libmpg123/lfs_wrap.c +++ b/src/libmpg123/lfs_wrap.c @@ -137,7 +137,11 @@ static void wrap_io_cleanup(void *handle) if(ioh->my_fd >= 0) { mdebug("closing my fd %d", ioh->my_fd); +#if defined(MPG123_COMPAT_MSVCRT_IO) + _close(ioh->my_fd); +#else close(ioh->my_fd); +#endif ioh->my_fd = -1; } } @@ -730,7 +734,11 @@ 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 if(part > 0) // == 0 is end of file { SATURATE_SUB(bytes, part, 0) @@ -755,6 +763,16 @@ 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 + 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) { @@ -763,6 +781,7 @@ static int64_t internal_lseek64(void *handle, int64_t offset, int whence) } return lseek(ioh->fd, (off_t)offset, whence); #endif +#endif } int INT123_wrap_open(mpg123_handle *mh, void *handle, const char *path, int fd, long timeout, int quiet) @@ -868,7 +887,20 @@ int INT123_wrap_open(mpg123_handle *mh, void *handle, const char *path, int fd, // 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 @@ -902,7 +934,7 @@ int attribute_align_arg mpg123_replace_reader(mpg123_handle *mh, mpg123_ssize_t ioh->iotype = IO_FD; ioh->fd = -1; /* On next mpg123_open_fd(), this gets a value. */ ioh->r_read = r_read != NULL ? r_read : fallback_read; - ioh->r_lseek = r_lseek != NULL ? r_lseek : lseek; + ioh->r_lseek = r_lseek != NULL ? r_lseek : fallback_lseek; } /* The real reader replacement will happen while opening. */