vdr
1.7.27
|
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 2.3 2012/02/26 13:58:26 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 HILOHILO(x) (x##_hi_hi << 24 | x##_hi_lo << 16 | x##_lo_hi << 8 | x##_lo_lo) 00023 #define BCD_TIME_TO_SECONDS(x) ((3600 * ((10*((x##_h & 0xF0)>>4)) + (x##_h & 0xF))) + \ 00024 (60 * ((10*((x##_m & 0xF0)>>4)) + (x##_m & 0xF))) + \ 00025 ((10*((x##_s & 0xF0)>>4)) + (x##_s & 0xF))) 00026 00027 namespace SI { 00028 00029 //Holds an array of unsigned char which is deleted 00030 //when the last object pointing to it is deleted. 00031 //Optimized for use in libsi. 00032 class CharArray { 00033 public: 00034 CharArray(); 00035 00036 CharArray(const CharArray &source); 00037 CharArray& operator=(const CharArray &source); 00038 ~CharArray(); 00039 00040 //can be called exactly once 00041 void assign(const unsigned char*data, int size, bool doCopy=true); 00042 //compares to a null-terminated string 00043 bool operator==(const char *string) const; 00044 //compares to another CharArray (data not necessarily null-terminated) 00045 bool operator==(const CharArray &other) const; 00046 00047 //returns another CharArray with its offset incremented by offset 00048 CharArray operator+(const int offset) const; 00049 00050 //access and convenience methods 00051 const unsigned char* getData() const { return data_->data+off; } 00052 const unsigned char* getData(int offset) const { return data_->data+offset+off; } 00053 template <typename T> const T* getData() const { return (T*)(data_->data+off); } 00054 template <typename T> const T* getData(int offset) const { return (T*)(data_->data+offset+off); } 00055 //sets p to point to data+offset, increments offset 00056 template <typename T> void setPointerAndOffset(const T* &p, int &offset) const { p=(T*)getData(offset); offset+=sizeof(T); } 00057 unsigned char operator[](const int index) const { return data_->data ? data_->data[off+index] : (unsigned char)0; } 00058 int getLength() const { return data_->size; } 00059 u_int16_t TwoBytes(const int index) const { return data_->data ? data_->TwoBytes(off+index) : u_int16_t(0); } 00060 u_int32_t FourBytes(const int index) const { return data_->data ? data_->FourBytes(off+index) : u_int32_t(0); } 00061 00062 bool isValid() const { return data_->valid; } 00063 bool checkSize(int offset) { return (data_->valid && (data_->valid=(offset>=0 && off+offset < data_->size))); } 00064 00065 void addOffset(int offset) { off+=offset; } 00066 private: 00067 class Data { 00068 public: 00069 Data(); 00070 virtual ~Data(); 00071 00072 virtual void assign(const unsigned char*data, int size) = 0; 00073 virtual void Delete() = 0; 00074 00075 u_int16_t TwoBytes(const int index) const 00076 { return u_int16_t((data[index] << 8) | data[index+1]); } 00077 u_int32_t FourBytes(const int index) const 00078 { return u_int32_t((data[index] << 24) | (data[index+1] << 16) | (data[index+2] << 8) | data[index+3]); } 00079 /*#ifdef CHARARRAY_THREADSAFE 00080 void Lock(); 00081 void Unlock(); 00082 #else 00083 void Lock() {} 00084 void Unlock() {} 00085 #endif 00086 Data(const Data& d); 00087 void assign(int size); 00088 */ 00089 00090 const unsigned char*data; 00091 int size; 00092 00093 // count_ is the number of CharArray objects that point at this 00094 // count_ must be initialized to 1 by all constructors 00095 // (it starts as 1 since it is pointed to by the CharArray object that created it) 00096 unsigned count_; 00097 00098 bool valid; 00099 00100 /* 00101 pthread_mutex_t mutex; 00102 pid_t lockingPid; 00103 pthread_t locked; 00104 */ 00105 }; 00106 class DataOwnData : public Data { 00107 public: 00108 DataOwnData() {} 00109 virtual ~DataOwnData(); 00110 virtual void assign(const unsigned char*data, int size); 00111 virtual void Delete(); 00112 }; 00113 class DataForeignData : public Data { 00114 public: 00115 DataForeignData() {} 00116 virtual ~DataForeignData(); 00117 virtual void assign(const unsigned char*data, int size); 00118 virtual void Delete(); 00119 }; 00120 Data* data_; 00121 int off; 00122 }; 00123 00124 00125 00126 //abstract base class 00127 class Parsable { 00128 public: 00129 void CheckParse(); 00130 protected: 00131 Parsable(); 00132 virtual ~Parsable() {} 00133 //actually parses given data. 00134 virtual void Parse() = 0; 00135 private: 00136 bool parsed; 00137 }; 00138 00139 //taken and adapted from libdtv, (c) Rolf Hakenes and VDR, (c) Klaus Schmidinger 00140 namespace DVBTime { 00141 time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese); 00142 time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese); 00143 inline unsigned char bcdToDec(unsigned char b) { return (unsigned char)(((b >> 4) & 0x0F) * 10 + (b & 0x0F)); } 00144 } 00145 00146 //taken and adapted from libdtv, (c) Rolf Hakenes 00147 class CRC32 { 00148 public: 00149 CRC32(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF); 00150 bool isValid() { return crc32(data, length, value) == 0; } 00151 static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF) { return crc32(d, len, CRCvalue) == 0; } 00152 static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue); 00153 protected: 00154 static u_int32_t crc_table[256]; 00155 00156 const char *data; 00157 int length; 00158 u_int32_t value; 00159 }; 00160 00161 } //end of namespace 00162 00163 #endif