-
Notifications
You must be signed in to change notification settings - Fork 44
/
ematch_nbyte.go
52 lines (44 loc) · 960 Bytes
/
ematch_nbyte.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
package tc
import (
"fmt"
)
// NByteMatch contains attributes of the Nbyte match discipline
type NByteMatch struct {
Offset uint16
Layer uint8
Needle []byte
}
type tcfEmNByte struct {
off uint16
len uint16
layer uint8
}
func unmarshalNByteMatch(data []byte, info *NByteMatch) error {
if len(data) < 8 {
return fmt.Errorf("unmarshalNByteMatch: incomplete data")
}
nbyte := tcfEmNByte{}
if err := unmarshalStruct(data[:5], nbyte); err != nil {
return err
}
info.Offset = nbyte.off
info.Layer = nbyte.layer
info.Needle = data[8:]
return nil
}
func marshalNByteMatch(info *NByteMatch) ([]byte, error) {
if info == nil {
return []byte{}, fmt.Errorf("marshalNByteMatch: %w", ErrNoArg)
}
nbyte := tcfEmNByte{
off: info.Offset,
len: uint16(len(info.Needle)),
layer: info.Layer,
}
tmp, err := marshalAndAlignStruct(nbyte)
if err != nil {
return []byte{}, err
}
tmp = append(tmp, info.Needle...)
return tmp, nil
}