-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpegps.go
242 lines (217 loc) · 5.72 KB
/
mpegps.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2016 Bob Ziuchkovski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package devo
import (
"bufio"
"encoding/binary"
"fmt"
"io"
)
const (
psPrefix = 0x000001
psSequenceHeader = 0xb3
psSequenceExtension = 0xb5
psGroupHeader = 0xb8
psProgramEnd = 0xb9
psPackStart = 0xba
psSystemHeader = 0xbb
psStreamMap = 0xbc
)
// Length of headers by flag bit (bit 0 .. 7)
var flagLengths = []int{
1, // Extension
2, // CRC
1, // Additional Copyright Info
1, // DSM Trick Mode
3, // ES Rate
6, // ESCR
5, // DTS
5, // PTS
}
type psDecryptor struct {
pool *cipherPool
}
func newPSDecryptor(mak string, iv []byte) *psDecryptor {
return &psDecryptor{
pool: newCipherPool(mak, iv),
}
}
func (dec *psDecryptor) decrypt(dst io.Writer, src *bufio.Reader) error {
var (
packet *psPacket
count int
err error
)
for {
count++
packet, err = readPSPacket(src)
if err != nil {
break
}
err = dec.processPacket(packet)
if err != nil {
break
}
err = writePSPacket(dst, packet)
if err != nil {
break
}
if packet.id == psProgramEnd {
break
}
}
// An EOF is unexpected. We expect to read psProgramEnd prior to hitting EOF.
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
if err != nil {
err = fmt.Errorf("failed while processing mpegps packet %d: %s", count, err)
}
return err
}
func (dec *psDecryptor) processPacket(packet *psPacket) (err error) {
switch packet.id {
case psStreamMap:
// Twiddle stream map for decrypted stream
packet.payload()[0] &= 0xdf
default:
if packet.scramble() != 0 {
dec.decryptPacket(packet)
}
}
return
}
func (dec *psDecryptor) decryptPacket(packet *psPacket) {
cipher := dec.pool.getCipher(packet.id, confounder(packet.privateData()[1:5]))
// We throw out the first four bytes of the cipher stream
// Don't ask why...this is the same thing tivodecode does
var dummy [4]byte
cipher.XORKeyStream(dummy[:], dummy[:])
// Use the rest of the stream to decrypt the packet payload
encrypted := packet.payload()
cipher.XORKeyStream(encrypted, encrypted)
packet.clearScramble()
}
type psPacket struct {
id uint8
content []byte
}
func (packet *psPacket) scramble() uint8 {
switch packet.id {
case psPackStart, psProgramEnd:
return 0
default:
return (packet.content[0] & 0x30) >> 4
}
}
func (packet *psPacket) clearScramble() {
if packet.scramble() != 0 {
packet.content[0] &= 0xcf
}
}
func (packet *psPacket) payload() []byte {
switch packet.id {
case psPackStart, psStreamMap, psSystemHeader:
return packet.content
default:
hdrlen := int(packet.content[2]) + 3
return packet.content[hdrlen:]
}
}
func (packet *psPacket) privateData() []byte {
flagPos, lenPos, remaining := 1, 2, 3
flags := packet.content[flagPos]
hdrlen := int(packet.content[lenPos]) + remaining
// Skip PTS, DTS, ESCR, ES Rate, DSM Trick Mode, Copyright Info, CRC, and Extension data
offset := remaining
for bit, len := range flagLengths {
if flags&(1<<uint(bit)) != 0 {
offset += len
}
}
// XXX We might be returning more than just the private data here...
return packet.content[offset : hdrlen-remaining]
}
func readPSPacket(src io.Reader) (packet *psPacket, err error) {
var code uint32
err = binary.Read(src, binary.BigEndian, &code)
if err != nil {
return
}
if (code >> 8) != psPrefix {
err = fmt.Errorf("invalid PS packet code: 0x%08x", code)
return
}
packet = &psPacket{id: wordOctet(code, 3)}
switch packet.id {
case psProgramEnd:
// Empty content
packet.content = make([]byte, 0)
case psPackStart:
// Pack start content
packet.content = make([]byte, 10)
_, err = io.ReadFull(src, packet.content)
if err != nil {
return
}
// Stuffing bytes
scount := packet.content[9] & 0x07
stuffing := make([]byte, scount)
_, err = io.ReadFull(src, stuffing)
if err != nil {
return
}
packet.content = append(packet.content, stuffing...)
default:
// Remaining packets are all in type-length-value format and we already have the type (code)
var length uint16
err = binary.Read(src, binary.BigEndian, &length)
if err != nil {
return
}
packet.content = make([]byte, length)
_, err = io.ReadFull(src, packet.content)
if err != nil {
return
}
}
return
}
func writePSPacket(dst io.Writer, p *psPacket) (err error) {
var code = (psPrefix << 8) | uint32(p.id)
err = binary.Write(dst, binary.BigEndian, code)
if err != nil {
return
}
switch p.id {
case psPackStart, psProgramEnd:
// Not in type-length-value format, so don't write length
default:
err = binary.Write(dst, binary.BigEndian, uint16(len(p.content)))
if err != nil {
return
}
}
_, err = dst.Write(p.content)
return
}
func psCode(id uint8) uint32 {
return (psPrefix << 8) | uint32(id)
}