forked from SirDataFR/iabtcfv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_disclosed_vendors.go
74 lines (65 loc) · 1.55 KB
/
segment_disclosed_vendors.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
package iabtcfv2
import (
"encoding/base64"
)
type DisclosedVendors struct {
SegmentType int
MaxVendorId int
IsRangeEncoding bool
DisclosedVendors map[int]bool
NumEntries int
RangeEntries []*RangeEntry
}
// Returns true if vendor id is disclosed for validating OOB signaling
func (d *DisclosedVendors) IsVendorDisclosed(id int) bool {
if d.IsRangeEncoding {
for _, entry := range d.RangeEntries {
if entry.StartVendorID <= id && id <= entry.EndVendorID {
return true
}
}
return false
}
return d.DisclosedVendors[id]
}
// Returns structure as a base64 raw url encoded string
func (d *DisclosedVendors) Encode() string {
bitSize := 20
if d.IsRangeEncoding {
bitSize += 12
entriesSize := len(d.RangeEntries)
for _, entry := range d.RangeEntries {
if entry.EndVendorID > entry.StartVendorID {
entriesSize += 16 * 2
} else {
entriesSize += 16
}
}
bitSize += entriesSize
} else {
if d.MaxVendorId == 0 {
for id, _ := range d.DisclosedVendors {
if id > d.MaxVendorId {
d.MaxVendorId = id
}
}
}
bitSize += d.MaxVendorId
}
var e = newTCEncoder(make([]byte, bitSize/8))
if bitSize%8 != 0 {
e = newTCEncoder(make([]byte, bitSize/8+1))
}
e.writeInt(d.SegmentType, 3)
e.writeInt(d.MaxVendorId, 16)
e.writeBool(d.IsRangeEncoding)
if d.IsRangeEncoding {
e.writeInt(len(d.RangeEntries), 12)
e.writeRangeEntries(d.RangeEntries)
} else {
for i := 0; i < d.MaxVendorId; i++ {
e.writeBool(d.IsVendorDisclosed(i + 1))
}
}
return base64.RawURLEncoding.EncodeToString(e.bytes)
}