|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_stdio.h>
|
| #include <corecrt_internal_ptd_propagation.h>
|
|
|
|
|
| static bool __cdecl buffer_contains_wide_characters(__crt_lowio_text_mode const text_mode) throw()
|
| {
|
| return text_mode == __crt_lowio_text_mode::utf8
|
| || text_mode == __crt_lowio_text_mode::utf16le;
|
| }
|
|
|
| static size_t __cdecl buffer_character_size(__crt_lowio_text_mode const text_mode) throw()
|
| {
|
| return buffer_contains_wide_characters(text_mode) ? sizeof(wchar_t) : sizeof(char);
|
| }
|
|
|
|
|
|
|
|
|
| template <typename Character>
|
| static __int64 __cdecl count_newlines_of_type(
|
| _In_reads_(buffer_last - buffer_first) char const* const buffer_first,
|
| _In_reads_(0) char const* const buffer_last
|
| ) throw()
|
| {
|
|
|
|
|
| _ASSERTE((buffer_last - buffer_first) % sizeof(Character) == 0);
|
|
|
| Character const* const typed_first = reinterpret_cast<Character const*>(buffer_first);
|
| Character const* const typed_last = reinterpret_cast<Character const*>(buffer_last);
|
|
|
| __int64 newline_count = 0;
|
| for (Character const* it = typed_first; it != typed_last; ++it)
|
| {
|
| if (*it == '\n')
|
| {
|
| ++newline_count;
|
| }
|
| }
|
|
|
| return newline_count;
|
| }
|
|
|
| static __int64 __cdecl count_newline_bytes(
|
| _In_reads_(buffer_last - buffer_first) char const* const buffer_first,
|
| _In_reads_(0) char const* const buffer_last,
|
| _In_ __crt_lowio_text_mode const text_mode
|
| ) throw()
|
| {
|
| if (buffer_contains_wide_characters(text_mode))
|
| {
|
| return count_newlines_of_type<wchar_t>(buffer_first, buffer_last) * sizeof(wchar_t);
|
| }
|
| else
|
| {
|
| return count_newlines_of_type<char>(buffer_first, buffer_last);
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| static __int64 __cdecl common_ftell_translated_utf8_nolock(
|
| __crt_stdio_stream const stream,
|
| __int64 const lowio_position,
|
| __crt_cached_ptd_host& ptd
|
| ) throw()
|
| {
|
| int const fh = _fileno(stream.public_stream());
|
|
|
|
|
|
|
| if (stream->_cnt == 0)
|
| {
|
| return lowio_position;
|
| }
|
|
|
| __int64 const current_buffer_position = (stream->_ptr - stream->_base) / static_cast<__int64>(sizeof(wchar_t));
|
|
|
|
|
|
|
|
|
|
|
|
|
| __int64 const base_buffer_position = _lseeki64_internal(fh, _startpos(fh), SEEK_SET, ptd);
|
| if (base_buffer_position != _startpos(fh))
|
| {
|
| return -1;
|
| }
|
|
|
| DWORD bytes_read;
|
| char raw_buffer[_INTERNAL_BUFSIZ];
|
| if (!ReadFile(reinterpret_cast<HANDLE>(_osfhnd(fh)), raw_buffer, _INTERNAL_BUFSIZ, &bytes_read, nullptr))
|
| return -1;
|
|
|
|
|
|
|
| if (_lseeki64_internal(fh, lowio_position, SEEK_SET, ptd) < 0)
|
| {
|
| return -1;
|
| }
|
|
|
|
|
| if (current_buffer_position > static_cast<__int64>(bytes_read))
|
| {
|
| return -1;
|
| }
|
|
|
|
|
|
|
| char const* const raw_first = raw_buffer;
|
| #pragma warning(disable:__WARNING_UNUSED_POINTER_ASSIGNMENT)
|
| char const* const raw_last = raw_buffer + bytes_read;
|
|
|
| char const* raw_it = raw_first;
|
| for (__int64 i = 0; i != current_buffer_position && raw_it < raw_last; ++i, ++raw_it)
|
| {
|
| if (*raw_it == CR)
|
| {
|
| if (raw_it < raw_last - 1 && *(raw_it + 1) == LF)
|
| ++raw_it;
|
| }
|
| else
|
| {
|
| raw_it += _utf8_no_of_trailbytes(static_cast<const unsigned char>(*raw_it));
|
| }
|
| }
|
|
|
| return base_buffer_position + (raw_it - raw_first);
|
| }
|
|
|
|
|
|
|
|
|
|
|
| static __int64 __cdecl common_ftell_read_mode_nolock(
|
| __crt_stdio_stream const stream,
|
| __int64 const lowio_position,
|
| __int64 const buffer_offset,
|
| __crt_cached_ptd_host& ptd
|
| ) throw()
|
| {
|
| int const fh = _fileno(stream.public_stream());
|
|
|
|
|
|
|
| __crt_lowio_text_mode const text_mode = _textmode(fh);
|
|
|
| __int64 const translation_factor = text_mode == __crt_lowio_text_mode::utf8
|
| ? static_cast<__int64>(sizeof(wchar_t))
|
| : static_cast<__int64>(sizeof(char));
|
|
|
|
|
|
|
| if (stream->_cnt == 0)
|
| {
|
| return lowio_position;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| __int64 bytes_read = stream->_cnt + static_cast<__int64>(stream->_ptr - stream->_base);
|
|
|
|
|
|
|
|
|
| if ((_osfile(fh) & FTEXT) == 0)
|
| {
|
| return lowio_position
|
| - (bytes_read / translation_factor)
|
| + (buffer_offset / translation_factor);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (_lseeki64_internal(fh, 0, SEEK_END, ptd) == lowio_position)
|
| {
|
| bytes_read += count_newline_bytes(stream->_base, stream->_base + bytes_read, text_mode);
|
|
|
|
|
|
|
| if (stream.ctrl_z())
|
| {
|
| bytes_read += buffer_character_size(text_mode);
|
| }
|
| }
|
|
|
|
|
| else
|
| {
|
| if (_lseeki64_internal(fh, lowio_position, SEEK_SET, ptd) == -1)
|
| return -1;
|
|
|
|
|
|
|
|
|
| if (bytes_read <= _SMALL_BUFSIZ &&
|
| stream.has_crt_buffer() &&
|
| !stream.has_setvbuf_buffer())
|
| {
|
| bytes_read = _SMALL_BUFSIZ;
|
| }
|
|
|
| else
|
| {
|
| bytes_read = stream->_bufsiz;
|
| }
|
|
|
|
|
|
|
|
|
| if (_osfile(fh) & FCRLF)
|
| {
|
| bytes_read += buffer_character_size(text_mode);
|
| }
|
| }
|
|
|
| return lowio_position
|
| - (bytes_read / translation_factor)
|
| + (buffer_offset / translation_factor);
|
| }
|
|
|
|
|
|
|
| template <typename Integer>
|
| static Integer __cdecl common_ftell_nolock(__crt_stdio_stream, __crt_cached_ptd_host& ptd) throw();
|
|
|
| template <>
|
| __int64 __cdecl common_ftell_nolock(__crt_stdio_stream const stream, __crt_cached_ptd_host& ptd) throw()
|
| {
|
| _UCRT_VALIDATE_RETURN(ptd, stream.public_stream(), EINVAL, -1);
|
|
|
| int const fh = _fileno(stream.public_stream());
|
|
|
| if (stream->_cnt < 0)
|
| {
|
| stream->_cnt = 0;
|
| }
|
|
|
|
|
|
|
| __int64 const lowio_position = _lseeki64_internal(fh, 0, SEEK_CUR, ptd);
|
| if (lowio_position < 0)
|
| {
|
| return -1;
|
| }
|
|
|
|
|
|
|
| if (!stream.has_big_buffer())
|
| {
|
| return lowio_position - stream->_cnt;
|
| }
|
|
|
|
|
| __crt_lowio_text_mode const text_mode = _textmode(fh);
|
|
|
|
|
|
|
| __int64 buffer_offset = stream->_ptr - stream->_base;
|
|
|
|
|
|
|
|
|
| if (stream.has_any_of(_IOWRITE | _IOREAD))
|
| {
|
| if (text_mode == __crt_lowio_text_mode::utf8 && _utf8translations(fh))
|
| {
|
| return common_ftell_translated_utf8_nolock(stream, lowio_position, ptd);
|
| }
|
|
|
|
|
|
|
| if (_osfile(fh) & FTEXT)
|
| {
|
| buffer_offset += count_newline_bytes(stream->_base, stream->_ptr, text_mode);
|
| }
|
| }
|
|
|
| else if (!stream.has_all_of(_IOUPDATE))
|
| {
|
| ptd.get_errno().set(EINVAL);
|
| return -1;
|
| }
|
|
|
|
|
|
|
| if (lowio_position == 0)
|
| {
|
| return buffer_offset;
|
| }
|
|
|
| if (stream.has_all_of(_IOREAD))
|
| {
|
| return common_ftell_read_mode_nolock(stream, lowio_position, buffer_offset, ptd);
|
| }
|
|
|
| if (text_mode == __crt_lowio_text_mode::utf8)
|
| {
|
| buffer_offset /= sizeof(wchar_t);
|
| }
|
|
|
| return lowio_position + buffer_offset;
|
| }
|
|
|
| template <>
|
| long __cdecl common_ftell_nolock(__crt_stdio_stream const stream, __crt_cached_ptd_host& ptd) throw()
|
| {
|
| __int64 const result = common_ftell_nolock<__int64>(stream, ptd);
|
| if (result > LONG_MAX)
|
| {
|
| ptd.get_errno().set(EINVAL);
|
| return -1;
|
| }
|
|
|
| return static_cast<long>(result);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename Integer>
|
| static Integer __cdecl common_ftell(__crt_stdio_stream const stream, __crt_cached_ptd_host& ptd) throw()
|
| {
|
| _UCRT_VALIDATE_RETURN(ptd, stream.valid(), EINVAL, -1);
|
|
|
| Integer return_value = 0;
|
|
|
| _lock_file(stream.public_stream());
|
| __try
|
| {
|
| return_value = common_ftell_nolock<Integer>(stream, ptd);
|
| }
|
| __finally
|
| {
|
| _unlock_file(stream.public_stream());
|
| }
|
|
|
| return return_value;
|
| }
|
|
|
|
|
|
|
|
|
| extern "C" long __cdecl ftell(FILE* const public_stream)
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_ftell<long>(__crt_stdio_stream(public_stream), ptd);
|
| }
|
|
|
| extern "C" long __cdecl _ftell_nolock(FILE* const public_stream)
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_ftell_nolock<long>(__crt_stdio_stream(public_stream), ptd);
|
| }
|
|
|
| extern "C" __int64 __cdecl _ftelli64(FILE* const public_stream)
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_ftell<__int64>(__crt_stdio_stream(public_stream), ptd);
|
| }
|
|
|
| extern "C" __int64 __cdecl _ftelli64_nolock(FILE* const public_stream)
|
| {
|
| __crt_cached_ptd_host ptd;
|
| return common_ftell_nolock<__int64>(__crt_stdio_stream(public_stream), ptd);
|
| }
|
|
|
| extern "C" __int64 __cdecl _ftelli64_nolock_internal(FILE* const public_stream, __crt_cached_ptd_host& ptd)
|
| {
|
| return common_ftell_nolock<__int64>(__crt_stdio_stream(public_stream), ptd);
|
| }
|
|
|