rts-sim-testing-service/third_party/message/btm_vobc_translate_fffe.go
2024-09-05 09:21:49 +08:00

284 lines
5.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package message
import (
"fmt"
)
func aa(src []byte, dest []byte) uint16 {
var (
SrcPos, TgtPos, Pos1, Pos2, iii, Gap uint16
Got1stFF uint8
)
srouceLen := uint16(len(src))
for SrcPos = 0; SrcPos < srouceLen; SrcPos++ {
if Got1stFF == 1 {
if src[SrcPos] == 0xff {
//Got2ndFF = 1
Pos2 = SrcPos
dest[TgtPos] = byte(Pos2 - Pos1)
TgtPos++
for iii = Pos1 + 1; iii < Pos2; iii++ {
dest[TgtPos] = src[iii]
TgtPos++
}
Got1stFF = 0
//Got2ndFF = 0
Pos1 = 0
Pos2 = 0
Gap = 0
} else {
/*已遇到前一个FF且当前遇到非FF*/
Gap++
if 252 == Gap {
Got1stFF = 0
Gap = 0
dest[TgtPos] = 0
TgtPos++
for iii = Pos1 + 1; iii <= SrcPos; iii++ {
dest[TgtPos] = src[iii]
TgtPos++
}
}
}
} else {
/*尚未遇到前一个FF*/
dest[TgtPos] = src[SrcPos]
TgtPos++
if 0xFF == src[SrcPos] {
/*遇到前一个FF*/
Got1stFF = 1
Pos1 = SrcPos
Gap = 0
}
}
}
if 1 == Got1stFF {
dest[TgtPos] = 0
TgtPos++
for iii = Pos1 + 1; iii < srouceLen; iii++ {
dest[TgtPos] = src[iii]
TgtPos++
}
}
return TgtPos
}
func TranslateFromFFFE(pSrc []byte) ([]byte, uint16) {
var (
srcPos, tgtPos, nonFFCount, nextFFPos uint16
char uint8
state int
)
if len(pSrc) == 0 {
return nil, 0 // 入参错误
}
pTgt := make([]byte, len(pSrc)) // 预分配与输入等长的缓冲区
tgtPos = 0
state = WAIT_FF_C1
nonFFCount = 0
for srcPos = 0; srcPos < uint16(len(pSrc)); srcPos++ {
char = pSrc[srcPos]
switch state {
case WAIT_FF_C1:
if char == 0xFF {
pTgt[tgtPos] = char
tgtPos++
state = WAIT_FF_C2
} else {
pTgt[tgtPos] = char
tgtPos++
}
case WAIT_FF_C2:
if char == 0xFD || char == 0xFE || char == 0xFF {
state = ABNORMAL_STATE
} else {
if char == 0 {
state = WAIT_NO_FF
} else if char == 1 {
pTgt[tgtPos] = 0xFF
tgtPos++
state = WAIT_FF_C1
} else {
nextFFPos = srcPos + uint16(char) - 1
state = WAIT_FF_POS
}
}
case WAIT_NO_FF:
nonFFCount++
if char == 0xFF && nonFFCount < 252 {
state = ABNORMAL_STATE
} else {
pTgt[tgtPos] = char
tgtPos++
if nonFFCount == 252 {
nonFFCount = 0
state = WAIT_FF_C1
}
}
case WAIT_FF_POS:
if char == 0xFF {
state = ABNORMAL_STATE
} else {
pTgt[tgtPos] = char
tgtPos++
if srcPos == nextFFPos {
pTgt[tgtPos] = 0xFF
tgtPos++
state = WAIT_FF_C1
}
}
default:
state = ABNORMAL_STATE
}
if state == ABNORMAL_STATE {
tgtPos = 0
break
}
}
// 退出时的状态判断
if state == WAIT_FF_C2 || state == WAIT_FF_POS {
tgtPos = 0
}
return pTgt[:tgtPos], tgtPos
}
// TranslateToFFFE 对给定的字节切片进行FFFE转义处理
func TranslateToFFFE(src []byte) ([]byte, error) {
if src == nil {
return nil, fmt.Errorf("source data is nil")
}
var tgt []byte
var pos1, pos2 int
var gap int
var got1stFF bool
for i, b := range src {
if got1stFF {
if b == 0xFF {
// 已遇到前一个FF且当前又遇到FF
got1stFF = false
pos2 = i
if gap > 252 {
// 间隙过大,特殊处理
tgt = append(tgt, 0)
tgt = append(tgt, src[pos1+1:pos2]...)
} else {
// 写入间隙长度
tgt = append(tgt, byte(gap))
// 写入间隙中的字节
tgt = append(tgt, src[pos1+1:pos2]...)
}
} else {
// 已遇到前一个FF且当前遇到非FF增加gap计数
gap++
}
} else {
// 尚未遇到前一个FF
tgt = append(tgt, b)
if b == 0xFF {
// 遇到前一个FF
got1stFF = true
pos1 = i
gap = 0
}
}
}
// 如果在数据末尾遇到了FF且没有第二个FF
if got1stFF {
if gap > 252 {
tgt = append(tgt, 0)
tgt = append(tgt, src[pos1+1:]...)
} else {
tgt = append(tgt, 0)
tgt = append(tgt, src[pos1+1:len(src)]...)
}
}
return tgt, nil
}
func TranslateToFFFE3(pSrc []uint8, SrcLen uint16) ([]byte, uint16) {
var (
SrcPos, TgtPos, Pos1, Pos2, iii uint16
Gap uint16
Got1stFF uint8
pTgt []uint8
)
if pSrc == nil {
fmt.Println("入口参数错误")
return nil, 0
}
pTgt = make([]uint8, 0, SrcLen*2) // 预分配足够的空间以避免频繁扩容
TgtPos = 0
for SrcPos = 0; SrcPos < SrcLen; SrcPos++ {
if Got1stFF == 1 {
if pSrc[SrcPos] == 0xFF {
// 已遇到前一个FF且当前又遇到FF
//Got2ndFF = 1
Pos2 = SrcPos
pTgt = append(pTgt, uint8(Pos2-Pos1-1))
TgtPos++
for iii = Pos1 + 1; iii < Pos2; iii++ {
pTgt = append(pTgt, pSrc[iii])
TgtPos++
}
Got1stFF = 0
//Got2ndFF = 0
Pos1 = 0
Pos2 = 0
Gap = 0
} else {
// 已遇到前一个FF且当前遇到非FF
Gap++
if Gap == 252 {
Got1stFF = 0
Gap = 0
pTgt = append(pTgt, 0)
TgtPos++
for iii = Pos1 + 1; iii <= SrcPos; iii++ {
pTgt = append(pTgt, pSrc[iii])
TgtPos++
}
}
}
} else {
// 尚未遇到前一个FF
pTgt = append(pTgt, pSrc[SrcPos])
TgtPos++
if pSrc[SrcPos] == 0xFF {
// 遇到前一个FF
Got1stFF = 1
Pos1 = SrcPos
Gap = 0
}
}
}
// 已经遇到了前一个FF, 且源数据已到了末尾仍未遇到后一个FF
if Got1stFF == 1 {
pTgt = append(pTgt, 0)
TgtPos++
for iii = Pos1 + 1; iii < SrcLen; iii++ {
pTgt = append(pTgt, pSrc[iii])
TgtPos++
}
}
return pTgt, TgtPos
}