mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-04 12:47:36 +03:00
move utils.go to its own package so that it's available for aeads
This commit is contained in:
parent
07f5daa7ba
commit
4a44e4982e
4 changed files with 32 additions and 20 deletions
|
@ -5,6 +5,8 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Tag in the QUIC crypto
|
// A Tag in the QUIC crypto
|
||||||
|
@ -116,9 +118,9 @@ func ParseCryptoMessage(data []byte) (Tag, map[Tag][]byte, error) {
|
||||||
|
|
||||||
// WriteCryptoMessage writes a crypto message
|
// WriteCryptoMessage writes a crypto message
|
||||||
func WriteCryptoMessage(b *bytes.Buffer, messageTag Tag, data map[Tag][]byte) {
|
func WriteCryptoMessage(b *bytes.Buffer, messageTag Tag, data map[Tag][]byte) {
|
||||||
writeUint32(b, uint32(messageTag))
|
utils.WriteUint32(b, uint32(messageTag))
|
||||||
writeUint16(b, uint16(len(data)))
|
utils.WriteUint16(b, uint16(len(data)))
|
||||||
writeUint16(b, 0)
|
utils.WriteUint16(b, 0)
|
||||||
|
|
||||||
// Save current position in the buffer, so that we can update the index in-place later
|
// Save current position in the buffer, so that we can update the index in-place later
|
||||||
indexStart := b.Len()
|
indexStart := b.Len()
|
||||||
|
@ -133,7 +135,7 @@ func WriteCryptoMessage(b *bytes.Buffer, messageTag Tag, data map[Tag][]byte) {
|
||||||
tags[i] = uint32(t)
|
tags[i] = uint32(t)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.Sort(Uint32Slice(tags))
|
sort.Sort(utils.Uint32Slice(tags))
|
||||||
|
|
||||||
offset := uint32(0)
|
offset := uint32(0)
|
||||||
for i, t := range tags {
|
for i, t := range tags {
|
||||||
|
|
14
frame.go
14
frame.go
|
@ -3,6 +3,8 @@ package quic
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A StreamFrame of QUIC
|
// A StreamFrame of QUIC
|
||||||
|
@ -29,20 +31,20 @@ func ParseStreamFrame(r *bytes.Reader) (*StreamFrame, error) {
|
||||||
}
|
}
|
||||||
streamIDLen := typeByte&0x03 + 1
|
streamIDLen := typeByte&0x03 + 1
|
||||||
|
|
||||||
sid, err := readUintN(r, streamIDLen)
|
sid, err := utils.ReadUintN(r, streamIDLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
frame.StreamID = uint32(sid)
|
frame.StreamID = uint32(sid)
|
||||||
|
|
||||||
frame.Offset, err = readUintN(r, offsetLen)
|
frame.Offset, err = utils.ReadUintN(r, offsetLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataLen uint16
|
var dataLen uint16
|
||||||
if dataLenPresent {
|
if dataLenPresent {
|
||||||
dataLen, err = readUint16(r)
|
dataLen, err = utils.ReadUint16(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -76,10 +78,10 @@ func WriteStreamFrame(b *bytes.Buffer, f *StreamFrame) {
|
||||||
}
|
}
|
||||||
typeByte ^= 0x03 // TODO: Send shorter stream ID if possible
|
typeByte ^= 0x03 // TODO: Send shorter stream ID if possible
|
||||||
b.WriteByte(typeByte)
|
b.WriteByte(typeByte)
|
||||||
writeUint32(b, f.StreamID)
|
utils.WriteUint32(b, f.StreamID)
|
||||||
if f.Offset != 0 {
|
if f.Offset != 0 {
|
||||||
writeUint64(b, f.Offset)
|
utils.WriteUint64(b, f.Offset)
|
||||||
}
|
}
|
||||||
writeUint16(b, uint16(len(f.Data)))
|
utils.WriteUint16(b, uint16(len(f.Data)))
|
||||||
b.Write(f.Data)
|
b.Write(f.Data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package quic
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/lucas-clemente/quic-go/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The PublicHeader of a QUIC packet
|
// The PublicHeader of a QUIC packet
|
||||||
|
@ -47,7 +49,7 @@ func ParsePublicHeader(b io.ByteReader) (*PublicHeader, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connection ID
|
// Connection ID
|
||||||
header.ConnectionID, err = readUintN(b, connectionIDLen)
|
header.ConnectionID, err = utils.ReadUintN(b, connectionIDLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -55,7 +57,7 @@ func ParsePublicHeader(b io.ByteReader) (*PublicHeader, error) {
|
||||||
// Version (optional)
|
// Version (optional)
|
||||||
if header.VersionFlag {
|
if header.VersionFlag {
|
||||||
var v uint64
|
var v uint64
|
||||||
v, err = readUintN(b, 4)
|
v, err = utils.ReadUintN(b, 4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -63,7 +65,7 @@ func ParsePublicHeader(b io.ByteReader) (*PublicHeader, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Packet number
|
// Packet number
|
||||||
header.PacketNumber, err = readUintN(b, packetNumberLen)
|
header.PacketNumber, err = utils.ReadUintN(b, packetNumberLen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package quic
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
func readUintN(b io.ByteReader, length uint8) (uint64, error) {
|
// ReadUintN reads N bytes
|
||||||
|
func ReadUintN(b io.ByteReader, length uint8) (uint64, error) {
|
||||||
var res uint64
|
var res uint64
|
||||||
for i := uint8(0); i < length; i++ {
|
for i := uint8(0); i < length; i++ {
|
||||||
bt, err := b.ReadByte()
|
bt, err := b.ReadByte()
|
||||||
|
@ -17,7 +18,8 @@ func readUintN(b io.ByteReader, length uint8) (uint64, error) {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readUint32(b io.ByteReader) (uint32, error) {
|
// ReadUint32 reads a uint32
|
||||||
|
func ReadUint32(b io.ByteReader) (uint32, error) {
|
||||||
var b1, b2, b3, b4 uint8
|
var b1, b2, b3, b4 uint8
|
||||||
var err error
|
var err error
|
||||||
if b1, err = b.ReadByte(); err != nil {
|
if b1, err = b.ReadByte(); err != nil {
|
||||||
|
@ -35,7 +37,8 @@ func readUint32(b io.ByteReader) (uint32, error) {
|
||||||
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
|
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readUint16(b io.ByteReader) (uint16, error) {
|
// ReadUint16 reads a uint16
|
||||||
|
func ReadUint16(b io.ByteReader) (uint16, error) {
|
||||||
var b1, b2 uint8
|
var b1, b2 uint8
|
||||||
var err error
|
var err error
|
||||||
if b1, err = b.ReadByte(); err != nil {
|
if b1, err = b.ReadByte(); err != nil {
|
||||||
|
@ -47,7 +50,8 @@ func readUint16(b io.ByteReader) (uint16, error) {
|
||||||
return uint16(b1) + uint16(b2)<<8, nil
|
return uint16(b1) + uint16(b2)<<8, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeUint64(b *bytes.Buffer, i uint64) {
|
// WriteUint64 writes a uint64
|
||||||
|
func WriteUint64(b *bytes.Buffer, i uint64) {
|
||||||
b.WriteByte(uint8(i & 0xff))
|
b.WriteByte(uint8(i & 0xff))
|
||||||
b.WriteByte(uint8((i >> 8) & 0xff))
|
b.WriteByte(uint8((i >> 8) & 0xff))
|
||||||
b.WriteByte(uint8((i >> 16) & 0xff))
|
b.WriteByte(uint8((i >> 16) & 0xff))
|
||||||
|
@ -58,14 +62,16 @@ func writeUint64(b *bytes.Buffer, i uint64) {
|
||||||
b.WriteByte(uint8((i >> 56) & 0xff))
|
b.WriteByte(uint8((i >> 56) & 0xff))
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeUint32(b *bytes.Buffer, i uint32) {
|
// WriteUint32 writes a uint32
|
||||||
|
func WriteUint32(b *bytes.Buffer, i uint32) {
|
||||||
b.WriteByte(uint8(i & 0xff))
|
b.WriteByte(uint8(i & 0xff))
|
||||||
b.WriteByte(uint8((i >> 8) & 0xff))
|
b.WriteByte(uint8((i >> 8) & 0xff))
|
||||||
b.WriteByte(uint8((i >> 16) & 0xff))
|
b.WriteByte(uint8((i >> 16) & 0xff))
|
||||||
b.WriteByte(uint8((i >> 24) & 0xff))
|
b.WriteByte(uint8((i >> 24) & 0xff))
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeUint16(b *bytes.Buffer, i uint16) {
|
// WriteUint16 writes a uint16
|
||||||
|
func WriteUint16(b *bytes.Buffer, i uint16) {
|
||||||
b.WriteByte(uint8(i & 0xff))
|
b.WriteByte(uint8(i & 0xff))
|
||||||
b.WriteByte(uint8((i >> 8) & 0xff))
|
b.WriteByte(uint8((i >> 8) & 0xff))
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue