package crypto

import (
	

	
)

// EncryptedMessageData is stored in EncryptedMessage.EncryptedData.
type EncryptedMessageData struct {
	Salt                   int64
	SessionID              int64
	MessageID              int64
	SeqNo                  int32
	MessageDataLen         int32
	MessageDataWithPadding []byte

	// Message to encode to MessageDataWithPadding.
	// Needed to prevent unnecessary allocations in EncodeWithoutCopy.
	Message bin.Encoder
}

// Encode implements bin.Encoder.
func ( EncryptedMessageData) ( *bin.Buffer) error {
	.PutLong(.Salt)
	.PutLong(.SessionID)
	.PutLong(.MessageID)
	.PutInt32(.SeqNo)
	.PutInt32(.MessageDataLen)
	.Put(.MessageDataWithPadding)
	return nil
}

// EncodeWithoutCopy is like Encode, but tries to encode Message and uses only one buffer
// to encode. If Message is nil, fallbacks to Encode.
func ( EncryptedMessageData) ( *bin.Buffer) error {
	if .Message == nil {
		return .Encode()
	}

	.PutLong(.Salt)
	.PutLong(.SessionID)
	.PutLong(.MessageID)
	.PutInt32(.SeqNo)
	 := .Len()
	.PutInt32(0)
	 := .Len()
	if  := .Encode(.Message);  != nil {
		return errors.Wrap(, "encode inner message")
	}
	 := .Len() - 

	(&bin.Buffer{Buf: .Buf[:]}).PutInt()
	return nil
}

// Decode implements bin.Decoder.
func ( *EncryptedMessageData) ( *bin.Buffer) error {
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.Salt = 
	}
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.SessionID = 
	}
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.MessageID = 
	}
	{
		,  := .Int32()
		if  != nil {
			return 
		}
		.SeqNo = 
	}
	{
		,  := .Int32()
		if  != nil {
			return 
		}
		.MessageDataLen = 
	}
	.MessageDataWithPadding = append(.MessageDataWithPadding[:0], .Buf...)
	if int(.MessageDataLen) > len(.MessageDataWithPadding) {
		return errors.Errorf(
			"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)",
			int(.MessageDataLen), len(.MessageDataWithPadding),
		)
	}

	return nil
}

// DecodeWithoutCopy is like Decode, but MessageDataWithPadding references to given buffer instead of
// copying.
func ( *EncryptedMessageData) ( *bin.Buffer) error {
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.Salt = 
	}
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.SessionID = 
	}
	{
		,  := .Long()
		if  != nil {
			return 
		}
		.MessageID = 
	}
	{
		,  := .Int32()
		if  != nil {
			return 
		}
		.SeqNo = 
	}
	{
		,  := .Int32()
		if  != nil {
			return 
		}
		.MessageDataLen = 
	}
	.MessageDataWithPadding = .Buf
	if int(.MessageDataLen) > len(.MessageDataWithPadding) {
		return errors.Errorf(
			"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)",
			int(.MessageDataLen), len(.MessageDataWithPadding),
		)
	}
	return nil
}

// Data returns message data without hash.
func ( *EncryptedMessageData) () []byte {
	return .MessageDataWithPadding[:.MessageDataLen]
}