MemoryMappedFile's constructor tries to map memory to a file. If this succeeds, valid() will return true and all fields are usable; otherwise, valid() will return false.
| 6820 | // If this succeeds, valid() will return true and all fields |
| 6821 | // are usable; otherwise, valid() will return false. |
| 6822 | MemoryMappedFile(const char *filename) { |
| 6823 | data = NULL; |
| 6824 | size = 0; |
| 6825 | #ifdef TINYEXR_USE_WIN32_MMAP |
| 6826 | windows_file_mapping = NULL; |
| 6827 | windows_file = |
| 6828 | CreateFileW(tinyexr::UTF8ToWchar(filename).c_str(), // lpFileName |
| 6829 | GENERIC_READ, // dwDesiredAccess |
| 6830 | FILE_SHARE_READ, // dwShareMode |
| 6831 | NULL, // lpSecurityAttributes |
| 6832 | OPEN_EXISTING, // dwCreationDisposition |
| 6833 | FILE_ATTRIBUTE_READONLY, // dwFlagsAndAttributes |
| 6834 | NULL); // hTemplateFile |
| 6835 | if (windows_file == INVALID_HANDLE_VALUE) { |
| 6836 | return; |
| 6837 | } |
| 6838 | |
| 6839 | windows_file_mapping = CreateFileMapping(windows_file, // hFile |
| 6840 | NULL, // lpFileMappingAttributes |
| 6841 | PAGE_READONLY, // flProtect |
| 6842 | 0, // dwMaximumSizeHigh |
| 6843 | 0, // dwMaximumSizeLow |
| 6844 | NULL); // lpName |
| 6845 | if (windows_file_mapping == NULL) { |
| 6846 | return; |
| 6847 | } |
| 6848 | |
| 6849 | data = reinterpret_cast<unsigned char *>( |
| 6850 | MapViewOfFile(windows_file_mapping, // hFileMappingObject |
| 6851 | FILE_MAP_READ, // dwDesiredAccess |
| 6852 | 0, // dwFileOffsetHigh |
| 6853 | 0, // dwFileOffsetLow |
| 6854 | 0)); // dwNumberOfBytesToMap |
| 6855 | if (!data) { |
| 6856 | return; |
| 6857 | } |
| 6858 | |
| 6859 | LARGE_INTEGER windows_file_size = {}; |
| 6860 | if (!GetFileSizeEx(windows_file, &windows_file_size) || |
| 6861 | static_cast<ULONGLONG>(windows_file_size.QuadPart) > |
| 6862 | std::numeric_limits<size_t>::max()) { |
| 6863 | UnmapViewOfFile(data); |
| 6864 | data = NULL; |
| 6865 | return; |
| 6866 | } |
| 6867 | size = static_cast<size_t>(windows_file_size.QuadPart); |
| 6868 | #elif defined(TINYEXR_USE_POSIX_MMAP) |
| 6869 | posix_descriptor = open(filename, O_RDONLY); |
| 6870 | if (posix_descriptor == -1) { |
| 6871 | return; |
| 6872 | } |
| 6873 | |
| 6874 | struct stat info; |
| 6875 | if (fstat(posix_descriptor, &info) < 0) { |
| 6876 | return; |
| 6877 | } |
| 6878 | // Make sure st_size is in the valid range for a size_t. The second case |
| 6879 | // can only fail if a POSIX implementation defines off_t to be a larger |
nothing calls this directly
no test coverage detected