You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
792 B

package transport
import (
"bytes"
"io"
"github.com/go-faster/errors"
"github.com/gotd/td/internal/proto/codec"
)
func detectCodec(c io.Reader) (Codec, io.Reader, error) {
var buf [4]byte
if _, err := io.ReadFull(c, buf[:1]); err != nil {
return nil, nil, errors.Wrap(err, "read first byte")
}
if buf[0] == codec.AbridgedClientStart[0] {
return Abridged.Codec(), c, nil
}
if _, err := io.ReadFull(c, buf[1:4]); err != nil {
return nil, nil, errors.Wrap(err, "read header")
}
switch buf {
case codec.IntermediateClientStart:
return Intermediate.Codec(), c, nil
case codec.PaddedIntermediateClientStart:
return PaddedIntermediate.Codec(), c, nil
default:
buffered := bytes.NewReader(buf[:])
r := io.MultiReader(buffered, c)
return Full.Codec(), r, nil
}
}