forked from wmcbrine/tivodecode-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtivodecode.cxx
291 lines (246 loc) · 6.83 KB
/
tivodecode.cxx
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* tivodecode-ng
* Copyright 2006-2015, Jeremy Drake et al.
* See COPYING file for license terms
*
* derived from mpegcat, copyright 2006 Kees Cook, used with permission
*/
#ifdef HAVE_CONFIG_H
#include "tdconfig.h"
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "getopt_long.h"
#include "cli_common.hxx"
#include "tivo_parse.hxx"
#include "tivo_decoder_ts.hxx"
#include "tivo_decoder_ps.hxx"
int o_no_verify;
static struct option long_options[] = {
{"mak", 1, 0, 'm'},
{"out", 1, 0, 'o'},
{"verbose", 0, 0, 'v'},
{"pkt-dump", 1, 0, 'p'},
{"metadata", 0, 0, 'D'},
{"no-verify", 0, 0, 'n'},
{"no-video", 0, 0, 'x'},
{"version", 0, 0, 'V'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
static void do_help(const char *arg0, int exitval)
{
std::cerr << "Usage: " << arg0 << " [--help] [--verbose|-v] "
"[--no-verify|-n] [--pkt-dump|-p] pkt_num {--mak|-m} mak "
"[--metadata|-D] [{--out|-o} outfile] <tivofile>\n\n"
" -m, --mak media access key (required)\n"
" -o, --out, output file (default stdout)\n"
" -v, --verbose, verbose\n"
" -p, --pkt-dump, verbose logging for specific TS packet number\n"
" -D, --metadata, dump TiVo recording metadata\n"
" -n, --no-verify, do not verify MAK while decoding\n"
" -x, --no-video, don't decode video, exit after metadata\n"
" -V, --version, print the version information and exit\n"
" -h, --help, print this help and exit\n\n"
"The file names specified for the output file or the "
"tivo file may be -, which\n"
"means stdout or stdin respectively\n\n";
std::exit(exitval);
}
int main(int argc, char *argv[])
{
int o_no_video = 0;
int o_dump_metadata = 0;
int makgiven = 0;
uint32_t pktDump = 0;
const char *tivofile = NULL;
const char *outfile = NULL;
char mak[12];
std::memset(mak, 0, sizeof(mak));
TuringState turing;
std::memset(&turing, 0, sizeof(turing));
TuringState metaturing;
std::memset(&metaturing, 0, sizeof(metaturing));
hoff_t current_meta_stream_pos = 0;
HappyFile *hfh = NULL, *ofh = NULL;
TiVoStreamHeader header;
pktDumpMap.clear();
while (1)
{
int c = getopt_long(argc, argv, "m:o:hnDxvVp:", long_options, 0);
if (c == -1)
break;
switch (c)
{
case 'm':
std::strncpy(mak, optarg, 11);
mak[11] = '\0';
makgiven = 1;
break;
case 'p':
std::sscanf(optarg, "%d", &pktDump);
pktDumpMap[pktDump] = true;
break;
case 'o':
outfile = optarg;
break;
case 'h':
do_help(argv[0], 1);
break;
case 'v':
o_verbose++;
break;
case 'n':
o_no_verify = 1;
break;
case 'D' :
o_dump_metadata = 1;
break;
case 'x':
o_no_video = 1;
break;
case '?':
do_help(argv[0], 2);
break;
case 'V':
do_version(10);
break;
default:
do_help(argv[0], 3);
break;
}
}
if (!makgiven)
makgiven = get_mak_from_conf_file(mak);
if (optind < argc)
{
tivofile = argv[optind++];
if (optind < argc)
do_help(argv[0], 4);
}
if (!makgiven || !tivofile)
{
do_help(argv[0], 5);
}
hfh = new HappyFile;
if (!std::strcmp(tivofile, "-"))
{
if (!hfh->attach(stdin))
return 10;
}
else
{
if (!hfh->open(tivofile, "rb"))
{
std::perror(tivofile);
return 6;
}
}
ofh = new HappyFile;
if (!outfile || !std::strcmp(outfile, "-"))
{
if (!ofh->attach(stdout))
return 10;
}
else
{
if (!ofh->open(outfile, "wb"))
{
std::perror("opening output file");
return 7;
}
}
print_qualcomm_msg();
if (false == header.read(hfh))
{
return(8);
}
header.dump();
TiVoStreamChunk *pChunks = new TiVoStreamChunk[header.chunks];
if (NULL == pChunks)
{
std::perror("allocate TiVoStreamChunks");
return(9);
}
for (int32_t i = 0; i < header.chunks; i++)
{
hoff_t chunk_start = hfh->tell() + pChunks[i].size();
if (false == pChunks[i].read(hfh))
{
std::perror("chunk read fail");
return(8);
}
if (TIVO_CHUNK_PLAINTEXT_XML == pChunks[i].type)
{
pChunks[i].setupTuringKey(&turing, (uint8_t*)mak);
pChunks[i].setupMetadataKey(&metaturing, (uint8_t*)mak);
}
else if (TIVO_CHUNK_ENCRYPTED_XML == pChunks[i].type)
{
uint16_t offsetVal = chunk_start - current_meta_stream_pos;
pChunks[i].decryptMetadata(&metaturing, offsetVal);
current_meta_stream_pos = chunk_start + pChunks[i].dataSize;
}
else
{
std::perror("Unknown chunk type");
return(8);
}
if (o_dump_metadata)
{
char buf[25];
std::sprintf(buf, "%s-%02d-%04x.xml", "chunk", i, pChunks[i].id);
HappyFile *chunkfh = new HappyFile;
if (!chunkfh->open(buf, "wb"))
{
std::perror("create metadata file");
return 8;
}
pChunks[i].dump();
if (false == pChunks[i].write(chunkfh))
{
std::perror("write chunk");
return 8;
}
chunkfh->close();
delete chunkfh;
}
}
// metaturing.destruct();
if (o_no_video)
std::exit(0);
if ((hfh->tell() > header.mpeg_offset) ||
(hfh->seek(header.mpeg_offset) < 0))
{
std::perror("Error reading header");
return 8; // I dunno
}
TiVoDecoder *pDecoder = NULL;
if (TIVO_FORMAT_PS == header.getFormatType())
{
pDecoder = new TiVoDecoderPS(&turing, hfh, ofh);
}
else if (TIVO_FORMAT_TS == header.getFormatType())
{
pDecoder = new TiVoDecoderTS(&turing, hfh, ofh);
}
if (NULL == pDecoder)
{
std::perror("Unable to create TiVo Decoder");
return 9;
}
if (false == pDecoder->process())
{
std::perror("Failed to process file");
return 9;
}
turing.destruct();
hfh->close();
delete hfh;
ofh->close();
delete ofh;
return 0;
}
/* vi:set ai ts=4 sw=4 expandtab: */