Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Aug 26, 2022
1 parent b99ab0f commit 4e3113e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 58 deletions.
40 changes: 20 additions & 20 deletions crc16/crc16.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,37 @@ var (
)

// 表格
type Table struct {
type CRC struct {
params Params
data [256]uint16
table [256]uint16
}

// 设置参数
func (this *Table) WithParams(params Params) *Table {
func (this *CRC) WithParams(params Params) *CRC {
this.params = params

return this
}

// 获取参数
func (this *Table) GetParams() Params {
func (this *CRC) GetParams() Params {
return this.params
}

// 设置数据
func (this *Table) WithData(data [256]uint16) *Table {
this.data = data
func (this *CRC) WithTable(table [256]uint16) *CRC {
this.table = table

return this
}

// 获取数据
func (this *Table) GetData() [256]uint16 {
return this.data
func (this *CRC) GetTable() [256]uint16 {
return this.table
}

// 生成数值
func (this *Table) MakeData() *Table {
func (this *CRC) MakeTable() *CRC {
for n := 0; n < 256; n++ {
crc := uint16(n) << 8

Expand All @@ -123,32 +123,32 @@ func (this *Table) MakeData() *Table {
}
}

this.data[n] = crc
this.table[n] = crc
}

return this
}

// 初始值
func (this *Table) Init() uint16 {
func (this *CRC) Init() uint16 {
return this.params.Init
}

// 更新
func (this *Table) Update(crc uint16, data []byte) uint16 {
func (this *CRC) Update(crc uint16, data []byte) uint16 {
for _, d := range data {
if this.params.RefIn {
d = bits.Reverse8(d)
}

crc = (crc << 8) ^ this.data[byte(crc >> 8) ^ d]
crc = (crc << 8) ^ this.table[byte(crc >> 8) ^ d]
}

return crc
}

// 完成
func (this *Table) Complete(crc uint16) uint16 {
func (this *CRC) Complete(crc uint16) uint16 {
if this.params.RefOut {
return bits.Reverse16(crc) ^ this.params.XorOut
}
Expand All @@ -159,20 +159,20 @@ func (this *Table) Complete(crc uint16) uint16 {
// Checksum
// LSB-MSB,即低字节在前
// Modbus,即高字节在前
func (this *Table) Checksum(data []byte) uint16 {
crc := this.MakeData().Init()
func (this *CRC) Checksum(data []byte) uint16 {
crc := this.MakeTable().Init()
crc = this.Update(crc, data)

return this.Complete(crc)
}

// 构造函数
func NewTable(params ...Params) *Table {
table := &Table{}
func NewCRC(params ...Params) *CRC {
crc := &CRC{}

if len(params) > 0 {
table.WithParams(params[0])
crc.WithParams(params[0])
}

return table
return crc
}
14 changes: 7 additions & 7 deletions crc16/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type Hash16 interface {
const Size = 2

type digest struct {
sum uint16
table *Table
sum uint16
crc *CRC
}

// Write
func (this *digest) Write(data []byte) (int, error) {
this.sum = this.table.Update(this.sum, data)
this.sum = this.crc.Update(this.sum, data)

return len(data), nil
}
Expand All @@ -32,7 +32,7 @@ func (this *digest) Sum(b []byte) []byte {

// Reset
func (this *digest) Reset() {
this.sum = this.table.params.Init
this.sum = this.crc.params.Init
}

// Size
Expand All @@ -47,13 +47,13 @@ func (this *digest) BlockSize() int {

// Sum16
func (this *digest) Sum16() uint16 {
return this.table.Complete(this.sum)
return this.crc.Complete(this.sum)
}

// 构造函数
func NewHash(table *Table) Hash16 {
func NewHash(crc *CRC) Hash16 {
h := &digest{
table: table,
crc: crc,
}
h.Reset()

Expand Down
61 changes: 30 additions & 31 deletions crc16/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,148 +5,147 @@ import (
)

// 构造函数
func NewCRC16(params ...Params) *Table {
return NewTable(params...)
func NewCRC16(params ...Params) *CRC {
return NewCRC(params...)
}

// 构造函数
func NewCRC16Hash(params Params) Hash16 {
table := &Table{}
table.params = params
crc := NewCRC16(params).MakeTable()

return NewHash(table.MakeData())
return NewHash(crc)
}

// =======================

// 生成
func Checksum(data []byte, params Params) uint16 {
return NewTable(params).Checksum(data)
return NewCRC16(params).Checksum(data)
}

// 生成 IBM
func ChecksumIBM(data []byte) uint16 {
return NewTable(CRC16_IBM).Checksum(data)
return NewCRC16(CRC16_IBM).Checksum(data)
}

// 生成 ARC
func ChecksumARC(data []byte) uint16 {
return NewTable(CRC16_ARC).Checksum(data)
return NewCRC16(CRC16_ARC).Checksum(data)
}

// 生成 AUG_CCITT
func ChecksumAUG_CCITT(data []byte) uint16 {
return NewTable(CRC16_AUG_CCITT).Checksum(data)
return NewCRC16(CRC16_AUG_CCITT).Checksum(data)
}

// 生成 BUYPASS
func ChecksumBUYPASS(data []byte) uint16 {
return NewTable(CRC16_BUYPASS).Checksum(data)
return NewCRC16(CRC16_BUYPASS).Checksum(data)
}

// 生成 CCITT
func ChecksumCCITT(data []byte) uint16 {
return NewTable(CRC16_CCITT).Checksum(data)
return NewCRC16(CRC16_CCITT).Checksum(data)
}

// 生成 CCITT_FALSE
func ChecksumCCITT_FALSE(data []byte) uint16 {
return NewTable(CRC16_CCITT_FALSE).Checksum(data)
return NewCRC16(CRC16_CCITT_FALSE).Checksum(data)
}

// 生成 CDMA2000
func ChecksumCDMA2000(data []byte) uint16 {
return NewTable(CRC16_CDMA2000).Checksum(data)
return NewCRC16(CRC16_CDMA2000).Checksum(data)
}

// 生成 DDS_110
func ChecksumDDS_110(data []byte) uint16 {
return NewTable(CRC16_DDS_110).Checksum(data)
return NewCRC16(CRC16_DDS_110).Checksum(data)
}

// 生成 DECT_R
func ChecksumDECT_R(data []byte) uint16 {
return NewTable(CRC16_DECT_R).Checksum(data)
return NewCRC16(CRC16_DECT_R).Checksum(data)
}

// 生成 DECT_X
func ChecksumDECT_X(data []byte) uint16 {
return NewTable(CRC16_DECT_X).Checksum(data)
return NewCRC16(CRC16_DECT_X).Checksum(data)
}

// 生成 DNP
func ChecksumDNP(data []byte) uint16 {
return NewTable(CRC16_DNP).Checksum(data)
return NewCRC16(CRC16_DNP).Checksum(data)
}

// 生成 GENIBUS
func ChecksumGENIBUS(data []byte) uint16 {
return NewTable(CRC16_GENIBUS).Checksum(data)
return NewCRC16(CRC16_GENIBUS).Checksum(data)
}

// 生成 MAXIM
func ChecksumMAXIM(data []byte) uint16 {
return NewTable(CRC16_MAXIM).Checksum(data)
return NewCRC16(CRC16_MAXIM).Checksum(data)
}

// 生成 MCRF4XX
func ChecksumMCRF4XX(data []byte) uint16 {
return NewTable(CRC16_MCRF4XX).Checksum(data)
return NewCRC16(CRC16_MCRF4XX).Checksum(data)
}

// 生成 RIELLO
func ChecksumRIELLO(data []byte) uint16 {
return NewTable(CRC16_RIELLO).Checksum(data)
return NewCRC16(CRC16_RIELLO).Checksum(data)
}

// 生成 T10_DIF
func ChecksumT10_DIF(data []byte) uint16 {
return NewTable(CRC16_T10_DIF).Checksum(data)
return NewCRC16(CRC16_T10_DIF).Checksum(data)
}

// 生成 TELEDISK
func ChecksumTELEDISK(data []byte) uint16 {
return NewTable(CRC16_TELEDISK).Checksum(data)
return NewCRC16(CRC16_TELEDISK).Checksum(data)
}

// 生成 TMS37157
func ChecksumTMS37157(data []byte) uint16 {
return NewTable(CRC16_TMS37157).Checksum(data)
return NewCRC16(CRC16_TMS37157).Checksum(data)
}

// 生成 USB
func ChecksumUSB(data []byte) uint16 {
return NewTable(CRC16_USB).Checksum(data)
return NewCRC16(CRC16_USB).Checksum(data)
}

// 生成 CRC_A
func ChecksumCRC_A(data []byte) uint16 {
return NewTable(CRC16_CRC_A).Checksum(data)
return NewCRC16(CRC16_CRC_A).Checksum(data)
}

// 生成 KERMIT
func ChecksumKERMIT(data []byte) uint16 {
return NewTable(CRC16_KERMIT).Checksum(data)
return NewCRC16(CRC16_KERMIT).Checksum(data)
}

// 生成 MODBUS
func ChecksumMODBUS(data []byte) uint16 {
return NewTable(CRC16_MODBUS).Checksum(data)
return NewCRC16(CRC16_MODBUS).Checksum(data)
}

// 生成 X_25
func ChecksumX_25(data []byte) uint16 {
return NewTable(CRC16_X_25).Checksum(data)
return NewCRC16(CRC16_X_25).Checksum(data)
}

// 生成 XMODEM
func ChecksumXMODEM(data []byte) uint16 {
return NewTable(CRC16_XMODEM).Checksum(data)
return NewCRC16(CRC16_XMODEM).Checksum(data)
}

// 生成 XMODEM2
func ChecksumXMODEM2(data []byte) uint16 {
return NewTable(CRC16_XMODEM2).Checksum(data)
return NewCRC16(CRC16_XMODEM2).Checksum(data)
}

// =======================
Expand Down

0 comments on commit 4e3113e

Please sign in to comment.