-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarbage.go
58 lines (53 loc) · 1.1 KB
/
garbage.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
package ssce
func (e *Encoder) garbageInst() []byte {
if e.opts.NoGarbage {
return nil
}
switch e.rand.Intn(2) {
case 0:
return nil
case 1:
return e.garbageJumpShort(2, 16)
default:
panic("invalid garbage instruction selection")
}
}
func (e *Encoder) garbageInstShort() []byte {
if e.opts.NoGarbage {
return nil
}
switch e.rand.Intn(2) {
case 0:
return nil
case 1:
return e.garbageJumpShort(2, 5)
default:
panic("invalid garbage instruction selection")
}
}
// jmp short [4-128)
func (e *Encoder) garbageJumpShort(min, max int) []byte {
if e.opts.NoGarbage {
return nil
}
if min < 1 || max > 127 {
panic("garbage jump short length out of range")
}
jmp := make([]byte, 0, 1+max/2)
offset := min + e.rand.Intn(max-min+1)
jmp = append(jmp, 0xEB, byte(offset))
jmp = append(jmp, e.randBytes(offset)...)
return jmp
}
func (e *Encoder) randBytes(n int) []byte {
buf := make([]byte, n)
_, _ = e.rand.Read(buf)
return buf
}
func (e *Encoder) randString(n int) []byte {
buf := make([]byte, n)
for i := 0; i < n; i++ {
buf[i] = byte(32 + e.rand.Intn(95)) // [32, 126]
}
return buf
}