/*
 * 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 ___SYSTEM_COMMON_H
#define ___SYSTEM_COMMON_H

#ifdef OPENSSL
#include <openssl/bn.h>
#endif
#include "data.h"

#define MBC(a,b)    (((a)<<8)+(b))
#define ADDC3(ab,c) ((ab)+((c)<<16))
#define MBC3(a,b,c) ADDC3(MBC((a),(b)),(c))
#define C2(x)       (((x)>>8)&0xFF)
#define C3(x)       (((x)>>16)&0xFF)
#define C2MASK      0xFFFF

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

class cHexKey : public cPlainKey {
private:
  int keylen;
  void *key;
protected:
  virtual bool SetKey(void *Key, int Keylen);
  virtual char *Print(void);
  virtual bool Cmp(void *Key, int Keylen);
  virtual void Get(void *mem);
  virtual int Size(void) { return keylen; }
public:
  cHexKey(bool Super);
  virtual ~cHexKey();
  virtual bool Parse(char *line) { return false; }
  virtual bool Save(FILE *f) { return false; }
  };

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

#ifdef OPENSSL

class cBNKey : public cPlainKey {
private:
  int keylen;
  void *key;
protected:
  virtual bool SetKey(void *Key, int Keylen);
  virtual char *Print(void);
  virtual bool Cmp(void *Key, int Keylen);
  virtual void Get(void *mem);
  virtual int Size(void) { return keylen; }
  virtual void Rotate(void *mem, int len) {};
public:
  cBNKey(bool Super);
  virtual ~cBNKey();
  virtual bool Parse(char *line) { return false; }
  virtual bool Save(FILE *f) { return false; }
  };

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

class cBNRKey : public cBNKey {
protected:
  virtual void Rotate(void *mem, int len);
public:
  cBNRKey(bool Super);
  };

void RotateBytes(unsigned char *in, int n);

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

class cDualKey : public cPlainKey {
private:
  bool rotate;
protected:
  bool keyIsBN;
  cPlainKey *real;
  //
  virtual bool SetKey(void *Key, int Keylen);
  virtual char *Print(void);
  virtual bool Cmp(void *Key, int Keylen);
  virtual void Get(void *mem);
  virtual int Size(void);
  virtual void SetIsBNKey(void)=0;
public:
  cDualKey(bool Super, bool Rotate);
  virtual ~cDualKey();
  };

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

class cBN {
private:
  BIGNUM *big;
public:
  cBN::cBN(void) { big=BN_new(); }
  cBN::~cBN() { BN_free(big); }
  operator BIGNUM* () { return big; }
};

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

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

#endif

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

class cPlainKeyStd : public cHexKey {
public:
  cPlainKeyStd(bool Super);
  virtual bool Parse(char *line);
  virtual bool Save(FILE *f);
  };

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

class cPlainKeyDummy : public cPlainKey {
private:
  virtual bool SetKey(void *Key, int Keylen) { return false; }
  virtual char *Print(void) { return 0; }
  virtual bool Cmp(void *Key, int Keylen) { return false; }
  virtual void Get(void *mem) {}
  virtual int Size(void) { return 0; }
public:
  cPlainKeyDummy(bool Super):cPlainKey(Super) {}
  virtual bool Parse(char *line) { return false; }
  virtual bool Save(FILE *f) { return false; }
  };

#endif //___SYSTEM_COMMON_H
