-
Notifications
You must be signed in to change notification settings - Fork 1
/
devo.go
134 lines (118 loc) · 3.86 KB
/
devo.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
// 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 implements TiVo file decryption. It supports both mpeg-ps and
// mpeg-ts input formats. Decrypting requires the correct media access key
// (MAK) from the source TiVo device. DeVo is intended for personal/educational
// use only. Decrypted files must NOT be distributed. Piracy is not condoned!
package devo
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
)
const tsType = 0x20
type fileHeader struct {
Magic [4]byte
_ [2]byte
Flags uint16
_ [2]byte
VideoOffset uint32
MetaSegments uint16
}
type metaHeader struct {
ChunkSize uint32
DataSize uint32
ID uint16
Type uint16
}
type metadata struct {
Header metaHeader
Content []byte
}
// Decrypt a TiVo file from src using the specified media access key (mak).
// The decrypted content is written to dst.
func Decrypt(dst io.Writer, src io.Reader, mak string) error {
header, meta, err := readFileMetadata(src)
if err != nil {
return fmt.Errorf("devo: error parsing metadata: %s", err)
}
// The first metadata segment is used in entirety as an initialization vector
iv := meta[0].Content
srcbuf := bufio.NewReader(src)
dstbuf := bufio.NewWriter(dst)
if header.Flags&tsType != 0 {
err = newTSDecryptor(mak, iv).decrypt(dstbuf, srcbuf)
} else {
err = newPSDecryptor(mak, iv).decrypt(dstbuf, srcbuf)
}
if err != nil {
seeker, ok := src.(io.Seeker)
if ok {
pos, _ := seeker.Seek(0, 1)
err = fmt.Errorf("devo: error processing input at offset 0x%08x: %s", pos, err)
} else {
err = fmt.Errorf("devo: error processing input: %s", err)
}
return err
}
return dstbuf.Flush()
}
func readFileMetadata(src io.Reader) (header fileHeader, meta []metadata, err error) {
var position int64
err = binary.Read(src, binary.BigEndian, &header)
if err != nil {
return
}
if string(header.Magic[:]) != "TiVo" {
err = fmt.Errorf("not a tivo file (missing magic 'TiVo' marker)")
return
}
position += 16 // Size of file header
// We skip the last metadata segment because it's redundant and sometimes has bogus header data
meta = make([]metadata, header.MetaSegments-1)
for i := range meta {
current := metadata{}
err = binary.Read(src, binary.BigEndian, ¤t.Header)
if err != nil {
return
}
current.Content = make([]byte, current.Header.DataSize)
_, err = src.Read(current.Content)
if err != nil {
return
}
meta[i] = current
var endMarker uint32
err = binary.Read(src, binary.BigEndian, &endMarker)
if err != nil {
return
}
if endMarker != 0x000000 {
err = fmt.Errorf("metadata offset error")
return
}
position += 16 + int64(current.Header.DataSize) // Size of current chunk
}
// Skip forward to video content
_, err = io.CopyN(ioutil.Discard, src, int64(header.VideoOffset)-position)
return
}