Documentation ¶
Overview ¶
Package crc16 implements the 16-bit cyclic redundancy check, or CRC-16, checksum. See http://en.wikipedia.org/wiki/Cyclic_redundancy_check for information.
Index ¶
Constants ¶
const Size = 2
The size of a CRC-16 checksum in bytes.
Variables ¶
var ( X25 = &Conf{ Poly: 0x1021, BitRev: true, IniVal: 0xffff, FinVal: 0xffff, BigEnd: false, } PPP = X25 Modbus = &Conf{ Poly: 0x8005, BitRev: true, IniVal: 0xffff, FinVal: 0x0, BigEnd: false, } XModem = &Conf{ Poly: 0x1021, BitRev: false, IniVal: 0x0000, FinVal: 0x0, BigEnd: true, } Kermit = &Conf{ Poly: 0x1021, BitRev: true, IniVal: 0x0, FinVal: 0x0, BigEnd: false, } )
Typical CRC-16 configurations. Mostly used are the CCITT (0x1021) and the IBM/ANSI (0x8005) polynomials, either bit-reversed or not. For more configurations see: http://reveng.sourceforge.net/crc-catalogue/
Functions ¶
func Update ¶
Update returns the CRC-16 checksum of p using the polynomial table tab constructed by MakeTable (bit-reversed order). The resulting CRC is in bit-reversed order (bit-15 corresponds to the X^0 term). Argument crc is the initial value of the CRC register.
Types ¶
type Conf ¶
type Conf struct { Poly uint16 // Polynomial to use. BitRev bool // Bit reversed CRC (bit-15 is X^0)? IniVal uint16 // Initial value of CRC register. FinVal uint16 // XOR CRC with this at the end. BigEnd bool // Emit *bytes* most significant first (see Hash.Sum)? // contains filtered or unexported fields }
Conf is a CRC configuration. It is passed to functions New and Checksum and specifies the parameters of the calculated checksum. The first time New or Checksum are called with a configuration structure c, they calculate the polynomial table for this configuration; subsequent calls with the same c use the table already calculated. A few commonly used configurations are defined as global variables (X25, PPP, Modbus, etc.)
type Table ¶
type Table [256]uint16
Table is a 256-word table representing the polynomial for efficient processing.
func MakeTable ¶
MakeTable returns the Table constructed from the specified polynomial. The table is calcuated in bit-reversed order (bit-15 corresponds to the X^0 term). Argument poly must be given bit-reversed (e.g. 0xA001 for the 0x8005 polynomial).
func MakeTableNBR ¶
MakeTableNBR returns the Table constructed from the specified polynomial. The table is calculated in non-bit-reversed order (bit-0 corresponds to the X^0 term).