Skip to content

Commit

Permalink
libmpg123: slightly rework the MSVCRT paths, using fallback functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thor committed Oct 28, 2024
1 parent aa20020 commit af82ec2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
71 changes: 32 additions & 39 deletions src/libmpg123/lfs_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit af82ec2

Please sign in to comment.