IEncryption.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // IEncryption.h: interface for the IEncryption class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_IENCRYPTION_H__7741547B_BA15_4851_A41B_2B4EC1DC12D5__INCLUDED_)
  5. #define AFX_IENCRYPTION_H__7741547B_BA15_4851_A41B_2B4EC1DC12D5__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. // function to be exported from dll to create instance of interface
  10. #ifdef _EXPORTING // declare this in project settings for dll _only_
  11. # define DLL_DECLSPEC __declspec(dllexport)
  12. #else
  13. # define DLL_DECLSPEC __declspec(dllimport)
  14. #endif
  15. #define IENCRYPTION_VERSION 0x0000
  16. class IEncryption;
  17. typedef IEncryption* (*PFNCREATE)(); // function prototype
  18. extern "C" DLL_DECLSPEC IEncryption* CreateEncryptionInterface();
  19. typedef int (*PFNGETVERSION)(); // function prototype
  20. extern "C" DLL_DECLSPEC int GetInterfaceVersion();
  21. // helper method
  22. static IEncryption* CreateEncryptionInterface(const TCHAR* szDllPath)
  23. {
  24. IEncryption* pInterface = NULL;
  25. HMODULE hDll = LoadLibrary(szDllPath);
  26. if (hDll)
  27. {
  28. PFNCREATE pCreate = (PFNCREATE)GetProcAddress(hDll, "CreateEncryptionInterface");
  29. if (pCreate)
  30. {
  31. // check version
  32. PFNGETVERSION pVersion = (PFNGETVERSION)GetProcAddress(hDll, "GetInterfaceVersion");
  33. if (!IENCRYPTION_VERSION || (pVersion && pVersion() >= IENCRYPTION_VERSION))
  34. pInterface = pCreate();
  35. }
  36. }
  37. return pInterface;
  38. }
  39. static BOOL IsEncryptionDll(const TCHAR* szDllPath)
  40. {
  41. HMODULE hDll = LoadLibrary(szDllPath);
  42. if (hDll)
  43. {
  44. PFNCREATE pCreate = (PFNCREATE)GetProcAddress(hDll, "CreateEncryptionInterface");
  45. FreeLibrary(hDll);
  46. return (NULL != pCreate);
  47. }
  48. return FALSE;
  49. }
  50. class IEncryption
  51. {
  52. public:
  53. virtual void Release() = 0; // releases the interface
  54. // returns a dynamically allocated buffer to the encrypted text
  55. // caller responsible for calling FreeBuffer on the returned buffer
  56. virtual bool Encrypt(const unsigned char* pInput, int nLenInput, const char* szPassword,
  57. unsigned char*& pOutput, int& nLenOutput) = 0;
  58. // returns a dynamically allocated buffer to the decrypted text
  59. // caller responsible for calling FreeBuffer on the returned buffer
  60. virtual bool Decrypt(const unsigned char* pInput, int nLenInput, const char* szPassword,
  61. unsigned char*& pOutput, int& nLenOutput) = 0;
  62. // frees a previously returned buffer and sets the ptr to NULL
  63. // eg for buffer allocated with 'new' use 'delete []'
  64. // eg for buffer allocated with 'strdup' use 'free'
  65. virtual void FreeBuffer(unsigned char*& pBuffer) = 0;
  66. };
  67. static void ReleaseEncryptionInterface(IEncryption*& pInterface)
  68. {
  69. if (pInterface)
  70. {
  71. pInterface->Release();
  72. pInterface = NULL;
  73. }
  74. }
  75. #endif // !defined(AFX_IENCRYPTION_H__7741547B_BA15_4851_A41B_2B4EC1DC12D5__INCLUDED_)