forked from quic-go/quic-go
wire: simplify packet number parsing, remove utils.ByteOrder interface (#4590)
This commit is contained in:
@@ -1,124 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
|
||||||
. "github.com/onsi/gomega"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ = Describe("Big Endian encoding / decoding", func() {
|
|
||||||
Context("converting", func() {
|
|
||||||
It("Uint16", func() {
|
|
||||||
b := []byte{0x13, 0x37}
|
|
||||||
Expect(BigEndian.Uint16(b)).To(Equal(uint16(0x1337)))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("Uint24", func() {
|
|
||||||
b := []byte{0x13, 0x99, 0x37}
|
|
||||||
Expect(BigEndian.Uint24(b)).To(Equal(uint32(0x139937)))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("Uint32", func() {
|
|
||||||
b := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
||||||
Expect(BigEndian.Uint32(b)).To(Equal(uint32(0xdeadbeef)))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("ReadUint16", func() {
|
|
||||||
It("reads a big endian", func() {
|
|
||||||
b := []byte{0x13, 0xEF}
|
|
||||||
val, err := BigEndian.ReadUint16(bytes.NewReader(b))
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
|
||||||
Expect(val).To(Equal(uint16(0x13EF)))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("throws an error if less than 2 bytes are passed", func() {
|
|
||||||
b := []byte{0x13, 0xEF}
|
|
||||||
for i := 0; i < len(b); i++ {
|
|
||||||
_, err := BigEndian.ReadUint16(bytes.NewReader(b[:i]))
|
|
||||||
Expect(err).To(MatchError(io.EOF))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("ReadUint24", func() {
|
|
||||||
It("reads a big endian", func() {
|
|
||||||
b := []byte{0x13, 0xbe, 0xef}
|
|
||||||
val, err := BigEndian.ReadUint24(bytes.NewReader(b))
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
|
||||||
Expect(val).To(Equal(uint32(0x13beef)))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("throws an error if less than 3 bytes are passed", func() {
|
|
||||||
b := []byte{0x13, 0xbe, 0xef}
|
|
||||||
for i := 0; i < len(b); i++ {
|
|
||||||
_, err := BigEndian.ReadUint24(bytes.NewReader(b[:i]))
|
|
||||||
Expect(err).To(MatchError(io.EOF))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("ReadUint32", func() {
|
|
||||||
It("reads a big endian", func() {
|
|
||||||
b := []byte{0x12, 0x35, 0xAB, 0xFF}
|
|
||||||
val, err := BigEndian.ReadUint32(bytes.NewReader(b))
|
|
||||||
Expect(err).ToNot(HaveOccurred())
|
|
||||||
Expect(val).To(Equal(uint32(0x1235ABFF)))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("throws an error if less than 4 bytes are passed", func() {
|
|
||||||
b := []byte{0x12, 0x35, 0xAB, 0xFF}
|
|
||||||
for i := 0; i < len(b); i++ {
|
|
||||||
_, err := BigEndian.ReadUint32(bytes.NewReader(b[:i]))
|
|
||||||
Expect(err).To(MatchError(io.EOF))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("WriteUint16", func() {
|
|
||||||
It("outputs 2 bytes", func() {
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint16(b, uint16(1))
|
|
||||||
Expect(b.Len()).To(Equal(2))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("outputs a big endian", func() {
|
|
||||||
num := uint16(0xFF11)
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint16(b, num)
|
|
||||||
Expect(b.Bytes()).To(Equal([]byte{0xFF, 0x11}))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("WriteUint24", func() {
|
|
||||||
It("outputs 3 bytes", func() {
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint24(b, uint32(1))
|
|
||||||
Expect(b.Len()).To(Equal(3))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("outputs a big endian", func() {
|
|
||||||
num := uint32(0xff11aa)
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint24(b, num)
|
|
||||||
Expect(b.Bytes()).To(Equal([]byte{0xff, 0x11, 0xaa}))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("WriteUint32", func() {
|
|
||||||
It("outputs 4 bytes", func() {
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint32(b, uint32(1))
|
|
||||||
Expect(b.Len()).To(Equal(4))
|
|
||||||
})
|
|
||||||
|
|
||||||
It("outputs a big endian", func() {
|
|
||||||
num := uint32(0xEFAC3512)
|
|
||||||
b := &bytes.Buffer{}
|
|
||||||
BigEndian.WriteUint32(b, num)
|
|
||||||
Expect(b.Bytes()).To(Equal([]byte{0xEF, 0xAC, 0x35, 0x12}))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A ByteOrder specifies how to convert byte sequences into 16-, 32-, or 64-bit unsigned integers.
|
|
||||||
type ByteOrder interface {
|
|
||||||
Uint32([]byte) uint32
|
|
||||||
Uint24([]byte) uint32
|
|
||||||
Uint16([]byte) uint16
|
|
||||||
|
|
||||||
ReadUint32(io.ByteReader) (uint32, error)
|
|
||||||
ReadUint24(io.ByteReader) (uint32, error)
|
|
||||||
ReadUint16(io.ByteReader) (uint16, error)
|
|
||||||
|
|
||||||
WriteUint32(*bytes.Buffer, uint32)
|
|
||||||
WriteUint24(*bytes.Buffer, uint32)
|
|
||||||
WriteUint16(*bytes.Buffer, uint16)
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BigEndian is the big-endian implementation of ByteOrder.
|
|
||||||
var BigEndian ByteOrder = bigEndian{}
|
|
||||||
|
|
||||||
type bigEndian struct{}
|
|
||||||
|
|
||||||
var _ ByteOrder = &bigEndian{}
|
|
||||||
|
|
||||||
// ReadUintN reads N bytes
|
|
||||||
func (bigEndian) ReadUintN(b io.ByteReader, length uint8) (uint64, error) {
|
|
||||||
var res uint64
|
|
||||||
for i := uint8(0); i < length; i++ {
|
|
||||||
bt, err := b.ReadByte()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
res ^= uint64(bt) << ((length - 1 - i) * 8)
|
|
||||||
}
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadUint32 reads a uint32
|
|
||||||
func (bigEndian) ReadUint32(b io.ByteReader) (uint32, error) {
|
|
||||||
var b1, b2, b3, b4 uint8
|
|
||||||
var err error
|
|
||||||
if b4, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b3, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b2, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b1, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadUint24 reads a uint24
|
|
||||||
func (bigEndian) ReadUint24(b io.ByteReader) (uint32, error) {
|
|
||||||
var b1, b2, b3 uint8
|
|
||||||
var err error
|
|
||||||
if b3, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b2, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b1, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadUint16 reads a uint16
|
|
||||||
func (bigEndian) ReadUint16(b io.ByteReader) (uint16, error) {
|
|
||||||
var b1, b2 uint8
|
|
||||||
var err error
|
|
||||||
if b2, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if b1, err = b.ReadByte(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return uint16(b1) + uint16(b2)<<8, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bigEndian) Uint32(b []byte) uint32 {
|
|
||||||
return binary.BigEndian.Uint32(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bigEndian) Uint24(b []byte) uint32 {
|
|
||||||
_ = b[2] // bounds check hint to compiler; see golang.org/issue/14808
|
|
||||||
return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bigEndian) Uint16(b []byte) uint16 {
|
|
||||||
return binary.BigEndian.Uint16(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteUint32 writes a uint32
|
|
||||||
func (bigEndian) WriteUint32(b *bytes.Buffer, i uint32) {
|
|
||||||
b.Write([]byte{uint8(i >> 24), uint8(i >> 16), uint8(i >> 8), uint8(i)})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteUint24 writes a uint24
|
|
||||||
func (bigEndian) WriteUint24(b *bytes.Buffer, i uint32) {
|
|
||||||
b.Write([]byte{uint8(i >> 16), uint8(i >> 8), uint8(i)})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteUint16 writes a uint16
|
|
||||||
func (bigEndian) WriteUint16(b *bytes.Buffer, i uint16) {
|
|
||||||
b.Write([]byte{uint8(i >> 8), uint8(i)})
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/quic-go/quic-go/internal/protocol"
|
"github.com/quic-go/quic-go/internal/protocol"
|
||||||
"github.com/quic-go/quic-go/internal/utils"
|
|
||||||
"github.com/quic-go/quic-go/quicvarint"
|
"github.com/quic-go/quic-go/quicvarint"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -302,11 +301,11 @@ func readPacketNumber(data []byte, pnLen protocol.PacketNumberLen) (protocol.Pac
|
|||||||
case protocol.PacketNumberLen1:
|
case protocol.PacketNumberLen1:
|
||||||
pn = protocol.PacketNumber(data[0])
|
pn = protocol.PacketNumber(data[0])
|
||||||
case protocol.PacketNumberLen2:
|
case protocol.PacketNumberLen2:
|
||||||
pn = protocol.PacketNumber(utils.BigEndian.Uint16(data[:2]))
|
pn = protocol.PacketNumber(binary.BigEndian.Uint16(data[:2]))
|
||||||
case protocol.PacketNumberLen3:
|
case protocol.PacketNumberLen3:
|
||||||
pn = protocol.PacketNumber(utils.BigEndian.Uint24(data[:3]))
|
pn = protocol.PacketNumber(uint32(data[2]) + uint32(data[1])<<8 + uint32(data[0])<<16)
|
||||||
case protocol.PacketNumberLen4:
|
case protocol.PacketNumberLen4:
|
||||||
pn = protocol.PacketNumber(utils.BigEndian.Uint32(data[:4]))
|
pn = protocol.PacketNumber(binary.BigEndian.Uint32(data[:4]))
|
||||||
default:
|
default:
|
||||||
return 0, fmt.Errorf("invalid packet number length: %d", pnLen)
|
return 0, fmt.Errorf("invalid packet number length: %d", pnLen)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user