From 5faf6a124d323eb20f95d720553678847e235057 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Sun, 28 Apr 2019 11:39:44 +0200 Subject: [PATCH] UWP VFS: Calculate buffer offset correctly when read fails due to EOF --- libretro-common/file/file_path.c | 2 +- libretro-common/include/file/file_path.h | 2 +- libretro-common/vfs/vfs_implementation_uwp.cpp | 12 +++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index c7710ba3a6..9b776594c0 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -1198,7 +1198,7 @@ void fill_pathname_home_dir(char *s, size_t len) } #endif -bool is_path_accessible_using_standard_io(char *path) +bool is_path_accessible_using_standard_io(const char *path) { #ifdef __WINRT__ bool result; diff --git a/libretro-common/include/file/file_path.h b/libretro-common/include/file/file_path.h index 34f9e0a71e..a7e4f8c299 100644 --- a/libretro-common/include/file/file_path.h +++ b/libretro-common/include/file/file_path.h @@ -498,7 +498,7 @@ bool path_is_valid(const char *path); int32_t path_get_size(const char *path); -bool is_path_accessible_using_standard_io(char *path); +bool is_path_accessible_using_standard_io(const char *path); RETRO_END_DECLS diff --git a/libretro-common/vfs/vfs_implementation_uwp.cpp b/libretro-common/vfs/vfs_implementation_uwp.cpp index 2bb3c6cc69..ef685fcd16 100644 --- a/libretro-common/vfs/vfs_implementation_uwp.cpp +++ b/libretro-common/vfs/vfs_implementation_uwp.cpp @@ -340,7 +340,7 @@ struct libretro_vfs_implementation_file char* orig_path; size_t buffer_size; int buffer_left; - + size_t buffer_fill; }; libretro_vfs_implementation_file *retro_vfs_file_open_impl(const char *path, unsigned mode, unsigned hints) @@ -401,6 +401,7 @@ libretro_vfs_implementation_file *retro_vfs_file_open_impl(const char *path, uns stream->buffer = (char*)malloc(buf_size); stream->bufferp = CreateNativeBuffer(stream->buffer, buf_size, 0); stream->buffer_left = 0; + stream->buffer_fill = 0; stream->buffer_size = buf_size; return stream; }); @@ -484,7 +485,7 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, void // Small read, use manually buffered IO if (stream->buffer_left < len) { // Exhaust the buffer - memcpy(s, &stream->buffer[stream->buffer_size - stream->buffer_left], stream->buffer_left); + memcpy(s, &stream->buffer[stream->buffer_fill - stream->buffer_left], stream->buffer_left); len -= stream->buffer_left; bytes_read += stream->buffer_left; stream->buffer_left = 0; @@ -496,9 +497,11 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, void return (int64_t)stream->bufferp->Length; }); }, -1); + stream->buffer_fill = stream->buffer_left; if (stream->buffer_left == -1) { stream->buffer_left = 0; + stream->buffer_fill = 0; return -1; } @@ -517,13 +520,13 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, void } // Internal buffer already contains requested amount - memcpy(s, &stream->buffer[stream->buffer_size - stream->buffer_left], len); + memcpy(s, &stream->buffer[stream->buffer_fill - stream->buffer_left], len); stream->buffer_left -= len; return len; } // Big read exceeding buffer size, exhaust small buffer and read rest in one go - memcpy(s, &stream->buffer[stream->buffer_size - stream->buffer_left], stream->buffer_left); + memcpy(s, &stream->buffer[stream->buffer_fill - stream->buffer_left], stream->buffer_left); len -= stream->buffer_left; bytes_read += stream->buffer_left; stream->buffer_left = 0; @@ -540,7 +543,6 @@ int64_t retro_vfs_file_read_impl(libretro_vfs_implementation_file *stream, void if (ret == -1) { return -1; } - return bytes_read + ret; }