vdr  2.2.0
util.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2003 by Marcel Wiesweg *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * $Id: util.h 3.0 2012/02/26 13:58:26 kls Exp $
10  * *
11  ***************************************************************************/
12 
13 #ifndef LIBSI_UTIL_H
14 #define LIBSI_UTIL_H
15 
16 #include <stdint.h>
17 #include <sys/types.h>
18 #include <pthread.h>
19 #include <time.h>
20 
21 #define HILO(x) (x##_hi << 8 | x##_lo)
22 #define HILOHILO(x) (x##_hi_hi << 24 | x##_hi_lo << 16 | x##_lo_hi << 8 | x##_lo_lo)
23 #define BCD_TIME_TO_SECONDS(x) ((3600 * ((10*((x##_h & 0xF0)>>4)) + (x##_h & 0xF))) + \
24  (60 * ((10*((x##_m & 0xF0)>>4)) + (x##_m & 0xF))) + \
25  ((10*((x##_s & 0xF0)>>4)) + (x##_s & 0xF)))
26 
27 namespace SI {
28 
29 //Holds an array of unsigned char which is deleted
30 //when the last object pointing to it is deleted.
31 //Optimized for use in libsi.
32 class CharArray {
33 public:
34  CharArray();
35 
36  CharArray(const CharArray &source);
37  CharArray& operator=(const CharArray &source);
38  ~CharArray();
39 
40  //can be called exactly once
41  void assign(const unsigned char*data, int size, bool doCopy=true);
42  //compares to a null-terminated string
43  bool operator==(const char *string) const;
44  //compares to another CharArray (data not necessarily null-terminated)
45  bool operator==(const CharArray &other) const;
46 
47  //returns another CharArray with its offset incremented by offset
48  CharArray operator+(const int offset) const;
49 
50  //access and convenience methods
51  const unsigned char* getData() const { return data_->data+off; }
52  const unsigned char* getData(int offset) const { return data_->data+offset+off; }
53  template <typename T> const T* getData() const { return (T*)(data_->data+off); }
54  template <typename T> const T* getData(int offset) const { return (T*)(data_->data+offset+off); }
55  //sets p to point to data+offset, increments offset
56  template <typename T> void setPointerAndOffset(const T* &p, int &offset) const { p=(T*)getData(offset); offset+=sizeof(T); }
57  unsigned char operator[](const int index) const { return data_->data ? data_->data[off+index] : (unsigned char)0; }
58  int getLength() const { return data_->size; }
59  u_int16_t TwoBytes(const int index) const { return data_->data ? data_->TwoBytes(off+index) : u_int16_t(0); }
60  u_int32_t FourBytes(const int index) const { return data_->data ? data_->FourBytes(off+index) : u_int32_t(0); }
61 
62  bool isValid() const { return data_->valid; }
63  bool checkSize(int offset) { return (data_->valid && (data_->valid=(offset>=0 && off+offset < data_->size))); }
64 
65  void addOffset(int offset) { off+=offset; }
66 private:
67  class Data {
68  public:
69  Data();
70  virtual ~Data();
71 
72  virtual void assign(const unsigned char*data, int size) = 0;
73  virtual void Delete() = 0;
74 
75  u_int16_t TwoBytes(const int index) const
76  { return u_int16_t((data[index] << 8) | data[index+1]); }
77  u_int32_t FourBytes(const int index) const
78  { return u_int32_t((data[index] << 24) | (data[index+1] << 16) | (data[index+2] << 8) | data[index+3]); }
79  /*#ifdef CHARARRAY_THREADSAFE
80  void Lock();
81  void Unlock();
82  #else
83  void Lock() {}
84  void Unlock() {}
85  #endif
86  Data(const Data& d);
87  void assign(int size);
88  */
89 
90  const unsigned char*data;
91  int size;
92 
93  // count_ is the number of CharArray objects that point at this
94  // count_ must be initialized to 1 by all constructors
95  // (it starts as 1 since it is pointed to by the CharArray object that created it)
96  unsigned count_;
97 
98  bool valid;
99 
100  /*
101  pthread_mutex_t mutex;
102  pid_t lockingPid;
103  pthread_t locked;
104  */
105  };
106  class DataOwnData : public Data {
107  public:
109  virtual ~DataOwnData();
110  virtual void assign(const unsigned char*data, int size);
111  virtual void Delete();
112  };
113  class DataForeignData : public Data {
114  public:
116  virtual ~DataForeignData();
117  virtual void assign(const unsigned char*data, int size);
118  virtual void Delete();
119  };
121  int off;
122 };
123 
124 
125 
126 //abstract base class
127 class Parsable {
128 public:
129  void CheckParse();
130 protected:
131  Parsable();
132  virtual ~Parsable() {}
133  //actually parses given data.
134  virtual void Parse() = 0;
135 private:
136  bool parsed;
137 };
138 
139 //taken and adapted from libdtv, (c) Rolf Hakenes and VDR, (c) Klaus Schmidinger
140 namespace DVBTime {
141 time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese);
142 time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese);
143 inline unsigned char bcdToDec(unsigned char b) { return (unsigned char)(((b >> 4) & 0x0F) * 10 + (b & 0x0F)); }
144 }
145 
146 //taken and adapted from libdtv, (c) Rolf Hakenes
147 class CRC32 {
148 public:
149  CRC32(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF);
150  bool isValid() { return crc32(data, length, value) == 0; }
151  static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF) { return crc32(d, len, CRCvalue) == 0; }
152  static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue);
153 protected:
154  static u_int32_t crc_table[256];
155 
156  const char *data;
157  int length;
158  u_int32_t value;
159 };
160 
161 } //end of namespace
162 
163 #endif
u_int32_t FourBytes(const int index) const
Definition: util.h:77
time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese)
Definition: util.c:190
bool isValid() const
Definition: util.h:62
const T * getData(int offset) const
Definition: util.h:54
CharArray & operator=(const CharArray &source)
Definition: util.c:35
static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF)
Definition: util.h:151
~CharArray()
Definition: util.c:23
time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese)
Definition: util.c:213
const char * data
Definition: util.h:156
Definition: descriptor.c:16
void setPointerAndOffset(const T *&p, int &offset) const
Definition: util.h:56
bool operator==(const char *string) const
Definition: util.c:64
unsigned count_
Definition: util.h:96
bool parsed
Definition: util.h:136
int length
Definition: util.h:157
u_int32_t value
Definition: util.h:158
Data * data_
Definition: util.h:120
void assign(const unsigned char *data, int size, bool doCopy=true)
Definition: util.c:50
CharArray operator+(const int offset) const
Definition: util.c:85
unsigned char operator[](const int index) const
Definition: util.h:57
virtual ~Data()
Definition: util.c:99
virtual void Delete()=0
unsigned char bcdToDec(unsigned char b)
Definition: util.h:143
bool checkSize(int offset)
Definition: util.h:63
int getLength() const
Definition: util.h:58
int off
Definition: util.h:121
u_int16_t TwoBytes(const int index) const
Definition: util.h:59
const T * getData() const
Definition: util.h:53
const unsigned char * data
Definition: util.h:90
const unsigned char * getData(int offset) const
Definition: util.h:52
void addOffset(int offset)
Definition: util.h:65
CharArray()
Definition: util.c:20
u_int32_t FourBytes(const int index) const
Definition: util.h:60
u_int16_t TwoBytes(const int index) const
Definition: util.h:75
bool isValid()
Definition: util.h:150
const unsigned char * getData() const
Definition: util.h:51
virtual void assign(const unsigned char *data, int size)=0
virtual ~Parsable()
Definition: util.h:132