-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMFiles.asm
362 lines (330 loc) · 10.2 KB
/
MMFiles.asm
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
; Memory Mapped Files Library - By Net Walker!
; November, 1997
; Revised on May, 1998 - First Public Version.
.386P
LOCALS
JUMPS
.MODEL FLAT, STDCALL
UNICODE=0
INCLUDE W32.inc
PUBLIC MakeMappedFile
PUBLIC EndMappedFile
PUBLIC MMSetFileSize
PUBLIC MMGetFileHandle
PUBLIC MMGetFileSize
PUBLIC MMGetOriginalFileSize
PUBLIC ShowErrorMsg
EXTRN wsprintf : PROC
MAX_FILES EQU 5
DEFAULT_ACTION EQU OPEN_EXISTING
;---------------------------------------------------------------------------
.DATA
;***************************************************
DataBuffer:
FileHandle DD 0
FileSize DD 0
MemoryHandle DD 0
MemoryAddress DD 0
DB 16*MAX_FILES dup(0) ; Space for 5 file parameters
;****************************************************
StrBuffer DB MAX_PATH dup(0)
; Error Messages
Caption DB "Error",0
ErrorMsg1 DB "Cannot Open File <%s>",0
ErrorMsg2 DB "File <%s> Not Found",0
ErrorMsg3 DB "Cannot Create Memory Object For File <%s>",0
ErrorMsg4 DB "Cannot Map View of File <%s>",0
ErrorMsg5 DB "Invalid File Name",0
ErrorMsg6 DB "Path For File <%s> Not Found ",0
ErrorMsg7 DB "Invalid File ID", 0
ErrorMsg8 DB "File ID already in use",0
;---------------------------------------------------------------------------
.CODE
MakeMappedFile PROC FileID:DWORD, \ ; ID for the Mapped file
FileNameOff:DWORD, \ ; Pointer to file path+name
FSize:DWORD, \ ; Size for the file (if NULL, use current file size)
Action:DWORD ; dwCreationDistribution (if NULL, use DEFAULT_ACTION)
push ebx ecx edx esi edi
; verify if FileName is not empty
mov eax, FileNameOff
cmp byte ptr [eax], 0
jz @@InvalidFileName
call GetNewDataOffset, FileID ; get offset for storing file data
cmp eax, FALSE
jz @@ReturnError
mov esi, eax
; verify if Action is NULL
cmp Action, 0
jnz @@CreateFile
mov Action, DEFAULT_ACTION ; if NULL, use DEFAULT_ACTION
;-------------------
@@CreateFile: ; open file
call CreateFile, FileNameOff, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, NULL, Action, FILE_ATTRIBUTE_NORMAL, 0
cmp eax, INVALID_HANDLE_VALUE
jz @@CreateFileError
mov dword ptr [esi+0], eax ; store FileHandle
; get Original FileSize
call GetFileSize, dword ptr [esi+0], NULL
mov [esi+4], eax ; store original file size
; verify if FileSize parameter is NULL (if NULL, use original file size)
cmp FSize, NULL
jnz @@CreateFileMapping
mov eax, dword ptr [esi+4]
mov FSize, eax ; use original file size
;-------------------
@@CreateFileMapping: ; create Memory Object
call CreateFileMapping, dword ptr [esi+0], NULL, PAGE_READWRITE, 0, FSize, NULL
test eax, eax
jz @@CreateFileMappingError
mov dword ptr [esi+8], eax ; store MemoryHandle
;-------------------
; map view of file
call MapViewOfFile, dword ptr [esi+8], FILE_MAP_WRITE + FILE_MAP_READ, 0, 0, 0
test eax, eax
jz @@MapViewOfFileError
; Memory Mapped File successfully created
mov dword ptr [esi+12], eax ; store MemoryAddress
jmp @@End ; we'll return the File Address on memory
;-------------------
@@InvalidFileName:
call ShowErrorMsg, offset ErrorMsg5
jmp @@ReturnError
;-------------------
@@CreateFileError:
call GetLastError
cmp eax, ERROR_FILE_NOT_FOUND
jnz @@1
call wsprintf, offset StrBuffer, offset ErrorMsg2, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
jmp @@ReturnError
@@1:
cmp eax, ERROR_PATH_NOT_FOUND
jnz @@2
call wsprintf, offset StrBuffer, offset ErrorMsg6, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
jmp @@ReturnError
@@2:
call wsprintf, offset StrBuffer, offset ErrorMsg1, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
jmp @@ReturnError
;-------------------
@@CreateFileMappingError:
call EndMappedFile, FileID
call wsprintf, offset StrBuffer, offset ErrorMsg3, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
jmp @@ReturnError
;-------------------
@@MapViewOfFileError:
call EndMappedFile, FileID
call wsprintf, offset StrBuffer, offset ErrorMsg4, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
;-------------------
@@ReturnError:
mov eax, FALSE
;-------------------
@@End:
pop edi esi edx ecx ebx
ret
MakeMappedFile ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
MMGetFileHandle PROC FileID:DWORD
call GetDataOffset, FileID
cmp eax, FALSE
jz @@ReturnError
mov eax, dword ptr [eax]
jmp @@End
@@ReturnError:
mov eax, FALSE
@@End:
ret
MMGetFileHandle ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
MMGetFileSize PROC FileID:DWORD
push ebx ecx edx esi edi
call GetDataOffset, FileID
cmp eax, FALSE
jz @@ReturnError
call GetFileSize, dword ptr [eax], NULL
jmp @@End
@@ReturnError:
mov eax, FALSE
@@End:
pop edi esi edx ecx ebx
ret
MMGetFileSize ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
MMGetOriginalFileSize PROC FileID:DWORD
push ebx ecx edx esi edi
call GetDataOffset, FileID
cmp eax, FALSE
jz @@ReturnError
mov eax, dword ptr [eax+4] ; get stored original file size
@@ReturnError:
mov eax, FALSE
@@End:
pop edi esi edx ecx ebx
ret
MMGetOriginalFileSize ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
MMSetFileSize PROC FileID:DWORD, NewSize:DWORD ; memory handle might changes
LOCAL esi:DWORD
push ebx ecx edx esi edi
call GetDataOffset, FileID
cmp eax, FALSE
jz @@ReturnError
mov esi, eax
;-------------------
; close Memory Object
call FlushViewOfFile, dword ptr [esi+12], 0
call UnmapViewOfFile, dword ptr [esi+12]
call CloseHandle, dword ptr [esi+8] ; close memory handle
;-------------------
; adjust file size
call SetFilePointer, dword ptr [esi+0], NewSize, NULL, FILE_BEGIN
call SetEndOfFile, dword ptr [esi+0]
; re-create MemoryObject
;-------------------
call CreateFileMapping, dword ptr [esi+0], NULL, PAGE_READWRITE, 0, FSize, NULL
test eax, eax
jz @@CreateFileMappingError
mov dword ptr dword ptr [esi+8], eax ; store MemoryHandle
;-------------------
; map view of file
call MapViewOfFileEx, dword ptr [esi+8], FILE_MAP_WRITE + FILE_MAP_READ, 0, 0, 0, dword ptr [esi+12]
test eax, eax
jz @@MapViewOfFileError
mov dword ptr [esi+12], eax ; store MemoryAddress (probably will not change)
jmp @@End
;-------------------
@@CreateFileMappingError:
call EndMappedFile, FileID
call wsprintf, offset StrBuffer, offset ErrorMsg3, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
jmp @@ReturnError
;-------------------
@@MapViewOfFileError:
call EndMappedFile, FileID
call wsprintf, offset StrBuffer, offset ErrorMsg4, FileNameOff
add esp, 12
call ShowErrorMsg, offset StrBuffer
;-------------------
@@ReturnError:
mov eax, FALSE
@@End:
pop edi esi edx ecx ebx
ret
MMSetFileSize ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
EndMappedFile PROC FileID:DWORD
push ebx ecx edx esi edi
call GetDataOffset, FileID
cmp eax, FALSE
jz @@ReturnError
mov esi, eax
cmp dword ptr [esi], 0
jz @@Closed
;-------------------
; close Memory Object
call FlushViewOfFile, dword ptr [esi+12], 0
call UnmapViewOfFile, dword ptr [esi+12]
call CloseHandle, dword ptr [esi+8] ; close memory handle
;-------------------
; close file
call CloseHandle, dword ptr [esi+0] ; close file handle
;-------------------
; clear variables
mov dword ptr [esi+0], 0
mov dword ptr [esi+4], 0
mov dword ptr [esi+8], 0
mov dword ptr [esi+12], 0
jmp @@End
;-------------------
@@ReturnError:
mov eax, FALSE
jmp @@End
@@Closed:
mov eax, TRUE
@@End:
pop edi esi edx ecx ebx
ret
EndMappedFile ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
GetNewDataOffset PROC FileID:DWORD
push ebx ecx edx esi edi
mov eax, FileID
; verify if File ID < MAX_FILES
cmp eax, MAX_FILES
jg @@InvalidFileID
; get pointer for DataBuffer
mov ecx, 16 ; Size for each DataBuffer entry
mul ecx
add eax, offset DataBuffer
; verify if file ID already in use
cmp dword ptr [eax], 0
jnz @@FileIDAlreadyInUse
; Return esi
jmp @@End
;-------------------
@@InvalidFileID:
call ShowErrorMsg, offset ErrorMsg7
jmp @@ReturnError
;-------------------
@@FileIDAlreadyInUse:
call ShowErrorMsg, offset ErrorMsg8
;-------------------
@@ReturnError:
mov eax, FALSE
;-------------------
@@End:
pop edi esi edx ecx ebx
ret
GetNewDataOffset ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
GetDataOffset PROC FileID:DWORD
push ebx ecx edx esi edi
mov eax, FileID
; verify if File ID < MAX_FILES
cmp eax, MAX_FILES
jg @@InvalidFileID
; get pointer for DataBuffer
mov ecx, 16 ; Size for each DataBuffer entry
mul ecx
add eax, offset DataBuffer
; Return esi
jmp @@End
;-------------------
@@InvalidFileID:
call ShowErrorMsg, offset ErrorMsg7
;-------------------
@@ReturnError:
mov eax, FALSE
;-------------------
@@End:
pop edi esi edx ecx ebx
ret
GetDataOffset ENDP
;--------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------
ShowErrorMsg PROC MsgOff:DWORD
push eax
call MessageBeep, 0FFFFFFFFh
call MessageBox, NULL, MsgOff, offset Caption, MB_ICONHAND
pop eax
ret
ShowErrorMsg ENDP
;--------------------------------------------------------------------------------------
END