-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinstance_tags.go
84 lines (66 loc) · 1.9 KB
/
instance_tags.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
package otr3
import (
"bytes"
"encoding/binary"
)
// GetOurInstanceTag returns our instance tag - it computes it if none has been computed yet
func (c *Conversation) GetOurInstanceTag() uint32 {
_ = c.generateInstanceTag()
return c.ourInstanceTag
}
// GetTheirInstanceTag returns the peers instance tag, or 0 if none has been computed yet
func (c *Conversation) GetTheirInstanceTag() uint32 {
return c.theirInstanceTag
}
func (c *Conversation) generateInstanceTag() error {
if c.ourInstanceTag != 0 {
return nil
}
var ret uint32
var dst [4]byte
for ret < minValidInstanceTag {
if err := c.randomInto(dst[:]); err != nil {
return err
}
ret = binary.BigEndian.Uint32(dst[:])
}
c.ourInstanceTag = ret
return nil
}
// ExtractInstanceTags returns our and theirs instance tags from the message, and ok if the message was parsed properly
func ExtractInstanceTags(m []byte) (ours, theirs uint32, ok bool) {
if bytes.HasPrefix(m, []byte("?OTR:")) {
msg, err := decode(encodedMessage(m))
if err != nil {
return 0, 0, false
}
if len(msg) < otrv3HeaderLen {
return 0, 0, false
}
_, senderInstanceTag, _ := ExtractWord(msg[messageHeaderPrefix:])
_, receiverInstanceTag, _ := ExtractWord(msg)
return receiverInstanceTag, senderInstanceTag, true
} else if bytes.HasPrefix(m, []byte("?OTR|")) {
if len(m) < 23 {
return 0, 0, false
}
header := m[:23]
headerPart := bytes.Split(header, fragmentSeparator)[0]
itagParts := bytes.Split(headerPart, fragmentItagsSeparator)
if len(itagParts) < 3 {
return 0, 0, false
}
senderInstanceTag, err1 := parseItag(itagParts[1])
if err1 != nil {
return 0, 0, false
}
receiverInstanceTag, err2 := parseItag(itagParts[2])
if err2 != nil {
return 0, 0, false
}
return receiverInstanceTag, senderInstanceTag, true
} else {
// All other prefixes are for older versions or don't have instance tags
return 0, 0, false
}
}