#ifndef __SNAFILE_H__
#define __SNAFILE_H__
 
#include <windows.h>
 
#include "ACP_Base.h"
#include "PTC.h"
 
#ifdef  __cplusplus
extern "C" {
#endif

/* Constants for SNA_File options */
#define SNA_C_ucUseChecksum             1
#define SNA_C_ucUseEncryption           2

/* Constants for SNA_File open mode */
#define SNA_C_ucWrite					1
#define SNA_C_ucRead					2
/*////////////////////////////////////////////////////////*/
#define SNA_M_CREATEFILE		PTC_CreateFile
#define SNA_M_WRITEFILE			PTC_WriteFile
#define SNA_M_READFILE			PTC_ReadFile
#define SNA_M_CLOSEHANDLE		PTC_CloseHandle
#define SNA_M_SETFILEPOINTER	PTC_SetFilePointer
#define SNA_M_FLUSHFILEBUFFERS	PTC_FlushFileBuffers
/*////////////////////////////////////////////////////////	*/
 
typedef ACP_tdxBool (*tdp_bfnWriteFunction)( const void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
typedef ACP_tdxBool (*tdp_bfnReadFunction)( void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
 
typedef struct SNA_tdstFile_
{
	HANDLE hFile;
	BOOL bReadSuccess;
	char a4_cChecksumBuffer[4];              /* buffer for 32 bit CRC.*/
	unsigned char ucBytesInChecksumBuffer ;
	unsigned long ulChecksum;
	unsigned long ulCryptKey;
 
	unsigned char ucOptions;                 /* options for file (checksums, buffer, encryption...)*/
 
	tdp_bfnWriteFunction p_fn_bDefaultWrite;
	tdp_bfnWriteFunction p_fn_bWriteWithoutOptions;
 
	tdp_bfnReadFunction p_fn_bDefaultRead;
	tdp_bfnReadFunction p_fn_bReadWithoutOptions;
 
} SNA_tdstFile;
 
 
/* Generic functions.*/
void SNA_fn_vInitFile(unsigned char ucOptions, struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bFOpen( char *_p_szfilename, unsigned char ucMode, unsigned char ucOptions, struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bFClose( struct SNA_tdstFile_ *_p_stFile );
size_t SNA_fn_ulFWrite( void *_p_vBuffer, size_t _ulSize, size_t _ulCount, struct SNA_tdstFile_ *_p_stFile );
size_t SNA_fn_ulFRead( void *_p_vBuffer, size_t _ulSize, size_t _ulCount, struct SNA_tdstFile_ *_p_stFile );
size_t SNA_fn_ulFWriteWithoutOptions( void *_p_vBuffer, size_t _ulSize, size_t _ulCount, struct SNA_tdstFile_ *_p_stFile );
size_t SNA_fn_ulFReadWithoutOptions( void *_p_vBuffer, size_t _ulSize, size_t _ulCount, struct SNA_tdstFile_ *_p_stFile );
int SNA_fn_bFseek( struct SNA_tdstFile_ *_p_stFile, long _lOffset, int _lOrigin );
 
/* Functions specific to checksums.*/
ACP_tdxBool SNA_fn_bFileUseChecksum( struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bTestChecksum( struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bWriteChecksum( struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bReadChecksum( struct SNA_tdstFile_ *_p_stFile );
 
/* Functions specific to encryption*/
ACP_tdxBool SNA_fn_bFileUseEncryption( struct SNA_tdstFile_ *_p_stFile );
void SNA_fn_vSetCryptKey( unsigned char _ulCryptKey, struct SNA_tdstFile_ *_p_stFile );
 
/* Functions specific to buffer*/
int SNA_fn_iFFlush( struct SNA_tdstFile_ *_p_stFile );
 
/* Private functions*/
ACP_tdxBool SNA_fn_bCryptAndWriteToFile( const void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bWriteToFile( const void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bReadAndDecryptFromFile( void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
ACP_tdxBool SNA_fn_bReadFromFile( void *_p_vData, unsigned long _ulSize, struct SNA_tdstFile_ *_p_stFile );
 
#ifdef  __cplusplus
}
#endif
 
 
#endif /* __SNAFILE_H__ */
