forked from nx111/oscam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-emulator-streamserver.c
1802 lines (1541 loc) · 39.8 KB
/
module-emulator-streamserver.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define MODULE_LOG_PREFIX "emu"
#include "globals.h"
#ifdef WITH_EMU
#include "cscrypt/des.h"
#include "ffdecsa/ffdecsa.h"
#include "module-emulator-osemu.h"
#include "module-emulator-streamserver.h"
#include "module-emulator-powervu.h"
#include "oscam-config.h"
#include "oscam-net.h"
#include "oscam-string.h"
#include "oscam-time.h"
#include "oscam-chk.h"
#define STREAM_UNDEFINED 0x00
#define STREAM_VIDEO 0x01
#define STREAM_AUDIO 0x02
#define STREAM_SUBTITLE 0x03
#define STREAM_TELETEXT 0x04
extern int32_t exit_oscam;
typedef struct
{
int32_t connfd;
int32_t connid;
} emu_stream_client_conn_data;
int8_t stream_server_thread_init = 0;
char emu_stream_source_host[256] = {"127.0.0.1"};
int32_t emu_stream_source_port = 8001;
char *emu_stream_source_auth = NULL;
int32_t emu_stream_relay_port = 17999;
int8_t emu_stream_emm_enabled = 0;
uint32_t cluster_size = 50;
static uint8_t emu_stream_server_mutex_init = 0;
static pthread_mutex_t emu_stream_server_mutex;
static int32_t glistenfd, gconncount = 0, gconnfd[EMU_STREAM_SERVER_MAX_CONNECTIONS];
pthread_mutex_t emu_fixed_key_srvid_mutex;
uint16_t emu_stream_cur_srvid[EMU_STREAM_SERVER_MAX_CONNECTIONS];
int8_t stream_server_has_ecm[EMU_STREAM_SERVER_MAX_CONNECTIONS];
pthread_mutex_t emu_fixed_key_data_mutex[EMU_STREAM_SERVER_MAX_CONNECTIONS];
emu_stream_client_key_data emu_fixed_key_data[EMU_STREAM_SERVER_MAX_CONNECTIONS];
LLIST *ll_emu_stream_delayed_keys[EMU_STREAM_SERVER_MAX_CONNECTIONS];
static void SearchTsPackets(uint8_t *buf, uint32_t bufLength, uint16_t *packetSize, uint16_t *startOffset)
{
uint32_t i;
(*packetSize) = 0;
(*startOffset) = 0;
for (i = 0; i < bufLength; i++)
{
if (buf[i] == 0x47)
{
// if three packets align, probably safe to assume correct size
if ((buf[i + 188] == 0x47) & (buf[i + 376] == 0x47))
{
(*packetSize) = 188;
(*startOffset) = i;
return;
}
else if ((buf[i + 204] == 0x47) & (buf[i + 408] == 0x47))
{
(*packetSize) = 204;
(*startOffset) = i;
return;
}
else if ((buf[i + 208] == 0x47) & (buf[i + 416] == 0x47))
{
(*packetSize) = 208;
(*startOffset) = i;
return;
}
}
}
}
typedef void (*ts_data_callback)(emu_stream_client_data *cdata);
static void ParseTsData(uint8_t table_id, uint8_t table_mask, uint8_t min_table_length, int8_t *flag,
uint8_t *data, uint16_t data_length, uint16_t *data_pos, int8_t payloadStart,
uint8_t *buf, int32_t len, ts_data_callback func, emu_stream_client_data *cdata)
{
int8_t found_start = 0;
int32_t free_data_length, copySize, i;
uint16_t section_length, offset = 0;
if (len < 1)
{
return;
}
if (*flag == 0 && !payloadStart)
{
return;
}
if (*flag == 0)
{
*data_pos = 0;
offset = 1 + buf[0];
}
else if (payloadStart)
{
offset = 1;
}
if (len - offset < 1)
{
return;
}
free_data_length = data_length - *data_pos;
copySize = (len - offset) > free_data_length ? free_data_length : (len - offset);
memcpy(data + (*data_pos), buf + offset, copySize);
(*data_pos) += copySize;
found_start = 0;
for (i = 0; i < *data_pos; i++)
{
if ((data[i] & table_mask) == table_id)
{
if (i != 0)
{
if ((*data_pos) - i > i)
{
memmove(data, &data[i], (*data_pos) - i);
}
else
{
memcpy(data, &data[i], (*data_pos) - i);
}
*data_pos -= i;
}
found_start = 1;
break;
}
}
if (!found_start)
{
*flag = 0;
return;
}
*flag = 2;
if (*data_pos < 3)
{
return;
}
section_length = SCT_LEN(data);
if (section_length > data_length || section_length < min_table_length)
{
*flag = 0;
return;
}
if ((*data_pos) < section_length)
{
return;
}
func(cdata);
found_start = 0;
for (i = section_length; i < *data_pos; i++)
{
if ((data[i] & table_mask) == table_id)
{
if ((*data_pos) - i > i)
{
memmove(data, &data[i], (*data_pos) - i);
}
else
{
memcpy(data, &data[i], (*data_pos) - i);
}
*data_pos -= i;
found_start = 1;
break;
}
}
if (!found_start)
{
*data_pos = 0;
}
*flag = 1;
}
static void ParsePatData(emu_stream_client_data *cdata)
{
int32_t i;
uint8_t *data = cdata->pat_data;
uint16_t srvid, section_length = SCT_LEN(data);
for (i = 8; i + 7 < section_length; i += 4)
{
srvid = b2i(2, data + i);
if (srvid == 0)
{
continue;
}
if (cdata->srvid == srvid)
{
cdata->pmt_pid = b2i(2, data + i + 2) & 0x1FFF;
cs_log_dbg(D_READER, "Stream client %i found pmt pid: 0x%04X (%i)",
cdata->connid, cdata->pmt_pid, cdata->pmt_pid);
break;
}
}
}
static int8_t stream_client_get_caid(emu_stream_client_data *cdata)
{
uint32_t tmp1 = (cdata->srvid << 16) | cdata->pmt_pid;
uint8_t tmp2[2];
if (emu_find_key('A', tmp1, 0, "FAKE", tmp2, 2, 0, 0, 0, NULL))
{
cdata->caid = b2i(2, tmp2);
return 1;
}
return 0;
}
static void ParseDescriptors(uint8_t *buffer, uint16_t info_length, uint8_t *type)
{
uint8_t descriptor_tag = buffer[0], descriptor_length = 0;
uint32_t j, k;
if (info_length < 1)
{
return;
}
for (j = 0; j + 1 < info_length; j += descriptor_length + 2)
{
descriptor_tag = buffer[j];
descriptor_length = buffer[j + 1];
switch (descriptor_tag)
{
case 0x05: // Registration descriptor
{
// "HDMV" format identifier is removed
// OSCam does not need to know about Blu-ray
const char format_identifiers_audio[10][5] =
{
"AC-3", "BSSD", "dmat", "DRA1", "DTS1",
"DTS2", "DTS3", "EAC3", "mlpa", "Opus",
};
for (k = 0; k < 10; k++)
{
if (memcmp(buffer + j + 2, format_identifiers_audio[k], 4) == 0)
{
*type = STREAM_AUDIO;
break;
}
}
break;
}
//case 0x09: // CA descriptor
//{
// break;
//}
case 0x46: // VBI teletext descriptor (DVB)
case 0x56: // teletext descriptor (DVB)
{
*type = STREAM_TELETEXT;
break;
}
case 0x59: // subtitling descriptor (DVB)
{
*type = STREAM_SUBTITLE;
break;
}
case 0x6A: // AC-3 descriptor (DVB)
case 0x7A: // enhanced AC-3 descriptor (DVB)
case 0x7B: // DTS descriptor (DVB)
case 0x7C: // AAC descriptor (DVB)
case 0x81: // AC-3 descriptor (ATSC)
case 0xCC: // Enhanced AC-3 descriptor (ATSC)
{
*type = STREAM_AUDIO;
break;
}
case 0x7F: // extension descriptor (DVB)
{
uint8_t extension_descriptor_tag = buffer[j + 2];
switch (extension_descriptor_tag)
{
case 0x0E: // DTS-HD descriptor (DVB)
case 0x0F: // DTS Neural descriptor (DVB)
case 0x15: // AC-4 descriptor (DVB)
case 0x21: // DTS-UHD descriptor (DVB)
*type = STREAM_AUDIO;
break;
}
break;
}
}
}
}
static void ParsePmtData(emu_stream_client_data *cdata)
{
int32_t i;
uint8_t *data = cdata->pmt_data;
uint8_t descriptor_tag = 0, descriptor_length = 0, stream_type;
uint16_t program_info_length = 0, es_info_length = 0, elementary_pid, caid;
uint16_t section_length = SCT_LEN(data);
cdata->pcr_pid = b2i(2, data + 8) & 0x1FFF;
if (cdata->pcr_pid != 0x1FFF)
{
cs_log_dbg(D_READER, "Stream client %i found pcr pid: 0x%04X (%i)",
cdata->connid, cdata->pcr_pid, cdata->pcr_pid);
}
program_info_length = b2i(2, data + 10) & 0xFFF;
if (12 + program_info_length >= section_length)
{
return;
}
// parse program descriptors (we are looking only for CA descriptor here)
for (i = 12; i + 1 < 12 + program_info_length; i += descriptor_length + 2)
{
descriptor_tag = data[i];
descriptor_length = data[i + 1];
if (descriptor_length < 1)
{
break;
}
if (i + 1 + descriptor_length >= 12 + program_info_length)
{
break;
}
if (descriptor_tag == 0x09 && descriptor_length >= 4)
{
caid = b2i(2, data + i + 2);
if (caid_is_powervu(caid) || caid == 0xA101) // add all supported caids here
{
if (cdata->caid == NO_CAID_VALUE)
{
cdata->caid = caid;
}
cdata->ecm_pid = b2i(2, data + i + 4) & 0x1FFF;
cs_log_dbg(D_READER, "Stream client %i found ecm pid: 0x%04X (%i)",
cdata->connid, cdata->ecm_pid, cdata->ecm_pid);
break;
}
}
}
for (i = 12 + program_info_length; i + 4 < section_length; i += 5 + es_info_length)
{
stream_type = data[i];
elementary_pid = b2i(2, data + i + 1) & 0x1FFF;
es_info_length = b2i(2, data + i + 3) & 0xFFF;
switch (stream_type)
{
case 0x01:
case 0x02:
case 0x10:
case 0x1B:
case 0x20:
case 0x24:
case 0x25:
case 0x42:
case 0xD1:
case 0xEA:
{
cdata->video_pid = elementary_pid;
cs_log_dbg(D_READER, "Stream client %i found video pid: 0x%04X (%i)",
cdata->connid, elementary_pid, elementary_pid);
break;
}
case 0x03:
case 0x04:
case 0x0F:
case 0x11:
case 0x1C:
case 0x2D:
case 0x2E:
case 0x81:
{
if (cdata->audio_pid_count >= EMU_STREAM_MAX_AUDIO_SUB_TRACKS)
{
continue;
}
cdata->audio_pids[cdata->audio_pid_count] = elementary_pid;
cdata->audio_pid_count++;
cs_log_dbg(D_READER, "Stream client %i found audio pid: 0x%04X (%i)",
cdata->connid, elementary_pid, elementary_pid);
break;
}
case 0x06:
//case 0x81: // some ATSC AC-3 streams do not contain the AC-3 descriptor!
case 0x87:
{
uint8_t type = STREAM_UNDEFINED;
ParseDescriptors(data + i + 5, es_info_length, &type);
if (type == STREAM_AUDIO)
{
if (cdata->audio_pid_count >= EMU_STREAM_MAX_AUDIO_SUB_TRACKS)
{
continue;
}
cdata->audio_pids[cdata->audio_pid_count] = elementary_pid;
cdata->audio_pid_count++;
cs_log_dbg(D_READER, "Stream client %i found audio pid: 0x%04X (%i)",
cdata->connid, elementary_pid, elementary_pid);
}
else if (type == STREAM_TELETEXT)
{
cdata->teletext_pid = elementary_pid;
cs_log_dbg(D_READER, "Stream client %i found teletext pid: 0x%04X (%i)",
cdata->connid, elementary_pid, elementary_pid);
}
break;
}
}
}
// If we haven't found a CAID for this service,
// search the keyDB for a fake one
if (cdata->caid == NO_CAID_VALUE && stream_client_get_caid(cdata) == 1)
{
cs_log_dbg(D_READER, "Stream client %i found fake caid: 0x%04X (%i)",
cdata->connid, cdata->caid, cdata->caid);
}
}
static void ParseCatData(emu_stream_client_data *cdata)
{
uint8_t *data = cdata->cat_data;
uint32_t i;
for (i = 8; i < (b2i(2, data + 1) & 0xFFF) - 1; i += data[i + 1] + 2)
{
if (data[i] != 0x09)
{
continue;
}
uint16_t caid = b2i(2, data + i + 2);
if (caid_is_powervu(caid)) // add all supported caids here
{
if (cdata->caid == NO_CAID_VALUE)
{
cdata->caid = caid;
}
cdata->emm_pid = b2i(2, data + i + 4) & 0x1FFF;;
cs_log_dbg(D_READER, "Stream client %i found emm pid: 0x%04X (%i)",
cdata->connid, cdata->emm_pid, cdata->emm_pid);
break;
}
}
}
static void ParseEmmData(emu_stream_client_data *cdata)
{
uint32_t keysAdded = 0;
emu_process_emm(NULL, cdata->caid, cdata->emm_data, &keysAdded);
if (keysAdded)
{
//refresh_entitlements(rdr);
cs_log("Stream client %i found %i keys", cdata->connid, keysAdded);
}
}
static void ParseEcmData(emu_stream_client_data *cdata)
{
uint8_t *data = cdata->ecm_data;
uint8_t dcw[16];
uint16_t section_length = SCT_LEN(data);
if (section_length < 11)
{
return;
}
if (caid_is_powervu(cdata->caid))
{
if (data[11] > cdata->ecm_nb || (cdata->ecm_nb == 255 && data[11] == 0) || ((cdata->ecm_nb - data[11]) > 5))
{
cdata->ecm_nb = data[11];
powervu_ecm(data, dcw, NULL, cdata->srvid, cdata->caid, cdata->tsid, cdata->onid, cdata->ens, &cdata->key);
}
}
//else if () // All other caids
//{
//emu_process_ecm();
//}
}
static void ParseTsPackets(emu_stream_client_data *data, uint8_t *stream_buf, uint32_t bufLength, uint16_t packetSize)
{
uint8_t payloadStart;
uint16_t pid, offset;
uint32_t i, tsHeader;
for (i = 0; i < bufLength; i += packetSize)
{
tsHeader = b2i(4, stream_buf + i);
pid = (tsHeader & 0x1FFF00) >> 8;
payloadStart = (tsHeader & 0x400000) >> 22;
if (tsHeader & 0x20)
{
offset = 4 + stream_buf[i + 4] + 1;
}
else
{
offset = 4;
}
if (packetSize - offset < 1)
{
continue;
}
if (pid == 0x0000 && data->have_pat_data != 1) // Search the PAT for the PMT pid
{
ParseTsData(0x00, 0xFF, 16, &data->have_pat_data, data->pat_data, sizeof(data->pat_data),
&data->pat_data_pos, payloadStart, stream_buf + i + offset, packetSize - offset, ParsePatData, data);
continue;
}
if (pid == data->pmt_pid && data->have_pmt_data != 1) // Search the PMT for PCR, ECM, Video and Audio pids
{
ParseTsData(0x02, 0xFF, 21, &data->have_pmt_data, data->pmt_data, sizeof(data->pmt_data),
&data->pmt_data_pos, payloadStart, stream_buf + i + offset, packetSize - offset, ParsePmtData, data);
continue;
}
// We have bot PAT and PMT data - No need to search the rest of the packets
if (data->have_pat_data == 1 && data->have_pmt_data == 1)
{
break;
}
}
}
static void DescrambleTsPacketsPowervu(emu_stream_client_data *data, uint8_t *stream_buf, uint32_t bufLength, uint16_t packetSize)
{
int8_t oddKeyUsed;
uint8_t scramblingControl, payloadStart, oddeven;
uint8_t *pdata, *packetClusterV[256];
uint8_t *packetClusterA[EMU_STREAM_MAX_AUDIO_SUB_TRACKS][64]; // separate cluster arrays for video and each audio track
uint16_t pid, offset;
uint32_t i, j, k, tsHeader;
uint32_t scrambled_packets = 0, scrambled_packetsA[EMU_STREAM_MAX_AUDIO_SUB_TRACKS] = { 0 };
uint32_t *deskey;
uint32_t cs = 0; // video cluster start
uint32_t ce = 1; // video cluster end
uint32_t csa[EMU_STREAM_MAX_AUDIO_SUB_TRACKS] = { 0 }; // cluster index for audio tracks
void *csakeyA[EMU_STREAM_MAX_AUDIO_SUB_TRACKS] = { 0 };
void *csakeyV = 0;
emu_stream_client_key_data *keydata;
packetClusterV[0] = NULL;
for (i = 0; i < bufLength; i += packetSize)
{
tsHeader = b2i(4, stream_buf + i);
pid = (tsHeader & 0x1FFF00) >> 8;
scramblingControl = tsHeader & 0xC0;
payloadStart = (tsHeader & 0x400000) >> 22;
if (tsHeader & 0x20)
{
offset = 4 + stream_buf[i + 4] + 1;
}
else
{
offset = 4;
}
if (packetSize - offset < 1)
{
continue;
}
if (emu_stream_emm_enabled && pid == 0x0001 && data->have_cat_data != 1) // Search the CAT for EMM pids
{
// set to null pid
stream_buf[i + 1] |= 0x1F;
stream_buf[i + 2] = 0xFF;
ParseTsData(0x01, 0xFF, 8, &data->have_cat_data, data->cat_data, sizeof(data->cat_data),
&data->cat_data_pos, payloadStart, stream_buf + i + offset, packetSize - offset, ParseCatData, data);
continue;
}
if (emu_stream_emm_enabled && data->emm_pid && pid == data->emm_pid) // Process the EMM data
{
// set to null pid
stream_buf[i + 1] |= 0x1F;
stream_buf[i + 2] = 0xFF;
ParseTsData(0x80, 0xF0, 3, &data->have_emm_data, data->emm_data, sizeof(data->emm_data),
&data->emm_data_pos, payloadStart, stream_buf + i + offset, packetSize - offset, ParseEmmData, data);
continue;
}
if (data->ecm_pid && pid == data->ecm_pid) // Process the ECM data
{
stream_server_has_ecm[data->connid] = 1;
// set to null pid
stream_buf[i + 1] |= 0x1F;
stream_buf[i + 2] = 0xFF;
ParseTsData(0x80, 0xFE, 3, &data->have_ecm_data, data->ecm_data, sizeof(data->ecm_data),
&data->ecm_data_pos, payloadStart, stream_buf + i + offset, packetSize - offset, ParseEcmData, data);
continue;
}
if (scramblingControl == 0)
{
continue;
}
if (!(stream_buf[i + 3] & 0x10))
{
stream_buf[i + 3] &= 0x3F;
continue;
}
oddKeyUsed = scramblingControl == 0xC0 ? 1 : 0;
if (!stream_server_has_ecm[data->connid])
{
keydata = &emu_fixed_key_data[data->connid];
SAFE_MUTEX_LOCK(&emu_fixed_key_data_mutex[data->connid]);
data->key.pvu_csa_used = keydata->pvu_csa_used;
}
else
{
keydata = &data->key;
}
if (keydata->pvu_csa_used)
{
oddeven = scramblingControl; // for detecting odd/even switch
if (pid == data->video_pid) // start with video pid, since it is most dominant
{
csakeyV = keydata->pvu_csa_ks[PVU_CW_VID];
if (csakeyV != NULL)
{
cs = 0;
ce = 1;
packetClusterV[cs] = stream_buf + i; // set first cluster start
packetClusterV[ce] = stream_buf + i + packetSize - 1;
scrambled_packets = 1;
// Now iterate through the rest of the packets and create clusters for batch decryption
for (j = i + packetSize; j < bufLength; j += packetSize)
{
tsHeader = b2i(4, stream_buf + j);
pid = (tsHeader & 0x1FFF00) >> 8;
if (pid == data->video_pid)
{
if (oddeven != (tsHeader & 0xC0)) // changed key so stop adding clusters
{
break;
}
if (cs > ce) // First video packet for each cluster
{
packetClusterV[cs] = stream_buf + j;
ce = cs + 1;
}
scrambled_packets++;
}
else
{
if (cs < ce) // First non-video packet - need to set end of video cluster
{
packetClusterV[ce] = stream_buf + j - 1;
cs = ce + 1;
}
if ((tsHeader & 0xC0) == 0)
{
continue;
}
if (oddeven != (tsHeader & 0xC0)) // changed key so stop adding clusters
{
j = bufLength; // to break out of outer loop also
break;
}
// Check for audio tracks and create single packet clusters
for (k = 0; k < data->audio_pid_count; k++)
{
if (pid == data->audio_pids[k])
{
packetClusterA[k][csa[k]] = stream_buf + j;
csa[k]++;
packetClusterA[k][csa[k]] = stream_buf + j + packetSize - 1;
csa[k]++;
scrambled_packetsA[k]++;
}
}
}
}
if (cs > ce) // last packet was not a video packet, so set null for end of all clusters
{
packetClusterV[cs] = NULL;
}
else
{
// last packet was a video packet, so set end of cluster to end of last packet
if (scrambled_packets > 1)
{
packetClusterV[ce] = stream_buf + j - 1;
}
packetClusterV[ce + 1] = NULL; // add null to end of cluster list
}
while (j >= cluster_size)
{
j = decrypt_packets(csakeyV, packetClusterV);
}
for (k = 0; k < data->audio_pid_count; k++)
{
// if audio track has scrambled packets, set null to mark end and decrypt
if (scrambled_packetsA[k])
{
csakeyA[k] = keydata->pvu_csa_ks[PVU_CW_A1 + k];
packetClusterA[k][csa[k]] = NULL;
decrypt_packets(csakeyA[k], packetClusterA[k]);
csa[k] = 0;
scrambled_packetsA[k] = 0;
}
}
}
}
else
{
for (j = 0; j < data->audio_pid_count; j++)
{
if (pid == data->audio_pids[j])
{
csakeyA[0] = keydata->pvu_csa_ks[PVU_CW_A1 + j];
}
}
if (csakeyA[0] != NULL)
{
packetClusterA[0][0] = stream_buf + i;
packetClusterA[0][1] = stream_buf + i + packetSize - 1;
packetClusterA[0][2] = NULL;
decrypt_packets(csakeyA[0], packetClusterA[0]);
}
}
}
else
{
deskey = NULL;
if (pid == data->video_pid)
{
deskey = keydata->pvu_des_ks[PVU_CW_VID][oddKeyUsed];
}
else
{
for (j = 0; j < data->audio_pid_count; j++)
{
if (pid == data->audio_pids[j])
{
deskey = keydata->pvu_des_ks[PVU_CW_A1 + j][oddKeyUsed];
}
}
}
if (deskey == NULL)
{
deskey = keydata->pvu_des_ks[PVU_CW_HSD][oddKeyUsed];
}
for (j = offset; j + 7 < 188; j += 8)
{
pdata = stream_buf + i + j;
des(pdata, deskey, 0);
}
stream_buf[i + 3] &= 0x3F;
}
if (!stream_server_has_ecm[data->connid])
{
SAFE_MUTEX_UNLOCK(&emu_fixed_key_data_mutex[data->connid]);
}
}
}
static void DescrambleTsPacketsRosscrypt1(emu_stream_client_data *data, uint8_t *stream_buf, uint32_t bufLength, uint16_t packetSize)
{
int8_t is_av_pid;
int32_t j;
uint8_t scramblingControl;
uint16_t pid, offset;
uint32_t i, tsHeader;
for (i = 0; i < bufLength; i += packetSize)
{
tsHeader = b2i(4, stream_buf + i);
pid = (tsHeader & 0x1FFF00) >> 8;
scramblingControl = tsHeader & 0xC0;
if (tsHeader & 0x20)
{
offset = 4 + stream_buf[i + 4] + 1;
}
else
{
offset = 4;
}
if (packetSize - offset < 1)
{
continue;
}
if (scramblingControl == 0)
{
continue;
}
if (!(stream_buf[i + 3] & 0x10))
{
stream_buf[i + 3] &= 0x3F;
continue;
}
is_av_pid = 0;
if (pid == data->video_pid)
{
is_av_pid = 1;
}
else
{
for (j = 0; j < data->audio_pid_count; j++)
{
if (pid == data->audio_pids[j])
{
is_av_pid = 1;
break;
}
}
}
if (is_av_pid)
{
static uint8_t dyn_key[184];
static uint8_t last_packet[184];
// Reset key on channel change
if (data->reset_key_data == 1)
{
memset(dyn_key, 0x00, 184);
memset(last_packet, 0x00, 184);
data->reset_key_data = 0;
}
if (memcmp(last_packet, stream_buf + i + 4, 184) == 0)
{
if (memcmp(dyn_key, stream_buf + i + 4, 184) != 0)
{
memcpy(dyn_key, stream_buf + i + 4, 184);
}
}
else
{
memcpy(last_packet, stream_buf + i + 4, 184);
}
for (j = 0; j < 184; j++)
{
stream_buf[i + 4 + j] ^= dyn_key[j];
}
stream_buf[i + 3] &= 0x3F;
}
}
}
static void DescrambleTsPacketsCompel(emu_stream_client_data *data, uint8_t *stream_buf, uint32_t bufLength, uint16_t packetSize)
{
int8_t is_pes_pid; // any PES pid
int32_t j;
const int8_t limit = 4;
uint8_t scramblingControl;
uint16_t pid, offset;
uint32_t i, tsHeader;
for (i = 0; i < bufLength; i += packetSize)
{
tsHeader = b2i(4, stream_buf + i);
pid = (tsHeader & 0x1FFF00) >> 8;
scramblingControl = tsHeader & 0xC0;
if (tsHeader & 0x20)
{
offset = 4 + stream_buf[i + 4] + 1;
}
else
{
offset = 4;
}
if (packetSize - offset < 1)
{
continue;
}
if (scramblingControl == 0)
{
continue;
}
if (!(stream_buf[i + 3] & 0x10))
{
stream_buf[i + 3] &= 0x3F;
continue;
}
is_pes_pid = 0;
if (pid == data->video_pid)
{
is_pes_pid = 1;
}
else if (pid == data->teletext_pid)
{
is_pes_pid = 1;
}
else
{
for (j = 0; j < data->audio_pid_count; j++)
{
if (pid == data->audio_pids[j])
{
is_pes_pid = 1;
break;