slimming/utils.go
2022-09-01 15:12:53 +08:00

84 lines
1.4 KiB
Go

package main
import (
"bytes"
"encoding/binary"
"encoding/gob"
"io"
"log"
"net"
"github.com/klauspost/compress/zstd"
)
func Compress(writer io.Writer, data [][]byte) {
// encode gob
enc := gob.NewEncoder(writer)
err := enc.Encode(data)
if err != nil {
log.Panic(err)
}
// zstd compress
zenc, err := zstd.NewWriter(writer)
if err != nil {
log.Panic(err)
}
err = zenc.Flush()
if err != nil {
log.Println(err)
}
}
func Decompress(data []byte) (frames [][]byte) {
var err error
var buf = bytes.NewBuffer(data)
var zdec *zstd.Decoder
zdec, err = zstd.NewReader(buf)
if err != nil {
log.Panic(err)
}
zdec.Close()
dec := gob.NewDecoder(buf)
err = dec.Decode(&frames)
if err != nil {
log.Panic(err)
}
return
}
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])
}