Files
coding/encode.go

24 lines
412 B
Go
Raw Normal View History

2023-12-24 17:57:00 -05:00
package coding
import (
"bytes"
2023-12-24 18:03:29 -05:00
2023-12-24 17:57:00 -05:00
"github.com/icza/bitio"
"github.com/samber/lo"
2023-12-24 18:03:29 -05:00
"github.com/securemesh/coding/heap"
2023-12-24 17:57:00 -05:00
)
2023-12-24 18:03:29 -05:00
func Encode(h *heap.Heap, msg []byte) []byte {
2023-12-24 17:57:00 -05:00
buf := &bytes.Buffer{}
w := bitio.NewWriter(buf)
for _, b := range msg {
index := h.IncrementSymbol(b)
code := codes[index]
lo.Must0(w.WriteBits(uint64(code.value), uint8(code.bits)))
}
lo.Must0(w.Close())
return buf.Bytes()
}