|
@ -24,14 +24,14 @@ type WordCodec struct { |
|
|
check ECC |
|
|
check ECC |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var _ Codec = WordCodec{} |
|
|
|
|
|
|
|
|
var _ Codec = &WordCodec{} |
|
|
|
|
|
|
|
|
func NewCodec(words []string) (codec WordCodec, err error) { |
|
|
|
|
|
|
|
|
func NewCodec(words []string) (codec *WordCodec, err error) { |
|
|
if len(words) != BankSize { |
|
|
if len(words) != BankSize { |
|
|
return codec, errors.Errorf("Bank must have %d words, found %d", BankSize, len(words)) |
|
|
return codec, errors.Errorf("Bank must have %d words, found %d", BankSize, len(words)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
res := WordCodec{ |
|
|
|
|
|
|
|
|
res := &WordCodec{ |
|
|
words: words, |
|
|
words: words, |
|
|
// TODO: configure this outside???
|
|
|
// TODO: configure this outside???
|
|
|
check: NewIEEECRC32(), |
|
|
check: NewIEEECRC32(), |
|
@ -40,7 +40,7 @@ func NewCodec(words []string) (codec WordCodec, err error) { |
|
|
return res, nil |
|
|
return res, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func LoadCodec(bank string) (codec WordCodec, err error) { |
|
|
|
|
|
|
|
|
func LoadCodec(bank string) (codec *WordCodec, err error) { |
|
|
words, err := loadBank(bank) |
|
|
words, err := loadBank(bank) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return codec, err |
|
|
return codec, err |
|
@ -96,7 +96,7 @@ func bytelenFromWords(numWords int) (length int, maybeShorter bool) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// TODO: add checksum
|
|
|
// TODO: add checksum
|
|
|
func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) { |
|
|
|
|
|
|
|
|
func (c *WordCodec) BytesToWords(raw []byte) (words []string, err error) { |
|
|
// always add a checksum to the data
|
|
|
// always add a checksum to the data
|
|
|
data := c.check.AddECC(raw) |
|
|
data := c.check.AddECC(raw) |
|
|
numWords := wordlenFromBytes(len(data)) |
|
|
numWords := wordlenFromBytes(len(data)) |
|
@ -120,7 +120,7 @@ func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) { |
|
|
return words, nil |
|
|
return words, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (c WordCodec) WordsToBytes(words []string) ([]byte, error) { |
|
|
|
|
|
|
|
|
func (c *WordCodec) WordsToBytes(words []string) ([]byte, error) { |
|
|
l := len(words) |
|
|
l := len(words) |
|
|
|
|
|
|
|
|
if l == 0 { |
|
|
if l == 0 { |
|
@ -167,7 +167,7 @@ func (c WordCodec) WordsToBytes(words []string) ([]byte, error) { |
|
|
// GetIndex finds the index of the words to create bytes
|
|
|
// GetIndex finds the index of the words to create bytes
|
|
|
// Generates a map the first time it is loaded, to avoid needless
|
|
|
// Generates a map the first time it is loaded, to avoid needless
|
|
|
// computation when list is not used.
|
|
|
// computation when list is not used.
|
|
|
func (c WordCodec) GetIndex(word string) (int, error) { |
|
|
|
|
|
|
|
|
func (c *WordCodec) GetIndex(word string) (int, error) { |
|
|
// generate the first time
|
|
|
// generate the first time
|
|
|
if c.bytes == nil { |
|
|
if c.bytes == nil { |
|
|
b := map[string]int{} |
|
|
b := map[string]int{} |
|
|