mirror of
https://github.com/refraction-networking/utls.git
synced 2025-04-04 12:37:35 +03:00
fix: use cryptobyte for internal helper
`helper.Uint8to16()` now calls `(*cryptobyte.String).ReadUint16()`
This commit is contained in:
parent
c6be42d855
commit
a72e17e699
1 changed files with 9 additions and 21 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue