package wire
import (
"io"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/quicvarint"
)
type CryptoFrame struct {
Offset protocol .ByteCount
Data []byte
}
func parseCryptoFrame(b []byte , _ protocol .Version ) (*CryptoFrame , int , error ) {
startLen := len (b )
frame := &CryptoFrame {}
offset , l , err := quicvarint .Parse (b )
if err != nil {
return nil , 0 , replaceUnexpectedEOF (err )
}
b = b [l :]
frame .Offset = protocol .ByteCount (offset )
dataLen , l , err := quicvarint .Parse (b )
if err != nil {
return nil , 0 , replaceUnexpectedEOF (err )
}
b = b [l :]
if dataLen > uint64 (len (b )) {
return nil , 0 , io .EOF
}
if dataLen != 0 {
frame .Data = make ([]byte , dataLen )
copy (frame .Data , b )
}
return frame , startLen - len (b ) + int (dataLen ), nil
}
func (f *CryptoFrame ) Append (b []byte , _ protocol .Version ) ([]byte , error ) {
b = append (b , cryptoFrameType )
b = quicvarint .Append (b , uint64 (f .Offset ))
b = quicvarint .Append (b , uint64 (len (f .Data )))
b = append (b , f .Data ...)
return b , nil
}
func (f *CryptoFrame ) Length (_ protocol .Version ) protocol .ByteCount {
return protocol .ByteCount (1 + quicvarint .Len (uint64 (f .Offset )) + quicvarint .Len (uint64 (len (f .Data ))) + len (f .Data ))
}
func (f *CryptoFrame ) MaxDataLen (maxSize protocol .ByteCount ) protocol .ByteCount {
headerLen := protocol .ByteCount (1 + quicvarint .Len (uint64 (f .Offset )) + 1 )
if headerLen > maxSize {
return 0
}
maxDataLen := maxSize - headerLen
if quicvarint .Len (uint64 (maxDataLen )) != 1 {
maxDataLen --
}
return maxDataLen
}
func (f *CryptoFrame ) MaybeSplitOffFrame (maxSize protocol .ByteCount , version protocol .Version ) (*CryptoFrame , bool ) {
if f .Length (version ) <= maxSize {
return nil , false
}
n := f .MaxDataLen (maxSize )
if n == 0 {
return nil , true
}
newLen := protocol .ByteCount (len (f .Data )) - n
new := &CryptoFrame {}
new .Offset = f .Offset
new .Data = make ([]byte , newLen )
new .Data , f .Data = f .Data , new .Data
copy (f .Data , new .Data [n :])
new .Data = new .Data [:n ]
f .Offset += n
return new , true
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .