2003-11-22 17:56:45 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (c) 2003 by Marcel Wiesweg *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
2012-02-26 14:02:17 +01:00
|
|
|
* $Id: util.h 2.3 2012/02/26 13:58:26 kls Exp $
|
2003-12-13 10:43:26 +01:00
|
|
|
* *
|
2003-11-22 17:56:45 +01:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef LIBSI_UTIL_H
|
|
|
|
#define LIBSI_UTIL_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2003-12-22 14:18:48 +01:00
|
|
|
#include <sys/types.h>
|
2003-11-22 17:56:45 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define HILO(x) (x##_hi << 8 | x##_lo)
|
2010-11-01 15:47:59 +01:00
|
|
|
#define HILOHILO(x) (x##_hi_hi << 24 | x##_hi_lo << 16 | x##_lo_hi << 8 | x##_lo_lo)
|
2003-11-22 17:56:45 +01:00
|
|
|
#define BCD_TIME_TO_SECONDS(x) ((3600 * ((10*((x##_h & 0xF0)>>4)) + (x##_h & 0xF))) + \
|
|
|
|
(60 * ((10*((x##_m & 0xF0)>>4)) + (x##_m & 0xF))) + \
|
|
|
|
((10*((x##_s & 0xF0)>>4)) + (x##_s & 0xF)))
|
|
|
|
|
|
|
|
namespace SI {
|
|
|
|
|
|
|
|
//Holds an array of unsigned char which is deleted
|
|
|
|
//when the last object pointing to it is deleted.
|
|
|
|
//Optimized for use in libsi.
|
|
|
|
class CharArray {
|
|
|
|
public:
|
|
|
|
CharArray();
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
CharArray(const CharArray &source);
|
|
|
|
CharArray& operator=(const CharArray &source);
|
|
|
|
~CharArray();
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
//can be called exactly once
|
2006-02-18 10:42:55 +01:00
|
|
|
void assign(const unsigned char*data, int size, bool doCopy=true);
|
2003-11-22 17:56:45 +01:00
|
|
|
//compares to a null-terminated string
|
2003-12-13 10:43:26 +01:00
|
|
|
bool operator==(const char *string) const;
|
2003-11-22 17:56:45 +01:00
|
|
|
//compares to another CharArray (data not necessarily null-terminated)
|
|
|
|
bool operator==(const CharArray &other) const;
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
//returns another CharArray with its offset incremented by offset
|
2006-02-18 10:42:55 +01:00
|
|
|
CharArray operator+(const int offset) const;
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
//access and convenience methods
|
|
|
|
const unsigned char* getData() const { return data_->data+off; }
|
|
|
|
const unsigned char* getData(int offset) const { return data_->data+offset+off; }
|
|
|
|
template <typename T> const T* getData() const { return (T*)(data_->data+off); }
|
|
|
|
template <typename T> const T* getData(int offset) const { return (T*)(data_->data+offset+off); }
|
|
|
|
//sets p to point to data+offset, increments offset
|
2006-02-18 10:42:55 +01:00
|
|
|
template <typename T> void setPointerAndOffset(const T* &p, int &offset) const { p=(T*)getData(offset); offset+=sizeof(T); }
|
2012-02-26 14:02:17 +01:00
|
|
|
unsigned char operator[](const int index) const { return data_->data ? data_->data[off+index] : (unsigned char)0; }
|
2003-11-22 17:56:45 +01:00
|
|
|
int getLength() const { return data_->size; }
|
2012-02-26 14:02:17 +01:00
|
|
|
u_int16_t TwoBytes(const int index) const { return data_->data ? data_->TwoBytes(off+index) : u_int16_t(0); }
|
|
|
|
u_int32_t FourBytes(const int index) const { return data_->data ? data_->FourBytes(off+index) : u_int32_t(0); }
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2004-10-16 10:14:19 +02:00
|
|
|
bool isValid() const { return data_->valid; }
|
2006-02-25 10:21:29 +01:00
|
|
|
bool checkSize(int offset) { return (data_->valid && (data_->valid=(offset>=0 && off+offset < data_->size))); }
|
2004-10-16 10:14:19 +02:00
|
|
|
|
2006-02-18 10:42:55 +01:00
|
|
|
void addOffset(int offset) { off+=offset; }
|
2003-11-22 17:56:45 +01:00
|
|
|
private:
|
|
|
|
class Data {
|
|
|
|
public:
|
|
|
|
Data();
|
|
|
|
virtual ~Data();
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2006-02-18 10:42:55 +01:00
|
|
|
virtual void assign(const unsigned char*data, int size) = 0;
|
2003-11-22 17:56:45 +01:00
|
|
|
virtual void Delete() = 0;
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2006-02-18 10:42:55 +01:00
|
|
|
u_int16_t TwoBytes(const int index) const
|
2012-02-26 14:02:17 +01:00
|
|
|
{ return u_int16_t((data[index] << 8) | data[index+1]); }
|
2006-02-18 10:42:55 +01:00
|
|
|
u_int32_t FourBytes(const int index) const
|
2012-02-26 14:02:17 +01:00
|
|
|
{ return u_int32_t((data[index] << 24) | (data[index+1] << 16) | (data[index+2] << 8) | data[index+3]); }
|
2003-11-22 17:56:45 +01:00
|
|
|
/*#ifdef CHARARRAY_THREADSAFE
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
#else
|
|
|
|
void Lock() {}
|
|
|
|
void Unlock() {}
|
|
|
|
#endif
|
|
|
|
Data(const Data& d);
|
2006-02-18 10:42:55 +01:00
|
|
|
void assign(int size);
|
2003-11-22 17:56:45 +01:00
|
|
|
*/
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
const unsigned char*data;
|
2006-02-18 10:42:55 +01:00
|
|
|
int size;
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
// count_ is the number of CharArray objects that point at this
|
|
|
|
// count_ must be initialized to 1 by all constructors
|
|
|
|
// (it starts as 1 since it is pointed to by the CharArray object that created it)
|
2004-10-16 10:14:19 +02:00
|
|
|
unsigned count_;
|
|
|
|
|
|
|
|
bool valid;
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
/*
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pid_t lockingPid;
|
|
|
|
pthread_t locked;
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
class DataOwnData : public Data {
|
|
|
|
public:
|
|
|
|
DataOwnData() {}
|
|
|
|
virtual ~DataOwnData();
|
2006-02-18 10:42:55 +01:00
|
|
|
virtual void assign(const unsigned char*data, int size);
|
2003-11-22 17:56:45 +01:00
|
|
|
virtual void Delete();
|
|
|
|
};
|
|
|
|
class DataForeignData : public Data {
|
|
|
|
public:
|
|
|
|
DataForeignData() {}
|
|
|
|
virtual ~DataForeignData();
|
2006-02-18 10:42:55 +01:00
|
|
|
virtual void assign(const unsigned char*data, int size);
|
2003-11-22 17:56:45 +01:00
|
|
|
virtual void Delete();
|
|
|
|
};
|
|
|
|
Data* data_;
|
2006-02-18 10:42:55 +01:00
|
|
|
int off;
|
2003-11-22 17:56:45 +01:00
|
|
|
};
|
|
|
|
|
2003-12-13 10:43:26 +01:00
|
|
|
|
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
//abstract base class
|
|
|
|
class Parsable {
|
|
|
|
public:
|
|
|
|
void CheckParse();
|
|
|
|
protected:
|
|
|
|
Parsable();
|
|
|
|
virtual ~Parsable() {}
|
|
|
|
//actually parses given data.
|
|
|
|
virtual void Parse() = 0;
|
|
|
|
private:
|
|
|
|
bool parsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
//taken and adapted from libdtv, (c) Rolf Hakenes and VDR, (c) Klaus Schmidinger
|
|
|
|
namespace DVBTime {
|
|
|
|
time_t getTime(unsigned char date_hi, unsigned char date_lo, unsigned char timehr, unsigned char timemi, unsigned char timese);
|
|
|
|
time_t getDuration(unsigned char timehr, unsigned char timemi, unsigned char timese);
|
2012-02-26 14:02:17 +01:00
|
|
|
inline unsigned char bcdToDec(unsigned char b) { return (unsigned char)(((b >> 4) & 0x0F) * 10 + (b & 0x0F)); }
|
2004-10-23 14:24:17 +02:00
|
|
|
}
|
2003-11-22 17:56:45 +01:00
|
|
|
|
|
|
|
//taken and adapted from libdtv, (c) Rolf Hakenes
|
|
|
|
class CRC32 {
|
|
|
|
public:
|
2003-12-22 14:18:48 +01:00
|
|
|
CRC32(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF);
|
2003-11-22 17:56:45 +01:00
|
|
|
bool isValid() { return crc32(data, length, value) == 0; }
|
2003-12-22 14:18:48 +01:00
|
|
|
static bool isValid(const char *d, int len, u_int32_t CRCvalue=0xFFFFFFFF) { return crc32(d, len, CRCvalue) == 0; }
|
2008-08-15 14:49:34 +02:00
|
|
|
static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue);
|
2003-11-22 17:56:45 +01:00
|
|
|
protected:
|
2003-12-22 14:18:48 +01:00
|
|
|
static u_int32_t crc_table[256];
|
2003-12-13 10:43:26 +01:00
|
|
|
|
2003-11-22 17:56:45 +01:00
|
|
|
const char *data;
|
|
|
|
int length;
|
2003-12-22 14:18:48 +01:00
|
|
|
u_int32_t value;
|
2003-11-22 17:56:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} //end of namespace
|
|
|
|
|
|
|
|
#endif
|