9922
|
1 /*
|
|
2 * This file was ported to MPlayer from xine CVS real.c,v 1.8 2003/03/30 17:11:50
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * Copyright (C) 2002 the xine project
|
|
7 *
|
|
8 * This file is part of xine, a free video player.
|
|
9 *
|
|
10 * xine is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * xine is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
23 *
|
|
24 *
|
|
25 * special functions for real streams.
|
|
26 * adopted from joschkas real tools.
|
|
27 *
|
|
28 */
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <string.h>
|
|
32
|
10115
|
33 #include "../config.h"
|
|
34 #include "../bswap.h"
|
9922
|
35 #include "real.h"
|
|
36 #include "asmrp.h"
|
|
37 #include "sdpplin.h"
|
|
38
|
|
39 /*
|
|
40 #define LOG
|
|
41 */
|
|
42
|
|
43 const unsigned char xor_table[] = {
|
|
44 0x05, 0x18, 0x74, 0xd0, 0x0d, 0x09, 0x02, 0x53,
|
|
45 0xc0, 0x01, 0x05, 0x05, 0x67, 0x03, 0x19, 0x70,
|
|
46 0x08, 0x27, 0x66, 0x10, 0x10, 0x72, 0x08, 0x09,
|
|
47 0x63, 0x11, 0x03, 0x71, 0x08, 0x08, 0x70, 0x02,
|
|
48 0x10, 0x57, 0x05, 0x18, 0x54, 0x00, 0x00, 0x00 };
|
|
49
|
|
50
|
10115
|
51 #define BE_32C(x,y) (*((uint32_t*)(x))=be2me_32(y))
|
9922
|
52
|
10115
|
53 #define BE_16(x) be2me_16(*(uint16_t*)(x))
|
9922
|
54
|
10115
|
55 #define BE_32(x) be2me_32(*(uint32_t*)(x))
|
9922
|
56
|
|
57 #define MAX(x,y) ((x>y) ? x : y)
|
|
58
|
|
59 #ifdef LOG
|
|
60 static void hexdump (const char *buf, int length) {
|
|
61
|
|
62 int i;
|
|
63
|
|
64 printf (" hexdump> ");
|
|
65 for (i = 0; i < length; i++) {
|
|
66 unsigned char c = buf[i];
|
|
67
|
|
68 printf ("%02x", c);
|
|
69
|
|
70 if ((i % 16) == 15)
|
|
71 printf ("\n ");
|
|
72
|
|
73 if ((i % 2) == 1)
|
|
74 printf (" ");
|
|
75
|
|
76 }
|
|
77 printf ("\n");
|
|
78 }
|
|
79 #endif
|
|
80
|
|
81
|
|
82 static void hash(char *field, char *param) {
|
|
83
|
|
84 uint32_t a, b, c, d;
|
|
85
|
|
86
|
|
87 /* fill variables */
|
10115
|
88 a= le2me_32(*(uint32_t*)(field));
|
|
89 b= le2me_32(*(uint32_t*)(field+4));
|
|
90 c= le2me_32(*(uint32_t*)(field+8));
|
|
91 d= le2me_32(*(uint32_t*)(field+12));
|
9922
|
92
|
|
93 #ifdef LOG
|
|
94 printf("real: hash input: %x %x %x %x\n", a, b, c, d);
|
|
95 printf("real: hash parameter:\n");
|
|
96 hexdump(param, 64);
|
10115
|
97 printf("real: hash field:\n");
|
|
98 hexdump(field, 64+24);
|
9922
|
99 #endif
|
|
100
|
10115
|
101 a = ((b & c) | (~b & d)) + le2me_32(*((uint32_t*)(param+0x00))) + a - 0x28955B88;
|
9922
|
102 a = ((a << 0x07) | (a >> 0x19)) + b;
|
10115
|
103 d = ((a & b) | (~a & c)) + le2me_32(*((uint32_t*)(param+0x04))) + d - 0x173848AA;
|
9922
|
104 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
10115
|
105 c = ((d & a) | (~d & b)) + le2me_32(*((uint32_t*)(param+0x08))) + c + 0x242070DB;
|
9922
|
106 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
10115
|
107 b = ((c & d) | (~c & a)) + le2me_32(*((uint32_t*)(param+0x0c))) + b - 0x3E423112;
|
9922
|
108 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
10115
|
109 a = ((b & c) | (~b & d)) + le2me_32(*((uint32_t*)(param+0x10))) + a - 0x0A83F051;
|
9922
|
110 a = ((a << 0x07) | (a >> 0x19)) + b;
|
10115
|
111 d = ((a & b) | (~a & c)) + le2me_32(*((uint32_t*)(param+0x14))) + d + 0x4787C62A;
|
9922
|
112 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
10115
|
113 c = ((d & a) | (~d & b)) + le2me_32(*((uint32_t*)(param+0x18))) + c - 0x57CFB9ED;
|
9922
|
114 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
10115
|
115 b = ((c & d) | (~c & a)) + le2me_32(*((uint32_t*)(param+0x1c))) + b - 0x02B96AFF;
|
9922
|
116 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
10115
|
117 a = ((b & c) | (~b & d)) + le2me_32(*((uint32_t*)(param+0x20))) + a + 0x698098D8;
|
9922
|
118 a = ((a << 0x07) | (a >> 0x19)) + b;
|
10115
|
119 d = ((a & b) | (~a & c)) + le2me_32(*((uint32_t*)(param+0x24))) + d - 0x74BB0851;
|
9922
|
120 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
10115
|
121 c = ((d & a) | (~d & b)) + le2me_32(*((uint32_t*)(param+0x28))) + c - 0x0000A44F;
|
9922
|
122 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
10115
|
123 b = ((c & d) | (~c & a)) + le2me_32(*((uint32_t*)(param+0x2C))) + b - 0x76A32842;
|
9922
|
124 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
10115
|
125 a = ((b & c) | (~b & d)) + le2me_32(*((uint32_t*)(param+0x30))) + a + 0x6B901122;
|
9922
|
126 a = ((a << 0x07) | (a >> 0x19)) + b;
|
10115
|
127 d = ((a & b) | (~a & c)) + le2me_32(*((uint32_t*)(param+0x34))) + d - 0x02678E6D;
|
9922
|
128 d = ((d << 0x0c) | (d >> 0x14)) + a;
|
10115
|
129 c = ((d & a) | (~d & b)) + le2me_32(*((uint32_t*)(param+0x38))) + c - 0x5986BC72;
|
9922
|
130 c = ((c << 0x11) | (c >> 0x0f)) + d;
|
10115
|
131 b = ((c & d) | (~c & a)) + le2me_32(*((uint32_t*)(param+0x3c))) + b + 0x49B40821;
|
9922
|
132 b = ((b << 0x16) | (b >> 0x0a)) + c;
|
|
133
|
10115
|
134 a = ((b & d) | (~d & c)) + le2me_32(*((uint32_t*)(param+0x04))) + a - 0x09E1DA9E;
|
9922
|
135 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
10115
|
136 d = ((a & c) | (~c & b)) + le2me_32(*((uint32_t*)(param+0x18))) + d - 0x3FBF4CC0;
|
9922
|
137 d = ((d << 0x09) | (d >> 0x17)) + a;
|
10115
|
138 c = ((d & b) | (~b & a)) + le2me_32(*((uint32_t*)(param+0x2c))) + c + 0x265E5A51;
|
9922
|
139 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
10115
|
140 b = ((c & a) | (~a & d)) + le2me_32(*((uint32_t*)(param+0x00))) + b - 0x16493856;
|
9922
|
141 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
10115
|
142 a = ((b & d) | (~d & c)) + le2me_32(*((uint32_t*)(param+0x14))) + a - 0x29D0EFA3;
|
9922
|
143 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
10115
|
144 d = ((a & c) | (~c & b)) + le2me_32(*((uint32_t*)(param+0x28))) + d + 0x02441453;
|
9922
|
145 d = ((d << 0x09) | (d >> 0x17)) + a;
|
10115
|
146 c = ((d & b) | (~b & a)) + le2me_32(*((uint32_t*)(param+0x3c))) + c - 0x275E197F;
|
9922
|
147 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
10115
|
148 b = ((c & a) | (~a & d)) + le2me_32(*((uint32_t*)(param+0x10))) + b - 0x182C0438;
|
9922
|
149 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
10115
|
150 a = ((b & d) | (~d & c)) + le2me_32(*((uint32_t*)(param+0x24))) + a + 0x21E1CDE6;
|
9922
|
151 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
10115
|
152 d = ((a & c) | (~c & b)) + le2me_32(*((uint32_t*)(param+0x38))) + d - 0x3CC8F82A;
|
9922
|
153 d = ((d << 0x09) | (d >> 0x17)) + a;
|
10115
|
154 c = ((d & b) | (~b & a)) + le2me_32(*((uint32_t*)(param+0x0c))) + c - 0x0B2AF279;
|
9922
|
155 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
10115
|
156 b = ((c & a) | (~a & d)) + le2me_32(*((uint32_t*)(param+0x20))) + b + 0x455A14ED;
|
9922
|
157 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
10115
|
158 a = ((b & d) | (~d & c)) + le2me_32(*((uint32_t*)(param+0x34))) + a - 0x561C16FB;
|
9922
|
159 a = ((a << 0x05) | (a >> 0x1b)) + b;
|
10115
|
160 d = ((a & c) | (~c & b)) + le2me_32(*((uint32_t*)(param+0x08))) + d - 0x03105C08;
|
9922
|
161 d = ((d << 0x09) | (d >> 0x17)) + a;
|
10115
|
162 c = ((d & b) | (~b & a)) + le2me_32(*((uint32_t*)(param+0x1c))) + c + 0x676F02D9;
|
9922
|
163 c = ((c << 0x0e) | (c >> 0x12)) + d;
|
10115
|
164 b = ((c & a) | (~a & d)) + le2me_32(*((uint32_t*)(param+0x30))) + b - 0x72D5B376;
|
9922
|
165 b = ((b << 0x14) | (b >> 0x0c)) + c;
|
|
166
|
10115
|
167 a = (b ^ c ^ d) + le2me_32(*((uint32_t*)(param+0x14))) + a - 0x0005C6BE;
|
9922
|
168 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
10115
|
169 d = (a ^ b ^ c) + le2me_32(*((uint32_t*)(param+0x20))) + d - 0x788E097F;
|
9922
|
170 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
10115
|
171 c = (d ^ a ^ b) + le2me_32(*((uint32_t*)(param+0x2c))) + c + 0x6D9D6122;
|
9922
|
172 c = ((c << 0x10) | (c >> 0x10)) + d;
|
10115
|
173 b = (c ^ d ^ a) + le2me_32(*((uint32_t*)(param+0x38))) + b - 0x021AC7F4;
|
9922
|
174 b = ((b << 0x17) | (b >> 0x09)) + c;
|
10115
|
175 a = (b ^ c ^ d) + le2me_32(*((uint32_t*)(param+0x04))) + a - 0x5B4115BC;
|
9922
|
176 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
10115
|
177 d = (a ^ b ^ c) + le2me_32(*((uint32_t*)(param+0x10))) + d + 0x4BDECFA9;
|
9922
|
178 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
10115
|
179 c = (d ^ a ^ b) + le2me_32(*((uint32_t*)(param+0x1c))) + c - 0x0944B4A0;
|
9922
|
180 c = ((c << 0x10) | (c >> 0x10)) + d;
|
10115
|
181 b = (c ^ d ^ a) + le2me_32(*((uint32_t*)(param+0x28))) + b - 0x41404390;
|
9922
|
182 b = ((b << 0x17) | (b >> 0x09)) + c;
|
10115
|
183 a = (b ^ c ^ d) + le2me_32(*((uint32_t*)(param+0x34))) + a + 0x289B7EC6;
|
9922
|
184 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
10115
|
185 d = (a ^ b ^ c) + le2me_32(*((uint32_t*)(param+0x00))) + d - 0x155ED806;
|
9922
|
186 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
10115
|
187 c = (d ^ a ^ b) + le2me_32(*((uint32_t*)(param+0x0c))) + c - 0x2B10CF7B;
|
9922
|
188 c = ((c << 0x10) | (c >> 0x10)) + d;
|
10115
|
189 b = (c ^ d ^ a) + le2me_32(*((uint32_t*)(param+0x18))) + b + 0x04881D05;
|
9922
|
190 b = ((b << 0x17) | (b >> 0x09)) + c;
|
10115
|
191 a = (b ^ c ^ d) + le2me_32(*((uint32_t*)(param+0x24))) + a - 0x262B2FC7;
|
9922
|
192 a = ((a << 0x04) | (a >> 0x1c)) + b;
|
10115
|
193 d = (a ^ b ^ c) + le2me_32(*((uint32_t*)(param+0x30))) + d - 0x1924661B;
|
9922
|
194 d = ((d << 0x0b) | (d >> 0x15)) + a;
|
10115
|
195 c = (d ^ a ^ b) + le2me_32(*((uint32_t*)(param+0x3c))) + c + 0x1fa27cf8;
|
9922
|
196 c = ((c << 0x10) | (c >> 0x10)) + d;
|
10115
|
197 b = (c ^ d ^ a) + le2me_32(*((uint32_t*)(param+0x08))) + b - 0x3B53A99B;
|
9922
|
198 b = ((b << 0x17) | (b >> 0x09)) + c;
|
|
199
|
10115
|
200 a = ((~d | b) ^ c) + le2me_32(*((uint32_t*)(param+0x00))) + a - 0x0BD6DDBC;
|
9922
|
201 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
10115
|
202 d = ((~c | a) ^ b) + le2me_32(*((uint32_t*)(param+0x1c))) + d + 0x432AFF97;
|
9922
|
203 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
10115
|
204 c = ((~b | d) ^ a) + le2me_32(*((uint32_t*)(param+0x38))) + c - 0x546BDC59;
|
9922
|
205 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
10115
|
206 b = ((~a | c) ^ d) + le2me_32(*((uint32_t*)(param+0x14))) + b - 0x036C5FC7;
|
9922
|
207 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
10115
|
208 a = ((~d | b) ^ c) + le2me_32(*((uint32_t*)(param+0x30))) + a + 0x655B59C3;
|
9922
|
209 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
10115
|
210 d = ((~c | a) ^ b) + le2me_32(*((uint32_t*)(param+0x0C))) + d - 0x70F3336E;
|
9922
|
211 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
10115
|
212 c = ((~b | d) ^ a) + le2me_32(*((uint32_t*)(param+0x28))) + c - 0x00100B83;
|
9922
|
213 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
10115
|
214 b = ((~a | c) ^ d) + le2me_32(*((uint32_t*)(param+0x04))) + b - 0x7A7BA22F;
|
9922
|
215 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
10115
|
216 a = ((~d | b) ^ c) + le2me_32(*((uint32_t*)(param+0x20))) + a + 0x6FA87E4F;
|
9922
|
217 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
10115
|
218 d = ((~c | a) ^ b) + le2me_32(*((uint32_t*)(param+0x3c))) + d - 0x01D31920;
|
9922
|
219 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
10115
|
220 c = ((~b | d) ^ a) + le2me_32(*((uint32_t*)(param+0x18))) + c - 0x5CFEBCEC;
|
9922
|
221 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
10115
|
222 b = ((~a | c) ^ d) + le2me_32(*((uint32_t*)(param+0x34))) + b + 0x4E0811A1;
|
9922
|
223 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
10115
|
224 a = ((~d | b) ^ c) + le2me_32(*((uint32_t*)(param+0x10))) + a - 0x08AC817E;
|
9922
|
225 a = ((a << 0x06) | (a >> 0x1a)) + b;
|
10115
|
226 d = ((~c | a) ^ b) + le2me_32(*((uint32_t*)(param+0x2c))) + d - 0x42C50DCB;
|
9922
|
227 d = ((d << 0x0a) | (d >> 0x16)) + a;
|
10115
|
228 c = ((~b | d) ^ a) + le2me_32(*((uint32_t*)(param+0x08))) + c + 0x2AD7D2BB;
|
9922
|
229 c = ((c << 0x0f) | (c >> 0x11)) + d;
|
10115
|
230 b = ((~a | c) ^ d) + le2me_32(*((uint32_t*)(param+0x24))) + b - 0x14792C6F;
|
9922
|
231 b = ((b << 0x15) | (b >> 0x0b)) + c;
|
|
232
|
|
233 #ifdef LOG
|
|
234 printf("real: hash output: %x %x %x %x\n", a, b, c, d);
|
|
235 #endif
|
|
236
|
10115
|
237 a += le2me_32(*((uint32_t *)(field+0)));
|
|
238 *((uint32_t *)(field+0)) = le2me_32(a);
|
|
239 b += le2me_32(*((uint32_t *)(field+4)));
|
|
240 *((uint32_t *)(field+4)) = le2me_32(b);
|
|
241 c += le2me_32(*((uint32_t *)(field+8)));
|
|
242 *((uint32_t *)(field+8)) = le2me_32(c);
|
|
243 d += le2me_32(*((uint32_t *)(field+12)));
|
|
244 *((uint32_t *)(field+12)) = le2me_32(d);
|
|
245
|
|
246 #ifdef LOG
|
|
247 printf("real: hash field:\n");
|
|
248 hexdump(field, 64+24);
|
|
249 #endif
|
9922
|
250 }
|
|
251
|
|
252 static void call_hash (char *key, char *challenge, int len) {
|
|
253
|
|
254 uint32_t *ptr1, *ptr2;
|
|
255 uint32_t a, b, c, d;
|
10115
|
256 uint32_t tmp;
|
|
257
|
9922
|
258 ptr1=(uint32_t*)(key+16);
|
|
259 ptr2=(uint32_t*)(key+20);
|
|
260
|
10115
|
261 a = le2me_32(*ptr1);
|
9922
|
262 b = (a >> 3) & 0x3f;
|
|
263 a += len * 8;
|
10115
|
264 *ptr1 = le2me_32(a);
|
9922
|
265
|
|
266 if (a < (len << 3))
|
|
267 {
|
|
268 #ifdef LOG
|
|
269 printf("not verified: (len << 3) > a true\n");
|
|
270 #endif
|
|
271 ptr2 += 4;
|
|
272 }
|
|
273
|
10115
|
274 tmp = le2me_32(*ptr2);
|
|
275 tmp += (len >> 0x1d);
|
|
276 *ptr2 = le2me_32(tmp);
|
9922
|
277 a = 64 - b;
|
|
278 c = 0;
|
|
279 if (a <= len)
|
|
280 {
|
|
281
|
|
282 memcpy(key+b+24, challenge, a);
|
|
283 hash(key, key+24);
|
|
284 c = a;
|
|
285 d = c + 0x3f;
|
|
286
|
|
287 while ( d < len ) {
|
|
288
|
|
289 #ifdef LOG
|
|
290 printf("not verified: while ( d < len )\n");
|
|
291 #endif
|
|
292 hash(key, challenge+d-0x3f);
|
|
293 d += 64;
|
|
294 c += 64;
|
|
295 }
|
|
296 b = 0;
|
|
297 }
|
|
298
|
|
299 memcpy(key+b+24, challenge+c, len-c);
|
|
300 }
|
|
301
|
|
302 static void calc_response (char *result, char *field) {
|
|
303
|
|
304 char buf1[128];
|
|
305 char buf2[128];
|
|
306 int i;
|
|
307
|
|
308 memset (buf1, 0, 64);
|
|
309 *buf1 = 128;
|
|
310
|
|
311 memcpy (buf2, field+16, 8);
|
|
312
|
10115
|
313 i = ( le2me_32(*((uint32_t*)(buf2))) >> 3 ) & 0x3f;
|
9922
|
314
|
|
315 if (i < 56) {
|
|
316 i = 56 - i;
|
|
317 } else {
|
|
318 #ifdef LOG
|
|
319 printf("not verified: ! (i < 56)\n");
|
|
320 #endif
|
|
321 i = 120 - i;
|
|
322 }
|
|
323
|
|
324 call_hash (field, buf1, i);
|
|
325 call_hash (field, buf2, 8);
|
|
326
|
|
327 memcpy (result, field, 16);
|
|
328
|
|
329 }
|
|
330
|
|
331
|
|
332 static void calc_response_string (char *result, char *challenge) {
|
|
333
|
|
334 char field[128];
|
|
335 char zres[20];
|
|
336 int i;
|
|
337
|
|
338 /* initialize our field */
|
|
339 BE_32C (field, 0x01234567);
|
|
340 BE_32C ((field+4), 0x89ABCDEF);
|
|
341 BE_32C ((field+8), 0xFEDCBA98);
|
|
342 BE_32C ((field+12), 0x76543210);
|
|
343 BE_32C ((field+16), 0x00000000);
|
|
344 BE_32C ((field+20), 0x00000000);
|
|
345
|
|
346 /* calculate response */
|
|
347 call_hash(field, challenge, 64);
|
|
348 calc_response(zres,field);
|
|
349
|
|
350 /* convert zres to ascii string */
|
|
351 for (i=0; i<16; i++ ) {
|
|
352 char a, b;
|
|
353
|
|
354 a = (zres[i] >> 4) & 15;
|
|
355 b = zres[i] & 15;
|
|
356
|
|
357 result[i*2] = ((a<10) ? (a+48) : (a+87)) & 255;
|
|
358 result[i*2+1] = ((b<10) ? (b+48) : (b+87)) & 255;
|
|
359 }
|
|
360 }
|
|
361
|
|
362 void real_calc_response_and_checksum (char *response, char *chksum, char *challenge) {
|
|
363
|
|
364 int ch_len, table_len, resp_len;
|
|
365 int i;
|
|
366 char *ptr;
|
|
367 char buf[128];
|
|
368
|
|
369 /* initialize return values */
|
|
370 memset(response, 0, 64);
|
|
371 memset(chksum, 0, 34);
|
|
372
|
|
373 /* initialize buffer */
|
|
374 memset(buf, 0, 128);
|
|
375 ptr=buf;
|
|
376 BE_32C(ptr, 0xa1e9149d);
|
|
377 ptr+=4;
|
|
378 BE_32C(ptr, 0x0e6b3b59);
|
|
379 ptr+=4;
|
|
380
|
|
381 /* some (length) checks */
|
|
382 if (challenge != NULL)
|
|
383 {
|
|
384 ch_len = strlen (challenge);
|
|
385
|
|
386 if (ch_len == 40) /* what a hack... */
|
|
387 {
|
|
388 challenge[32]=0;
|
|
389 ch_len=32;
|
|
390 }
|
|
391 if ( ch_len > 56 ) ch_len=56;
|
|
392
|
|
393 /* copy challenge to buf */
|
|
394 memcpy(ptr, challenge, ch_len);
|
|
395 }
|
|
396
|
|
397 if (xor_table != NULL)
|
|
398 {
|
|
399 table_len = strlen(xor_table);
|
|
400
|
|
401 if (table_len > 56) table_len=56;
|
|
402
|
|
403 /* xor challenge bytewise with xor_table */
|
|
404 for (i=0; i<table_len; i++)
|
|
405 ptr[i] = ptr[i] ^ xor_table[i];
|
|
406 }
|
|
407
|
|
408 calc_response_string (response, buf);
|
|
409
|
|
410 /* add tail */
|
|
411 resp_len = strlen (response);
|
|
412 strcpy (&response[resp_len], "01d0a8e3");
|
|
413
|
|
414 /* calculate checksum */
|
|
415 for (i=0; i<resp_len/4; i++)
|
|
416 chksum[i] = response[i*4];
|
|
417 }
|
|
418
|
|
419
|
|
420 /*
|
|
421 * takes a MLTI-Chunk and a rule number got from match_asm_rule,
|
|
422 * returns a pointer to selected data and number of bytes in that.
|
|
423 */
|
|
424
|
|
425 static int select_mlti_data(const char *mlti_chunk, int mlti_size, int selection, char *out) {
|
|
426
|
|
427 int numrules, codec, size;
|
|
428 int i;
|
|
429
|
|
430 /* MLTI chunk should begin with MLTI */
|
|
431
|
|
432 if ((mlti_chunk[0] != 'M')
|
|
433 ||(mlti_chunk[1] != 'L')
|
|
434 ||(mlti_chunk[2] != 'T')
|
|
435 ||(mlti_chunk[3] != 'I'))
|
|
436 {
|
|
437 #ifdef LOG
|
|
438 printf("libreal: MLTI tag not detected, copying data\n");
|
|
439 #endif
|
|
440 memcpy(out, mlti_chunk, mlti_size);
|
|
441 return mlti_size;
|
|
442 }
|
|
443
|
|
444 mlti_chunk+=4;
|
|
445
|
|
446 /* next 16 bits are the number of rules */
|
|
447 numrules=BE_16(mlti_chunk);
|
|
448 if (selection >= numrules) return 0;
|
|
449
|
|
450 /* now <numrules> indices of codecs follows */
|
|
451 /* we skip to selection */
|
|
452 mlti_chunk+=(selection+1)*2;
|
|
453
|
|
454 /* get our index */
|
|
455 codec=BE_16(mlti_chunk);
|
|
456
|
|
457 /* skip to number of codecs */
|
|
458 mlti_chunk+=(numrules-selection)*2;
|
|
459
|
|
460 /* get number of codecs */
|
|
461 numrules=BE_16(mlti_chunk);
|
|
462
|
|
463 if (codec >= numrules) {
|
|
464 printf("codec index >= number of codecs. %i %i\n", codec, numrules);
|
|
465 return 0;
|
|
466 }
|
|
467
|
|
468 mlti_chunk+=2;
|
|
469
|
|
470 /* now seek to selected codec */
|
|
471 for (i=0; i<codec; i++) {
|
|
472 size=BE_32(mlti_chunk);
|
|
473 mlti_chunk+=size+4;
|
|
474 }
|
|
475
|
|
476 size=BE_32(mlti_chunk);
|
|
477
|
|
478 #ifdef LOG
|
|
479 hexdump(mlti_chunk+4, size);
|
|
480 #endif
|
|
481 memcpy(out,mlti_chunk+4, size);
|
|
482 return size;
|
|
483 }
|
|
484
|
|
485 /*
|
|
486 * looking at stream description.
|
|
487 */
|
|
488
|
|
489 rmff_header_t *real_parse_sdp(char *data, char *stream_rules, uint32_t bandwidth) {
|
|
490
|
|
491 sdpplin_t *desc;
|
|
492 rmff_header_t *header;
|
|
493 char buf[2048];
|
|
494 int len, i;
|
|
495 int max_bit_rate=0;
|
|
496 int avg_bit_rate=0;
|
|
497 int max_packet_size=0;
|
|
498 int avg_packet_size=0;
|
|
499 int duration=0;
|
|
500
|
|
501
|
|
502 if (!data) return NULL;
|
|
503
|
|
504 desc=sdpplin_parse(data);
|
|
505
|
|
506 if (!desc) return NULL;
|
|
507
|
|
508 header=calloc(1,sizeof(rmff_header_t));
|
|
509
|
|
510 header->fileheader=rmff_new_fileheader(4+desc->stream_count);
|
|
511 header->cont=rmff_new_cont(
|
|
512 desc->title,
|
|
513 desc->author,
|
|
514 desc->copyright,
|
|
515 desc->abstract);
|
|
516 header->data=rmff_new_dataheader(0,0);
|
|
517 header->streams=calloc(1,sizeof(rmff_mdpr_t*)*(desc->stream_count+1));
|
|
518 #ifdef LOG
|
|
519 printf("number of streams: %u\n", desc->stream_count);
|
|
520 #endif
|
|
521
|
|
522 for (i=0; i<desc->stream_count; i++) {
|
|
523
|
|
524 int j=0;
|
|
525 int n;
|
|
526 char b[64];
|
|
527 int rulematches[16];
|
|
528
|
|
529 #ifdef LOG
|
|
530 printf("calling asmrp_match with:\n%s\n%u\n", desc->stream[i]->asm_rule_book, bandwidth);
|
|
531 #endif
|
|
532 n=asmrp_match(desc->stream[i]->asm_rule_book, bandwidth, rulematches);
|
|
533 for (j=0; j<n; j++) {
|
|
534 #ifdef LOG
|
|
535 printf("asmrp rule match: %u for stream %u\n", rulematches[j], desc->stream[i]->stream_id);
|
|
536 #endif
|
|
537 sprintf(b,"stream=%u;rule=%u,", desc->stream[i]->stream_id, rulematches[j]);
|
|
538 strcat(stream_rules, b);
|
|
539 }
|
|
540
|
|
541 if (!desc->stream[i]->mlti_data) return NULL;
|
|
542
|
|
543 len=select_mlti_data(desc->stream[i]->mlti_data, desc->stream[i]->mlti_data_size, rulematches[0], buf);
|
|
544
|
|
545 header->streams[i]=rmff_new_mdpr(
|
|
546 desc->stream[i]->stream_id,
|
|
547 desc->stream[i]->max_bit_rate,
|
|
548 desc->stream[i]->avg_bit_rate,
|
|
549 desc->stream[i]->max_packet_size,
|
|
550 desc->stream[i]->avg_packet_size,
|
|
551 desc->stream[i]->start_time,
|
|
552 desc->stream[i]->preroll,
|
|
553 desc->stream[i]->duration,
|
|
554 desc->stream[i]->stream_name,
|
|
555 desc->stream[i]->mime_type,
|
|
556 len,
|
|
557 buf);
|
|
558
|
|
559 duration=MAX(duration,desc->stream[i]->duration);
|
|
560 max_bit_rate+=desc->stream[i]->max_bit_rate;
|
|
561 avg_bit_rate+=desc->stream[i]->avg_bit_rate;
|
|
562 max_packet_size=MAX(max_packet_size, desc->stream[i]->max_packet_size);
|
|
563 if (avg_packet_size)
|
|
564 avg_packet_size=(avg_packet_size + desc->stream[i]->avg_packet_size) / 2;
|
|
565 else
|
|
566 avg_packet_size=desc->stream[i]->avg_packet_size;
|
|
567 }
|
|
568
|
|
569 if (stream_rules)
|
|
570 stream_rules[strlen(stream_rules)-1]=0; /* delete last ',' in stream_rules */
|
|
571
|
|
572 header->prop=rmff_new_prop(
|
|
573 max_bit_rate,
|
|
574 avg_bit_rate,
|
|
575 max_packet_size,
|
|
576 avg_packet_size,
|
|
577 0,
|
|
578 duration,
|
|
579 0,
|
|
580 0,
|
|
581 0,
|
|
582 desc->stream_count,
|
|
583 desc->flags);
|
|
584
|
|
585 rmff_fix_header(header);
|
|
586
|
|
587 return header;
|
|
588 }
|
|
589
|
|
590 int real_get_rdt_chunk(rtsp_t *rtsp_session, char *buffer) {
|
|
591
|
|
592 int n=1;
|
|
593 uint8_t header[8];
|
|
594 rmff_pheader_t ph;
|
|
595 int size;
|
|
596 int flags1;
|
|
597 int unknown1;
|
|
598 uint32_t ts;
|
|
599
|
|
600 n=rtsp_read_data(rtsp_session, header, 8);
|
|
601 if (n<8) return 0;
|
|
602 if (header[0] != 0x24)
|
|
603 {
|
|
604 printf("rdt chunk not recognized: got 0x%02x\n", header[0]);
|
|
605 return 0;
|
|
606 }
|
|
607 size=(header[1]<<12)+(header[2]<<8)+(header[3]);
|
|
608 flags1=header[4];
|
|
609 if ((flags1!=0x40)&&(flags1!=0x42))
|
|
610 {
|
|
611 #ifdef LOG
|
|
612 printf("got flags1: 0x%02x\n",flags1);
|
|
613 #endif
|
|
614 header[0]=header[5];
|
|
615 header[1]=header[6];
|
|
616 header[2]=header[7];
|
|
617 n=rtsp_read_data(rtsp_session, header+3, 5);
|
|
618 if (n<5) return 0;
|
|
619 #ifdef LOG
|
|
620 printf("ignoring bytes:\n");
|
|
621 hexdump(header, 8);
|
|
622 #endif
|
|
623 n=rtsp_read_data(rtsp_session, header+4, 4);
|
|
624 if (n<4) return 0;
|
|
625 flags1=header[4];
|
|
626 size-=9;
|
|
627 }
|
|
628 unknown1=(header[5]<<12)+(header[6]<<8)+(header[7]);
|
|
629 n=rtsp_read_data(rtsp_session, header, 6);
|
|
630 if (n<6) return 0;
|
|
631 ts=BE_32(header);
|
|
632
|
|
633 #ifdef LOG
|
|
634 printf("ts: %u size: %u, flags: 0x%02x, unknown values: %u 0x%02x 0x%02x\n",
|
|
635 ts, size, flags1, unknown1, header[4], header[5]);
|
|
636 #endif
|
|
637 size+=2;
|
|
638
|
|
639 ph.object_version=0;
|
|
640 ph.length=size;
|
|
641 ph.stream_number=(flags1>>1)&1;
|
|
642 ph.timestamp=ts;
|
|
643 ph.reserved=0;
|
|
644 ph.flags=0; /* TODO: determine keyframe flag and insert here? */
|
|
645 rmff_dump_pheader(&ph, buffer);
|
|
646 size-=12;
|
|
647 n=rtsp_read_data(rtsp_session, buffer+12, size);
|
|
648
|
|
649 return n+12;
|
|
650 }
|
|
651
|
|
652 rmff_header_t *real_setup_and_get_header(rtsp_t *rtsp_session, uint32_t bandwidth) {
|
|
653
|
|
654 char *description=NULL;
|
|
655 char *session_id=NULL;
|
|
656 rmff_header_t *h;
|
|
657 char *challenge1;
|
|
658 char challenge2[64];
|
|
659 char checksum[34];
|
|
660 char subscribe[256];
|
|
661 char buf[256];
|
|
662 char *mrl=rtsp_get_mrl(rtsp_session);
|
|
663 unsigned int size;
|
|
664 int status;
|
|
665
|
|
666 /* get challenge */
|
|
667 challenge1=strdup(rtsp_search_answers(rtsp_session,"RealChallenge1"));
|
|
668 #ifdef LOG
|
|
669 printf("real: Challenge1: %s\n", challenge1);
|
|
670 #endif
|
|
671
|
|
672 /* request stream description */
|
|
673 rtsp_schedule_field(rtsp_session, "Accept: application/sdp");
|
|
674 sprintf(buf, "Bandwidth: %u", bandwidth);
|
|
675 rtsp_schedule_field(rtsp_session, buf);
|
|
676 rtsp_schedule_field(rtsp_session, "GUID: 00000000-0000-0000-0000-000000000000");
|
|
677 rtsp_schedule_field(rtsp_session, "RegionData: 0");
|
|
678 rtsp_schedule_field(rtsp_session, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586");
|
|
679 rtsp_schedule_field(rtsp_session, "SupportsMaximumASMBandwidth: 1");
|
|
680 rtsp_schedule_field(rtsp_session, "Language: en-US");
|
|
681 rtsp_schedule_field(rtsp_session, "Require: com.real.retain-entity-for-setup");
|
|
682 status=rtsp_request_describe(rtsp_session,NULL);
|
|
683
|
|
684 if ( status<200 || status>299 )
|
|
685 {
|
|
686 char *alert=rtsp_search_answers(rtsp_session,"Alert");
|
|
687 if (alert) {
|
|
688 printf("real: got message from server:\n%s\n", alert);
|
|
689 }
|
|
690 rtsp_send_ok(rtsp_session);
|
|
691 return NULL;
|
|
692 }
|
|
693
|
|
694 /* receive description */
|
|
695 size=0;
|
|
696 if (!rtsp_search_answers(rtsp_session,"Content-length"))
|
|
697 printf("real: got no Content-length!\n");
|
|
698 else
|
|
699 size=atoi(rtsp_search_answers(rtsp_session,"Content-length"));
|
|
700
|
|
701 if (!rtsp_search_answers(rtsp_session,"ETag"))
|
|
702 printf("real: got no ETag!\n");
|
|
703 else
|
|
704 session_id=strdup(rtsp_search_answers(rtsp_session,"ETag"));
|
|
705
|
|
706 #ifdef LOG
|
|
707 printf("real: Stream description size: %i\n", size);
|
|
708 #endif
|
|
709
|
|
710 description=malloc(sizeof(char)*(size+1));
|
|
711
|
|
712 rtsp_read_data(rtsp_session, description, size);
|
|
713 description[size]=0;
|
|
714
|
|
715 /* parse sdp (sdpplin) and create a header and a subscribe string */
|
|
716 strcpy(subscribe, "Subscribe: ");
|
|
717 h=real_parse_sdp(description, subscribe+11, bandwidth);
|
|
718 if (!h) return NULL;
|
|
719 rmff_fix_header(h);
|
|
720
|
|
721 #ifdef LOG
|
|
722 printf("Title: %s\nCopyright: %s\nAuthor: %s\nStreams: %i\n",
|
|
723 h->cont->title, h->cont->copyright, h->cont->author, h->prop->num_streams);
|
|
724 #endif
|
|
725
|
|
726 /* setup our streams */
|
|
727 real_calc_response_and_checksum (challenge2, checksum, challenge1);
|
|
728 sprintf(buf, "RealChallenge2: %s, sd=%s", challenge2, checksum);
|
|
729 rtsp_schedule_field(rtsp_session, buf);
|
|
730 sprintf(buf, "If-Match: %s", session_id);
|
|
731 rtsp_schedule_field(rtsp_session, buf);
|
|
732 rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
|
|
733 sprintf(buf, "%s/streamid=0", mrl);
|
|
734 rtsp_request_setup(rtsp_session,buf);
|
|
735
|
|
736 if (h->prop->num_streams > 1) {
|
|
737 rtsp_schedule_field(rtsp_session, "Transport: x-pn-tng/tcp;mode=play,rtp/avp/tcp;unicast;mode=play");
|
|
738 sprintf(buf, "If-Match: %s", session_id);
|
|
739 rtsp_schedule_field(rtsp_session, buf);
|
|
740
|
|
741 sprintf(buf, "%s/streamid=1", mrl);
|
|
742 rtsp_request_setup(rtsp_session,buf);
|
|
743 }
|
|
744 /* set stream parameter (bandwidth) with our subscribe string */
|
|
745 rtsp_schedule_field(rtsp_session, subscribe);
|
|
746 rtsp_request_setparameter(rtsp_session,NULL);
|
|
747
|
|
748 /* and finally send a play request */
|
|
749 rtsp_schedule_field(rtsp_session, "Range: npt=0-");
|
|
750 rtsp_request_play(rtsp_session,NULL);
|
|
751
|
|
752 return h;
|
|
753 }
|
|
754
|