| 1 |
package sysenc |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"reflect" |
| 9 |
"slices" |
| 10 |
"unsafe" |
| 11 |
|
| 12 |
"github.com/cilium/ebpf/internal" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
func Marshal(data any, size int) (Buffer, error) { |
| 24 |
if data == nil { |
| 25 |
return Buffer{}, errors.New("can't marshal a nil value") |
| 26 |
} |
| 27 |
|
| 28 |
var buf []byte |
| 29 |
var err error |
| 30 |
switch value := data.(type) { |
| 31 |
case encoding.BinaryMarshaler: |
| 32 |
buf, err = value.MarshalBinary() |
| 33 |
case string: |
| 34 |
buf = unsafe.Slice(unsafe.StringData(value), len(value)) |
| 35 |
case []byte: |
| 36 |
buf = value |
| 37 |
case int16: |
| 38 |
buf = internal.NativeEndian.AppendUint16(make([]byte, 0, 2), uint16(value)) |
| 39 |
case uint16: |
| 40 |
buf = internal.NativeEndian.AppendUint16(make([]byte, 0, 2), value) |
| 41 |
case int32: |
| 42 |
buf = internal.NativeEndian.AppendUint32(make([]byte, 0, 4), uint32(value)) |
| 43 |
case uint32: |
| 44 |
buf = internal.NativeEndian.AppendUint32(make([]byte, 0, 4), value) |
| 45 |
case int64: |
| 46 |
buf = internal.NativeEndian.AppendUint64(make([]byte, 0, 8), uint64(value)) |
| 47 |
case uint64: |
| 48 |
buf = internal.NativeEndian.AppendUint64(make([]byte, 0, 8), value) |
| 49 |
default: |
| 50 |
if buf := unsafeBackingMemory(data); len(buf) == size { |
| 51 |
return newBuffer(buf), nil |
| 52 |
} |
| 53 |
|
| 54 |
buf, err = binary.Append(nil, internal.NativeEndian, value) |
| 55 |
} |
| 56 |
if err != nil { |
| 57 |
return Buffer{}, err |
| 58 |
} |
| 59 |
|
| 60 |
if len(buf) != size { |
| 61 |
return Buffer{}, fmt.Errorf("%T doesn't marshal to %d bytes", data, size) |
| 62 |
} |
| 63 |
|
| 64 |
return newBuffer(buf), nil |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func Unmarshal(data any, buf []byte) error { |
| 72 |
switch value := data.(type) { |
| 73 |
case encoding.BinaryUnmarshaler: |
| 74 |
return value.UnmarshalBinary(buf) |
| 75 |
|
| 76 |
case *string: |
| 77 |
*value = string(buf) |
| 78 |
return nil |
| 79 |
|
| 80 |
case *[]byte: |
| 81 |
|
| 82 |
*value = slices.Clone(buf) |
| 83 |
return nil |
| 84 |
|
| 85 |
default: |
| 86 |
if dataBuf := unsafeBackingMemory(data); len(dataBuf) == len(buf) { |
| 87 |
copy(dataBuf, buf) |
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
n, err := binary.Decode(buf, internal.NativeEndian, value) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
if n != len(buf) { |
| 97 |
return fmt.Errorf("unmarshaling %T doesn't consume all data", data) |
| 98 |
} |
| 99 |
|
| 100 |
return nil |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func unsafeBackingMemory(data any) []byte { |
| 110 |
if data == nil { |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
value := reflect.ValueOf(data) |
| 115 |
var valueSize int |
| 116 |
switch value.Kind() { |
| 117 |
case reflect.Pointer: |
| 118 |
if value.IsNil() { |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
if elemType := value.Type().Elem(); elemType.Kind() != reflect.Slice { |
| 123 |
valueSize = int(elemType.Size()) |
| 124 |
break |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
value = value.Elem() |
| 130 |
fallthrough |
| 131 |
|
| 132 |
case reflect.Slice: |
| 133 |
valueSize = int(value.Type().Elem().Size()) * value.Len() |
| 134 |
|
| 135 |
default: |
| 136 |
|
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if size := binary.Size(data); size == -1 || size != valueSize { |
| 144 |
|
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
if hasUnexportedFields(reflect.TypeOf(data)) { |
| 149 |
return nil |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
return unsafe.Slice((*byte)(value.UnsafePointer()), valueSize) |
| 161 |
} |