| 1 | /* Copyright (C) 2010-2020 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (nbio_windowsmmap.c). |
| 5 | * --------------------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, |
| 8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation the rights to |
| 10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <file/nbio.h> |
| 24 | |
| 25 | #if defined(_WIN32) |
| 26 | #if defined(_MSC_VER) && _MSC_VER >= 1500 |
| 27 | |
| 28 | #ifndef HAVE_MMAP_WIN32 |
| 29 | #define HAVE_MMAP_WIN32 |
| 30 | #endif |
| 31 | |
| 32 | #elif !defined(_MSC_VER) |
| 33 | |
| 34 | #ifndef HAVE_MMAP_WIN32 |
| 35 | #define HAVE_MMAP_WIN32 |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | #endif |
| 40 | |
| 41 | #if defined(HAVE_MMAP_WIN32) |
| 42 | |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | |
| 46 | #include <encodings/utf.h> |
| 47 | |
| 48 | #include <windows.h> |
| 49 | |
| 50 | /* Assume W-functions do not work below Win2K and Xbox platforms */ |
| 51 | #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX) |
| 52 | |
| 53 | #ifndef LEGACY_WIN32 |
| 54 | #define LEGACY_WIN32 |
| 55 | #endif |
| 56 | |
| 57 | #endif |
| 58 | |
| 59 | #ifndef FILE_SHARE_ALL |
| 60 | #define FILE_SHARE_ALL (FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE) |
| 61 | #endif |
| 62 | |
| 63 | struct nbio_mmap_win32_t |
| 64 | { |
| 65 | HANDLE file; |
| 66 | void* ptr; |
| 67 | size_t len; |
| 68 | bool is_write; |
| 69 | }; |
| 70 | |
| 71 | static void *nbio_mmap_win32_open(const char * filename, unsigned mode) |
| 72 | { |
| 73 | static const DWORD dispositions[] = { OPEN_EXISTING, CREATE_ALWAYS, OPEN_ALWAYS, OPEN_EXISTING, CREATE_ALWAYS }; |
| 74 | HANDLE mem; |
| 75 | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 |
| 76 | LARGE_INTEGER len; |
| 77 | #else |
| 78 | SIZE_T len; |
| 79 | #endif |
| 80 | struct nbio_mmap_win32_t* handle = NULL; |
| 81 | void* ptr = NULL; |
| 82 | bool is_write = (mode == NBIO_WRITE || mode == NBIO_UPDATE || mode == BIO_WRITE); |
| 83 | DWORD access = (is_write ? GENERIC_READ|GENERIC_WRITE : GENERIC_READ); |
| 84 | #if !defined(_WIN32) || defined(LEGACY_WIN32) |
| 85 | HANDLE file = CreateFile(filename, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL); |
| 86 | #else |
| 87 | wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename); |
| 88 | #ifdef __WINRT__ |
| 89 | HANDLE file = CreateFile2(filename_wide, access, FILE_SHARE_ALL, dispositions[mode], NULL); |
| 90 | #else |
| 91 | HANDLE file = CreateFileW(filename_wide, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL); |
| 92 | #endif |
| 93 | |
| 94 | if (filename_wide) |
| 95 | free(filename_wide); |
| 96 | #endif |
| 97 | |
| 98 | if (file == INVALID_HANDLE_VALUE) |
| 99 | return NULL; |
| 100 | |
| 101 | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 |
| 102 | /* GetFileSizeEx is new for Windows 2000 */ |
| 103 | GetFileSizeEx(file, &len); |
| 104 | mem = CreateFileMapping(file, NULL, is_write ? PAGE_READWRITE : PAGE_READONLY, 0, 0, NULL); |
| 105 | ptr = MapViewOfFile(mem, is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len.QuadPart); |
| 106 | #else |
| 107 | GetFileSize(file, &len); |
| 108 | mem = CreateFileMapping(file, NULL, is_write ? PAGE_READWRITE : PAGE_READONLY, 0, 0, NULL); |
| 109 | ptr = MapViewOfFile(mem, is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len); |
| 110 | #endif |
| 111 | |
| 112 | CloseHandle(mem); |
| 113 | |
| 114 | handle = (struct nbio_mmap_win32_t*)malloc(sizeof(struct nbio_mmap_win32_t)); |
| 115 | |
| 116 | handle->file = file; |
| 117 | handle->is_write = is_write; |
| 118 | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 |
| 119 | handle->len = len.QuadPart; |
| 120 | #else |
| 121 | handle->len = len; |
| 122 | #endif |
| 123 | handle->ptr = ptr; |
| 124 | |
| 125 | return handle; |
| 126 | } |
| 127 | |
| 128 | static void nbio_mmap_win32_begin_read(void *data) |
| 129 | { |
| 130 | /* not needed */ |
| 131 | } |
| 132 | |
| 133 | static void nbio_mmap_win32_begin_write(void *data) |
| 134 | { |
| 135 | /* not needed */ |
| 136 | } |
| 137 | |
| 138 | static bool nbio_mmap_win32_iterate(void *data) |
| 139 | { |
| 140 | /* not needed */ |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | static void nbio_mmap_win32_resize(void *data, size_t len) |
| 145 | { |
| 146 | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 |
| 147 | LARGE_INTEGER len_li; |
| 148 | #else |
| 149 | SIZE_T len_li; |
| 150 | #endif |
| 151 | HANDLE mem; |
| 152 | struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data; |
| 153 | |
| 154 | if (!handle) |
| 155 | return; |
| 156 | |
| 157 | if (len < handle->len) |
| 158 | { |
| 159 | /* this works perfectly fine if this check is removed, |
| 160 | * but it won't work on other nbio implementations */ |
| 161 | /* therefore, it's blocked so nobody accidentally |
| 162 | * relies on it. */ |
| 163 | abort(); |
| 164 | } |
| 165 | |
| 166 | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500 |
| 167 | /* SetFilePointerEx is new for Windows 2000 */ |
| 168 | len_li.QuadPart = len; |
| 169 | SetFilePointerEx(handle->file, len_li, NULL, FILE_BEGIN); |
| 170 | #else |
| 171 | len_li = len; |
| 172 | SetFilePointer(handle->file, len_li, NULL, FILE_BEGIN); |
| 173 | #endif |
| 174 | |
| 175 | if (!SetEndOfFile(handle->file)) |
| 176 | abort(); /* this one returns void and I can't find any other way for it to report failure */ |
| 177 | handle->len = len; |
| 178 | |
| 179 | UnmapViewOfFile(handle->ptr); |
| 180 | mem = CreateFileMapping(handle->file, NULL, handle->is_write ? PAGE_READWRITE : PAGE_READONLY, 0, 0, NULL); |
| 181 | handle->ptr = MapViewOfFile(mem, handle->is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len); |
| 182 | CloseHandle(mem); |
| 183 | |
| 184 | if (!handle->ptr) |
| 185 | abort(); |
| 186 | } |
| 187 | |
| 188 | static void *nbio_mmap_win32_get_ptr(void *data, size_t* len) |
| 189 | { |
| 190 | struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data; |
| 191 | if (!handle) |
| 192 | return NULL; |
| 193 | if (len) |
| 194 | *len = handle->len; |
| 195 | return handle->ptr; |
| 196 | } |
| 197 | |
| 198 | static void nbio_mmap_win32_cancel(void *data) |
| 199 | { |
| 200 | /* not needed */ |
| 201 | } |
| 202 | |
| 203 | static void nbio_mmap_win32_free(void *data) |
| 204 | { |
| 205 | struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data; |
| 206 | if (!handle) |
| 207 | return; |
| 208 | CloseHandle(handle->file); |
| 209 | UnmapViewOfFile(handle->ptr); |
| 210 | free(handle); |
| 211 | } |
| 212 | |
| 213 | nbio_intf_t nbio_mmap_win32 = { |
| 214 | nbio_mmap_win32_open, |
| 215 | nbio_mmap_win32_begin_read, |
| 216 | nbio_mmap_win32_begin_write, |
| 217 | nbio_mmap_win32_iterate, |
| 218 | nbio_mmap_win32_resize, |
| 219 | nbio_mmap_win32_get_ptr, |
| 220 | nbio_mmap_win32_cancel, |
| 221 | nbio_mmap_win32_free, |
| 222 | "nbio_mmap_win32", |
| 223 | }; |
| 224 | #else |
| 225 | nbio_intf_t nbio_mmap_win32 = { |
| 226 | NULL, |
| 227 | NULL, |
| 228 | NULL, |
| 229 | NULL, |
| 230 | NULL, |
| 231 | NULL, |
| 232 | NULL, |
| 233 | NULL, |
| 234 | "nbio_mmap_win32", |
| 235 | }; |
| 236 | |
| 237 | #endif |