1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (nbio_windowsmmap.c).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
23 #include <file/nbio.h>
26 #if defined(_MSC_VER) && _MSC_VER >= 1500
28 #ifndef HAVE_MMAP_WIN32
29 #define HAVE_MMAP_WIN32
32 #elif !defined(_MSC_VER)
34 #ifndef HAVE_MMAP_WIN32
35 #define HAVE_MMAP_WIN32
41 #if defined(HAVE_MMAP_WIN32)
46 #include <encodings/utf.h>
50 /* Assume W-functions do not work below Win2K and Xbox platforms */
51 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
59 #ifndef FILE_SHARE_ALL
60 #define FILE_SHARE_ALL (FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE)
63 struct nbio_mmap_win32_t
71 static void *nbio_mmap_win32_open(const char * filename, unsigned mode)
73 static const DWORD dispositions[] = { OPEN_EXISTING, CREATE_ALWAYS, OPEN_ALWAYS, OPEN_EXISTING, CREATE_ALWAYS };
75 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500
80 struct nbio_mmap_win32_t* handle = 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);
87 wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename);
89 HANDLE file = CreateFile2(filename_wide, access, FILE_SHARE_ALL, dispositions[mode], NULL);
91 HANDLE file = CreateFileW(filename_wide, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL);
98 if (file == INVALID_HANDLE_VALUE)
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);
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);
114 handle = (struct nbio_mmap_win32_t*)malloc(sizeof(struct nbio_mmap_win32_t));
117 handle->is_write = is_write;
118 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500
119 handle->len = len.QuadPart;
128 static void nbio_mmap_win32_begin_read(void *data)
133 static void nbio_mmap_win32_begin_write(void *data)
138 static bool nbio_mmap_win32_iterate(void *data)
144 static void nbio_mmap_win32_resize(void *data, size_t len)
146 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0500
147 LARGE_INTEGER len_li;
152 struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data;
157 if (len < handle->len)
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
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);
172 SetFilePointer(handle->file, len_li, NULL, FILE_BEGIN);
175 if (!SetEndOfFile(handle->file))
176 abort(); /* this one returns void and I can't find any other way for it to report failure */
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);
188 static void *nbio_mmap_win32_get_ptr(void *data, size_t* len)
190 struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data;
198 static void nbio_mmap_win32_cancel(void *data)
203 static void nbio_mmap_win32_free(void *data)
205 struct nbio_mmap_win32_t* handle = (struct nbio_mmap_win32_t*)data;
208 CloseHandle(handle->file);
209 UnmapViewOfFile(handle->ptr);
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,
225 nbio_intf_t nbio_mmap_win32 = {