Files
coding/encode.go

26 lines
498 B
Go
Raw Permalink 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-29 12:52:34 -08:00
"github.com/securemesh/coding/codes"
2023-12-30 20:14:01 -07:00
"github.com/securemesh/coding/state"
2023-12-24 17:57:00 -05:00
)
2023-12-30 20:14:01 -07:00
func Encode(st *state.State, msg []byte) []byte {
2023-12-24 17:57:00 -05:00
buf := &bytes.Buffer{}
w := bitio.NewWriter(buf)
2023-12-31 16:09:35 -08:00
for i := 0; i < len(msg); {
l, index := st.IncrementSymbol(msg[i:])
i += l
2023-12-31 19:24:17 -08:00
code := codes.CodeForIndex(uint32(index))
2023-12-29 12:52:34 -08:00
lo.Must0(w.WriteBits(uint64(code.Value), uint8(code.Bits)))
2023-12-24 17:57:00 -05:00
}
lo.Must0(w.Close())
return buf.Bytes()
}