-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecoder.go
126 lines (104 loc) · 2.63 KB
/
decoder.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package hdlc
import (
"bufio"
"bytes"
"io"
)
// A Decoder reads HDLC frames from an input stream.
type Decoder struct {
bufReader *bufio.Reader
inSync bool
decoderBuf bytes.Buffer
accm asyncControlCharacterMap
}
// NewDecoder returns a new decoder that reads from r.
// The decoder introduces its own buffering and may read data from r beyond the requested frames.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
bufReader: bufio.NewReader(r),
inSync: false,
}
}
// ReadFrame re-syncs the Decoder if necessary and reads the next frame.
// Returns ErrInvalidFrame if the decoding fails. Returns ErrEmptyFrame when the
// frame has no content to decode.
func (fd *Decoder) ReadFrame() (*Frame, error) {
var err error
frame := Frame{}
if !fd.inSync {
if err := fd.resync(); err != nil {
return nil, err
}
fd.inSync = true
}
content, err := fd.readFrame()
if err != nil {
return nil, err
}
if len(content) == 0 {
return nil, ErrEmptyFrame
}
decodedContentBuf := &fd.decoderBuf
decodedContentBuf.Reset()
inEscape := false
for _, b := range content {
if b == escapeSym {
if inEscape {
return nil, ErrInvalidFrame
}
inEscape = true
} else if inEscape {
decodedContentBuf.WriteByte(b ^ 0x20)
inEscape = false
} else if fd.accm.contains(b) {
// skip byte introduced by DCE
} else {
decodedContentBuf.WriteByte(b)
}
}
if inEscape || decodedContentBuf.Len() < 3 {
return nil, ErrInvalidFrame
}
decodedContent := decodedContentBuf.Bytes()
decodedContentNoFCS := decodedContent[:len(decodedContent)-2]
frame.FCS = decodedContent[len(decodedContent)-2:]
if bytes.Equal(decodedContent[0:len(addressCtrlSeq)], addressCtrlSeq) {
frame.HasAddressCtrlPrefix = true
frame.Payload = decodedContentNoFCS[len(addressCtrlSeq):]
} else {
frame.Payload = decodedContentNoFCS
}
return &frame, nil
}
// SetACCM sets the decoder's Async-Control-Character-Map (ACCM). The default
// ACCM value is 0, meaning that the decoder does not escape any control
// characters.
//
// Note that this implementation does not negotiate the ACCM with the peer.
func (fd *Decoder) SetACCM(chars ControlChar) *Decoder {
fd.accm = asyncControlCharacterMap(chars)
return fd
}
func (fd Decoder) readFrame() ([]byte, error) {
frame, err := fd.bufReader.ReadBytes(flagSym)
if err != nil {
return nil, err
}
return frame[:len(frame)-1], err
}
func (fd Decoder) resync() error {
for {
_, err := fd.bufReader.ReadBytes(flagSym)
if err != nil {
return err
}
peek1, err := fd.bufReader.Peek(1)
if err != nil {
return err
}
if peek1[0] != flagSym {
break
}
}
return nil
}