tetragon

Форк
0
/
strutls.go 
52 строки · 1.5 Кб
1
// SPDX-License-Identifier: Apache-2.0
2
// Copyright Authors of Tetragon
3

4
package strutils
5

6
import (
7
	"strconv"
8
	"strings"
9
)
10

11
// UTF8FromBPFBytes transforms bpf (C) strings to valid utf-8 strings
12
//
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.
17
//
18
// This function ensures that by replacing all invalid runes with '�'.
19
//
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), "�")
27
}
28

29
func ParseSize(str string) (int, error) {
30
	suffix := str[len(str)-1:]
31

32
	if !strings.Contains("KMG", suffix) {
33
		return strconv.Atoi(str)
34
	}
35

36
	val, err := strconv.Atoi(str[0 : len(str)-1])
37
	if err != nil {
38
		return 0, err
39
	}
40

41
	switch suffix {
42
	case "K":
43
		return val * 1024, nil
44
	case "M":
45
		return val * 1024 * 1024, nil
46
	case "G":
47
		return val * 1024 * 1024 * 1024, nil
48
	}
49

50
	// never reached
51
	return 0, nil
52
}
53

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.