-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_MemoryBuffer.ahk
175 lines (136 loc) · 4.41 KB
/
class_MemoryBuffer.ahk
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
/*
* Abstraction of basic memory buffer handling
*
*/
class MemoryBuffer
{
static HEAP_NO_SERIALIZE := 0x00000001
static HEAP_GENERATE_EXCEPTIONS := 0x00000004
static HEAP_ZERO_MEMORY := 0x00000008
static HEAP_CREATE_ENABLE_EXECUTE := 0x00040000
static Heap := 0 ; Heap handle
Address := 0 ; lp to our buffer
Size := 0 ; size of the buffer
/*
* Creates a new MemoryBuffer from the existing buffer
*
* srcPtr pointer to the source
* size size of the source
*/
Create(srcPtr, size)
{
buf := new MemoryBuffer()
buf.AllocMemory( size )
buf.memcpy( buf.Address, srcPtr, size )
return buf
}
/*
* Creates a new MemoryBuffer from the given file
*/
CreateFromFile(filePath){
buf := new MemoryBuffer()
if(!FileExist(filePath))
throw Exception("File must exist and be readable!")
binFile := FileOpen(filePath, "r")
buf.AllocMemory(binFile.Length)
buf.Size := binFile.RawRead(buf.Address+0, binFile.Length)
binFile.Close()
return buf
}
/*
* Create a new MemoryBuffer from the given base64 encoded data string
*/
CreateFromBase64(base64str){
static CryptStringToBinary := "Crypt32.dll\CryptStringToBinary" (A_IsUnicode ? "W" : "A")
static CRYPT_STRING_BASE64 := 0x00000001
buf := new MemoryBuffer()
len := StrLen(base64str)
DllCall(CryptStringToBinary, "ptr",&base64str, "uint", len, "uint", CRYPT_STRING_BASE64, "ptr",0, "uint*",cp, "ptr",0,"ptr",0) ; get size
buf.AllocMemory(cp)
DllCall(CryptStringToBinary, "ptr",&base64str, "uint", len, "uint", CRYPT_STRING_BASE64, "ptr", buf.Address, "uint*",cp, "ptr",0,"ptr",0)
return buf
}
GetPtr(){
return this.Address
}
/*
* Write the binary buffer to a file
*
* returns true on succes, otherwise false
*/
WriteToFile(filePath){
binFile := FileOpen(filePath, "rw")
bytesWritten := binFile.RawWrite(this.Address+0, this.Size)
binFile.Close()
return (bytesWritten == this.Size) ; we expect that all bytes were written down
}
/*
* Free this Buffer, releases the reserved memory
*/
Free(){
if(this.IsValid())
{
DllCall("HeapFree"
, Ptr, this.Heap
, Ptr, 0
, Ptr, this.Address)
this.Address := 0
this.Size := 0
}
}
/*
* Is this Buffer a valid allocation of memory?
*/
IsValid(){
return (this.Heap != 0 && this.Address != 0)
}
/*
* Returns a base64 encoded string of this buffer
*/
ToBase64(){
static CryptBinaryToString := "Crypt32.dll\CryptBinaryToString" (A_IsUnicode ? "W" : "A")
static CRYPT_STRING_BASE64 := 0x00000001
DllCall(CryptBinaryToString, "ptr",this.Address, "uint",this.Size, "uint",CRYPT_STRING_BASE64, "ptr",0, "uint*",cp) ; get size
VarSetCapacity(str, cp*(A_IsUnicode ? 2:1))
DllCall(CryptBinaryToString, "ptr",this.Address, "uint",this.Size, "uint",CRYPT_STRING_BASE64, "str",str, "uint*",cp)
return str
}
/*
* Destructor
*/
__Delete()
{
this.Free()
}
ToString(){
return "MemoryBuffer: @ " this.GetPtr() " size: " this.Size " bytes"
}
memcpy(dst, src, cnt) {
return DllCall("MSVCRT\memcpy"
, Ptr, dst
, Ptr, src
, Ptr, cnt)
}
/*
* Allocates the requested size of memory
* returns the base adress of the new reserved memory
*/
AllocMemory(size){
if(!MemoryBuffer.Heap)
{
;MemoryBuffer.Heap := DllCall("GetProcessHeap", Ptr)
MemoryBuffer.Heap := DllCall("HeapCreate"
, UInt, 0 ; heap options
, Ptr, 0 ; initital size: 0 = 1 page
, Ptr, 0, Ptr) ; max size: 0 = no limit
}
if(!this.Address)
this.Free()
this.Address := DllCall("HeapAlloc"
, Ptr, MemoryBuffer.Heap
, UInt, MemoryBuffer.HEAP_ZERO_MEMORY
, Ptr, size, Ptr)
this.Size := size
;MsgBox % "allocated " size " bytes memory @" this.Address
}
}