From 66d1b272b39822a06fc5793eacd7f75a90957c18 Mon Sep 17 00:00:00 2001 From: xzb <223@qq.com> Date: Thu, 2 Nov 2023 14:37:50 +0800 Subject: [PATCH] rssp lfsr --- third_party/message/rssp.go | 4 +- .../message/{rssp_crc.go => rssp_code.go} | 50 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) rename third_party/message/{rssp_crc.go => rssp_code.go} (80%) diff --git a/third_party/message/rssp.go b/third_party/message/rssp.go index 40bb183..973d23a 100644 --- a/third_party/message/rssp.go +++ b/third_party/message/rssp.go @@ -72,7 +72,7 @@ func RsspPackCrc16Check(pack []byte) bool { // RsspRsd 实时安全数据包 type RsspRsd struct { RsspHead - //安全校验域-序列号(4Byte) + //安全校验域-序列号(4Byte),可以是一个随时间周期变化的计数器 Sn uint32 //安全校验域-安全数据长度(2Byte) Sdl uint16 @@ -325,3 +325,5 @@ func PackToString(pack []byte) string { } return b.String() } + +//////////////////////////////////////////////////////////////////// diff --git a/third_party/message/rssp_crc.go b/third_party/message/rssp_code.go similarity index 80% rename from third_party/message/rssp_crc.go rename to third_party/message/rssp_code.go index 1548a50..13adc52 100644 --- a/third_party/message/rssp_crc.go +++ b/third_party/message/rssp_code.go @@ -7,11 +7,15 @@ var crc16Table []uint32 = nil var crc32C1Table []uint32 = nil var crc32C2Table []uint32 = nil -const ( +const ( //CRC生成多项式 RsspCrc16GX uint32 = 0b1_0000_1000_0001_0001 //生成多项式 G(X)=X16+X11+X4+1 RsspCrc32C1 uint32 = 0x100d4e63 //安全通道1 CRC32生成多项式 RsspCrc32C2 uint32 = 0x8ce56011 //安全通道1 CRC32生成多项式 ) +const ( //时间戳生成多项式 + RsspTsC1 uint32 = 0x0fc22f87 + RsspTsC2 uint32 = 0xc3e887e1 +) // InitRsspCrcTable 初始化RSSP协议中需要的CRC表 func InitRsspCrcTable() { @@ -193,3 +197,47 @@ func Crc(data []byte, } return (register1 ^ final_xor_value) & significant_mask } + +//////////////////////////////////////////////////////// + +// 线性反馈移位寄存器 +type RsspLFSR struct { + polynomial uint32 //生成多项式 + width int //寄存器宽度 + register uint32 //寄存器 +} + +func NewRsspLFSR(polynomial uint32, width int, init_value uint32) *RsspLFSR { + var wd_msb_mask uint32 = 1 << (width - 1) + var wd_mask uint32 = 0xffffffff >> (32 - width) + polynomial &= wd_mask + polynomial = (polynomial >> 1) | wd_msb_mask + // + return &RsspLFSR{polynomial: polynomial, width: width, register: init_value} +} +func (r *RsspLFSR) move() { + var significant_mask uint32 = 0xffffffff >> (32 - r.width) + // + r.register &= significant_mask + cb := r.register & r.polynomial & significant_mask + out := bitXor(cb, r.width) + r.register >>= 1 + r.register = r.register | (out << (r.width - 1)) + r.register &= significant_mask +} + +func (r *RsspLFSR) GetAndMove() uint32 { + rt := r.register + r.move() + return rt +} + +// return 0 或 1 +func bitXor(data uint32, width int) uint32 { + var v uint32 = 0 + var lsb_mask uint32 = 1 + for i := 0; i < width; i++ { + v ^= data >> i + } + return v & lsb_mask +}