-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
encrypted_message_data.go
158 lines (145 loc) · 3.17 KB
/
encrypted_message_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package crypto
import (
"fmt"
"golang.org/x/xerrors"
"github.com/gotd/td/bin"
)
// 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 (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
}
// EncodeWithoutCopy is like Encode, but tries to encode Message and uses only one buffer
// to encode. If Message is nil, fallbacks to Encode.
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 fmt.Errorf("encode inner message: %w", err)
}
msgLen := b.Len() - originalLength
(&bin.Buffer{Buf: b.Buf[lengthOffset:lengthOffset]}).PutInt(msgLen)
return nil
}
// Decode implements bin.Decoder.
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 xerrors.Errorf(
"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)",
int(e.MessageDataLen), len(e.MessageDataWithPadding),
)
}
return nil
}
// DecodeWithoutCopy is like Decode, but MessageDataWithPadding references to given buffer instead of
// copying.
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 xerrors.Errorf(
"MessageDataLen field is bigger then MessageDataWithPadding length (%d > %d)",
int(e.MessageDataLen), len(e.MessageDataWithPadding),
)
}
return nil
}
// Data returns message data without hash.
func (e *EncryptedMessageData) Data() []byte {
return e.MessageDataWithPadding[:e.MessageDataLen]
}