package codec
import (
"io"
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/crypto"
)
var PaddedIntermediateClientStart = [4 ]byte {0xdd , 0xdd , 0xdd , 0xdd }
type PaddedIntermediate struct {}
var (
_ TaggedCodec = PaddedIntermediate {}
)
func (i PaddedIntermediate ) WriteHeader (w io .Writer ) error {
if _ , err := w .Write (PaddedIntermediateClientStart [:]); err != nil {
return errors .Wrap (err , "write padded intermediate header" )
}
return nil
}
func (i PaddedIntermediate ) ReadHeader (r io .Reader ) error {
var b [4 ]byte
if _ , err := io .ReadFull (r , b [:]); err != nil {
return errors .Wrap (err , "read padded intermediate header" )
}
if b != PaddedIntermediateClientStart {
return ErrProtocolHeaderMismatch
}
return nil
}
func (i PaddedIntermediate ) ObfuscatedTag () [4 ]byte {
return PaddedIntermediateClientStart
}
func (i PaddedIntermediate ) Write (w io .Writer , b *bin .Buffer ) error {
if err := checkOutgoingMessage (b ); err != nil {
return err
}
if err := checkAlign (b , 4 ); err != nil {
return err
}
if err := writePaddedIntermediate (crypto .DefaultRand (), w , b ); err != nil {
return errors .Wrap (err , "write padded intermediate" )
}
return nil
}
func (i PaddedIntermediate ) Read (r io .Reader , b *bin .Buffer ) error {
if err := readPaddedIntermediate (r , b ); err != nil {
return errors .Wrap (err , "read padded intermediate" )
}
return checkProtocolError (b )
}
func writePaddedIntermediate (randSource io .Reader , w io .Writer , b *bin .Buffer ) error {
length := b .Len ()
b .Expand (4 )
defer func () {
b .Buf = b .Buf [:length ]
}()
_ , err := io .ReadFull (randSource , b .Buf [length :length +4 ])
if err != nil {
return err
}
n := int (b .Buf [length -1 ]) % 4
b .Buf = b .Buf [:length +n ]
return writeIntermediate (w , b )
}
func readPaddedIntermediate (r io .Reader , b *bin .Buffer ) error {
if err := readIntermediate (r , b , true ); err != nil {
return err
}
padding := b .Len () % 4
b .Buf = b .Buf [:b .Len ()-padding ]
return nil
}
The pages are generated with Golds v0.6.7 . (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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds .