-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
71 lines (53 loc) · 1.22 KB
/
table.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
package psi
import (
"github.com/mh-orange/ts"
)
type Table []byte
func CreateTable(id uint8, data []byte) Table {
// table is data length + 8 (for header bits) + 4 (for CRC)
length := len(data) + 12
table := make(Table, length)
table[0] = id
table[1] |= 0x30
table[1] = uint8(uint16(length-3) >> 8 & 0x03)
table[2] = uint8(0xff & (length - 3))
copy(table[8:], data)
copy(table[length-4:], ts.ComputeCRC(table[0:length-4]))
return table
}
func (t Table) ID() uint8 {
return ts.Uimsbf8(t[0])
}
func (t Table) SectionSyntaxIndicator() bool {
return ts.Bool(t[1], 0)
}
func (t Table) PrivateIndicator() bool {
return ts.Bool(t[1], 1)
}
func (t Table) SectionLength() int {
return int(ts.Uimsbf16(t[1:3], 12))
}
func (t Table) IDExtension() uint16 {
return ts.Uimsbf16(t[3:5], 16)
}
func (t Table) VersionNumber() uint8 {
return uint8((0x3f & t[5]) >> 1)
}
func (t Table) IsCurrent() bool {
return ts.Bool(t[5], 7)
}
func (t Table) IsNext() bool {
return !t.IsCurrent()
}
func (t Table) SectionNumber() uint8 {
return ts.Uimsbf8(t[6])
}
func (t Table) LastSectionNumber() uint8 {
return ts.Uimsbf8(t[7])
}
func (t Table) Data() []byte {
return t[8 : len(t)-4]
}
func (t Table) CRC() []byte {
return t[len(t)-4:]
}