-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinnt_mapped_file.c
94 lines (81 loc) · 1.73 KB
/
winnt_mapped_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "winnt_utils.h"
#include "mapped_file.h"
struct winnt_mapped_file
{
int64_t length;
void* data;
/* above fields are "inherited"! */
HANDLE hFile;
HANDLE hFileMapping;
};
int munmap_file(struct mapped_file *amf)
{
struct winnt_mapped_file* mf = (struct winnt_mapped_file*)amf;
if (mf->data && (0 == UnmapViewOfFile(mf->data)))
{
Error(TEXT("UnmapViewOfFile"));
goto error;
}
mf->data = 0;
if (mf->hFileMapping)
CloseHandle(mf->hFileMapping);
mf->hFileMapping = 0;
if (mf->hFile)
CloseHandle(mf->hFile);
mf->hFile = 0;
free(mf);
return 0;
error:
return 1;
}
struct mapped_file * mmap_file(const char* path)
{
struct winnt_mapped_file *mf = (struct winnt_mapped_file *)malloc(sizeof(struct winnt_mapped_file));
mf->hFile = 0;
mf->hFileMapping = 0;
mf->data = 0;
BSTR unicodestr = getBSTR(path);
if (!unicodestr)
goto error;
mf->hFile = CreateFile(
unicodestr,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
SysFreeString(unicodestr);
if (INVALID_HANDLE_VALUE == mf->hFile)
{
Error(TEXT("CreateFile"));
goto error;
}
if (0 == GetFileSizeEx(mf->hFile, (PLARGE_INTEGER)&(mf->length)))
{
Error(TEXT("GetFileSizeEx"));
goto error;
}
mf->hFileMapping = CreateFileMapping(
mf->hFile,
NULL,
PAGE_READONLY,
0,
0,
NULL);
if (!mf->hFileMapping)
{
Error(TEXT("CreateFileMapping"));
goto error;
}
mf->data = MapViewOfFile(mf->hFileMapping, FILE_MAP_READ, 0, 0, 0);
if (!mf->data)
{
Error(TEXT("MapViewOfFile"));
goto error;
}
return (struct mapped_file*)mf;
error:
munmap_file((struct mapped_file*)mf);
return NULL;
}