-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_other.go
55 lines (46 loc) · 1.2 KB
/
common_other.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// +build !386,!amd64,!amd64p32
package socket_tracer
import (
"errors"
"unsafe"
)
var errBadSize = errors.New("bad size for integer")
func copyInt(dst unsafe.Pointer, src unsafe.Pointer, len uint8) error {
copy((*(*[maxIntSizeBytes]byte)(src))[:len], (*(*[maxIntSizeBytes]byte)(src))[:len])
return nil
}
func readInt(ptr unsafe.Pointer, len uint8, signed bool) (value interface{}, err error) {
asSlice := (*(*[maxIntSizeBytes]byte)(ptr))[:]
switch len {
case 1:
if signed {
value = int8(asSlice[0])
} else {
value = uint8(asSlice[0])
}
case 2:
if signed {
value = int16(MachineEndian.Uint16(asSlice))
} else {
value = MachineEndian.Uint16(asSlice)
}
case 4:
if signed {
value = int32(MachineEndian.Uint32(asSlice))
} else {
value = MachineEndian.Uint32(asSlice)
}
case 8:
if signed {
value = int64(MachineEndian.Uint64(asSlice))
} else {
value = MachineEndian.Uint64(asSlice)
}
default:
return nil, errBadSize
}
return
}