Categorygithub.com/nokute78/go-bit
repository
2.2.1
Repository: https://github.com/nokute78/go-bit.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

go-bit

Go Go Report Card GoDoc

A library to read/write bits from a byte slice.

Installation

$ go get github.com/nokute78/go-bit/pkg/bit

Usage

The package supports binary.Read like API.

It is an example to decode TCP header. (Big Endian)

package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"github.com/nokute78/go-bit/pkg/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.

TagDescription
`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

Document

https://godoc.org/github.com/nokute78/go-bit/pkg/bit/v2

Old Document

https://godoc.org/github.com/nokute78/go-bit/pkg/bit

License

Apache License v2.0