slimming/utils.go
2022-09-01 16:19:32 +08:00

56 lines
1.3 KiB
Go

package main
import (
"encoding/binary"
"net"
"github.com/klauspost/compress/zstd"
)
var encoder, _ = zstd.NewWriter(nil)
// Compress a buffer.
// If you have a destination buffer, the allocation in the call can also be eliminated.
func Compress(src []byte) []byte {
return encoder.EncodeAll(src, make([]byte, 0, len(src)))
}
// Create a reader that caches decompressors.
// For this operation type we supply a nil Reader.
var decoder, _ = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
// Decompress a buffer. We don't supply a destination buffer,
// so it will be allocated by the decoder.
func Decompress(src []byte) ([]byte, error) {
return decoder.DecodeAll(src, nil)
}
func SwapBytes(frame1 []byte, start1 int, frame2 []byte, start2 int, size int) {
var temp []byte = make([]byte, size)
copy(temp, frame1[start1:start1+size])
copy(frame1[start1:start1+size], frame2[start2:start2+size])
copy(frame2[start2:start2+size], temp)
}
type ARP []byte
func (arp ARP) Destination() net.HardwareAddr {
return net.HardwareAddr(arp[:6])
}
func (arp ARP) Source() net.HardwareAddr {
return net.HardwareAddr(arp[6:12])
}
func (arp ARP) Op() uint16 {
return binary.BigEndian.Uint16(arp[20:22])
}
func (arp ARP) IPv4Source() net.IP {
return net.IP(arp[28:32])
}
func (arp ARP) IPv4Destination() net.IP {
return net.IP(arp[38:42])
}