github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/uprobetracer/bytes.go (about) 1 // Copyright 2024 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package uprobetracer 16 17 import ( 18 "bytes" 19 "encoding/binary" 20 "errors" 21 "unsafe" 22 ) 23 24 func readFromBytes[T any](obj *T, rawData []byte) error { 25 if int(unsafe.Sizeof(*obj)) != len(rawData) { 26 return errors.New("reading from bytes: length mismatched") 27 } 28 buffer := bytes.NewBuffer(rawData) 29 err := binary.Read(buffer, binary.NativeEndian, obj) 30 if err != nil { 31 return err 32 } 33 return nil 34 } 35 36 func readStringFromBytes(data []byte, startPos uint32) string { 37 res := "" 38 for i := startPos; i < uint32(len(data)); i++ { 39 if data[i] == 0 { 40 return res 41 } 42 res += string(data[i]) 43 } 44 return "" 45 }