-
Notifications
You must be signed in to change notification settings - Fork 5
/
env.go
84 lines (69 loc) · 1.88 KB
/
env.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
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
package gopwn
type ENVOptions struct {
CyclicOptions
}
type env struct {
arch Arch
endian Endian
cyclicOptions CyclicOptions
assembler *Assembler
disassembler *Disassembler
}
func NewEnv(arch Arch, endian Endian, optFns ...func(o *ENVOptions)) (*env, error) {
options := ENVOptions{}
for _, fn := range optFns {
fn(&options)
}
assembler, err := NewAssembler(arch)
if err != nil {
return nil, err
}
disassembler, err := NewDisassembler(arch)
if err != nil {
return nil, err
}
return &env{
arch: arch,
endian: endian,
assembler: assembler,
disassembler: disassembler,
}, nil
}
func NewEnvFromBinary(path string, optFns ...func(o *ENVOptions)) (*env, error) {
elf, err := NewELF(path)
if err != nil {
return nil, err
}
defer elf.Close()
return NewEnv(elf.Architecture(), elf.Endianness(), optFns...)
}
func (e *env) Arch() Arch {
return e.arch
}
func (e *env) Endianness() Endian {
return e.endian
}
func (e *env) Close() error {
if err := e.assembler.Close(); err != nil {
return err
}
return e.disassembler.Close()
}
func (e *env) Cyclic(length int, optFns ...func(o *CyclicOptions)) []byte {
var envOpsFuncs []func(o *CyclicOptions)
envOpsFuncs = append(envOpsFuncs, func(o *CyclicOptions) { o = &e.cyclicOptions }) //nolint
envOpsFuncs = append(envOpsFuncs, optFns...)
return Cyclic(length, envOpsFuncs...)
}
func (e *env) CyclicFind(subseq []byte, optFns ...func(o *CyclicOptions)) int {
var envOpsFuncs []func(o *CyclicOptions)
envOpsFuncs = append(envOpsFuncs, func(o *CyclicOptions) { o = &e.cyclicOptions }) //nolint
envOpsFuncs = append(envOpsFuncs, optFns...)
return CyclicFind(subseq, envOpsFuncs...)
}
func (e *env) Assemble(assembly string) ([]byte, error) {
return e.assembler.Assemble(assembly)
}
func (e *env) Disam(data []byte, vma uint64) (string, error) {
return e.disassembler.Disam(data, vma)
}