|
SASC
DVBSoftCAM
|
00001 /*************************************************************************** 00002 * Copyright (c) 2003 by Marcel Wiesweg * 00003 * * 00004 * This program is free software; you can redistribute it and/or modify * 00005 * it under the terms of the GNU General Public License as published by * 00006 * the Free Software Foundation; either version 2 of the License, or * 00007 * (at your option) any later version. * 00008 * * 00009 * $Id: util.h 1.7 2006/02/25 10:13:28 kls Exp $ 00010 * * 00011 ***************************************************************************/ 00012 00013 #ifndef LIBSI_UTIL_H 00014 #define LIBSI_UTIL_H 00015 00016 #include <stdint.h> 00017 #include <sys/types.h> 00018 #include <pthread.h> 00019 #include <time.h> 00020 00021 #define HILO(x) (x##_hi << 8 | x##_lo) 00022 #define BCD_TIME_TO_SECONDS(x) ((3600 * ((10*((x##_h & 0xF0)>>4)) + (x##_h & 0xF))) + \ 00023 (60 * ((10*((x##_m & 0xF0)>>4)) + (x##_m & 0xF))) + \ 00024 ((10*((x##_s & 0xF0)>>4)) + (x##_s & 0xF))) 00025 00026 namespace SI { 00027 00028 //Holds an array of unsigned char which is deleted 00029 //when the last object pointing to it is deleted. 00030 //Optimized for use in libsi. 00031 class CharArray { 00032 public: 00033 CharArray(); 00034 00035 CharArray(const CharArray &source); 00036 CharArray& operator=(const CharArray &source); 00037 ~CharArray(); 00038 00039 //can be called exactly once 00040 void assign(const unsigned char*data, int size, bool doCopy=true); 00041 //compares to a null-terminated string 00042 bool operator==(const char *string) const; 00043 //compares to another CharArray (data not necessarily null-terminated) 00044 bool operator==(const CharArray &other) const; 00045 00046 //returns another CharArray with its offset incremented by offset 00047 CharArray operator+(const int offset) const; 00048 00049 //access and convenience methods 00050 const unsigned char* getData() const { return data_->data+off; } 00051 const unsigned char* getData(int offset) const { return data_->data+offset+off; } 00052 template <typename T> const T* getData() const { return (T*)(data_->data+off); } 00053 template <typename T> const T* getData(int offset) const { return (T*)(data_->data+offset+off); } 00054 //sets p to point to data+offset, increments offset 00055 template <typename T> void setPointerAndOffset(const T* &p, int &offset) const { p=(T*)getData(offset); offset+=sizeof(T); } 00056 unsigned char operator[](const int index) const { return data_->data ? data_->data[off+index] : 0; } 00057 int getLength() const { return data_->size; } 00058 u_int16_t TwoBytes(const int index) const { return data_->data ? data_->TwoBytes(off+index) : 0; } 00059 u_int32_t FourBytes(const int index) const { return data_->data ? data_->FourBytes(off+index) : 0; } 00060 00061 bool isValid() const { return data_->valid; } 00062 bool checkSize(int offset) { return (data_->valid && (data_->valid=(offset>=0 && off+offset < data_->size))); } 00063 00064 void addOffset(int offset) { off+=offset; } 00065 private: 00066 class Data { 00067 public: 00068 Data(); 00069 virtual ~Data(); 00070 00071 virtual void assign(const unsigned char*data, int size) = 0; 00072 virtual void Delete() = 0; 00073 00074 u_int16_t TwoBytes(const int index) const 00075 { return (data[index] << 8) | data[index+1]; } 00076 u_int32_t FourBytes(const int index) const 00077 { return (data[index] << 24) | (data[index+1] << 16) | (data[index+2] << 8) | data[index+3]; } 00078 /*#ifdef CHARARRAY_THREADSAFE 00079 void Lock(); 00080 void Unlock(); 00081 #else 00082 void Lock() {} 00083 void Unlock() {} 00084 #endif 00085 Data(const Data& d); 00086 void assign(int size); 00087 */ 00088 00089 const unsigned char*data; 00090 int size; 00091 00092 // count_ is the number of CharArray objects that point at this 00093 // count_ must be initialized to 1 by all constructors 00094 // (it starts as 1 since it is pointed to by the CharArray object that created it) 00095 unsigned count_; 00096 00097 bool valid; 00098 00099 /* 00100 pthread_mutex_t mutex; 00101 pid_t lockingPid; 00102 pthread_t locked; 00103 */ 00104 }; 00105 class DataOwnData : public Data { 00106 public: 00107 DataOwnData() {} 00108 virtual ~DataOwnData(); 00109 virtual void assign(const unsigned char*data, int size); 00110 virtual void Delete(); 00111 }; 00112 class DataForeignData : public Data { 00113 public: 00114 DataForeignData() {} 00115 virtual ~DataForeignData(); 00116 virtual void assign(const unsigned char*data, int size); 00117 virtual void Delete(); 00118 }; 00119 Data* data_; 00120 int off; 00121 }; 00122 00123 00124 00125 //abstract base class 00126 class Parsable { 00127 public: 00128 void CheckParse(); 00129 protected: 00130 Parsable(); 00131 virtual ~Parsable() {} 00132 //actually parses given data. 00133 virtual void Parse() = 0; 00134 private: 00135 bool parsed; 00136 }; 00137 00138 //taken and adapted from libdtv, (c) Rolf Hakenes and VDR, (c) Klaus Schmidinger 00139 namespace DVBTime { 00140 time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese); 00141 time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese); 00142 inline unsigned char bcdToDec(unsigned char b) { return ((b >> 4) & 0x0F) * 10 + (b & 0x0F); } 00143 } 00144 00145 //taken and adapted from libdtv, (c) Rolf Hakenes 00146 class CRC32 { 00147 public: 00148 CRC32(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF); 00149 bool isValid() { return crc32(data, length, value) == 0; } 00150 static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF) { return crc32(d, len, CRCvalue) == 0; } 00151 protected: 00152 static u_int32_t crc_table[256]; 00153 static u_int32_t crc32 (const char *d, int len, u_int32_t CRCvalue); 00154 00155 const char *data; 00156 int length; 00157 u_int32_t value; 00158 }; 00159 00160 } //end of namespace 00161 00162 #endif