package crypto
import (
"github.com/go-faster/errors"
"github.com/gotd/td/bin"
)
type EncryptedMessageData struct {
Salt int64
SessionID int64
MessageID int64
SeqNo int32
MessageDataLen int32
MessageDataWithPadding []byte
Message bin .Encoder
}
func (e EncryptedMessageData ) Encode (b *bin .Buffer ) error {
b .PutLong (e .Salt )
b .PutLong (e .SessionID )
b .PutLong (e .MessageID )
b .PutInt32 (e .SeqNo )
b .PutInt32 (e .MessageDataLen )
b .Put (e .MessageDataWithPadding )
return nil
}
func (e EncryptedMessageData ) EncodeWithoutCopy (b *bin .Buffer ) error {
if e .Message == nil {
return e .Encode (b )
}
b .PutLong (e .Salt )
b .PutLong (e .SessionID )
b .PutLong (e .MessageID )
b .PutInt32 (e .SeqNo )
lengthOffset := b .Len ()
b .PutInt32 (0 )
originalLength := b .Len ()
if err := b .Encode (e .Message ); err != nil {
return errors .Wrap (err , "encode inner message" )
}
msgLen := b .Len () - originalLength
(&bin .Buffer {Buf : b .Buf [lengthOffset :lengthOffset ]}).PutInt (msgLen )
return nil
}
func (e *EncryptedMessageData ) Decode (b *bin .Buffer ) error {
{
v , err := b .Long ()
if err != nil {
return err
}
e .Salt = v
}
{
v , err := b .Long ()
if err != nil {
return err
}
e .SessionID = v
}
{
v , err := b .Long ()
if err != nil {
return err
}
e .MessageID = v
}
{
v , err := b .Int32 ()
if err != nil {
return err
}
e .SeqNo = v
}
{
v , err := b .Int32 ()
if err != nil {
return err
}
e .MessageDataLen = v
}
e .MessageDataWithPadding = append (e .MessageDataWithPadding [:0 ], b .Buf ...)
if int (e .MessageDataLen ) > len (e .MessageDataWithPadding ) {
return errors .Errorf (
"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)" ,
int (e .MessageDataLen ), len (e .MessageDataWithPadding ),
)
}
return nil
}
func (e *EncryptedMessageData ) DecodeWithoutCopy (b *bin .Buffer ) error {
{
v , err := b .Long ()
if err != nil {
return err
}
e .Salt = v
}
{
v , err := b .Long ()
if err != nil {
return err
}
e .SessionID = v
}
{
v , err := b .Long ()
if err != nil {
return err
}
e .MessageID = v
}
{
v , err := b .Int32 ()
if err != nil {
return err
}
e .SeqNo = v
}
{
v , err := b .Int32 ()
if err != nil {
return err
}
e .MessageDataLen = v
}
e .MessageDataWithPadding = b .Buf
if int (e .MessageDataLen ) > len (e .MessageDataWithPadding ) {
return errors .Errorf (
"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)" ,
int (e .MessageDataLen ), len (e .MessageDataWithPadding ),
)
}
return nil
}
func (e *EncryptedMessageData ) Data () []byte {
return e .MessageDataWithPadding [:e .MessageDataLen ]
}
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 .