//  ME5.C
//  Source code for the C component of Cryptosystem ME6

#include <malloc.h>

#include "stdafx.h"

#include "me5.h"

 //  Entry point for DLL
/*---------------------------------*/
BOOL APIENTRY DllMain(HANDLE hModule, 
                      DWORD  ul_reason_for_call, 
                      LPVOID lpReserved)
{
return ( true );
}

//  Returns 0 if no error, otherwise 1.
/*-------------------------------*/
int _stdcall allocate_buffers(void)
{
buffers_are_allocated = true;
buffer = calloc(BUFFSIZE,1);     //  extbuffsize no longer used

if ( buffer == NULL )
    buffers_are_allocated = false;
else
    {
    buffer1 = calloc(BUFFSIZE,1);
    if ( buffer1 == NULL )
        buffers_are_allocated = false;
    else
        {
        pm_buff = calloc(BUFFSIZE,1);
        if ( pm_buff == NULL )
            buffers_are_allocated = false;
        }
    }

#if COMPRESS
if ( buffers_are_allocated )
    buffers_are_allocated = allocate_compression_space();
#endif

return ( buffers_are_allocated );
}

//  This function also erases data in buffers.
/*----------------------------------*/
void _stdcall deallocate_buffers(void)
{
if ( buffers_are_allocated )
    {
    memset(buffer,0,BUFFSIZE);
    memset(buffer1,0,BUFFSIZE);
    memset(pm_buff,0,BUFFSIZE);
    free(buffer);
    free(buffer1);
    free(pm_buff);

    #if COMPRESS
    free_compression_space();   
    //  This also overwrites the compression buffers with 0.
    #endif

    buffers_are_allocated = false;
    }
}