forked from fkie-cad/yapscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
68 lines (58 loc) · 1.65 KB
/
helper.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
package yapscan
import (
"fmt"
"reflect"
"strings"
"github.com/hillu/go-yara/v4"
)
// AddressesFromMatches returns one value for each given yara.MatchString.
// The returned values are equal to the given offset plus the Offset field of
// each yara.MatchString.
func AddressesFromMatches(matches []yara.MatchString, offset uint64) []uint64 {
addrs := make([]uint64, len(matches))
for i, m := range matches {
addrs[i] = m.Offset + offset
}
return addrs
}
// FormatSlice calls fmt.Sprintf(format, element, args...) for each
// element in the given slice. The returned string slice contains the
// formatted output.
func FormatSlice(format string, slice interface{}, args ...interface{}) []string {
ref := reflect.ValueOf(slice)
if ref.Kind() != reflect.Slice {
panic("argument \"slice\" must be a slice")
}
strs := make([]string, ref.Len())
for i := 0; i < ref.Len(); i++ {
printfArgs := make([]interface{}, 1, len(args)+1)
printfArgs[0] = ref.Index(i).Interface()
printfArgs = append(printfArgs, args...)
strs[i] = fmt.Sprintf(format, printfArgs...)
}
return strs
}
// Join joins all elements of a string slice, using the defaultGlue
// for all but the last two elements.
func Join(parts []string, defaultGlue, finalGlue string) string {
switch len(parts) {
case 0:
return ""
case 1:
return parts[0]
}
n := (len(parts)-2)*len(defaultGlue) + len(finalGlue)
for i := range parts {
n += len(parts[i])
}
var b strings.Builder
b.Grow(n)
b.WriteString(parts[0])
for _, part := range parts[1 : len(parts)-1] {
b.WriteString(defaultGlue)
b.WriteString(part)
}
b.WriteString(finalGlue)
b.WriteString(parts[len(parts)-1])
return b.String()
}