-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtcencoder.go
121 lines (106 loc) · 3.05 KB
/
tcencoder.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
package iabtcfv2
import (
"time"
)
const (
decisecondsPerSecond = 10
nanosecondsPerDecisecond = int64(time.Millisecond * 100)
)
type TCEncoder struct {
*Bits
}
func NewTCEncoder(src []byte) *TCEncoder {
return &TCEncoder{NewBits(src)}
}
func NewTCEncoderFromSize(bitSize int) *TCEncoder {
if bitSize%8 != 0 {
return NewTCEncoder(make([]byte, bitSize/8+1))
}
return NewTCEncoder(make([]byte, bitSize/8))
}
func (r *TCEncoder) ReadTime() time.Time {
var ds = int64(r.ReadInt(bitsTime))
return time.Unix(ds/decisecondsPerSecond, (ds%decisecondsPerSecond)*nanosecondsPerDecisecond).UTC()
}
func (r *TCEncoder) WriteTime(v time.Time) {
r.WriteNumber(v.UnixNano()/nanosecondsPerDecisecond, bitsTime)
}
func (r *TCEncoder) ReadChars(n uint) string {
var buf = make([]byte, 0, n/bitsChar)
for i := uint(0); i < n/bitsChar; i++ {
buf = append(buf, byte(r.ReadInt(bitsChar))+'A')
}
return string(buf)
}
func (r *TCEncoder) WriteChars(v string, n uint) {
for i := uint(0); i < n/bitsChar; i++ {
char := v[i]
r.WriteInt(int(byte(char)-'A'), bitsChar)
}
}
func (r *TCEncoder) ReadBitField(n uint) map[int]bool {
var m = make(map[int]bool)
for i := uint(0); i < n; i++ {
if r.ReadBool() {
m[int(i)+1] = true
}
}
return m
}
func (b *Bits) WriteBools(getBool func(int) bool, n int) {
for i := 1; i <= n; i++ {
b.WriteBool(getBool(i))
}
}
func (r *TCEncoder) WriteRangeEntries(entries []*RangeEntry) {
r.WriteInt(len(entries), bitsNumEntries)
for _, entry := range entries {
if entry.EndVendorID > entry.StartVendorID {
r.WriteBool(true)
r.WriteInt(entry.StartVendorID, bitsVendorId)
r.WriteInt(entry.EndVendorID, bitsVendorId)
} else {
r.WriteBool(false)
r.WriteInt(entry.StartVendorID, bitsVendorId)
}
}
}
func (r *TCEncoder) ReadRangeEntries() (int, []*RangeEntry) {
n := r.ReadInt(bitsNumEntries)
var ret = make([]*RangeEntry, 0, n)
for i := uint(0); i < uint(n); i++ {
var isRange = r.ReadBool()
var start, end int
start = r.ReadInt(bitsVendorId)
if isRange {
end = r.ReadInt(bitsVendorId)
} else {
end = start
}
ret = append(ret, &RangeEntry{StartVendorID: start, EndVendorID: end})
}
return n, ret
}
func (r *TCEncoder) WritePubRestrictions(entries []*PubRestriction) {
r.WriteInt(len(entries), bitsNumPubRestrictions)
for _, entry := range entries {
r.WriteInt(entry.PurposeId, bitsPubRestrictionsEntryPurposeId)
r.WriteInt(int(entry.RestrictionType), bitsPubRestrictionsEntryRestrictionType)
r.WriteRangeEntries(entry.RangeEntries)
}
}
func (r *TCEncoder) ReadPubRestrictions() (int, []*PubRestriction) {
n := r.ReadInt(bitsNumPubRestrictions)
var ret = make([]*PubRestriction, 0, n)
for i := uint(0); i < uint(n); i++ {
var purposeId = r.ReadInt(bitsPubRestrictionsEntryPurposeId)
var restrictionType = r.ReadInt(bitsPubRestrictionsEntryRestrictionType)
numEntries, rangeEntries := r.ReadRangeEntries()
ret = append(ret, &PubRestriction{PurposeId: purposeId,
RestrictionType: RestrictionType(restrictionType),
NumEntries: numEntries,
RangeEntries: rangeEntries,
})
}
return n, ret
}