代码拉取完成,页面将自动刷新
同步操作将从 YoMo/y3-codec-golang 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package y3
import (
"errors"
"fmt"
"github.com/yomorun/y3-codec-golang/pkg/encoding"
"github.com/yomorun/y3-codec-golang/internal/mark"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
// DecodePrimitivePacket parse out whole buffer to a PrimitivePacket
//
// Examples:
// [0x01, 0x01, 0x01] -> Key=0x01, Value=0x01
// [0x41, 0x06, 0x03, 0x01, 0x61, 0x04, 0x01, 0x62] -> key=0x03, value=0x61; key=0x04, value=0x62
func DecodePrimitivePacket(buf []byte) (packet *PrimitivePacket, endPos int, sizeL int, err error) {
logger := utils.Logger.WithPrefix(utils.DefaultLogger, "BasePacket::Decode")
logger.Debugf("buf=%#X", buf)
if buf == nil || len(buf) < primitivePacketBufferMinimalLength {
return nil, 0, 0, errors.New("invalid y3 packet minimal size")
}
p := &PrimitivePacket{valBuf: buf}
var pos = 0
// first byte is `Tag`
p.tag = mark.NewTag(buf[pos])
pos++
// read `Varint` from buf for `Length of value`
tmpBuf := buf[pos:]
var bufLen int32
codec := encoding.VarCodec{}
err = codec.DecodePVarInt32(tmpBuf, &bufLen)
if err != nil {
return nil, 0, 0, err
}
sizeL = codec.Size
if sizeL < 1 {
return nil, 0, sizeL, errors.New("malformed, size of Length can not smaller than 1")
}
// 根据文档表述,p.length指的是value的长度,所以修改为bufLen的值
//p.length = uint32(len)
//pos += int(bufLen)
p.length = uint32(bufLen)
pos += sizeL
endPos = pos + int(p.length)
logger.Debugf(">>> sizeL=%v, length=%v, pos=%v, endPos=%v", sizeL, p.length, pos, endPos)
if pos > endPos || endPos > len(buf) || pos > len(buf) {
return nil, 0, sizeL, fmt.Errorf("beyond the boundary, pos=%v, endPos=%v", pos, endPos)
}
p.valBuf = buf[pos:endPos]
logger.Debugf("valBuf = %#X", p.valBuf)
return p, endPos, sizeL, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。