//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DCL.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFMain *FMain;

//---------------------------------------------------------------------------
__fastcall TFMain::TFMain(TComponent* Owner)
 : TForm(Owner)
{
}
//---------------------------------------------------------------------------
HINSTANCE hinstDLL = 0;
bool ( __stdcall * bpFDeltaCompressionLibInitialization )( unsigned char );
bool ( __stdcall * bpFDeltaCompressionLibUnZip )( void * , unsigned short int *, unsigned char [], unsigned short int * );
bool ( __stdcall * bpFDeltaCompressionLibFinalization )( void );
//---------------------------------------------------------------------------
void __fastcall TFMain::FormCreate( TObject *Sender )
{
 // On charge le librairie en mémoire / We load the library in memory
 if( ( hinstDLL = LoadLibrary( "DeltaCompressionLib.dll" ) ) )
  {
   bpFDeltaCompressionLibInitialization = ( bool ( __stdcall * )( unsigned char ) )GetProcAddress( hinstDLL, "bFDeltaCompressionLibInitialization" );
   bpFDeltaCompressionLibUnZip          = ( bool ( __stdcall * )( void * , unsigned short int *, unsigned char [], unsigned short int * ) )GetProcAddress( hinstDLL, "bFDeltaCompressionLibUnZip" );
   bpFDeltaCompressionLibFinalization   = ( bool ( __stdcall * )( void ) )GetProcAddress( hinstDLL, "bFDeltaCompressionLibFinalization" );
  }
}
//---------------------------------------------------------------------------
void __fastcall TFMain::BPUnZipClick(TObject *Sender)
{
 unsigned char tucInput[ 64 ];
 unsigned short int uiInputSize;
 unsigned short int tuiOutput[ 512 ];
 unsigned short int uiOutputSize;
 char tcView[ 1024 ];

 // Intégrité / Integrity
 if(   ( bpFDeltaCompressionLibInitialization == NULL )
    || ( bpFDeltaCompressionLibUnZip          == NULL )
    || ( bpFDeltaCompressionLibFinalization   == NULL ) )
  {
   MessageDlg( "Les fonctions de la Dll n'ont pas été chargées",
               mtError,
               TMsgDlgButtons() << mbOK,
               0 );
   return;
  }
 // On regarde s'il y a qques chose à décompresser / We look for something to decompress
 if( eInput->Text == "" )
  {
   MessageDlg( "Il n'y a pas de données à décompresser",
               mtWarning,
               TMsgDlgButtons() << mbOK,
               0 );
   return;
  }
 // On ouvre la session en mode 16 bits / We open the session in 16-bit mode
 bpFDeltaCompressionLibInitialization( 16 );
 // On formate
 uiInputSize  = HexToBin( eInput->Text.c_str(), tucInput, eInput->Text.Length() );
 // On indique la taille du buffer disponible pour les données à décompresser
 // We indicate the size of the buffer available for the data to decompress
 // ... et en fonction de la taille du type de donnée
 // ... and depending on the size of the data type
 uiOutputSize = sizeof( tuiOutput ) / sizeof( unsigned short int );
 // On appelle la fonction de décompression / We call the decompression function
 bpFDeltaCompressionLibUnZip( tuiOutput, &uiOutputSize, tucInput, &uiInputSize );
 // On ferme la session / We close the session
 bpFDeltaCompressionLibFinalization;
 // On convertit en qque chose de visualisable / We convert to something visualizable
 BinToHex( ( char * )tuiOutput, tcView, uiOutputSize * sizeof( unsigned short int ) );
 // On affiche le résultat / We display the result
 eOutput->Text = tcView;
}
//---------------------------------------------------------------------------
void __fastcall TFMain::FormClose(TObject *Sender, TCloseAction &Action)
{
 if( hinstDLL ) FreeLibrary( hinstDLL );
}
//---------------------------------------------------------------------------

