-
Notifications
You must be signed in to change notification settings - Fork 10
/
tdcat.cxx
161 lines (136 loc) · 3.95 KB
/
tdcat.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
/*
* tivodecode-ng
* Copyright 2006-2018, Jeremy Drake et al.
* See COPYING.md for license terms
*
* derived from mpegcat, copyright 2006 Kees Cook, used with permission
*/
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "getopt_td.hxx"
#include "cli_common.hxx"
#include "tivo_parse.hxx"
static struct td_option long_options[] = {
{"mak", true, 'm'},
{"out", true, 'o'},
{"version", false, 'V'},
{"help", false, 'h'},
{"chunk-1", false, '1'},
{"chunk-2", false, '2'},
{"", false, 0}
};
static void do_help(const char *arg0, int exitval)
{
std::cerr << "Usage: " << arg0 << " [--help] {--mak|-m} mak [--chunk-1|-1]"
" [--chunk-2|-2][{--out|-o} outfile] <tivofile>\n\n"
" -m, --mak media access key (required)\n"
" -o, --out, output file (default stdout)\n"
" -V, --version, print the version information and exit\n"
" -h, --help, print this help and exit\n\n"
" -1, --chunk-1 output chunk 1 (default if unspecified)\n"
" -2, --chunk-2 output chunk 2\n\n"
"The file name specified for the tivo file may be -, which means "
"stdin.\n\n";
std::exit(exitval);
}
int main(int argc, const char **argv)
{
TuringState metaturing;
TiVoStreamHeader header;
bool o_chunk_1 = true;
bool o_chunk_2 = false;
std::string tivofile = "";
std::string outfile = "";
std::string mak = "";
int64_t current_meta_stream_pos = 0;
while (true)
{
int c = getopt_td(argc, argv, "m:o:Vh12", long_options, 0);
if (c == -1)
break;
switch (c)
{
case 'm':
mak = td_optarg;
break;
case 'o':
outfile = td_optarg;
break;
case 'h':
do_help(argv[0], 1);
break;
case '?':
do_help(argv[0], 2);
break;
case 'V':
do_version(10);
break;
case '1':
o_chunk_1 = true;
o_chunk_2 = false;
break;
case '2':
o_chunk_1 = false;
o_chunk_2 = true;
break;
default:
do_help(argv[0], 3);
break;
}
}
if ("" == mak)
mak = get_mak_from_conf_file();
if (td_optind < argc)
{
tivofile = argv[td_optind++];
if (td_optind < argc)
do_help(argv[0], 4);
}
if ("" == mak || "" == tivofile)
do_help(argv[0], 5);
HappyFile hfh(tivofile, "rb");
HappyFile ofh(outfile, "wb");
if (!o_chunk_1 && !o_chunk_2)
o_chunk_1 = true;
print_qualcomm_msg();
if (false == header.read(hfh))
return 8;
header.dump();
TiVoStreamChunk *pChunks = new TiVoStreamChunk[header.chunks];
for (uint16_t i = 0; i < header.chunks; i++)
{
int64_t chunk_start = hfh.tell() + 12;
if (false == pChunks[i].read(hfh))
{
std::perror("chunk read fail");
return 8;
}
if (TIVO_CHUNK_PLAINTEXT_XML == pChunks[i].type)
{
pChunks[i].setupMetadataKey(metaturing, 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_chunk_1 && pChunks[i].id == 1) ||
(o_chunk_2 && pChunks[i].id == 2))
{
pChunks[i].dump();
if (false == pChunks[i].write(ofh))
{
std::perror("write chunk");
return 8;
}
}
}
return 0;
}