-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bin.ahk
305 lines (305 loc) · 6.69 KB
/
Bin.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
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
class Bin
{
ObjDefine := {}
static sizeof := {Int64:8,Int:4,Short:2,Char:1,UInt:4,UShort:2,UChar:1,Double:8,Float:4,Ptr:A_PtrSize,UPtr:A_PtrSize}
static Encodings := {"":1+A_IsUnicode,"Utf-8":1,"Utf-16":2,base:{__call:Bin.getCharacterSize}}
class Region
{
__New(Offset=0,Size=0)
{
this.1 := Offset
this.2 := Size
}
_Test(Region)
{
return ( -(Region.1<0) | ( (Region.1 + Region.2) > this.2) )
}
}
class AccessFn
{
_GetIntend(Region="")
{
return Region?this.Region._Test(Region):0
}
_SetIntend(Region="")
{
if !IsObject(Region)
return 0
if (Region.2<0)
return -2
d := this.Region._Test(Region)
if (d>0)
return this._Resize(Region.1+Region.2)
return d
}
_Resize(Size)
{
if (Size!=this.Region.2)
{
if !IsObject(this.AllocedRegion)
this.AllocedRegion := this.Region
if !(Size>this.AllocedRegion.2)
this.Region.2 := Size
else
return !this.realloc(Size)
}
return 0
}
}
class AllocFn
{
alloc(Size=0)
{
this.Data := ""
this.SetCapacity("Data",Size)
this.Region := {1:(Size>0)? this.GetAddress("Data") : 0 ,2:Size,base:Bin.Region}
}
realloc(Size=0)
{
this.SetCapacity("Data",Size)
this.Region := {1:this.GetAddress("Data"),2:Size,base:Bin.Region}
return 1
}
AllocedRegion[]
{
get{
return {1:this.GetAddress("Data"),2:this.GetCapacity("Data"),base:Bin.Region}
}
}
}
class ObjHelper
{
Merge(Objects)
{
For Each,Obj in Objects
For Key,Value in Obj
This[Key] := Value
}
}
class LinkerFn
{
class File
{
ToFile(FileName,Encoding="")
{
if this._GetIntend()
return
oFile := Encoding ? FileOpen( FileName, "w", Encoding ) : FileOpen( FileName, "w" )
oFile.RawWrite( This.Region.1, This.Region.2 )
oFile.Close()
return this
}
FromFile(FileName,Encoding="")
{
oFile := Encoding ? FileOpen( FileName, "r", Encoding ) : FileOpen( FileName, "r" )
if this._SetIntend([0,oFile.Length])
return
Size := oFile.RawRead( (this.region.1+0), oFile.Length )
this._Resize( Size )
return this
}
}
class Str
{
StrGet(Encoding="")
{
if this._GetIntend()
return
max := floor(this.region.2/Bin.Encodings[ Encoding ])
if Encoding
ret := StrGet( (this.Region.1+0), max, Encoding)
else
ret := StrGet( (this.Region.1+0), max)
return ret
}
StrPut(String,Encoding="")
{
Len := Encoding ? StrPut( String, Encoding ) : StrPut(String)
if this._SetIntend( [ 0, Len * Bin.Encodings[ Encoding ] ] )
return
if (Encoding)
StrPut(String,(this.Region.1+0),Encoding)
else
StrPut(String,(this.Region.1+0))
return this
}
Str[]{
get{
return this.StrGet()
}
set{
return this.StrPut(value)
}
}
}
class Crypt
{
CryptTo(Flags)
{
if this._GetIntend()
return
DllCall("crypt32\CryptBinaryToString", "ptr", this.Region.1, "uint", this.Region.2, "uint", Flags, "ptr", 0, "uint*", Size)
oBuf := Bin.alloc(Size*(A_IsUnicode+1))
if oBuf._SetIntend([0,Size*(A_IsUnicode+1)])
return
DllCall("crypt32\CryptBinaryToString", "ptr", this.Region.1, "uint", this.Region.2, "uint", Flags, "ptr", oBuf.Region.1, "uint*", Size)
return oBuf
}
CryptFrom(oBuf,Flags)
{
if oBuf._GetIntend()
return
DllCall("crypt32\CryptStringToBinary", "ptr", oBuf.Region.1, "uint", 0, "uint", Flags, "ptr", 0, "uint*", Size, "ptr", 0, "ptr", 0)
if this._SetIntend([0,Size])
return
DllCall("crypt32\CryptStringToBinary", "ptr", oBuf.Region.1, "uint", 0, "uint", Flags, "ptr", this.Region.1, "uint*", Size, "ptr", 0, "ptr", 0)
return this
}
Hex[]{
set{
return This.CryptFrom(value,4)
}
get{
return This.CryptTo(4)
}
}
base64[]{
set{
return This.CryptFrom(value,1)
}
get{
return This.CryptTo(1)
}
}
}
class Num
{
NumGet(OffSet=0,Type="UPtr")
{
if this._GetIntend([OffSet,Bin.sizeof[Type]])
return
return NumGet((this.Region.1+0),OffSet,Type)
}
NumPut(Num,Offset=0,Type="UPtr")
{
if this._SetIntend([Offset,Bin.sizeof[type]])
return
NumPut(Num,(this.Region.1+0),OffSet,Type)
return this
}
}
class Rtl
{
MoveTo(Target)
{
if this._GetIntend()
return
if IsObject(Target)
{
if Target._SetIntend([0,this.Region.2])
return
DllCall("RtlMoveMemory", "ptr", Target.Region.1,"ptr", this.Region.1, "ptr", this.Region.2,"Cdecl")
}
else
DllCall("RtlMoveMemory", "ptr", Target,"ptr", this.Region.1, "ptr", this.Region.2,"Cdecl")
}
MoveFrom(Source,Size="")
{
if IsObject(Source)
{
if Source._GetIntend()
return
if This._SetIntend([0,Source.Region.2])
return
DllCall("RtlMoveMemory", "ptr", this.Region.1,"ptr", Source.Region.1, "ptr", Source.Region.2,"Cdecl")
}
else
{
if This._SetIntend([0,Size])
return
DllCall("RtlMoveMemory", "ptr", this.Region.1,"ptr", Source, "ptr", Size,"Cdecl")
}
}
Zero(Size="")
{
Size := Size ? Size : this.Region.2
if This._SetIntend([0,Size])
return
DllCall("RtlZeroMemory","ptr",this.region.1,"ptr",Size,"Cdecl")
}
Copy()
{
return Bin.MoveFrom(This)
}
}
class Struct
{
Offset(Offset,Size="")
{
Size:=(Size ? Size : this.Region.2-Offset)
if this._SetIntend([Offset,Size])
return
oBuf := Bin.Build("wrapper")
oBuf.Region := {1:this.Region.1 + Offset,2: Size ,base:bin.Region}
return oBuf
}
}
}
getCharacterSize(Encoding="")
{
static Test,Init := VarSetCapacity(Test,4*3,0)
StrPut(" ",&Test,3,Encoding)
For Each,CharType in {1:"UChar",2:"UShort",4:"UInt"}
if (NumGet(Test,0,CharType)=NumGet(Test,Each,CharType))
return this[Encoding] := Each
return this[Encoding] := ""
}
Define(Key,Obj)
{
if (Key == "default")
{
this.Actions := new this.ObjHelper()
this.Actions.Merge(Obj)
}
This.ObjDefine[Key] := Obj
}
Build(Key="default")
{
This.ObjHelper.Merge.Call( oBinBase := {}, This.ObjDefine[Key] )
oBin := {base:oBinBase}
return oBin
}
Call(Id,p*)
{
if (!ObjHasKey(This,Id)&&this.Actions.HasKey(Id))
{
oBin := this.Build()
oBin.Alloc()
(oBin[Id]).Call(oBin,p*)
return oBin
}
}
Set(Id,value)
{
if (!ObjHasKey(This,Id)&&this.Actions.HasKey(Id))
{
oBin := this.Build()
oBin.Alloc()
oBin[Id] := value
return oBin
}
}
Init()
{
Static a:= Bin.Init()
LinkerFn := {}
For each,Format in this.LinkerFn
{
If IsObject(Format)
This.ObjHelper.Merge.Call(LinkerFn,[Format])
}
This.Define("default",[This.AccessFn,This.AllocFn,LinkerFn])
THis.Define("wrapper",[This.AccessFn,LinkerFn])
This.base := {__Call:this.Call,__Set:this.Set}
}
}