forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdr.go
73 lines (60 loc) · 1.3 KB
/
xdr.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package nfs
import (
"encoding/binary"
)
// XDR maps the External Data Representation
type xdr struct {
data []byte
offset uint32
}
func newXDR(data []byte) *xdr {
x := makeXDR(data)
return &x
}
func makeXDR(data []byte) xdr {
return xdr{data: data, offset: 0}
}
func (r *xdr) size() int {
return len(r.data)
}
func (r *xdr) getInt() int32 {
i := int32(binary.BigEndian.Uint32(r.data[r.offset : r.offset+4]))
r.offset += 4
return int32(i)
}
func (r *xdr) getUInt() uint32 {
i := uint32(binary.BigEndian.Uint32(r.data[r.offset : r.offset+4]))
r.offset += 4
return i
}
func (r *xdr) getHyper() int64 {
i := int64(binary.BigEndian.Uint64(r.data[r.offset : r.offset+8]))
r.offset += 8
return i
}
func (r *xdr) getUHyper() uint64 {
i := uint64(binary.BigEndian.Uint64(r.data[r.offset : r.offset+8]))
r.offset += 8
return i
}
func (r *xdr) getString() string {
return string(r.getDynamicOpaque())
}
func (r *xdr) getOpaque(length uint32) []byte {
padding := (4 - (length & 3)) & 3
b := r.data[r.offset : r.offset+length]
r.offset += length + padding
return b
}
func (r *xdr) getDynamicOpaque() []byte {
l := r.getUInt()
return r.getOpaque(l)
}
func (r *xdr) getUIntVector() []uint32 {
l := r.getUInt()
v := make([]uint32, int(l))
for i := 0; i < len(v); i++ {
v[i] = r.getUInt()
}
return v
}