This commit is contained in:
xzb 2023-10-30 17:46:52 +08:00
parent 37aa8ec4d1
commit 713ff3b614

View File

@ -13,27 +13,16 @@ const (
RsspCrc32C2 uint32 = 0x8ce56011 //安全通道1 CRC32生成多项式
)
func CreateRsspCrcTable() {
// InitRsspCrcTable 初始化RSSP协议中需要的CRC表
func InitRsspCrcTable() {
if crc16Table == nil {
var table = make([]uint32, 0, 256)
for bt := 0x00; bt <= 0xff; bt++ {
table = append(table, Lookup(uint32(bt), RsspCrc16GX, 16, false))
}
crc16Table = table
crc16Table = CreateCrcTable(RsspCrc16GX, 16, false)
}
if crc32C1Table == nil {
var table = make([]uint32, 0, 256)
for bt := 0x00; bt <= 0xff; bt++ {
table = append(table, Lookup(uint32(bt), RsspCrc32C1, 32, false))
}
crc32C1Table = table
crc32C1Table = CreateCrcTable(RsspCrc32C1, 32, false)
}
if crc32C2Table == nil {
var table = make([]uint32, 0, 256)
for bt := 0x00; bt <= 0xff; bt++ {
table = append(table, Lookup(uint32(bt), RsspCrc32C2, 32, false))
}
crc32C2Table = table
crc32C2Table = CreateCrcTable(RsspCrc32C2, 32, false)
}
}
func RsspCrc16(data []byte) uint16 {
@ -46,6 +35,15 @@ func RsspC2Crc32(data []byte) uint32 {
return CrcTableBased(data, 32, 0, false, false, 0, crc32C2Table)
}
// CreateCrcTable 创建CRC表支持8、16、32位
func CreateCrcTable(polynomial uint32, width int, input_reflected bool) []uint32 {
var table = make([]uint32, 0, 256)
for bt := 0x00; bt <= 0xff; bt++ {
table = append(table, lookup(uint32(bt), polynomial, width, input_reflected))
}
return table
}
/////////////////////////////////////////////////////////////////////////////
// 反转(0b00001010->0b01010000)
@ -64,8 +62,8 @@ func reflect(data uint32, width int) uint32 {
return register1 & significant_mask
}
// Lookup 计算单个数值的crc
func Lookup(data uint32,
// 计算单个数值的crc
func lookup(data uint32,
polynomial uint32,
width int,
input_reflected bool) uint32 {
@ -158,37 +156,6 @@ func CrcTableBased(
return (register1 ^ final_xor_value) & significant_mask
}
// MSB优先(字节中高位bit即最左侧bit优先)
func CrcBitOrientedMsbFirst(data []byte, polynomial uint32, width int) uint32 {
var register1 uint32 = 0
var significant_mask uint32 = 0xffffffff >> (32 - width)
var register_msb_mask uint32 = 1 << (width - 1)
var register_lsb_mask uint32 = 1
var byte_msb_mask uint32 = 0x80
length := len(data)
for i := 0; i < length+(width/8); i++ {
var byteData uint32 = 0
if i < length {
byteData = uint32(data[i])
}
for j := 0; j < 8; j++ {
need_xor := (register1 & register_msb_mask) == register_msb_mask
register1 <<= 1
need_or := (byteData & byte_msb_mask) == byte_msb_mask
byteData <<= 1
if need_or {
register1 |= register_lsb_mask
}
if need_xor {
register1 ^= polynomial
}
}
}
return register1 & significant_mask
}
func Crc(data []byte,
polynomial uint32,
width int,
@ -212,11 +179,11 @@ func Crc(data []byte,
if input_reflected {
shift_out = register1 & register_lsb_mask
value = Lookup(shift_out^byteData, polynomial, width, input_reflected)
value = lookup(shift_out^byteData, polynomial, width, input_reflected)
register1 = (register1 >> 8) ^ value
} else {
shift_out = (register1 >> (width - 8)) & register_lsb_mask
value = Lookup(shift_out^byteData, polynomial, width, input_reflected)
value = lookup(shift_out^byteData, polynomial, width, input_reflected)
register1 = (register1 << 8) ^ value
}
}