|
SASC-NG
DVBSoftCAM
|
00001 /* 00002 * Softcam plugin to VDR (C++) 00003 * 00004 * This code is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This code is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html 00018 */ 00019 00020 #ifndef ___DATA_H 00021 #define ___DATA_H 00022 00023 #include <vdr/thread.h> 00024 #include <vdr/tools.h> 00025 00026 #include "misc.h" 00027 00028 class cStructLoaders; 00029 class cLoaders; 00030 class cPidFilter; 00031 class cPlainKeys; 00032 class cRewriter; 00033 00034 // ---------------------------------------------------------------- 00035 00036 class cFileMap : public cSimpleItem, private cMutex { 00037 private: 00038 char *filename; 00039 bool rw; 00040 // 00041 int fd, len, count; 00042 unsigned char *addr; 00043 bool failed; 00044 // 00045 void Clean(void); 00046 public: 00047 cFileMap(const char *Filename, bool Rw); 00048 ~cFileMap(); 00049 bool Map(void); 00050 bool Unmap(void); 00051 void Sync(void); 00052 int Size(void) const { return len; } 00053 unsigned char *Addr(void) const { return addr; } 00054 bool IsFileMap(const char *Name, bool Rw); 00055 }; 00056 00057 // ---------------------------------------------------------------- 00058 00059 class cFileMaps : public cSimpleList<cFileMap>, private cMutex { 00060 private: 00061 char *cfgDir; 00062 public: 00063 cFileMaps(void); 00064 ~cFileMaps(); 00065 void SetCfgDir(const char *CfgDir); 00066 cFileMap *GetFileMap(const char *name, const char *domain, bool rw); 00067 }; 00068 00069 extern cFileMaps filemaps; 00070 00071 //-------------------------------------------------------------- 00072 00073 class cStructItem : public cSimpleItem { 00074 private: 00075 char *comment; 00076 bool deleted, special; 00077 protected: 00078 void SetSpecial(void) { special=true; } 00079 public: 00080 cStructItem(void); 00081 virtual ~cStructItem(); 00082 virtual cString ToString(bool hide) { return ""; } 00083 bool Save(FILE *f); 00084 // 00085 void SetComment(const char *com); 00086 const char *Comment(void) const { return comment; } 00087 void Delete(void) { deleted=true; } 00088 bool Deleted(void) const { return deleted; } 00089 bool Special(void) const { return special; } 00090 bool Valid(void) const { return !deleted && !special; } 00091 }; 00092 00093 //-------------------------------------------------------------- 00094 00095 #define SL_READWRITE 1 00096 #define SL_MISSINGOK 2 00097 #define SL_WATCH 4 00098 #define SL_VERBOSE 8 00099 #define SL_NOPURGE 16 00100 #define SL_CUSTOMMASK 0xFF 00101 #define SL_LOADED 0x100 00102 #define SL_MODIFIED 0x200 00103 #define SL_DISABLED 0x400 00104 #define SL_SHUTUP 0x800 00105 #define SL_NOACCESS 0x1000 00106 00107 #define SL_SETFLAG(x) flags|=(x) 00108 #define SL_CLRFLAG(x) flags&=~(x) 00109 #define SL_TSTFLAG(x) (flags&(x)) 00110 00111 class cStructLoader : public cSimpleList<cStructItem> { 00112 friend class cStructLoaders; 00113 private: 00114 cStructLoader *next; 00115 cRwLock lock; 00116 // 00117 protected: 00118 int flags; 00119 const char *type, *filename; 00120 char *path; 00121 time_t mtime; 00122 // 00123 void CheckAccess(void); 00124 bool CheckUnmodified(void); 00125 void LoadFinished(void); 00126 void OpenFailed(void); 00127 bool CheckDoSave(void); 00128 time_t MTime(bool log); 00129 // 00130 virtual cStructItem *ParseLine(char *line)=0; 00131 void Modified(bool mod=true) { if(mod) SL_SETFLAG(SL_MODIFIED); else SL_CLRFLAG(SL_MODIFIED); } 00132 bool IsModified(void) const { return SL_TSTFLAG(SL_MODIFIED); } 00133 void ListLock(bool rw) { lock.Lock(rw); } 00134 void ListUnlock(void) { lock.Unlock(); } 00135 virtual void PreLoad(void) {} 00136 virtual void PostLoad(void) {} 00137 public: 00138 cStructLoader(const char *Type, const char *Filename, int Flags); 00139 virtual ~cStructLoader(); 00140 void AddItem(cStructItem *n, const char *com, cStructItem *ref); 00141 void DelItem(cStructItem *d, bool keep=false); 00142 cStructItem *NextValid(cStructItem *it) const; 00143 // 00144 void SetCfgDir(const char *cfgdir); 00145 virtual void Load(bool reload); 00146 virtual void Save(void); 00147 void Purge(void); 00148 void SafeClear(void); 00149 void Disable(void) { SL_SETFLAG(SL_DISABLED); } 00150 }; 00151 00152 //-------------------------------------------------------------- 00153 00154 template<class T> class cStructList : public cStructLoader { 00155 public: 00156 cStructList<T>(const char *Type, const char *Filename, int Flags):cStructLoader(Type,Filename,Flags) {} 00157 T *First(void) const { return (T *)NextValid(cStructLoader::First()); } 00158 T *Next(const T *item) const { return (T *)NextValid(cStructLoader::Next(item)); } 00159 }; 00160 00161 //-------------------------------------------------------------- 00162 00163 class cStructLoaderPlain : public cStructLoader { 00164 protected: 00165 virtual cStructItem *ParseLine(char *line) { return 0; } 00166 virtual bool ParseLinePlain(const char *line)=0; 00167 virtual void PreLoad(void); 00168 virtual void PreSave(FILE *f); 00169 virtual void PostSave(FILE *f) {}; 00170 public: 00171 cStructLoaderPlain(const char *Type, const char *Filename, int Flags); 00172 virtual void Load(bool reload); 00173 virtual void Save(void); 00174 }; 00175 00176 //-------------------------------------------------------------- 00177 00178 template<class T> class cStructListPlain : public cStructLoaderPlain { 00179 public: 00180 cStructListPlain<T>(const char *Type, const char *Filename, int Flags):cStructLoaderPlain(Type,Filename,Flags) {} 00181 T *First(void) const { return (T *)NextValid(cStructLoaderPlain::First()); } 00182 T *Next(const T *item) const { return (T *)NextValid(cStructLoaderPlain::Next(item)); } 00183 }; 00184 00185 //-------------------------------------------------------------- 00186 00187 class cStructLoaders { 00188 friend class cStructLoader; 00189 private: 00190 static cStructLoader *first; 00191 static cTimeMs lastReload, lastPurge, lastSave; 00192 // 00193 static void Register(cStructLoader *ld); 00194 public: 00195 static void SetCfgDir(const char *cfgdir); 00196 static void Load(bool reload); 00197 static void Save(bool force=false); 00198 static void Purge(void); 00199 }; 00200 00201 // ---------------------------------------------------------------- 00202 00203 class cPid : public cSimpleItem { 00204 public: 00205 int pid, sct, mask; 00206 cPidFilter *filter; 00207 // 00208 cPid(int Pid, int Section, int Mask); 00209 }; 00210 00211 // ---------------------------------------------------------------- 00212 00213 class cPids : public cSimpleList<cPid> { 00214 public: 00215 void AddPid(int Pid, int Section, int Mask); 00216 bool HasPid(int Pid, int Section, int Mask); 00217 }; 00218 00219 // ---------------------------------------------------------------- 00220 00221 #define SIDGRP_SHIFT 100000 // for group in split ECM handling 00222 00223 class cEcmInfo : public cStructItem { 00224 private: 00225 bool cached, failed; 00226 // 00227 void Setup(void); 00228 protected: 00229 int caDescrLen, dataIdx; 00230 unsigned char *caDescr; 00231 // 00232 void ClearCaDescr(void); 00233 public: 00234 char *name; 00235 int ecm_pid, ecm_table; 00236 int caId, provId, emmCaId; 00237 int prgId, grPrgId, source, transponder; 00238 cRewriter *rewriter; 00239 int rewriterId; 00240 int dvbAdapter, dvbFrontend; 00241 // 00242 cEcmInfo(void); 00243 cEcmInfo(const cEcmInfo *e); 00244 cEcmInfo(const char *Name, int Pid, int CaId, int ProvId); 00245 ~cEcmInfo(); 00246 virtual cString ToString(bool hide=false) { return ""; } 00247 bool Compare(const cEcmInfo *e); 00248 void SetDvb(int DvbAdapter, int DvbFrontend); 00249 void SetSource(int GrPrgId, int Source, int Transponder); 00250 void SetName(const char *Name); 00251 void SetDataIdx(int idx); 00252 const unsigned char *Data(void) const; 00253 bool AddCaDescr(const cEcmInfo *e); 00254 bool AddCaDescr(const unsigned char *descr, int len); 00255 const unsigned char *GetCaDescr(int *l) const; 00256 void SetRewriter(void); 00257 void Fail(bool st) { failed=st; } 00258 bool Failed(void) const { return failed; } 00259 void SetCached(void) { cached=true; } 00260 bool Cached(void) const { return cached; } 00261 }; 00262 00263 // ---------------------------------------------------------------- 00264 00265 class cMutableKey; 00266 00267 class cPlainKey : public cStructItem { 00268 friend class cPlainKeys; 00269 friend class cMutableKey; 00270 private: 00271 bool super; 00272 protected: 00273 void SetSupersede(bool val) { super=val; } 00274 bool CanSupersede(void) const { return super; } 00275 virtual int IdSize(void); 00276 virtual cString Print(void)=0; 00277 virtual cString PrintKeyNr(void); 00278 virtual bool CmpExtId(cPlainKey *k) { return true; } 00279 public: 00280 int type, id, keynr; 00281 // 00282 cPlainKey(bool CanSupersede); 00283 virtual bool Parse(const char *line)=0; 00284 virtual cString ToString(bool hide); 00285 virtual bool Cmp(cPlainKey *k)=0; 00286 virtual void Get(void *mem)=0; 00287 virtual int Size(void)=0; 00288 bool Set(int Type, int Id, int Keynr, void *Key, int Keylen); 00289 virtual bool SetKey(void *Key, int Keylen)=0; 00290 virtual bool SetBinKey(unsigned char *Mem, int Keylen)=0; 00291 }; 00292 00293 // ---------------------------------------------------------------- 00294 00295 class cMutableKey : public cPlainKey { 00296 private: 00297 cPlainKey *real; 00298 protected: 00299 virtual cString Print(void); 00300 virtual cPlainKey *Alloc(void) const=0; 00301 public: 00302 cMutableKey(bool Super); 00303 virtual ~cMutableKey(); 00304 virtual bool Cmp(cPlainKey *k); 00305 virtual void Get(void *mem); 00306 virtual int Size(void); 00307 virtual bool SetKey(void *Key, int Keylen); 00308 virtual bool SetBinKey(unsigned char *Mem, int Keylen); 00309 }; 00310 00311 // ---------------------------------------------------------------- 00312 00313 class cPlainKeyType { 00314 friend class cPlainKeys; 00315 private: 00316 int type; 00317 cPlainKeyType *next; 00318 public: 00319 cPlainKeyType(int Type, bool Super); 00320 virtual ~cPlainKeyType() {} 00321 virtual cPlainKey *Create(void)=0; 00322 }; 00323 00324 // ---------------------------------------------------------------- 00325 00326 template<class PKT, int KT, bool SUP=true> class cPlainKeyTypeReg : public cPlainKeyType { 00327 public: 00328 cPlainKeyTypeReg(void):cPlainKeyType(KT,SUP) {} 00329 virtual cPlainKey *Create(void) { return new PKT(SUP); } 00330 }; 00331 00332 // ---------------------------------------------------------------- 00333 00334 class cLastKey { 00335 private: 00336 int lastType, lastId, lastKeynr; 00337 public: 00338 cLastKey(void); 00339 bool NotLast(int Type, int Id, int Keynr); 00340 }; 00341 00342 // ---------------------------------------------------------------- 00343 00344 extern const char *externalAU; 00345 00346 class cPlainKeys : private cThread, public cStructList<cPlainKey> { 00347 friend class cPlainKeyType; 00348 private: 00349 static cPlainKeyType *first; 00350 cTimeMs trigger, last; 00351 cLastKey lastkey; 00352 // 00353 static void Register(cPlainKeyType *pkt, bool Super); 00354 cPlainKey *NewFromType(int type); 00355 bool AddNewKey(cPlainKey *nk, const char *reason); 00356 void ExternalUpdate(void); 00357 protected: 00358 virtual void Action(void); 00359 virtual void PostLoad(void); 00360 public: 00361 cPlainKeys(void); 00362 virtual cPlainKey *ParseLine(char *line); 00363 cPlainKey *FindKey(int Type, int Id, int Keynr, int Size, cPlainKey *key=0); 00364 cPlainKey *FindKeyNoTrig(int Type, int Id, int Keynr, int Size, cPlainKey *key=0); 00365 void Trigger(int Type, int Id, int Keynr); 00366 cString KeyString(int Type, int Id, int Keynr); 00367 bool NewKey(int Type, int Id, int Keynr, void *Key, int Keylen); 00368 bool NewKeyParse(char *line, const char *reason); 00369 void HouseKeeping(void); 00370 #ifdef TESTER 00371 void TestExternalUpdate(void); 00372 #endif 00373 }; 00374 00375 extern cPlainKeys keys; 00376 00377 #endif //___DATA_H