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.
| 8269 | // If this succeeds, valid() will return true and all fields |
| 8270 | // are usable; otherwise, valid() will return false. |
| 8271 | MemoryMappedFile(const char *filename) { |
| 8272 | data = NULL; |
| 8273 | size = 0; |
| 8274 | #ifdef TINYEXR_USE_WIN32_MMAP |
| 8275 | windows_file_mapping = NULL; |
| 8276 | windows_file = |
| 8277 | CreateFileW(tinyexr::UTF8ToWchar(filename).c_str(), // lpFileName |
| 8278 | GENERIC_READ, // dwDesiredAccess |
| 8279 | FILE_SHARE_READ, // dwShareMode |
| 8280 | NULL, // lpSecurityAttributes |
| 8281 | OPEN_EXISTING, // dwCreationDisposition |
| 8282 | FILE_ATTRIBUTE_READONLY, // dwFlagsAndAttributes |
| 8283 | NULL); // hTemplateFile |
| 8284 | if (windows_file == INVALID_HANDLE_VALUE) { |
| 8285 | return; |
| 8286 | } |
| 8287 | |
| 8288 | windows_file_mapping = CreateFileMapping(windows_file, // hFile |
| 8289 | NULL, // lpFileMappingAttributes |
| 8290 | PAGE_READONLY, // flProtect |
| 8291 | 0, // dwMaximumSizeHigh |
| 8292 | 0, // dwMaximumSizeLow |
| 8293 | NULL); // lpName |
| 8294 | if (windows_file_mapping == NULL) { |
| 8295 | return; |
| 8296 | } |
| 8297 | |
| 8298 | data = reinterpret_cast<unsigned char *>( |
| 8299 | MapViewOfFile(windows_file_mapping, // hFileMappingObject |
| 8300 | FILE_MAP_READ, // dwDesiredAccess |
| 8301 | 0, // dwFileOffsetHigh |
| 8302 | 0, // dwFileOffsetLow |
| 8303 | 0)); // dwNumberOfBytesToMap |
| 8304 | if (!data) { |
| 8305 | return; |
| 8306 | } |
| 8307 | |
| 8308 | LARGE_INTEGER windows_file_size = {}; |
| 8309 | if (!GetFileSizeEx(windows_file, &windows_file_size) || |
| 8310 | static_cast<ULONGLONG>(windows_file_size.QuadPart) > |
| 8311 | std::numeric_limits<size_t>::max()) { |
| 8312 | UnmapViewOfFile(data); |
| 8313 | data = NULL; |
| 8314 | return; |
| 8315 | } |
| 8316 | size = static_cast<size_t>(windows_file_size.QuadPart); |
| 8317 | #elif defined(TINYEXR_USE_POSIX_MMAP) |
| 8318 | posix_descriptor = open(filename, O_RDONLY); |
| 8319 | if (posix_descriptor == -1) { |
| 8320 | return; |
| 8321 | } |
| 8322 | |
| 8323 | struct stat info; |
| 8324 | if (fstat(posix_descriptor, &info) < 0) { |
| 8325 | return; |
| 8326 | } |
| 8327 | // Make sure st_size is in the valid range for a size_t. The second case |
| 8328 | // can only fail if a POSIX implementation defines off_t to be a larger |
nothing calls this directly
no test coverage detected