|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
|
|
| #define ENABLE_INTSAFE_SIGNED_FUNCTIONS
|
| #include <intsafe.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static bool __cdecl common_fseek_binary_mode_read_only_fast_track_nolock(
|
| __crt_stdio_stream const stream,
|
| __int64 offset,
|
| int whence
|
| ) throw()
|
| {
|
|
|
|
|
|
|
| if (whence == SEEK_END)
|
| {
|
| return false;
|
| }
|
|
|
|
|
| if (!stream.has_any_buffer())
|
| {
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
| if (stream.has_any_of(_IOWRITE | _IOUPDATE))
|
| {
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (stream->_cnt <= 0)
|
| {
|
| return false;
|
| }
|
|
|
|
|
|
|
|
|
| int const fh = stream.lowio_handle();
|
| if ((_osfile(fh) & FTEXT) != 0 || _textmode(fh) != __crt_lowio_text_mode::ansi)
|
| {
|
| return false;
|
| }
|
|
|
|
|
|
|
| if (whence == SEEK_SET)
|
| {
|
| __int64 const lowio_position = _lseeki64_nolock(fh, 0, SEEK_CUR);
|
| if (lowio_position < 0)
|
| {
|
| return false;
|
| }
|
|
|
| __int64 const stdio_position = lowio_position - stream->_cnt;
|
| if (FAILED(LongLongSub(offset, stdio_position, &offset)))
|
| {
|
| return false;
|
| }
|
| whence = SEEK_CUR;
|
| }
|
|
|
|
|
|
|
|
|
| __int64 const minimum_reverse_seek = -(stream->_ptr - stream->_base);
|
| __int64 const maximum_forward_seek = stream->_cnt;
|
|
|
| bool const seek_is_within_buffer = minimum_reverse_seek <= offset && offset <= maximum_forward_seek;
|
| if (!seek_is_within_buffer)
|
| {
|
| return false;
|
| }
|
|
|
| stream->_ptr += offset;
|
|
|
|
|
|
|
| stream->_cnt -= static_cast<int>(offset);
|
| return true;
|
| }
|
|
|
|
|
|
|
| static int __cdecl common_fseek_nolock(
|
| __crt_stdio_stream const stream,
|
| __int64 offset,
|
| int whence,
|
| __crt_cached_ptd_host& ptd
|
| ) throw()
|
| {
|
| if (!stream.is_in_use())
|
| {
|
| ptd.get_errno().set(EINVAL);
|
| return -1;
|
| }
|
|
|
| stream.unset_flags(_IOEOF);
|
|
|
| if (common_fseek_binary_mode_read_only_fast_track_nolock(stream, offset, whence))
|
| {
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
| if (whence == SEEK_CUR)
|
| {
|
| offset += _ftelli64_nolock_internal(stream.public_stream(), ptd);
|
| whence = SEEK_SET;
|
| }
|
|
|
| __acrt_stdio_flush_nolock(stream.public_stream(), ptd);
|
|
|
|
|
|
|
|
|
|
|
| __acrt_stdio_reset_buffer(stream);
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (stream.has_all_of(_IOUPDATE))
|
| {
|
| stream.unset_flags(_IOWRITE | _IOREAD);
|
| }
|
| else if (stream.has_all_of(_IOREAD | _IOBUFFER_CRT) && !stream.has_any_of(_IOBUFFER_SETVBUF))
|
| {
|
| stream->_bufsiz = _SMALL_BUFSIZ;
|
| }
|
|
|
| if (_lseeki64_nolock_internal(stream.lowio_handle(), offset, whence, ptd) == -1)
|
| {
|
| return -1;
|
| }
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl common_fseek(
|
| __crt_stdio_stream const stream,
|
| __int64 const offset,
|
| int const whence,
|
| __crt_cached_ptd_host& ptd
|
| ) throw()
|
| {
|
| _UCRT_VALIDATE_RETURN(ptd, stream.valid(), EINVAL, -1);
|
| _UCRT_VALIDATE_RETURN(ptd, whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END, EINVAL, -1);
|
|
|
| int return_value = -1;
|
|
|
| _lock_file(stream.public_stream());
|
| __try
|
| {
|
| return_value = common_fseek_nolock(stream, offset, whence, ptd);
|
| }
|
| __finally
|
| {
|
| _unlock_file(stream.public_stream());
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl fseek(
|
| FILE* const public_stream,
|
| long const offset,
|
| int const whence
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_fseek(__crt_stdio_stream(public_stream), offset, whence, ptd);
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _fseek_nolock(
|
| FILE* const public_stream,
|
| long const offset,
|
| int const whence
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_fseek_nolock(__crt_stdio_stream(public_stream), offset, whence, ptd);
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _fseeki64(
|
| FILE* const public_stream,
|
| __int64 const offset,
|
| int const whence
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_fseek(__crt_stdio_stream(public_stream), offset, whence, ptd);
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _fseeki64_nolock(
|
| FILE* const public_stream,
|
| __int64 const offset,
|
| int const whence
|
| )
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_fseek_nolock(__crt_stdio_stream(public_stream), offset, whence, ptd);
|
| }
|
|
|