Skip to content

Commit 9261e64

Browse files
committed
Add README
Add a readme file. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent d7bbe5c commit 9261e64

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# COBS
2+
3+
A Golang module for the
4+
[Consistent Overhead Byte Stuffing (COBS)](https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing)
5+
algorithm.
6+
7+
## Usage
8+
9+
This module provides both simple helper functions to Encode/Decode `[]byte` arrays and
10+
Encoder/Decoder structs to stream bytes to an `io.Writer` instance.
11+
12+
### `Encode`/`Decode` functions
13+
14+
The helper functions will allocate buffers to hold the encoded/decoded data and return a `[]byte`
15+
slice or an error.
16+
17+
The following example encodes a string with embedded zeroes:
18+
19+
```go
20+
package main
21+
22+
import (
23+
"os"
24+
25+
"github.com/pdgendt/cobs"
26+
)
27+
28+
func main() {
29+
enc, _ := cobs.Encode([]byte{'H', 'e', 'l', 'l', 'o', 0x00, 'w', 'o', 'r', 'l', 'd', '!'})
30+
31+
os.Stdout.write(enc)
32+
}
33+
```
34+
35+
### `Encoder`/`Decoder` structs
36+
37+
The structs require an `io.Writer` instance on creation. As soon as data is available it is written,
38+
for the `Encoder` this is done for each group, with a maximum of 255 bytes, for the `Decoder` every
39+
`byte` is written individually.
40+
41+
The structs implement the `io.Writer` interface to allow chaining.
42+
43+
The following example encodes bytes read from `os.Stdin` and writes them to `os.Stdout`:
44+
45+
```go
46+
package main
47+
48+
import (
49+
"io"
50+
"os"
51+
52+
"github.com/pdgendt/cobs"
53+
)
54+
55+
func main() {
56+
enc := cobs.NewEncoder(os.Stdout)
57+
58+
if _, err := io.Copy(enc, os.Stdin); err != nil {
59+
panic(err)
60+
}
61+
62+
// Close needs to be called to flush the last group
63+
if err := enc.Close(); err != nil {
64+
panic(err)
65+
}
66+
}
67+
```
68+
69+
## CLI tools
70+
71+
The [cmd/](cmd/) directory contains simple encode/decode command line tools that take in data
72+
from `stdin` and writes it to `stdout`.
73+
74+
This can be used to pipe encoded/decoded data to other processes.
75+
76+
```shell
77+
$ echo "Hello world" | go run cmd/encode/main.go | go run cmd/decode/main.go
78+
Hello world
79+
```

0 commit comments

Comments
 (0)