forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
executable file
·201 lines (165 loc) · 4.22 KB
/
utils.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
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
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package utils
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/Velocidex/ordereddict"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
errors "github.com/pkg/errors"
vjson "www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/types"
)
func InString(hay []string, needle string) bool {
for _, x := range hay {
if x == needle {
return true
}
}
return false
}
func StringSliceEq(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func hard_wrap(text string, colBreak int) string {
text = strings.TrimSpace(text)
text = strings.Replace(text, "\r\n", "\n", -1)
wrapped := ""
var i int
for i = 0; len(text[i:]) > colBreak; i += colBreak {
wrapped += text[i:i+colBreak] + "\n"
}
wrapped += text[i:]
return wrapped
}
func Stringify(value interface{}, scope vfilter.Scope, min_width int) string {
// Deal with pointers to things as those things.
if reflect.TypeOf(value).Kind() == reflect.Ptr {
return Stringify(reflect.Indirect(
reflect.ValueOf(value)).Interface(), scope, min_width)
}
if reflect.TypeOf(value).Kind() == reflect.Slice {
result := []string{}
a_value := reflect.ValueOf(value)
for i := 0; i < a_value.Len(); i++ {
result = append(
result, Stringify(
a_value.Index(i).Interface(), scope, min_width))
}
return strings.Join(result, "\n")
}
json_marshall := func(value interface{}) string {
if k, err := vjson.Marshal(value); err == nil {
if len(k) > 0 && k[0] == '"' && k[len(k)-1] == '"' {
k = k[1 : len(k)-1]
}
return hard_wrap(string(k), min_width)
}
return ""
}
switch t := value.(type) {
case ordereddict.Dict:
return t.String()
case map[string]interface{}:
result := []string{}
for k, v := range t {
result = append(result, fmt.Sprintf("%v: %v", k, v))
}
return strings.Join(result, "\n")
case types.StringProtocol:
return t.ToString(scope)
case []byte:
return hard_wrap(string(t), min_width)
case string:
return hard_wrap(t, min_width)
// If we have a custom marshaller we use it.
case json.Marshaler:
return json_marshall(value)
default:
// For normal structs json is a pretty good encoder.
if reflect.TypeOf(value).Kind() == reflect.Struct {
return json_marshall(value)
}
// Anything else we output something useful.
return hard_wrap(fmt.Sprintf("%v", value), min_width)
}
}
func SlicesEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for idx, a_item := range a {
if a_item != b[idx] {
return false
}
}
return true
}
// Force coersion to int64
func ToInt64(x interface{}) (int64, bool) {
switch t := x.(type) {
case bool:
if t {
return 1, true
} else {
return 0, true
}
case int:
return int64(t), true
case uint8:
return int64(t), true
case int8:
return int64(t), true
case uint16:
return int64(t), true
case int16:
return int64(t), true
case uint32:
return int64(t), true
case int32:
return int64(t), true
case uint64:
return int64(t), true
case int64:
return t, true
case float64:
return int64(t), true
default:
return 0, false
}
}
func ParseIntoProtobuf(source interface{}, destination proto.Message) error {
if source == nil {
return errors.New("Nil")
}
serialized, err := json.Marshal(source)
if err != nil {
return err
}
return jsonpb.UnmarshalString(
string(serialized), destination)
}