54 lines
947 B
Go
54 lines
947 B
Go
package algos
|
|
|
|
// ByteBuf 字节缓冲区
|
|
type ByteBuf struct {
|
|
mem []byte
|
|
dataSize int
|
|
pos int
|
|
}
|
|
|
|
// NewByteBuf 创建新的字节缓冲区
|
|
func NewByteBuf(data []byte, size *int) *ByteBuf {
|
|
buf := &ByteBuf{
|
|
pos: 0,
|
|
}
|
|
|
|
if data != nil {
|
|
buf.mem = data
|
|
}
|
|
|
|
if size != nil {
|
|
buf.dataSize = *size
|
|
} else if data != nil {
|
|
buf.dataSize = len(data)
|
|
} else {
|
|
panic("Either size or data must be provided")
|
|
}
|
|
|
|
return buf
|
|
}
|
|
|
|
// Data 获取数据
|
|
func (b *ByteBuf) Data() []byte {
|
|
return b.mem
|
|
}
|
|
|
|
// Size 获取大小
|
|
func (b *ByteBuf) Size() int {
|
|
return b.dataSize
|
|
}
|
|
|
|
// RemovePadding 移除填充
|
|
func (b *ByteBuf) RemovePadding() []byte {
|
|
paddingSize := PKCS7DataLength(b.mem, b.dataSize, 16)
|
|
if paddingSize == 0 {
|
|
return b.mem[:b.dataSize]
|
|
}
|
|
b.dataSize = paddingSize
|
|
dst := make([]byte, b.dataSize)
|
|
copy(dst, b.mem[:b.dataSize])
|
|
b.mem = dst
|
|
return b.mem
|
|
}
|
|
|