-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtvhd-stream.c
143 lines (123 loc) · 3.24 KB
/
mtvhd-stream.c
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
/* DVB USB framework compliant Linux driver for the SKNET MonsterTV HD ISDB-T
* receiver. (Post process of stream completion: local decryption)
*
* Copyright (c) 2009 Gombei Nanashi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mtvhd.h"
/* Decrypt stream */
static void mtvhd_stream_complete(struct usb_data_stream *stream,
u8 *data, size_t len)
{
struct dvb_usb_adapter *adap = stream->user_priv;
struct mtvhd_adapter_state *st = adap->priv;
u8 *p, *p1;
p = data;
if (st->buff_used) {
if (len < TS_SIZE - st->buff_used) {
memcpy(&st->packet_buff[st->buff_used], data, len);
st->buff_used += len;
return;
}
memcpy(&st->packet_buff[st->buff_used], data, TS_SIZE - st->buff_used);
st->decrypt(st->crypto_ctx, st->packet_buff);
st->complete_orig(stream, st->packet_buff, TS_SIZE);
p += TS_SIZE - st->buff_used;
st->buff_used = 0;
}
p1 = p;
while (p < data + len) {
if (*p != 0x47) {
p++;
continue;
}
if (p + TS_SIZE > data + len) {
break;
}
st->decrypt(st->crypto_ctx, p);
p += TS_SIZE;
}
if (p != p1) {
st->complete_orig(stream, p1, p - p1);
}
/* save the remaining data */
if (p < data + len) {
st->buff_used = data + len - p;
memcpy(st->packet_buff, p, st->buff_used);
}
return;
}
/* Dummy routine for passthrough */
static void mtvhd_dummy_decrypt(void *ctx, u8 *data)
{
return;
}
/* Release context for current crypto library */
void mtvhd_crypto_release(struct mtvhd_adapter_state *st)
{
switch (st->crypto_mode) {
case MTVHD_CRYPTO_DES:
mtvhd_des_release(st);
break;
default:
; /* Nothing to do */
}
st->decrypt = mtvhd_dummy_decrypt;
st->crypto_mode = 0;
return;
}
/* Initialize for specific crypto library */
int mtvhd_crypto_init(struct mtvhd_adapter_state *st, int mode)
{
int ret = 0;
if (mode == st->crypto_mode) {
return 0;
}
if (st->crypto_mode != 0) {
mtvhd_crypto_release(st);
}
switch (mode) {
#ifdef CONFIG_DVB_USB_MTVHD_V1
case MTVHD_CRYPTO_XOR:
st->decrypt = mtvhd_xor_decrypt;
st->crypto_mode = MTVHD_CRYPTO_XOR;
deb_info("Crypto algorithm: XOR\n");
break;
#endif
case MTVHD_CRYPTO_DES:
ret = mtvhd_des_init(st);
if (ret == 0) {
st->decrypt = mtvhd_des_decrypt;
st->crypto_mode = MTVHD_CRYPTO_DES;
deb_info("Crypto algorithm: DES\n");
}
break;
case 0:
/* Just release */
break;
default:
ret = -EINVAL;
}
return ret;
}
/* Hook streaming complete process */
int mtvhd_stream_init(struct dvb_usb_adapter *adap)
{
struct mtvhd_adapter_state *st = adap->priv;
st->decrypt = mtvhd_dummy_decrypt;
st->complete_orig = adap->fe_adap[0].stream.complete;
adap->fe_adap[0].stream.complete = mtvhd_stream_complete;
return 0;
}