/
table.go
132 lines (100 loc) · 2.09 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
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
// Copyright (c) 2014 Kohei YOSHIDA. All rights reserved.
// This software is licensed under the 3-Clause BSD License
// that can be found in LICENSE file.
package tsparser
import (
"errors"
)
var (
ErrInvalidTableLength = errors.New("Invalid table length")
ErrInvalidSectionLength = errors.New("Invalid value of section_length")
)
type TableId uint8
const (
ProgramAssociationTable TableId = 0x00
)
type Table []byte
func (t Table) TableId() TableId {
return TableId(t[0])
}
func (t Table) SectionSyntaxIndicator() bool {
return t[1]&0x80 > 0
}
func (t Table) PrivateIndicator() bool {
return t[1]&0x40 > 0
}
func (t Table) dataStartsAt() int {
start := 3
if t.SectionSyntaxIndicator() {
start += 5
}
return start
}
func (t Table) dataEndsAt() int {
end := t.SectionLength() + 3
if t.SectionSyntaxIndicator() {
end -= 4
}
return end
}
func (t Table) SectionLength() int {
length := int(t[1]&0x0f)<<8 | int(t[2])
return length
}
func (t Table) TableIdExtension() uint16 {
if t.SectionSyntaxIndicator() {
return uint16(t[3])<<8 | uint16(t[4])
}
return 0
}
func (t Table) VersionNumber() uint8 {
if t.SectionSyntaxIndicator() {
return uint8(t[5]&0x3e) >> 1
}
return 0
}
func (t Table) CurrentNextIndicator() bool {
if t.SectionSyntaxIndicator() {
return t[5]&0x01 > 1
}
return false
}
func (t Table) SectionNumber() uint8 {
if t.SectionSyntaxIndicator() {
return 0
}
return uint8(t[6])
}
func (t Table) LastSectionNumber() uint8 {
if t.SectionSyntaxIndicator() {
return 0
}
return uint8(t[7])
}
func (t Table) CRC32() uint32 {
if t.SectionSyntaxIndicator() {
end := t.dataEndsAt()
return AsUint32(t[end : end+4])
}
return 0
}
func (t Table) CheckCRC() bool {
if t.SectionSyntaxIndicator() {
return CheckCRC32([]byte(t[0 : t.dataEndsAt()+4]))
}
return true
}
func (t Table) Data() []byte {
return t[t.dataStartsAt():t.dataEndsAt()]
}
func (t Table) validate() error {
if len(t) < 3 {
return ErrInvalidTableLength
}
start := t.dataStartsAt()
end := t.dataEndsAt()
if start > end || len(t) < end {
return ErrInvalidSectionLength
}
return nil
}