-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.cpp
248 lines (213 loc) · 6.45 KB
/
File.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "File.h"
#include <intrin.h>
typedef struct _iobuf
{
void *_Placeholder;
} FILE;
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0
#define EIO 5
#define FILE_POOL_DEFAULT_TAG 'file'
typedef struct _FILE_CONTROL_BLOCK
{
HANDLE hFile;
NTSTATUS err;
} FILE_CONTROL_BLOCK;
FILE_CONTROL_BLOCK *
fget_core(FILE *f)
{
return (FILE_CONTROL_BLOCK *)f->_Placeholder;
}
void
fset_core(FILE *f, FILE_CONTROL_BLOCK *fcb)
{
f->_Placeholder = fcb;
}
void *__cdecl fopen2(char const *_FileName, char const *_Mode)
{
NTSTATUS st;
HANDLE hFile;
char *path = NULL;
IO_STATUS_BLOCK isb;
OBJECT_ATTRIBUTES oa;
ANSI_STRING FileNameA;
UNICODE_STRING FileNameW;
ULONG AccessMask = 0;
ULONG ShareAccess = 0;
ULONG CreateOptions = 0;
ULONG CreateDisposition = 0;
{
char c_r[] = {'r', 0};
char c_rb[] = {'r', 'b', 0};
char c_w[] = {'w', 0};
char c_wb[] = {'w', 'b', 0};
char c_rw[] = {'r', 'w', 0};
char c_rwb[] = {'r', 'w', 'b', 0};
char c_ajia[] = {'a', '+', 0};
char c_ajiab[] = {'a', '+', 'b', 0};
char c_abjia[] = {'a', 'b', '+', 0};
const char *mode = _Mode;
if (!strcmp(mode, c_r) || !strcmp(mode, c_rb))
{
AccessMask = FILE_READ_DATA | FILE_READ_ATTRIBUTES;
CreateDisposition = FILE_OPEN;
}
else if (!strcmp(mode, c_w) || !strcmp(mode, c_wb))
{
AccessMask = FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES;
CreateDisposition = FILE_SUPERSEDE;
}
else if (!strcmp(mode, c_rw) || !strcmp(mode, c_rwb))
{
AccessMask = FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES;
CreateDisposition = FILE_OPEN_IF;
}
else if (!strcmp(mode, c_ajia) || !strcmp(mode, c_ajiab) || !strcmp(mode, c_abjia))
{
AccessMask =
FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_APPEND_DATA;
CreateDisposition = FILE_OPEN_IF;
}
else
{
return NULL;
}
AccessMask |= SYNCHRONIZE;
ShareAccess = FILE_SHARE_READ;
CreateOptions = FILE_SYNCHRONOUS_IO_NONALERT;
}
path = (char *)ExAllocatePoolWithTag(PagedPool, 512, FILE_POOL_DEFAULT_TAG);
if (!path)
return NULL;
RtlSecureZeroMemory(path, 512);
RtlCopyMemory(path, _FileName, min(512, strlen(_FileName)));
RtlInitAnsiString(&FileNameA, path);
if (!NT_SUCCESS(RtlAnsiStringToUnicodeString(&FileNameW, &FileNameA, TRUE)))
{
ExFreePoolWithTag(path, FILE_POOL_DEFAULT_TAG);
return NULL;
}
InitializeObjectAttributes(&oa, &FileNameW, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
st = ZwCreateFile(
&hFile,
AccessMask,
&oa,
&isb,
NULL,
FILE_ATTRIBUTE_NORMAL,
ShareAccess,
CreateDisposition,
CreateOptions,
NULL,
0);
ExFreePoolWithTag(path, FILE_POOL_DEFAULT_TAG);
path = NULL;
RtlFreeUnicodeString(&FileNameW);
if (!NT_SUCCESS(st))
return NULL;
FILE *f = (FILE *)ExAllocatePoolWithTag(PagedPool, sizeof(FILE), FILE_POOL_DEFAULT_TAG);
FILE_CONTROL_BLOCK *fcb =
(FILE_CONTROL_BLOCK *)ExAllocatePoolWithTag(PagedPool, sizeof(FILE_CONTROL_BLOCK), FILE_POOL_DEFAULT_TAG);
fcb->err = 0;
fcb->hFile = hFile;
fset_core(f, fcb);
return (void *)f;
}
__int64 __cdecl _ftelli64_2(FILE *_Stream)
{
IO_STATUS_BLOCK isb;
FILE_POSITION_INFORMATION fpi;
FILE_CONTROL_BLOCK *fcb = fget_core(_Stream);
fcb->err = ZwQueryInformationFile(fcb->hFile, &isb, &fpi, sizeof(fpi), FilePositionInformation);
if (NT_SUCCESS(fcb->err))
{
return fpi.CurrentByteOffset.QuadPart;
}
else
{
return -1;
}
}
int __cdecl _fseeki64_2(FILE *_Stream, __int64 _Offset, int _Origin)
{
IO_STATUS_BLOCK isb;
FILE_POSITION_INFORMATION fpi;
FILE_CONTROL_BLOCK *fcb = fget_core(_Stream);
switch (_Origin)
{
case SEEK_CUR: {
long cur_pos = ftell2(_Stream);
if (cur_pos >= 0)
{
fpi.CurrentByteOffset.QuadPart = cur_pos + _Offset;
fcb->err = ZwSetInformationFile(fcb->hFile, &isb, &fpi, sizeof(fpi), FilePositionInformation);
if (NT_SUCCESS(fcb->err))
{
return 0;
}
}
}
break;
case SEEK_END: {
FILE_STANDARD_INFORMATION fsi;
fcb->err = ZwQueryInformationFile(fcb->hFile, &isb, &fsi, sizeof(fsi), FileStandardInformation);
if (NT_SUCCESS(fcb->err))
{
fpi.CurrentByteOffset.QuadPart = fsi.EndOfFile.QuadPart + _Offset;
fcb->err = ZwSetInformationFile(fcb->hFile, &isb, &fpi, sizeof(fpi), FilePositionInformation);
if (NT_SUCCESS(fcb->err))
{
return 0;
}
}
}
break;
case SEEK_SET: {
fpi.CurrentByteOffset.QuadPart = _Offset;
fcb->err = ZwSetInformationFile(fcb->hFile, &isb, &fpi, sizeof(fpi), FilePositionInformation);
if (NT_SUCCESS(fcb->err))
{
return 0;
}
}
break;
default:
break;
}
return EIO;
}
int __cdecl fseek2(void *_Stream, long _Offset, int _Origin)
{
return _fseeki64_2((FILE *)_Stream, _Offset, _Origin);
}
long __cdecl ftell2(void *f)
{
return (long)_ftelli64_2((FILE *)f);
}
int __cdecl fclose2(void *f)
{
FILE_CONTROL_BLOCK *fcb = fget_core((FILE *)f);
ZwClose(fcb->hFile);
ExFreePoolWithTag(fcb, FILE_POOL_DEFAULT_TAG);
ExFreePoolWithTag(f, FILE_POOL_DEFAULT_TAG);
return 0;
}
size_t __cdecl fread2(void *Buffer, size_t ElementSize, size_t ElementCount, void *f)
{
IO_STATUS_BLOCK isb;
FILE_CONTROL_BLOCK *fcb = fget_core((FILE *)f);
size_t rd_size = ElementSize * ElementCount;
RtlSecureZeroMemory(&isb, sizeof(isb));
fcb->err = ZwReadFile(fcb->hFile, NULL, NULL, NULL, &isb, Buffer, (ULONG)rd_size, NULL, NULL);
return isb.Information;
}
size_t __cdecl fwrite2(void const *Buffer, size_t ElementSize, size_t ElementCount, void *f)
{
IO_STATUS_BLOCK isb;
FILE_CONTROL_BLOCK *fcb = fget_core((FILE *)f);
size_t wt_size = ElementSize * ElementCount;
RtlSecureZeroMemory(&isb, sizeof(isb));
fcb->err = ZwWriteFile(fcb->hFile, NULL, NULL, NULL, &isb, (PVOID)Buffer, (ULONG)wt_size, NULL, NULL);
return isb.Information;
}