bitstring

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeInteger       = "integer"
	TypeFloat         = "float"
	TypeBinary        = "binary"
	TypeBitstring     = "bitstring"
	TypeUTF           = "utf"   // Generic UTF type
	TypeUTF8          = "utf8"  // UTF-8 specific
	TypeUTF16         = "utf16" // UTF-16 specific
	TypeUTF32         = "utf32" // UTF-32 specific
	TypeRestBinary    = "rest_binary"
	TypeRestBitstring = "rest_bitstring"
)

Segment types constants

View Source
const (
	EndiannessBig    = "big"
	EndiannessLittle = "little"
	EndiannessNative = "native"
)

Endianness constants

View Source
const (
	Signed   = true
	Unsigned = false
)

Signedness constants

View Source
const (
	DefaultUnitInteger   = 1
	DefaultUnitFloat     = 1
	DefaultUnitBinary    = 8
	DefaultUnitBitstring = 1
	DefaultUnitUTF       = 1
)

Default unit values for different types

View Source
const (
	DefaultSizeInteger = 8
	DefaultSizeFloat   = 64
)

Default size values for different types (in bits)

View Source
const (
	// Overflow errors
	CodeOverflow       = "OVERFLOW"
	CodeSignedOverflow = "SIGNED_OVERFLOW"

	// Insufficient data errors
	CodeInsufficientBits = "INSUFFICIENT_BITS"

	// Invalid segment configuration errors
	CodeInvalidSize       = "INVALID_SIZE"
	CodeInvalidType       = "INVALID_TYPE"
	CodeInvalidEndianness = "INVALID_ENDIANNESS"

	// Binary-specific errors
	CodeBinarySizeRequired = "BINARY_SIZE_REQUIRED"
	CodeBinarySizeMismatch = "BINARY_SIZE_MISMATCH"
	CodeInvalidBinaryData  = "INVALID_BINARY_DATA"

	// Bitstring-specific errors
	CodeInvalidBitstringData = "INVALID_BITSTRING_DATA"

	// UTF-specific errors
	CodeUTFSizeSpecified        = "UTF_SIZE_SPECIFIED"
	CodeInvalidUnicodeCodepoint = "INVALID_UNICODE_CODEPOINT"

	// Type mismatch errors
	CodeTypeMismatch = "TYPE_MISMATCH"

	// General validation errors
	CodeInvalidSegment   = "INVALID_SEGMENT"
	CodeInvalidUnit      = "INVALID_UNIT"
	CodeInvalidFloatSize = "INVALID_FLOAT_SIZE"
	CodeUTFUnitModified  = "UTF_UNIT_MODIFIED"
)

Error codes for bitstring operations

Variables

View Source
var (
	ErrInvalidSize      = errors.New("invalid size")
	ErrSizeTooLarge     = errors.New("size too large")
	ErrInvalidPosition  = errors.New("invalid position")
	ErrLengthTooLarge   = errors.New("length too large")
	ErrInvalidAlignment = errors.New("invalid alignment")
)

Size handling errors

Functions

func AlignData

func AlignData(data []byte, offset, alignment uint) ([]byte, error)

AlignData aligns data to a specified boundary

func CalculateTotalSize

func CalculateTotalSize(segment Segment) (uint, error)

CalculateTotalSize calculates the total size in bits for a segment

func CountLeadingZeros

func CountLeadingZeros(data []byte) uint

CountLeadingZeros counts the number of leading zero bits in the data

func CountTrailingZeros

func CountTrailingZeros(data []byte) uint

CountTrailingZeros counts the number of trailing zero bits in the data

func ExtractBits

func ExtractBits(data []byte, start, length uint) ([]byte, error)

ExtractBits extracts a sequence of bits from data

func GetBitValue

func GetBitValue(data []byte, pos uint) (bool, error)

GetBitValue gets the value of a single bit at the specified position

func PadData

func PadData(data []byte, bitLen, target uint) []byte

