https://github.com/borud/hdlc
HDLC'ish unframer
https://github.com/borud/hdlc
Last synced: about 2 months ago
JSON representation
HDLC'ish unframer
- Host: GitHub
- URL: https://github.com/borud/hdlc
- Owner: borud
- License: apache-2.0
- Created: 2022-04-01T17:46:13.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-30T18:54:29.000Z (about 4 years ago)
- Last Synced: 2025-02-24T10:43:35.516Z (over 1 year ago)
- Language: Go
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HDLC
[](https://godoc.org/github.com/borud/hdlc)
HDLC is a library for unframing HDLC-like frames. Instead of implementing this as an io.Reader,
we read whole frames from a channel. This avoids misunderstandings that might arise if you use the io.Reader interface.
Assuming you have an `io.Reader` named `myReader` this is how you unframe things.
```go
import "github.com/borud/hdlc"
...
unframer := NewUnframer(myReader)
...
for range unframer.Frames() {
// do something with the frames
}
```
## Framing format
The framing format defines three special values:
```go
const (
FlagEscape = byte(0x7d)
FlagSep = byte(0x7e)
FlagAbort = byte(0x7f)
XORMask = byte(0x20)
)
```
- Each frame starts and ends with a `FlagSep` byte
- If we encounter a `FlagAbort` value it means we abort the current frame and wait for the next frame beginning before accumulating data.
- If values equal to `FlagEscape`, `FlagSep` or `FlagAbort` occur in the payload it needs to be escaped. Escaping happens by replacing the value with two bytes: FlagEscape followed by the value we are escaping XOR'ed by XORMask (we flip the 5th bit).
Unframing is just applying this process in reverse.
### API choice
Rather than implement the unframer as an `io.Reader` we explicitly chose an API that reflects that this is not an `io.Reader`. We are not reading a stream, we are reading discrete frames. This means that if you were to wrap it in something that assumes a stream, you would run into problems.