-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheepg.c
3766 lines (3501 loc) · 140 KB
/
eepg.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
/*
* Extended Epg plugin to VDR (C++)
*
* (C) 2008-2009 Dingo35
*
* This code is based on:
* -Premiere plugin (C) 2005-2007 Stefan Huelswitt <[email protected]>
* -mhwepg program (C) 2002, 2003 Jean-Claude Repetto <[email protected]>
* -LoadEpg plugin written by Luca De Pieri <[email protected]>
* -Freesat patch written by dom /at/ suborbital.org.uk
*
*
* This code 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 code 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <vdr/plugin.h>
#include <vdr/filter.h>
#include <vdr/epg.h>
#include <vdr/channels.h>
#include <vdr/dvbdevice.h>
#include <vdr/i18n.h>
#include <vdr/config.h>
#include <libsi/section.h>
#include <libsi/descriptor.h>
#include <libsi/si.h>
#include "eepg.h"
#include "dish.h"
#if APIVERSNUM > 10725
#include "epghandler.h"
#endif
#include "log.h"
#include "setupeepg.h"
#include "equivhandler.h"
#include "util.h"
#include "eit2.h"
#include <map>
#include <string>
#include <limits>
#include <stdarg.h>
#include <dirent.h>
#if defined(APIVERSNUM) && APIVERSNUM < 10401
#error You need at least VDR API version 1.4.1 for this plugin
#endif
#if APIVERSNUM < 10507
#define trNOOP(s) (s)
#endif
#define PMT_SCAN_TIMEOUT 10 // seconds
#define PMT_SCAN_IDLE 3600 // seconds
static const char *VERSION = "0.0.7";
static const char *DESCRIPTION = trNOOP ("Parses Extended EPG data");
using namespace std;
using namespace util;
const char *optPats[] = {
"%s",
"%s (Option %d)",
"%s (O%d)",
"#%2$d %1$s",
"[%2$d] %1$s"
};
#define NUM_PATS (sizeof(optPats)/sizeof(char *))
char *cs_hexdump (int m, const uchar * buf, int n)
{
int i;
static char dump[1024];
dump[i = 0] = '\0';
m = (m) ? 3 : 2;
if (m * n >= (int) sizeof (dump))
n = (sizeof (dump) / m) - 1;
while (i < n)
sprintf (dump + (m * i++), "%02X%s", *buf++, (m > 2) ? " " : "");
return (dump);
}
cSetupEEPG* SetupPE = cSetupEEPG::getInstance();
// --- cMenuSetupPremiereEpg ------------------------------------------------------------
class cMenuSetupPremiereEpg:public cMenuSetupPage
{
private:
cSetupEEPG* data;
const char *optDisp[NUM_PATS];
char buff[NUM_PATS][32];
protected:
virtual void Store (void);
public:
cMenuSetupPremiereEpg (void);
};
cMenuSetupPremiereEpg::cMenuSetupPremiereEpg (void)
{
data = cSetupEEPG::getInstance();
cOsdItem *item = new cOsdItem(tr ("PremiereEPG"));
if (item) {
item->SetSelectable(false);
Add(item);
}
// AddCategory (tr ("PremiereEPG"));
optDisp[0] = tr ("off");
for (unsigned int i = 1; i < NUM_PATS; i++) {
snprintf (buff[i], sizeof (buff[i]), optPats[i], "Event", 1);
optDisp[i] = buff[i];
}
Add (new cMenuEditStraItem (tr ("Tag option events"), &data->OptPat, NUM_PATS, optDisp));
Add (new cMenuEditBoolItem (tr ("Show order information"), &data->OrderInfo));
Add (new cMenuEditBoolItem (tr ("Show rating information"), &data->RatingInfo));
Add (new cMenuEditBoolItem (tr ("Fix EPG data"), &data->FixEpg));
item = new cOsdItem(tr ("General"));
if (item) {
item->SetSelectable(false);
Add(item);
}
// AddCategory (tr ("General"));
Add (new cMenuEditBoolItem (tr ("Display summary message"), &data->DisplayMessage));
Add (new cMenuEditBoolItem (tr ("Replace empty Short Text with Category - Genre"), &data->ReplaceEmptyShText));
Add (new cMenuEditBoolItem (tr ("Try to fix CharSet for events"), &data->FixCharset));
#ifdef DEBUG
Add (new cMenuEditIntItem (tr ("Level of logging verbosity"), &data->LogLevel, 0, 5));
Add (new cMenuEditBoolItem (tr ("Process EIT info with EEPG"), &data->ProcessEIT));
#endif
}
void cMenuSetupPremiereEpg::Store (void)
{
//SetupPE = data;
SetupStore ("OptionPattern", SetupPE->OptPat);
SetupStore ("OrderInfo", SetupPE->OrderInfo);
SetupStore ("RatingInfo", SetupPE->RatingInfo);
SetupStore ("FixEpg", SetupPE->FixEpg);
SetupStore ("DisplayMessage", SetupPE->DisplayMessage);
SetupStore ("ReplaceEmptyShText", SetupPE->ReplaceEmptyShText);
SetupStore ("FixCharset", SetupPE->FixCharset);
#ifdef DEBUG
SetupStore ("LogLevel", SetupPE->LogLevel);
SetupStore ("ProcessEIT", SetupPE->ProcessEIT);
#endif
}
//#define Asprintf(a, b, c...) void( asprintf(a, b, c) < 0 ? esyslog("memory allocation error - %s", b) : void() )
// --- CRC16 -------------------------------------------------------------------
#define POLY 0xA001 // CRC16
unsigned int crc16 (unsigned int crc, unsigned char const *p, int len)
{
while (len--) {
crc ^= *p++;
for (int i = 0; i < 8; i++)
crc = (crc & 1) ? (crc >> 1) ^ POLY : (crc >> 1);
}
return crc & 0xFFFF;
}
// --- cFilterEEPG ------------------------------------------------------
#define STARTTIME_BIAS (20*60)
static int LastVersionNagra = -1; //currently only used for Nagra, should be stored per transponder, per system
class cFilterEEPG:public cFilter
{
private:
int pmtpid, pmtsid, pmtidx, pmtnext;
int UnprocessedFormat[HIGHEST_FORMAT + 1]; //stores the pid when a format is detected on this transponder, and that are not processed yet
int nChannels, nThemes, nTitles, nSummaries, NumberOfTables, Version;
int TitleCounter, SummaryCounter, NoSummaryCounter, RejectTableId;
bool EndChannels, EndThemes; //only used for ??
int MHWStartTime; //only used for MHW1
bool ChannelsOk;
EFormat Format; //the format that this filter currently is processing
std::map < int, int >ChannelSeq; // ChannelSeq[ChannelId] returns the recordnumber of the channel
Summary_t *Summaries[MAX_TITLES];
Title_t *Titles[MAX_TITLES];
sChannel sChannels[MAX_CHANNELS];
unsigned char Themes[MAX_THEMES][64];
std::map < unsigned short int, unsigned char *>buffer; //buffer[Table_Extension_Id] returns the pointer to the buffer for this TEI
std::map < unsigned short int, int >bufsize; //bufsize[Table_Extension_Id] returns the buffersize of the buffer for this TEI
unsigned short int NagraTIE[64]; //at this moment a max of 31 table_ids could be used, so 64 should be enough ....stores the Table_Extension_Id's of summaries received, so they can be processed. Processing while receiving somehow drops sections, the 0x0000 marker will be missed ...
unsigned short int NagraCounter;
unsigned char InitialChannel[8];
unsigned char InitialTitle[64];
unsigned char InitialSummary[64];
void NextPmt (void);
void ProccessContinuous(u_short Pid, u_char Tid, int Length, const u_char *Data);
bool load_sky_file (const char *filename);
int sky_huffman_decode (const u_char * Data, int Length, unsigned char *DecodeText);
protected:
virtual void Process (u_short Pid, u_char Tid, const u_char * Data, int Length);
virtual void AddFilter (u_short Pid, u_char Tid);
virtual void AddFilter (u_short Pid, u_char Tid, unsigned char Mask);
virtual void ProcessNextFormat (bool FirstTime);
virtual int GetChannelsSKYBOX (const u_char * Data, int Length);
virtual bool GetThemesSKYBOX (void);
virtual int GetTitlesSKYBOX (const u_char * Data, int Length);
virtual int GetSummariesSKYBOX (const u_char * Data, int Length);
virtual int GetChannelsMHW (const u_char * Data, int Length);
virtual int GetThemesMHW1 (const u_char * Data, int Length);
virtual int GetNagra (const u_char * Data, int Length);
virtual void ProcessNagra (void);
virtual void GetTitlesNagra (const u_char * Data, int Length, unsigned short TableIdExtension);
virtual char *GetSummaryTextNagra (const u_char * DataStart, long int Offset, unsigned int EventId);
virtual int GetChannelsNagra (const u_char * Data, int Length);
virtual int GetThemesNagra (const u_char * Data, int Length, unsigned short TableIdExtension);
virtual int GetTitlesMHW1 (const u_char * Data, int Length);
virtual int GetSummariesMHW1 (const u_char * Data, int Length);
virtual int GetThemesMHW2 (const u_char * Data, int Length);
virtual int GetTitlesMHW2 (const u_char * Data, int Length);
virtual int GetSummariesMHW2 (const u_char * Data, int Length);
virtual void FreeSummaries (void);
virtual void FreeTitles (void);
//virtual void PrepareToWriteToSchedule (sChannel * C, cSchedules * s, cSchedule * ps/*[MAX_EQUIVALENCES]*/); //gets a channel and returns an array of schedules that WriteToSchedule can write to. Call this routine before a batch of titles with the same ChannelId will be WriteToScheduled; batchsize can be 1
//virtual void FinishWriteToSchedule (sChannel * C, cSchedules * s, cSchedule * ps[MAX_EQUIVALENCES]);
virtual void WriteToSchedule (tChannelID channelID, cSchedules* s, unsigned int EventId, unsigned int StartTime,
unsigned int Duration, char *Text, char *SummText, unsigned short int ThemeId,
unsigned short int TableId, unsigned short int Version, char Rating = 0x00, unsigned char ShortTextLenght = 0);
virtual void LoadIntoSchedule (void);
//virtual void LoadEquivalentChannels (void);
void ProcessPremiere(const u_char *& Data);
public:
cFilterEEPG (void);
virtual void SetStatus (bool On);
void Trigger (void);
bool InitDictionary (void); //Initialize the Huffman tables for SKY and Freesat
static const int EIT_PID = 0x12;
};
cFilterEEPG::cFilterEEPG (void)
{
nSummaries = 0;
nTitles = 0;
Trigger ();
//Set (0x00, 0x00);
}
void cFilterEEPG::Trigger (void)
{
LogI(3, prep("trigger\n"));
pmtpid = 0;
pmtidx = 0;
pmtnext = 0;
}
void cFilterEEPG::SetStatus (bool On)
{
// LogI(0, prep("setstatus %d\n"), On);
if (!On) {
FreeSummaries ();
FreeTitles ();
Format = MHW1;
ChannelsOk = false;
NumberOfTables = 0;
} else {
//Set(0x00,0x00);
for (int i = 0; i <= HIGHEST_FORMAT; i++)
UnprocessedFormat[i] = 0; //pid 0 is assumed to be nonvalid for EEPG transfers
AddFilter (0, 0);
}
cFilter::SetStatus (On);
Trigger ();
}
void cFilterEEPG::NextPmt (void)
{
Del (pmtpid, SI::TableIdPMT);
pmtpid = 0;
pmtidx++;
LogE(3, prep("PMT next\n"));
}
// ------------------- Freesat -------------------
/* FreeSat Huffman decoder for VDR
*
* Insert GPL licence
*/
/* The following features can be controlled:
*
* FREEVIEW_NO_SYSLOG - Disable use of isyslog
*/
#ifndef FREEVIEW_NO_SYSLOG
#include <vdr/tools.h>
/* Logging via vdr */
#ifndef isyslog
#define isyslog(a...) void( (SysLogLevel > 1) ? syslog_with_tid(LOG_INFO, a) : void() )
#endif
void syslog_with_tid (int priority, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
#else
#define isyslog(a...) fprintf(stderr,a)
#endif
static sNodeH* sky_tables[2];
/** \brief Convert a textual character description into a value
*
* \param str - Encoded (in someway) string
*
* \return Raw character
*/
static unsigned char resolve_char (char *str)
{
int val;
if (strcmp (str, "ESCAPE") == 0) {
return ESCAPE;
} else if (strcmp (str, "STOP") == 0) {
return STOP;
} else if (strcmp (str, "START") == 0) {
return START;
} else if (sscanf (str, "0x%02x", &val) == 1) {
return val;
}
return str[0];
}
/** \brief Decode a binary string into a value
*
* \param binary - Binary string to decode
*
* \return Decoded value
*/
static unsigned long decode_binary (char *binary)
{
unsigned long mask = 0x80000000;
unsigned long maskval = 0;
unsigned long val = 0;
size_t i;
for (i = 0; i < strlen (binary); i++) {
if (binary[i] == '1') {
val |= mask;
}
maskval |= mask;
mask >>= 1;
}
return val;
}
/** \brief Load an individual freesat data file
*
* \param tableid - Table id that should be loaded
* \param filename - Filename to load
* \return Success of operation
*/
static bool load_freesat_file (int tableid, const char *filename)
{
char buf[1024];
char *from, *to, *binary;
FILE *fp;
tableid--;
if ((fp = fopen (filename, "r")) != NULL) {
LogI(2, prep("Loading table %d Filename <%s>"), tableid + 1, filename);
while (fgets (buf, sizeof (buf), fp) != NULL) {
from = binary = to = NULL;
int elems = sscanf (buf, "%m[^:]:%m[^:]:%m[^:]:", &from, &binary, &to);
if (elems == 3) {
int bin_len = strlen (binary);
int from_char = resolve_char (from);
char to_char = resolve_char (to);
unsigned long bin = decode_binary (binary);
int i = table_size[tableid][from_char]++;
tables[tableid][from_char] =
(struct hufftab *) REALLOC (tables[tableid][from_char], (i + 1) * sizeof (tables[tableid][from_char][0]));
tables[tableid][from_char][i].value = bin;
tables[tableid][from_char][i].next = to_char;
tables[tableid][from_char][i].bits = bin_len;
/* char from; unsigned int value; short bits; char next; */
LogI(2, prep("%02x;%08x;%04x;%02x"), from_char, bin, bin_len, to_char);
free (from);
free (to);
free (binary);
}
}
fclose (fp);
} else {
LogE(0, prep("Cannot load <%s> for table %d"), filename, tableid + 1);
return false;
}
return true;
}
/** \brief Load an individual sky data file
*
* \param filename - Filename to load
* \return Success of operation
*/
bool cFilterEEPG::load_sky_file (const char *filename)
{
FILE *FileDict;
char *Line;
char Buffer[256];
sNodeH *nH;
int tableId;
FileDict = fopen (filename, "r");
if (FileDict == NULL) {
LogE (0, prep("Error opening file '%s'. %s"), filename, strerror (errno));
return false;
} else {
int i;
int LenPrefix;
char string1[256];
char string2[256];
tableId = Format == SKY_IT ? 0 : 1;
if (!sky_tables[tableId]) {
sky_tables[tableId] = (sNodeH*) calloc(1,sizeof(sNodeH));
if (!sky_tables[tableId]) {
LogE (0, prep("Not enough memory to load file '%s'."), filename);
return false;
}
}
while ((Line = fgets (Buffer, sizeof (Buffer), FileDict)) != NULL) {
if (!isempty (Line)) {
memset (string1, 0, sizeof (string1));
memset (string2, 0, sizeof (string2));
if (sscanf (Line, "%c=%[^\n]\n", string1, string2) == 2
|| (sscanf (Line, "%[^=]=%[^\n]\n", string1, string2) == 2)) {
nH = sky_tables[tableId];
LenPrefix = strlen (string2);
for (i = 0; i < LenPrefix; i++) {
switch (string2[i]) {
case '0':
if (nH->P0 == NULL) {
nH->P0 = new sNodeH ();
nH = nH->P0;
nH->Value = NULL;
nH->P0 = NULL;
nH->P1 = NULL;
if ((LenPrefix - 1) == i) {
Asprintf (&nH->Value, "%s", string1);
}
} else {
nH = nH->P0;
if (nH->Value != NULL || (LenPrefix - 1) == i) {
LogE (0 ,prep("Error, huffman prefix code already exists for \"%s\"=%s with '%s'"), string1,
string2, nH->Value);
}
}
break;
case '1':
if (nH->P1 == NULL) {
nH->P1 = new sNodeH ();
nH = nH->P1;
nH->Value = NULL;
nH->P0 = NULL;
nH->P1 = NULL;
if ((LenPrefix - 1) == i) {
Asprintf (&nH->Value, "%s", string1);
}
} else {
nH = nH->P1;
if (nH->Value != NULL || (LenPrefix - 1) == i) {
LogE (0, prep("Error, huffman prefix code already exists for \"%s\"=%s with '%s'"), string1,
string2, nH->Value);
}
}
break;
default:
break;
}
}
}
}
}
fclose (FileDict);
}
// check tree huffman nodes
FileDict = fopen (filename, "r");
if (FileDict) {
int i;
int LenPrefix;
char string1[256];
char string2[256];
while ((Line = fgets (Buffer, sizeof (Buffer), FileDict)) != NULL) {
if (!isempty (Line)) {
memset (string1, 0, sizeof (string1));
memset (string2, 0, sizeof (string2));
if (sscanf (Line, "%c=%[^\n]\n", string1, string2) == 2
|| (sscanf (Line, "%[^=]=%[^\n]\n", string1, string2) == 2)) {
nH = sky_tables[tableId];
LenPrefix = strlen (string2);
for (i = 0; i < LenPrefix; i++) {
switch (string2[i]) {
case '0':
if (nH->P0 != NULL) {
nH = nH->P0;
}
break;
case '1':
if (nH->P1 != NULL) {
nH = nH->P1;
}
break;
default:
break;
}
}
if (nH->Value != NULL) {
if (memcmp (nH->Value, string1, strlen (nH->Value)) != 0) {
LogE (0, prep("Error, huffman prefix value '%s' not equal to '%s'"), nH->Value, string1);
}
} else {
LogE (0, prep("Error, huffman prefix value is not exists for \"%s\"=%s"), string1, string2);
}
}
}
}
fclose (FileDict);
}
return true;
}
#define THEME_TR ".tr"
void load_theme_dictionaries (void)
{
char Buffer[1024];
char *Line;
FILE *File;
DIR *dp = opendir(cSetupEEPG::getInstance()->getConfDir());
struct dirent *dirp;
if(!dp) {
LogE (0, prep("Can't read configuration folder '%s'"), cSetupEEPG::getInstance()->getConfDir());
return ;
}
if (tableDict.size() > 0)
tableDict.clear();
while ((dirp = readdir(dp)) != NULL) {
string fname = dirp->d_name; // filename
if (dirp->d_type == DT_DIR || // if entry is a directory
fname.find(THEME_TR, (fname.length() - strlen(THEME_TR))) == string::npos){
continue;
}
fname = string(cSetupEEPG::getInstance()->getConfDir()) + "/" + fname;
//Test if file is changed and reload
struct stat st;
if (stat(fname.c_str(), &st)) {
LogE(0, prep("Error obtaining stats for '%s' "), fname.c_str());
continue;
}
File = fopen (fname.c_str(), "r");
if (!File) continue;
memset (Buffer, 0, sizeof (Buffer));
char origThemeName[256];
char transThemeName[256];
while ((Line = fgets (Buffer, sizeof (Buffer), File)) != NULL) {
Line = compactspace (skipspace (stripspace (Line)));
//Skip empty and commented lines
if (isempty (Line) || Line[0] == '#' || Line[0] == ';') continue;
if (sscanf (Line, "%[^=]=%[^\n]\n", origThemeName, transThemeName) == 2) {
string origTh(compactspace (skipspace (stripspace (origThemeName))));
string transTh(compactspace (skipspace (stripspace (transThemeName))));
if (!tableDict.count(origTh) && !transTh.empty()) {
tableDict.insert(pair<string,string>(string(origThemeName),string(transThemeName)));
LogD(4, prep("Original '%s' translation to '%s'."), origTh.c_str(), transTh.c_str());
}
} //if scanf
} //while
fclose (File);
LogD(3, prep("Loaded %i translations from %s."), tableDict.size(), fname.c_str());
}
closedir(dp);
LogD(2, prep("Loaded %i translations."), tableDict.size());
LogD(2, prep("Original <-> Translation"));
map<string,string>::iterator it;
for ( it=tableDict.begin() ; it != tableDict.end(); it++ )
LogD(2, prep("%s <-> %s"), (*it).first.c_str(), it->second.c_str());
}
/** \brief Decode an EPG string as necessary
*
* \param src - Possibly encoded string
* \param size - Size of the buffer
*
* \retval NULL - Can't decode
* \return A decoded string
*/
char *freesat_huffman_decode (const unsigned char *src, size_t size)
{
int tableid;
// freesat_decode_error = 0;
if (src[0] == 0x1f && (src[1] == 1 || src[1] == 2)) {
int uncompressed_len = 30;
char *uncompressed = (char *) calloc (1, uncompressed_len + 1);
unsigned value = 0, byte = 2, bit = 0;
int p = 0;
unsigned char lastch = START;
tableid = src[1] - 1;
while (byte < 6 && byte < size) {
value |= src[byte] << ((5 - byte) * 8);
byte++;
}
//freesat_table_load (); /**< Load the tables as necessary */
do {
bool found = false;
unsigned bitShift = 0;
if (lastch == ESCAPE) {
char nextCh = (value >> 24) & 0xff;
found = true;
// Encoded in the next 8 bits.
// Terminated by the first ASCII character.
bitShift = 8;
if ((nextCh & 0x80) == 0)
lastch = nextCh;
if (p >= uncompressed_len) {
uncompressed_len += 10;
uncompressed = (char *) REALLOC (uncompressed, uncompressed_len + 1);
}
uncompressed[p++] = nextCh;
uncompressed[p] = 0;
} else {
int j;
for (j = 0; j < table_size[tableid][lastch]; j++) {
unsigned mask = 0, maskbit = 0x80000000;
short kk;
for (kk = 0; kk < tables[tableid][lastch][j].bits; kk++) {
mask |= maskbit;
maskbit >>= 1;
}
if ((value & mask) == tables[tableid][lastch][j].value) {
char nextCh = tables[tableid][lastch][j].next;
bitShift = tables[tableid][lastch][j].bits;
if (nextCh != STOP && nextCh != ESCAPE) {
if (p >= uncompressed_len) {
uncompressed_len += 10;
uncompressed = (char *) REALLOC (uncompressed, uncompressed_len + 1);
}
uncompressed[p++] = nextCh;
uncompressed[p] = 0;
}
found = true;
lastch = nextCh;
break;
}
}
}
if (found) {
// Shift up by the number of bits.
unsigned b;
for (b = 0; b < bitShift; b++) {
value = (value << 1) & 0xfffffffe;
if (byte < size)
value |= (src[byte] >> (7 - bit)) & 1;
if (bit == 7) {
bit = 0;
byte++;
} else
bit++;
}
} else {
LogE (0, prep("Missing table %d entry: <%s>"), tableid + 1, uncompressed);
// Entry missing in table.
return uncompressed;
}
} while (lastch != STOP && value != 0);
return uncompressed;
}
return NULL;
}
int cFilterEEPG::sky_huffman_decode (const u_char * Data, int Length, unsigned char *DecodeText)
{
sNodeH *nH, H=(Format==SKY_IT)?*sky_tables[0]:*sky_tables[1];
int i;
int p;
int q;
bool CodeError;
bool IsFound;
unsigned char Byte;
unsigned char lastByte;
unsigned char Mask;
unsigned char lastMask;
nH = &H;
p = 0;
q = 0;
DecodeText[0] = '\0';
//DecodeErrorText[0] = '\0';
CodeError = false;
IsFound = false;
lastByte = 0;
lastMask = 0;
for (i = 0; i < Length; i++) {
Byte = Data[i];
Mask = 0x80;
if (i == 0) {
Mask = 0x20;
lastByte = i;
lastMask = Mask;
}
loop1:
if (IsFound) {
lastByte = i;
lastMask = Mask;
IsFound = false;
}
if ((Byte & Mask) == 0) {
if (CodeError) {
//DecodeErrorText[q] = 0x30;
q++;
goto nextloop1;
}
if (nH->P0 != NULL) {
nH = nH->P0;
if (nH->Value != NULL) {
memcpy (&DecodeText[p], nH->Value, strlen (nH->Value));
p += strlen (nH->Value);
nH = &H;
IsFound = true;
}
} else {
memcpy (&DecodeText[p], "<...?...>", 9);
p += 9;
i = lastByte;
Byte = Data[lastByte];
Mask = lastMask;
CodeError = true;
goto loop1;
}
} else {
if (CodeError) {
//DecodeErrorText[q] = 0x31;
q++;
goto nextloop1;
}
if (nH->P1 != NULL) {
nH = nH->P1;
if (nH->Value != NULL) {
memcpy (&DecodeText[p], nH->Value, strlen (nH->Value));
p += strlen (nH->Value);
nH = &H;
IsFound = true;
}
} else {
memcpy (&DecodeText[p], "<...?...>", 9);
p += 9;
i = lastByte;
Byte = Data[lastByte];
Mask = lastMask;
CodeError = true;
goto loop1;
}
}
nextloop1:
Mask = Mask >> 1;
if (Mask > 0) {
goto loop1;
}
}
DecodeText[p] = '\0';
//DecodeErrorText[q] = '\0';
return p;
}
bool cFilterEEPG::GetThemesSKYBOX (void) //TODO can't we read this from the DVB stream?
{
string FileName = cSetupEEPG::getInstance()->getConfDir();
FILE *FileThemes;
char *Line;
char Buffer[256];
const char **SkyThemes;
bool updateFile = false;
if (Format == SKY_IT) {
FileName += "/sky_it.themes";
SkyThemes = SkyItThemes;
}
else if (Format == SKY_UK) {
FileName += "/sky_uk.themes";
SkyThemes = SkyUkThemes;
}
else {
LogE (0, prep("Error, wrong format detected in GetThemesSKYBOX. Format = %i."), Format);
return false;
}
//asprintf( &FileName, "%s/%s", ConfDir, ( lProviders + CurrentProvider )->Parm3 );
FileThemes = fopen (FileName.c_str(), "r");
if (FileThemes == NULL) {
LogE (0, prep("Error opening file '%s'. %s"), FileName.c_str(), strerror (errno));
return false;
} else {
int id = 0;
nThemes = 0;
char thId[256];
char theme[256];
while ((Line = fgets (Buffer, sizeof (Buffer), FileThemes)) != NULL) {
memset (thId, 0, sizeof (thId));
memset (theme, 0, sizeof (theme));
if (!isempty (Line)) {
if (sscanf (Line, "%[^=] =%[^\n] ", thId, theme) == 2 && !isempty (theme)) {
snprintf ((char *) Themes[id], 255, "%s", theme);
nThemes++;
} else {
if (SkyThemes[id]) {
updateFile = true;
snprintf ((char *) Themes[id], 255, "%s", SkyThemes[id]);
LogD (1, prep("Theme '%s' missing in theme file '%s'"), SkyThemes[id], FileName.c_str());
} else
Themes[id][0] = '\0';
}
id ++;
}
}
fclose (FileThemes);
if (updateFile) {
FileThemes = fopen (FileName.c_str(), "w");
if (FileThemes == NULL) {
LogE (0, prep("Error re-creating file '%s', %s"), FileName.c_str(), strerror (errno));
} else {
for (int i = 0; i < 256; i++) {
if (Themes[i]) {
fprintf (FileThemes, "0x%02x=%s\n", i, (char *) Themes[i]);
}
else {
fprintf (FileThemes, "0x%02x=\n", i);
}
}
LogI (0, prep("Success updating file '%s'"), FileName.c_str());
fclose (FileThemes);
}
}
}
return true;
}
/**
* \brief Initialize the Huffman dictionaries if they are not already initialized.
*
*/
static cMutex InitDictionary_mutex;
bool cFilterEEPG::InitDictionary (void)
{
// This function must be serialised because it updates sky_tables and tables
// which are both globals.
cMutexLock MutexLock(&InitDictionary_mutex);
string FileName = cSetupEEPG::getInstance()->getConfDir();
switch (Format) {
case SKY_IT:
if (sky_tables[0] == NULL) {
FileName += "/sky_it.dict";
LogD (4, prep("EEPGDebug: loading sky_it.dict"));
return load_sky_file(FileName.c_str());
} else
LogD (4, prep("EEPGDebug: sky_it.dict already loaded"));
break;
case SKY_UK:
if (sky_tables[1] == NULL) {
FileName += "/sky_uk.dict";
LogD (4, prep("EEPGDebug: loading sky_uk.dict"));
return load_sky_file(FileName.c_str());
} else
LogD (4, prep("EEPGDebug: sky_uk.dict already loaded"));
break;
case FREEVIEW:
if (tables[0][0] == NULL) {
LogD (4, prep("EEPGDebug: loading freesat.dict"));
FileName += "/freesat.t1";
if (!load_freesat_file (1, FileName.c_str()))
return false;
FileName = cSetupEEPG::getInstance()->getConfDir();
FileName += "/freesat.t2";
return load_freesat_file (2, FileName.c_str());
} else
LogD (4, prep("EEPGDebug: freesat.dict already loaded"));
break;
default:
LogE (0 ,prep("Error, wrong format detected in ReadFileDictionary. Format = %i."), Format);
return false;
}
return true;
}
/**
* \brief Get MHW channels
*
* \return 0 = fatal error, code 1 = success, code 2 = last item processed
*/
int cFilterEEPG::GetChannelsMHW (const u_char * Data, int Length)
{
if (Format != MHW1 && Format != MHW2) return 0;
if ((Format == MHW1) || (nChannels == 0)) { //prevents MHW2 from reading channels twice while waiting for themes on same filter
sChannelMHW1 *Channel;
int Size, Off;
Size = sizeof (sChannelMHW1);
Off = 4;
if (Format == MHW1) {
//Channel = (sChannelMHW1 *) (Data + 4);
nChannels = (Length - Off) / sizeof (sChannelMHW1);
}
if (Format == MHW2) {
if (Length > 120)
nChannels = Data[120];
else {
LogE(0, prep("Error, channels packet too short for MHW2."));
return 0;
}
int pName = ((nChannels * 8) + 121);
if (Length > pName) {
//Channel = (sChannelMHW1 *) (Data + 120);
Size -= 14; //MHW2 is 14 bytes shorter
Off = 121; //and offset differs
} else {
LogE(0, prep("Error, channels length does not match pname."));
return 0;
}
}
if (nChannels > MAX_CHANNELS) {
LogE(0, prep("EEPG: Error, %i channels found more than %i"), nChannels, MAX_CHANNELS);
return 0;
} else {
LogI(1, "| ID | %-26.26s | %-22.22s | FND | %-8.8s |\n", "Channel ID", "Channel Name", "Sky Num.");
LogI(1, "|------|-%-26.26s-|-%-22.22s-|-----|-%-8.8s-|\n", "------------------------------",
"-----------------------------", "--------------------");
int pName = ((nChannels * 8) + 121); //TODO double ...
LogD(1, prep("Length:%d pName:%d diff:%d"), Length, pName, Length - pName);
for (int i = 0; i < nChannels; i++) {
Channel = (sChannelMHW1 *) (Data + Off);
sChannel *C = &sChannels[i];
C->ChannelId = i;
ChannelSeq[C->ChannelId] = i; //fill lookup table to go from channel-id to sequence nr in table
C->SkyNumber = 0;
if (Format == MHW1)
memcpy (C->Name, &Channel->Name, 16); //MHW1
else { //MHW2
int lenName = Data[pName] & 0x0f;
//LogD (1, prep("EEPGDebug: MHW2 lenName:%d"), lenName);
decodeText2(&Data[pName+1],lenName,(char*)C->Name,256);
//memcpy (C->Name, &Data[pName + 1], lenName);
//else
//memcpy (C->Name, &Data[pName + 1], 256);
pName += (lenName + 1);
}
//C->NumberOfEquivalences = 1; //there is always an original channel. every equivalence adds 1
C->Src = Source (); //assume all EPG channels are on same satellite, if not, manage this via equivalents!!!
C->Nid = HILO16 (Channel->NetworkId);