fix: use cryptobyte for internal helper

`helper.Uint8to16()` now calls `(*cryptobyte.String).ReadUint16()`
This commit is contained in:
Gaukas Wang 2023-03-03 00:17:53 -07:00
parent c6be42d855
commit a72e17e699
No known key found for this signature in database
GPG key ID: 9E2F8986D76F8B5D

View file

@ -1,35 +1,23 @@
package helper
import (
"bytes"
"encoding/binary"
"io"
)
"errors"
func ReadUint16(r io.Reader, v *uint16) error {
var buf [2]byte
if _, err := io.ReadFull(r, buf[:]); err != nil {
return err
}
*v = binary.BigEndian.Uint16(buf[:])
return nil
}
"golang.org/x/crypto/cryptobyte"
)
// Uint8to16 converts a slice of uint8 to a slice of uint16.
// e.g. []uint8{0x00, 0x01, 0x00, 0x02} -> []uint16{0x0001, 0x0002}
func Uint8to16(in []uint8) ([]uint16, error) {
reader := bytes.NewReader(in)
s := cryptobyte.String(in)
var out []uint16
for {
for !s.Empty() {
var v uint16
err := ReadUint16(reader, &v)
if err == io.EOF {
break
if s.ReadUint16(&v) {
out = append(out, v)
} else {
return nil, errors.New("ReadUint16 failed")
}
if err != nil {
return nil, err
}
out = append(out, v)
}
return out, nil
}