vdr 2.6.1
libsi/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 2.3 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
27namespace 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.
32class CharArray {
33public:
35
36 CharArray(const CharArray &source);
37 CharArray& operator=(const CharArray &source);
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; }
66private:
67 class Data {
68 public:
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:
117 virtual void assign(const unsigned char*data, int size);
118 virtual void Delete();
119 };
120 Data* data_;
121 int off;
122};
123
124
125
126//abstract base class
127class Parsable {
128public:
130protected:
132 virtual ~Parsable() {}
133 //actually parses given data.
134 virtual void Parse() = 0;
135private:
136 bool parsed;
137};
138
139//taken and adapted from libdtv, (c) Rolf Hakenes and VDR, (c) Klaus Schmidinger
140namespace DVBTime {
141time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese);
142time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese);
143inline 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
147class CRC32 {
148public:
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);
153protected:
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
static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue)
static u_int32_t crc_table[256]
static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF)
Definition: libsi/util.h:151
CRC32(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF)
u_int32_t value
const char * data
static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue)
Definition: util.c:267
bool isValid()
Definition: libsi/util.h:150
virtual void assign(const unsigned char *data, int size)
virtual void assign(const unsigned char *data, int size)
u_int16_t TwoBytes(const int index) const
const unsigned char * data
virtual void assign(const unsigned char *data, int size)=0
virtual void Delete()=0
u_int32_t FourBytes(const int index) const
const T * getData() const
Definition: libsi/util.h:53
unsigned char operator[](const int index) const
Definition: libsi/util.h:57
bool operator==(const CharArray &other) const
void assign(const unsigned char *data, int size, bool doCopy=true)
void setPointerAndOffset(const T *&p, int &offset) const
Definition: libsi/util.h:56
CharArray(const CharArray &source)
CharArray operator+(const int offset) const
int getLength() const
Definition: libsi/util.h:58
u_int32_t FourBytes(const int index) const
Definition: libsi/util.h:60
const unsigned char * getData(int offset) const
Definition: libsi/util.h:52
const unsigned char * getData() const
Definition: libsi/util.h:51
void addOffset(int offset)
Definition: libsi/util.h:65
u_int16_t TwoBytes(const int index) const
Definition: libsi/util.h:59
bool operator==(const char *string) const
const T * getData(int offset) const
Definition: libsi/util.h:54
CharArray & operator=(const CharArray &source)
bool isValid() const
Definition: libsi/util.h:62
bool checkSize(int offset)
Definition: libsi/util.h:63
virtual ~Parsable()
Definition: libsi/util.h:132
void CheckParse()
virtual void Parse()=0
unsigned char bcdToDec(unsigned char b)
time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese)
Definition: util.c:190
time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese)
Definition: util.c:213