slimming/utils.go

58 lines
1.4 KiB
Go
Raw Normal View History

2022-08-31 09:52:16 +00:00
package main
import (
2022-09-01 06:16:36 +00:00
"encoding/binary"
"net"
2022-09-01 08:15:02 +00:00
"github.com/klauspost/compress/zstd"
2022-08-31 09:52:16 +00:00
)
2022-09-01 08:19:32 +00:00
var encoder, _ = zstd.NewWriter(nil)
2022-08-31 09:52:16 +00:00
2022-09-01 08:19:32 +00:00
// Compress a buffer.
// If you have a destination buffer, the allocation in the call can also be eliminated.
func Compress(src []byte) []byte {
2022-09-01 08:26:54 +00:00
return src
2022-09-01 08:19:32 +00:00
return encoder.EncodeAll(src, make([]byte, 0, len(src)))
2022-08-31 09:52:16 +00:00
}
2022-09-01 08:19:32 +00:00
// Create a reader that caches decompressors.
// For this operation type we supply a nil Reader.
var decoder, _ = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
2022-08-31 09:52:16 +00:00
2022-09-01 08:19:32 +00:00
// Decompress a buffer. We don't supply a destination buffer,
// so it will be allocated by the decoder.
func Decompress(src []byte) ([]byte, error) {
2022-09-01 08:26:54 +00:00
return src, nil
2022-09-01 08:19:32 +00:00
return decoder.DecodeAll(src, nil)
2022-08-31 09:52:16 +00:00
}
2022-09-01 02:42:06 +00:00
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)
}
2022-09-01 06:16:36 +00:00
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])
}