crcutil

package module
v0.1.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: MIT Imports: 2 Imported by: 0

README

Go Reference

crcutil

This module implements support for CRC calculation.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// CRC-3-GSM: x^3 + x^1 + 1
	GSM3 = &Poly[uint8]{Word: 3, Width: 3}
)

Functions

func BitwiseUpdateFn

func BitwiseUpdateFn[T, D Word](poly *Poly[T]) func(poly *Poly[T], crc T, data D, dataWidth int) T

BitwiseUpdateFn returns a function that returns the result of adding the bits defined by data and dataWidth into the crc. The actual function returned depends on the polynomial's representation (normal or reversed form).

func UpdateBitwise

func UpdateBitwise[T, D Word](poly *Poly[T], crc T, data D, dataWidth int) T

Types

type Poly

type Poly[T Word] struct {
	Word       T
	Width      int
	Reversed   bool
	Reciprocal bool
}

Poly defines a polynomial in a specific representation.

Example

This example calculates representations of the CCITT-16 polynomial as shown in https://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Polynomial_representations

package main

import (
	"fmt"
)

var ccitt16 = &Poly[uint16]{Word: 0x1021, Width: 16}

// This example calculates representations of the CCITT-16
// polynomial as shown in
// https://en.wikipedia.org/wiki/Mathematics_of_cyclic_redundancy_checks#Polynomial_representations
func main() {
	reversed := ccitt16.ReversedForm()
	fmt.Printf("%#04x %#04x %#04x",
		reversed.Word,
		ccitt16.ReciprocalForm().Word,
		reversed.ReciprocalForm().Word)
}
Output:

0x8408 0x0811 0x8810

func (*Poly[T]) MakeTable

func (poly *Poly[T]) MakeTable(opts ...TableOption) []T

MakeTable creates a lookup table for the specified polynomial. The size of the table returned equals the length of the value range determined by the number of data bits.

func (*Poly[T]) NormalForm

func (p *Poly[T]) NormalForm() *Poly[T]

NormalForm returns the normal form of the polynomial.

func (*Poly[T]) ReciprocalForm

func (p *Poly[T]) ReciprocalForm() *Poly[T]

ReciprocalForm returns the reciprocal form of the polynomial that can be obtained by mirroring its coefficients. It returns the unchanged polynomial if it is already in its reciprocal form.

func (*Poly[T]) ReversedForm

func (p *Poly[T]) ReversedForm() *Poly[T]

ReversedForm returns the reversed, lsbit-first form of the polynomial. It returns the unchanged polynomial if it is already in its reversed form.

type TableOption

type TableOption func(*tableConf)

func WithDataWidth

func WithDataWidth(w int) TableOption

WithDataWidthByte sets the data width to 8 bits, which will result in a table of 256 entries for byte-wise processing.

func WithInitialValue

func WithInitialValue(initial uint32) TableOption

WithInitialValue ensures that when a table is created the specified initial value will be applied to the calculation of each table entry. In cases where a table later is used manually, like when a CRC is calulated over some bits only, this saves one XOR operation.

func WithReversedBits

func WithReversedBits() TableOption

WithReversedBits table option mirrors the bits of each table entry.

type Word

type Word interface {
	~uint8 | ~uint16 | ~uint32
}

A Word holds the word representation of a polynomial.

Jump to

Keyboard shortcuts

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