1
// SPDX-License-Identifier: Apache-2.0
2
// Copyright Authors of Tetragon
11
// UTF8FromBPFBytes transforms bpf (C) strings to valid utf-8 strings
13
// NB(kkourt): strings we get from BPF/kernel are C strings: null-terminated sequence of bytes. They
14
// may or may not be valid utf-8 strings. This is true for pathnames (cwd, binary) as well as
15
// program arguments. Many of these fields, however, are represented as the protobuf string type,
16
// which always has to be utf-8 encoded.
18
// This function ensures that by replacing all invalid runes with '�'.
20
// Note that this approach means that we loose information.
21
// Alternatively, we could use strconf.Quote() or similar to quote the strings but this would add
22
// overhead and it will also break the way we 've been representing strings up until now. A better
23
// solution would be to update the fields in the proto description to be bytes, and let the proto
24
// clients (e.g., tetra CLI and JSON writer) choose their preffered approach.
25
func UTF8FromBPFBytes(b []byte) string {
26
return strings.ToValidUTF8(string(b), "�")
29
func ParseSize(str string) (int, error) {
30
suffix := str[len(str)-1:]
32
if !strings.Contains("KMG", suffix) {
33
return strconv.Atoi(str)
36
val, err := strconv.Atoi(str[0 : len(str)-1])
43
return val * 1024, nil
45
return val * 1024 * 1024, nil
47
return val * 1024 * 1024 * 1024, nil