#ifndef _KVI_CRYPT_H_
#define _KVI_CRYPT_H_

//
//   File : kvi_crypt.h
//   Creation date : Fri Nov 03 2000 01:45:21 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
//
//   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 opinion) any later version.
//
//   This program 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.
//

#include "kvi_settings.h"


//
// Base class for all IRC crypt engines
// These intend to encrypt plain text into something
// that can be sent through the IRC protocol...
// so it should not contain NULL, CR , LF and other
// similar stuff...
//

#ifdef COMPILE_CRYPT_SUPPORT

	#include "kvi_string.h"
	#include <qasciidict.h>
	#include <qobject.h>
	//#include <qguardedptr.h>

	class KviCryptEngine;

	typedef KviCryptEngine * (*crypt_engine_allocator_func)();
	typedef void (*crypt_engine_deallocator_func)(KviCryptEngine *);

	class KVILIB_API KviCryptEngine : public QObject
	{
		friend class KviCryptEngineManager;
		Q_OBJECT
	public:
		KviCryptEngine();
		virtual ~KviCryptEngine();
	private:
		crypt_engine_deallocator_func m_deallocFunc;    // this is accessed by KviCryptEngineManager only
		KviStr                        m_szLastError;
		int                           m_iMaxEncryptLen;
	public:
		// Returns false if the engine is a "text transformation" engine only
		// and produces human readable text: KVIrc will avoid to use the escape character
		// for this.
		virtual bool isCryptographicEngine(){ return true; };
		void setMaxEncryptLen(int m){ m_iMaxEncryptLen = m; };
		int maxEncryptLen(){ return m_iMaxEncryptLen; };
		virtual bool init(const char * encKey,int encKeyLen,const char * decKey,int decKeyLen);
		//
		// Encrypts plainText and returns the encrypted
		// data in outBuffer. The encrypted data must be
		// suitable for sending thru an IRC (eventually DCC
		// that is less restrictive) connection: so
		// no NULL, CR and LF in the output.
		// 0x01 should be also avoided since
		// it is the CTCP delimiter.
		// Converting the result in a HEX string
		// is a good trick...also Base64 could be used.
		// Should return false in case of an error.
		// Theoretically we could allow NULLs in plainText
		// but this is not the case of KVIrc.
		//
		virtual bool encrypt(const char * plainText,KviStr &outBuffer);
		//
		// Decrypts the data in inBuffer and puts the decrypted
		// stuff in plainText. inBuffer is the thingie
		// that we got from outBuffer of encrupt() so it
		// follows the same rules.
		// Should return false in case of error.
		//
		virtual bool decrypt(const char * inBuffer,KviStr &plainText);
		//
		// Returns the string containing the description
		// of the last error or an empty string if there
		// was no error after the last init() call.
		//
		const char * lastError(){ return m_szLastError.ptr(); };
	protected:
		//
		// The following two should have clear meaning
		//
		void clearLastError(){ m_szLastError = ""; };
		void setLastError(const char * err){ m_szLastError = err; };
	};

	#define KVI_CRYPTENGINE_CAN_ENCRYPT 1
	#define KVI_CRYPTENGINE_CAN_DECRYPT 2
	#define KVI_CRYPTENGINE_WANT_ENCRYPT_KEY 4
	#define KVI_CRYPTENGINE_WANT_DECRYPT_KEY 8

	class KVILIB_API KviCryptEngineDescription
	{
	public:
		KviCryptEngineDescription(){};
		virtual ~KviCryptEngineDescription(){};
	public:
		KviStr                        szName;           // engine name
		KviStr                        szDescription;    // details
		KviStr                        szAuthor;         // algorithm author
		int                           iFlags;           // properties
		crypt_engine_allocator_func   allocFunc;        // engine allocator
		crypt_engine_deallocator_func deallocFunc;      // deallocation function (if called from outside the origin module)
		void *                        providerHandle;   // used to identify the provider module
#ifdef COMPILE_ON_WINDOWS
	public:
		// On windows we need to override new and delete operators
		// to ensure that always the right new/delete pair is called for an object instance
		// This bug is present in all the classes exported by a module that
		// can be instantiated/destroyed from external modules.
		// (this is a well known bug described in Q122675 of MSDN)
		void       * operator new(size_t tSize);
		void         operator delete(void * p);
#endif
	};


	class KVILIB_API KviCryptEngineManager
	{
	public:
		KviCryptEngineManager();
		virtual ~KviCryptEngineManager();
	private:
		QAsciiDict<KviCryptEngineDescription> * m_pEngineDict;
	public:
		const QAsciiDict<KviCryptEngineDescription> * engineDict(){ return m_pEngineDict; };
		void registerEngine(KviCryptEngineDescription * d);
		void unregisterEngine(const char * szName);
		void unregisterEngines(void * providerHandle);
		//
		// Allocates a crypt engine
		// Please note that the engine may be deleted from outside
		// so you'd better connect the "destroyed" signal
		//
		KviCryptEngine * allocateEngine(const char * szName);
		void deallocateEngine(KviCryptEngine * e);
	};


#endif //COMPILE_CRYPT_SUPPORT


#endif //!_KVI_CRYPT_H_
