Skip to content

Commit

Permalink
libmpg123: use _read/_lseek/_open on MSVCRT (bug 373), patch by manx
Browse files Browse the repository at this point in the history
git-svn-id: svn://scm.orgis.org/mpg123/trunk@5446 35dc7657-300d-0410-a2e5-dc2837fedb53
  • Loading branch information
thor committed Oct 28, 2024
1 parent 497d2d9 commit aa20020
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/compat/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions src/compat/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <io.h>
#endif
Expand Down
34 changes: 33 additions & 1 deletion src/libmpg123/lfs_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit aa20020

Please sign in to comment.