89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package balisecodec
|
||
|
||
import (
|
||
"fmt"
|
||
"log/slog"
|
||
)
|
||
|
||
// 将830位的二进制数组以10位为单位组成一个左边为最高有效位(MSB)的无符号整数数组,除了第一个10位值,其余值求和,然后循环2的10次方次与其他值求和结果相加后模2的10次方,若结果和第一个10位值相同,则结束,此值即为原始的第一个10位值,将此值替换为第一个10位二进制数组,依然是左边为MSB
|
||
func revertFirst10Bits(b []byte) []byte {
|
||
if len(b) != 830 {
|
||
panic("invalid length")
|
||
}
|
||
// 将830位的二进制数组以10位为单位组成一个左边为最高有效位(MSB)的无符号整数数组
|
||
w10s := make([]uint16, 83)
|
||
for i := 0; i < 83; i++ {
|
||
w10s[i] = uint16(ToValLeftMsb(b[i*10 : i*10+10]))
|
||
//// 打印输出
|
||
//for j := 0; j < 10; j++ {
|
||
// fmt.Printf("%01b", b[i*10+j])
|
||
//}
|
||
//print(" ")
|
||
//if i != 0 && i%10 == 9 {
|
||
// println()
|
||
//}
|
||
}
|
||
//println()
|
||
// 将除了第一个10位字整数求和
|
||
sum := uint64(0)
|
||
for i := 1; i < 83; i++ {
|
||
sum += uint64(w10s[i])
|
||
}
|
||
// 循环2的10次方次与其他值求和结果相加后模2的10次方
|
||
for i := 0; i < 1024; i++ {
|
||
test := sum + uint64(i)
|
||
if test%1024 == uint64(w10s[0]) {
|
||
w10s[0] = uint16(i)
|
||
break
|
||
}
|
||
}
|
||
slog.Info("还原第一个10位值", "sum", sum, "bits[0]", w10s[0], "bits[0]b", fmt.Sprintf("%010b", w10s[0]))
|
||
bits := make([]byte, 830)
|
||
// 将整个10位数组转换为二进制数组,依然是MSB
|
||
u0bits := ToBitsLeftMsb(int(w10s[0]), 10)
|
||
for i := 0; i < 10; i++ {
|
||
bits[i] = u0bits[i]
|
||
}
|
||
for i := 10; i < 830; i++ {
|
||
bits[i] = b[i]
|
||
}
|
||
return bits
|
||
}
|
||
|
||
// 解扰,由加扰计算得到的S,作为初始状态为S的32位线性反馈移位寄存器对数据进行解扰
|
||
func descrambling(dn []byte, S uint32) []byte {
|
||
if len(dn) != 830 {
|
||
panic("invalid length")
|
||
}
|
||
// const Polynomial = 0x000000AF
|
||
out := make([]byte, len(dn))
|
||
t := S // 寄存器初始值
|
||
for i := 0; i < len(dn); i++ {
|
||
msb := (t >> 31) & 1
|
||
out[i] = (dn[i] ^ byte(msb)) & 1
|
||
// fmt.Printf("i=%d, t=%032b, msb=%d, dn=%d, out=%d\n", i, t, msb, dn[i], out[i])
|
||
xor := uint32(dn[i])
|
||
t = (xor << 30) ^ (xor << 29) ^ (xor << 28) ^ (xor << 26) ^ (xor << 24) ^ t
|
||
t = (t << 1) | xor
|
||
}
|
||
return out
|
||
}
|
||
|
||
// 转换913位数据为830位数据
|
||
func convert913To830(b913 []byte) ([]byte, error) {
|
||
if len(b913) != 913 {
|
||
panic("invalid length")
|
||
}
|
||
b830 := make([]byte, 830)
|
||
for i := 0; i < 83; i++ {
|
||
b10, err := From11(b913[i*11 : i*11+11])
|
||
if err != nil {
|
||
return nil, buildError(err.Error())
|
||
}
|
||
for j := 0; j < 10; j++ {
|
||
b830[i*10+j] = b10[j]
|
||
}
|
||
}
|
||
return b830, nil
|
||
}
|