-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPEDecreaseFileSize.asm
127 lines (109 loc) · 3.56 KB
/
PEDecreaseFileSize.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
;==============================================================================
;
; PE Library
;
; Copyright (c) 2019 by fearless
;
; http://github.com/mrfearless
;
;==============================================================================
.686
.MMX
.XMM
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
include PE.inc
.CODE
PE_ALIGN
;------------------------------------------------------------------------------
; Decrease (resize) PE file. Adjustments to pointers and other data to be handled by
; other functions. Move data before calling this.
; Returns: TRUE on success or FALSE otherwise.
;------------------------------------------------------------------------------
PEDecreaseFileSize PROC USES EBX hPE:DWORD, dwNewSize:DWORD
LOCAL bReadOnly:DWORD
LOCAL PEFilesize:DWORD
LOCAL hPEFile:DWORD
LOCAL PEMemMapHandle:DWORD
LOCAL PEMemMapPtr:DWORD
LOCAL PENewFileSize:DWORD
LOCAL PENewMemMapHandle:DWORD
LOCAL PENewMemMapPtr:DWORD
.IF hPE == NULL || dwNewSize == 0
xor eax, eax
ret
.ENDIF
;---------------------------------------------------
; Get existing file, map and view handles
;---------------------------------------------------
mov ebx, hPE
mov eax, [ebx].PEINFO.PEFilesize
.IF dwNewSize > eax ; if size is greater than existing file's size
xor eax, eax
ret
.ENDIF
mov PEFilesize, eax
mov eax, [ebx].PEINFO.PEOpenMode
mov bReadOnly, eax
mov eax, [ebx].PEINFO.PEFileHandle
mov hPEFile, eax
mov eax, [ebx].PEINFO.PEMemMapHandle
mov PEMemMapHandle, eax
mov eax, [ebx].PEINFO.PEMemMapPtr
mov PEMemMapPtr, eax
;---------------------------------------------------
; Close existing mapping
;---------------------------------------------------
Invoke UnmapViewOfFile, PEMemMapPtr
Invoke CloseHandle, PEMemMapHandle
Invoke SetFilePointer, hPEFile, dwNewSize, 0, FILE_BEGIN
Invoke SetEndOfFile, hPEFile
Invoke FlushFileBuffers, hPEFile
;---------------------------------------------------
; Create file mapping of new size
;---------------------------------------------------
mov eax, dwNewSize
mov PENewFileSize, eax
.IF bReadOnly == TRUE
Invoke CreateFileMapping, hPEFile, NULL, PAGE_READONLY, 0, 0, NULL ; Create memory mapped file
.ELSE
Invoke CreateFileMapping, hPEFile, NULL, PAGE_READWRITE, 0, 0, NULL ; Create memory mapped file
.ENDIF
.IF eax == NULL
xor eax, eax
ret
.ENDIF
mov PENewMemMapHandle, eax
;---------------------------------------------------
; Map the view
;---------------------------------------------------
.IF bReadOnly == TRUE
Invoke MapViewOfFileEx, PENewMemMapHandle, FILE_MAP_READ, 0, 0, 0, NULL
.ELSE
Invoke MapViewOfFileEx, PENewMemMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL
.ENDIF
.IF eax == NULL
Invoke CloseHandle, PENewMemMapHandle
xor eax, eax
ret
.ENDIF
mov PENewMemMapPtr, eax
;---------------------------------------------------
; Update handles and information
;---------------------------------------------------
mov ebx, hPE
mov eax, PENewMemMapPtr
mov [ebx].PEINFO.PEMemMapPtr, eax
mov eax, PENewMemMapHandle
mov [ebx].PEINFO.PEMemMapHandle, eax
mov eax, PENewFileSize
mov [ebx].PEINFO.PEFilesize, eax
mov eax, TRUE
ret
PEDecreaseFileSize ENDP
PE_LIBEND