Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nokute78/go-bit
A library to read/write bits array like binary.Read/Write.
https://github.com/nokute78/go-bit
binary bit endian go golang
Last synced: 3 months ago
JSON representation
A library to read/write bits array like binary.Read/Write.
- Host: GitHub
- URL: https://github.com/nokute78/go-bit
- Owner: nokute78
- License: apache-2.0
- Created: 2019-05-16T03:57:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-20T23:49:28.000Z (over 2 years ago)
- Last Synced: 2023-07-27T22:24:51.260Z (over 1 year ago)
- Topics: binary, bit, endian, go, golang
- Language: Go
- Homepage:
- Size: 120 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-bit
![Go](https://github.com/nokute78/go-bit/workflows/Go/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/nokute78/go-bit)](https://goreportcard.com/report/github.com/nokute78/go-bit)
[![Go Reference](https://pkg.go.dev/badge/github.com/nokute78/go-bit/v2.svg)](https://pkg.go.dev/github.com/nokute78/go-bit/v2)A library to read/write bits from a byte slice.
## Installation
```
go get -u github.com/nokute78/go-bit/v2
```**CAUTION**: `github.com/nokute78/go-bit/pkg/bit/v2` will be deprecated.
Please use `github.com/nokute78/go-bit/v2`.## Usage
The package supports `binary.Read` like API.
It is an example to decode TCP header. (Big Endian)
1. Save below code as tcp.go.
2. `go mod init go-bit-example`
3. `go mod tidy`
4. `go run tcp.go````go
package mainimport (
"bytes"
"encoding/binary"
"fmt"
"github.com/nokute78/go-bit/v2"
)func main() {
type TcpHeader struct {
SrcPort uint16
DstPort uint16
SeqNo uint32
AckNo uint32
HeaderLen [4]bit.Bit
Reserved [3]bit.Bit `bit:"skip"`
NS bit.Bit
CWR bit.Bit
ECE bit.Bit
URG bit.Bit
ACK bit.Bit
PSH bit.Bit
RST bit.Bit
SYN bit.Bit
FIN bit.Bit
WinSize uint16
CheckSum uint16
EmePointer uint16
}s := TcpHeader{}
br := bytes.NewReader([]byte{0xd8, 0x65, 0x01, 0xbb, 0x4b, 0xe0, 0x76, 0xcd, 0x48, 0xc8, 0x70, 0x8f, 0x50, 0x10, 0x10,
0x18, 0x0e, 0xc1, 0x00, 0x00})if err := bit.Read(br, binary.BigEndian, &s); err != nil {
fmt.Printf("error:%s", err)
}fmt.Printf("src=%d dst=%d\n", s.SrcPort, s.DstPort)
fmt.Printf("SeqNo=%d AckNo=%d\n", s.SeqNo, s.AckNo)
fmt.Printf("HeaderLen(raw)=%v\n", s.HeaderLen)
fmt.Printf("Ack=%t\n", s.ACK)
}
```## Struct Tag
The package supports struct tags.
|Tag|Description|
|---|-----------|
|`` `bit:"skip"` ``|Ignore the field. Offset is updated by the size of the field. It is useful for reserved field.|
|`` `bit:"-"` `` |Ignore the field. Offset is not updated.|
|`` `bit:"BE"` ``|Decode the field as big endian. It is useful for mixed endian data.|
|`` `bit:"LE"` ``|Decode the field as little endian. It is useful for mixed endian data.|## Tool
* [readbit](v2/cmd/readbit/README.md)## Document
https://pkg.go.dev/github.com/nokute78/go-bit/v2
## Old Document
https://godoc.org/github.com/nokute78/go-bit/pkg/bit
## License
[Apache License v2.0](https://www.apache.org/licenses/LICENSE-2.0)