-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvmem.go
45 lines (39 loc) · 1.03 KB
/
vmem.go
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
package hinako
type virtualAllocatedMemory struct {
Addr uintptr
Size uint
oldProtect uintptr
}
func newVirtualAllocatedMemory(size uint, protectLevel int) (*virtualAllocatedMemory, error) {
addr, _, err := virtualAlloc.Call(0, uintptr(size), _MEM_COMMIT, uintptr(protectLevel))
if addr == 0 {
return nil, err
}
return &virtualAllocatedMemory{Addr: addr, Size: size}, nil
}
func (vmem *virtualAllocatedMemory) Read(p []byte) (int, error) {
err := unsafeReadMemory(vmem.Addr, p)
if err != nil {
return 0, err
}
return len(p), nil
}
func (vmem *virtualAllocatedMemory) Write(p []byte) (int, error) {
err := unsafeWriteMemory(vmem.Addr, p)
if err != nil {
return 0, err
}
return len(p), nil
}
func (vmem *virtualAllocatedMemory) WriteAt(p []byte, off int64) (int, error) {
err := unsafeWriteMemory(vmem.Addr+uintptr(off), p)
if err != nil {
return 0, err
}
return len(p), nil
}
func (vmem *virtualAllocatedMemory) Close() {
if r, _, err := virtualFree.Call(vmem.Addr, 0, _MEM_RELEASE); r == 0 {
panic(err)
}
}