1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2020 The GemRB Project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 */
17 #include <cassert>
18
19 #ifndef WIN32
20 #include <sys/mman.h>
21 #endif
22
23 #include "MappedFileMemoryStream.h"
24 #include "System/VFS.h"
25
26 namespace GemRB {
27
MappedFileMemoryStream(const std::string & fileName)28 MappedFileMemoryStream::MappedFileMemoryStream(const std::string& fileName)
29 : MemoryStream(fileName.c_str(), nullptr, 0),
30 fileHandle(nullptr),
31 fileOpened(false),
32 fileMapped(false)
33 {
34 #ifdef WIN32
35 TCHAR t_name[MAX_PATH] = {0};
36 mbstowcs(t_name, fileName.c_str(), MAX_PATH - 1);
37
38 this->fileHandle =
39 CreateFile(
40 t_name,
41 GENERIC_READ,
42 FILE_SHARE_READ,
43 nullptr,
44 OPEN_EXISTING,
45 FILE_ATTRIBUTE_NORMAL,
46 nullptr
47 );
48 this->fileOpened = fileHandle != INVALID_HANDLE_VALUE;
49
50 if (fileOpened) {
51 LARGE_INTEGER fileSize;
52 GetFileSizeEx(fileHandle, &fileSize);
53 assert(fileSize.QuadPart <= ULONG_MAX);
54 size = static_cast<unsigned long>(fileSize.QuadPart);
55 }
56 #else
57 this->fileHandle = fopen(fileName.c_str(), "rb");
58 this->fileOpened = fileHandle != nullptr;
59
60 if (fileOpened) {
61 struct stat statData{};
62 int ret = fstat(fileno(static_cast<FILE*>(fileHandle)), &statData);
63 assert(ret != -1);
64 this->size = statData.st_size;
65 }
66 #endif
67
68 if (fileOpened) {
69 this->data = static_cast<char*>(readonly_mmap(fileHandle));
70 this->fileMapped = data != nullptr;
71 }
72 }
73
isOk() const74 bool MappedFileMemoryStream::isOk() const {
75 return fileOpened && fileMapped;
76 }
77
Clone()78 DataStream* MappedFileMemoryStream::Clone() {
79 return new MappedFileMemoryStream(originalfile);
80 }
81
Read(void * dest,unsigned int length)82 int MappedFileMemoryStream::Read(void* dest, unsigned int length) {
83 if (!fileMapped) {
84 return GEM_ERROR;
85 }
86
87 return MemoryStream::Read(dest, length);
88 }
89
Seek(int pos,int startPos)90 int MappedFileMemoryStream::Seek(int pos, int startPos) {
91 if (!fileMapped) {
92 return GEM_ERROR;
93 }
94
95 return MemoryStream::Seek(pos, startPos);
96 }
97
Write(const void *,unsigned int)98 int MappedFileMemoryStream::Write(const void*, unsigned int) {
99 return GEM_ERROR;
100 }
101
~MappedFileMemoryStream()102 MappedFileMemoryStream::~MappedFileMemoryStream() {
103 if (fileMapped) {
104 munmap(data, size);
105 }
106
107 this->data = nullptr;
108
109 if (fileOpened) {
110 #ifdef WIN32
111 CloseHandle(fileHandle);
112 #else
113 fclose(static_cast<FILE*>(fileHandle));
114 #endif
115 }
116 }
117
118 }
119