PadData pads data to reach a target bit length

func SetBitValue

func SetBitValue(data []byte, pos uint, value bool) error

SetBitValue sets the value of a single bit at the specified position

func SetBits

func SetBits(target, data []byte, start uint) error

SetBits sets a sequence of bits in target data

func ValidateSegment

func ValidateSegment(segment *Segment) error

ValidateSegment checks if a segment has valid configuration

func ValidateSize

func ValidateSize(size uint, unit uint) error

ValidateSize validates the size and unit for a segment

Types

type BitString

type BitString struct {
	// contains filtered or unexported fields
}

BitString represents a sequence of bits

func NewBitString

func NewBitString() *BitString

NewBitString creates an empty bitstring

func NewBitStringFromBits

func NewBitStringFromBits(data []byte, length uint) *BitString

NewBitStringFromBits creates a bitstring from bits with specific length

func NewBitStringFromBytes

func NewBitStringFromBytes(data []byte) *BitString

NewBitStringFromBytes creates a bitstring from byte slice

func (*BitString) Clone

func (bs *BitString) Clone() *BitString

Clone creates a copy of bitstring

func (*BitString) IsBinary

func (bs *BitString) IsBinary() bool

IsBinary checks if bitstring length is multiple of 8

func (*BitString) IsEmpty

func (bs *BitString) IsEmpty() bool

IsEmpty checks if bitstring is empty

func (*BitString) Length

func (bs *BitString) Length() uint

Length returns bitstring length in bits

func (*BitString) ToBytes

func (bs *BitString) ToBytes() []byte

ToBytes converts bitstring to byte slice (pads with zeros if needed)

type BitStringError

type BitStringError struct {
	Code    string
	Message string
	Context interface{}
}

BitStringError represents an error in bitstring operations

func NewBitStringError

func NewBitStringError(code, message string) *BitStringError

NewBitStringError creates a new BitStringError with the given code and message

func NewBitStringErrorWithContext

func NewBitStringErrorWithContext(code, message string, context interface{}) *BitStringError

NewBitStringErrorWithContext creates a new BitStringError with the given code, message, and context

func (*BitStringError) Error

func (e *BitStringError) Error() string

type Segment

type Segment struct {
	Value         interface{}
	Size          uint
	SizeSpecified bool // Flag to indicate if size was explicitly specified
	Type          string
	Signed        bool
	Endianness    string
	Unit          uint
	UnitSpecified bool   // Flag to indicate if unit was explicitly specified
	DynamicSize   *uint  // Pointer to variable for dynamic size
	DynamicExpr   string // Expression for dynamic size calculation
	IsDynamic     bool   // Flag to indicate if size is dynamic
}

Segment represents a single segment in bitstring construction/matching

func NewSegment

func NewSegment(value interface{}, options ...SegmentOption) *Segment

NewSegment creates a new segment with the given value and options

type SegmentOption

type SegmentOption func(*Segment)

SegmentOption is a function type for configuring segments

func WithDynamicSize

func WithDynamicSize(sizeVar *uint) SegmentOption

WithDynamicSize sets the size for a segment using a variable reference

func WithDynamicSizeExpression

func WithDynamicSizeExpression(expr string) SegmentOption

WithDynamicSizeExpression sets the size for a segment using an expression

func WithEndianness

func WithEndianness(endianness string) SegmentOption

WithEndianness sets the endianness for a segment

func WithSigned

func WithSigned(signed bool) SegmentOption

WithSigned sets the signedness for a segment

func WithSize

func WithSize(size uint) SegmentOption

WithSize sets the size for a segment

func WithType

func WithType(segmentType string) SegmentOption

WithType sets the type for a segment

func WithUnit

func WithUnit(unit uint) SegmentOption

WithUnit sets the unit for a segment

type SegmentResult

type SegmentResult struct {
	Value     interface{}
	Matched   bool
	Remaining *BitString
}

SegmentResult represents result of segment matching

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL