/*
 * Softcam plugin to VDR (C++)
 *
 * This code 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.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#ifndef ___MISC_H
#define ___MISC_H

#include <stdio.h>

// ----------------------------------------------------------------

const char *HexStr(char *str, const unsigned char *mem, int len);
const char *HexStrSpaced(char *str, const unsigned char *mem, int len);
int GetHex(const char * &line, unsigned char *store, int count, bool fixedLen=true);
int GetChar(const char * &line, int *store, int count);
unsigned long long Bin2LongLong(unsigned char *mem, int len);
#define Bin2Int(mem,len)	((int)Bin2LongLong((mem),(len)))

bool CheckNull(const unsigned char *data, int len);
unsigned char XorSum(const unsigned char *mem, int len);
void RotateBytes(unsigned char *in, int n);
void RotateBytes(unsigned char *out, const unsigned char *in, int n);
unsigned int crc32_le(unsigned int crc, unsigned char const *p, int len);

void LogText(const char *head, const char *format, ...);
#define LogError(a, ...)	LogText("ERROR",a,__VA_ARGS__)
#define LogWarn(a, ...)	LogText("WARNING",a,__VA_ARGS__)
#define LogInfo(a, ...)	LogText("\nINFO",a,__VA_ARGS__)

void HexDump(const unsigned char *buffer, int n, bool hideMess = false);

// ----------------------------------------------------------------

#include <openssl/bn.h>

class cBN {
private:
	BIGNUM *big;
public:
	cBN(void) { big=BN_new(); }
	~cBN() { BN_free(big); }
	operator BIGNUM* () { return big; }
  bool Get(const unsigned char *in, int n);
  bool GetLE(const unsigned char *in, int n);
  int Put(unsigned char *out, int n) const;
  int PutLE(unsigned char *out, int n) const;
};

class cBNctx {
private:
	BN_CTX *ctx;
public:
	cBNctx(void) { ctx=BN_CTX_new(); }
	~cBNctx() { BN_CTX_free(ctx); }
	operator BN_CTX* () { return ctx; }
};

// ----------------------------------------------------------------

class cSimpleListBase;

class cSimpleItem {
friend class cSimpleListBase;
private:
	cSimpleItem *next;
public:
	virtual ~cSimpleItem() {}
	cSimpleItem *Next(void) const { return next; }
};

class cSimpleListBase {
protected:
	cSimpleItem *first, *last;
	int count;
public:
	cSimpleListBase(void);
	~cSimpleListBase();
	void Add(cSimpleItem *Item, cSimpleItem *After=0);
	void Ins(cSimpleItem *Item);
	void Del(cSimpleItem *Item, bool Del=true);
	void Clear(void);
	int Count(void) const { return count; }
};

template<class T> class cSimpleList : public cSimpleListBase {
public:
	T *First(void) const { return (T *)first; }
	T *Last(void) const { return (T *)last; }
	T *Next(const T *item) const { return (T *)item->cSimpleItem::Next(); }
};

#endif //___MISC_H