#include <windows.h>

#define REG_OFFSET		0x80000
#define REG_SIZE_LIMIT	0x40000

DWORD RegLegth=0;

TCHAR Flash[]   = (TEXT("FSH1:"));
TCHAR FlashFx[] = (TEXT("FFX1:"));

char fLoadReg[]="\\Temp\\adsload.reg";
TCHAR fLoadRegW[]=(TEXT("\\Temp\\adsload.reg"));

extern char fxLoadReg[];	// "\\Temp\\load.reg";
extern TCHAR fxLoadRegW[];	//(TEXT("\\Temp\\load.reg"));

BOOL ReadAdsLoadRegistry(void);
BOOL ReadAdsLoadRegistry1(void);
BOOL ReadAdsLoadRegistry2(void);

//
// Write mofified registry file to Flash
//

BOOL WriteADSLOADRegistry(void)
{
	int i=0;

	HANDLE hFSH;
	HANDLE hFFX;
	DWORD temp=0;
	DWORD FileSize=0,fSize;
	DWORD dwBytesRead=0;
	char  RegData[256];
	DWORD dwBytesWritten=0;
	DWORD NewHeaderSize=0;

#ifdef DEV_STATUS	
	TCHAR TempNameW[256];
#endif


	DEBUGMSG(1,(L" WriteAdsLoadRegistry : Entry \r\n"));

	hFFX = CreateFile (fxLoadRegW,				// Port Name (Unicode compatible)
				GENERIC_READ,					// Open for Read
           		0,								// not shared
           		NULL,							// Always NULL for Windows CE
           		OPEN_EXISTING ,					// For communication resource
           		0,								// Non-overlapped operation only
           		NULL);							// Always NULL for Windows CE


	if (hFFX == INVALID_HANDLE_VALUE) 
	{
		RETAILMSG(1,(L" Could not open FILE %s.\r\n",fLoadRegW));
		return FALSE;
	} 

	fSize = GetFileSize( hFFX, NULL); 

	DEBUGMSG(1,(L" WriteAdsLoadRegistry : fSize=0x%x.\r\n",fSize));
	
	hFSH = CreateFile (Flash,		// Port Name (Unicode compatible)
			GENERIC_WRITE ,			// Open for Read
            0,						// not shared
            NULL,					// Always NULL for Windows CE
            OPEN_EXISTING ,			// For communication resource
            0,						// Non-overlapped operation only
            NULL);					// Always NULL for Windows CE

	if (hFSH == INVALID_HANDLE_VALUE) 
	{
		RETAILMSG(1,(L" Could not open FLASH Driver.\r\n"));
		return FALSE;
	} 


	
	temp= SetFilePointer(hFSH, (REG_OFFSET), NULL, FILE_BEGIN);

	WriteFile(	hFSH,				// where to write to
				&fSize,		// what to write
				4,					// number of bytes to be written to port
				&dwBytesWritten,	// number of bytes that were actually written
				NULL);				// overlapped I/O not supported

	FileSize = fSize;
	while( FileSize > 0 )
	{
		if (FileSize < 256)
		{
			ReadFile(hFFX,				// where to read from 
				 	RegData,			// where to put the character
				 	FileSize,			// number of bytes to read
				 	&dwBytesRead,		// how many bytes were actually read
			 		NULL);				// overlapped I/O not supported

			FileSize -= dwBytesRead;
#ifdef DEV_STATUS
			atow(RegData,TempNameW,dwBytesRead);
			RETAILMSG(1,(L"%s\r\n", TempNameW));
#endif
			WriteFile(hFSH,				// where to write to
					RegData,			// what to write
					dwBytesRead,		// number of bytes to be written to port
					&dwBytesWritten,	// number of bytes that were actually written
					NULL);				// overlapped I/O not supported

		}
		else
		{
			ReadFile(hFFX,				// where to read from 
				 	RegData,			// where to put the character
				 	256,				// number of bytes to read
				 	&dwBytesRead,		// how many bytes were actually read
				 	NULL);				// overlapped I/O not supported

			FileSize -= dwBytesRead;
#ifdef DEV_STATUS
			atow(RegData,TempNameW,dwBytesRead);
			RETAILMSG(1,(L"%s\r\n", TempNameW));
#endif

			WriteFile(hFSH,				// where to write to
					RegData,			// what to write
					dwBytesRead,		// number of bytes to be written to port
					&dwBytesWritten,	// number of bytes that were actually written
					NULL);				// overlapped I/O not supported
		}
		
		DEBUGMSG(1,(L" FileSize= %d  \r\n",FileSize));
	}
	
	CloseHandle(hFSH);
	CloseHandle(hFFX);
	
	DEBUGMSG(1,(L" WriteAdsLoadRegistry : End \r\n"));

	return TRUE;
}




BOOL ReadAdsLoadRegistry1()
{
	int i=0;
	HANDLE hFSH;
	HANDLE hFFX;
	DWORD temp;
	DWORD FileSize,ReadFileSize;
	DWORD dwBytesRead,dwBytesWritten;
	char  RegData[256];

	//TCHAR fLoadRegW[256];

	//mbstowcs( fLoadRegW, fLoadReg, 256 );

	DEBUGMSG(1,(L" ReadAdsLoadRegistry: +Entry.\r\n"));
	
	 hFSH = CreateFile(	Flash,					// Port Name (Unicode compatible)
						GENERIC_READ,			// Open for Read
            			FILE_SHARE_READ,		// not shared
            			NULL,					// Always NULL for Windows CE
            			OPEN_EXISTING,			// For communication resource
            			0,						// Non-overlapped operation only
            			NULL);					// Always NULL for Windows CE

	if (hFSH == INVALID_HANDLE_VALUE) 
	{
		RETAILMSG(1,(L" ReadAdsLoadRegistry: Could not open FLASH Driver.\r\n"));
		return FALSE;
	} 

	temp= SetFilePointer(hFSH, (REG_OFFSET), NULL, FILE_BEGIN);

	ReadFile(hFSH,					// where to read from 
			 &FileSize,				// where to put the character
			 4,						// number of bytes to read
			 &dwBytesRead,			// how many bytes were actually read
			 NULL);					// overlapped I/O not supported


	DEBUGMSG(1,(L" ADSLOAD.REG Size = 0x%x(%d).\r\n",FileSize,FileSize));

	if ((FileSize == 0) || (FileSize >= REG_SIZE_LIMIT))
	{
		CloseHandle(hFSH);
		return 0;
	}

	hFFX = CreateFile (	fLoadRegW,				// Port Name (Unicode compatible)
						GENERIC_WRITE ,			// Open for Read
						FILE_SHARE_WRITE,		// not shared
            			NULL,					// Always NULL for Windows CE
	        			CREATE_ALWAYS,			// For communication resource
		    			0,						// Non-overlapped operation only
						NULL);					// Always NULL for Windows CE

	if (hFFX == INVALID_HANDLE_VALUE) 
	{
		CloseHandle(hFSH);
		RETAILMSG(1,(L" ReadAdsLoadRegistry: Could not open File %s.\r\n",fLoadRegW));
		return FALSE;
	} 
	

	while(FileSize > 0)
	{
		if (FileSize >= 256)
		{
			ReadFileSize = 256;
		}
		else
		{
			ReadFileSize = FileSize;
		}

		ReadFile(hFSH,					// where to read from 
			 	RegData,				// where to put the character
			 	ReadFileSize,			// number of bytes to read
			 	&dwBytesRead,			// how many bytes were actually read
		 		NULL);					// overlapped I/O not supported


		if(dwBytesRead == 0 )
		{
			break;
		}
		else
		{
			FileSize -= dwBytesRead;

			WriteFile(	hFFX,				// where to write to
						RegData,			// what to write
						dwBytesRead,		// number of bytes to be written to port
						&dwBytesWritten,	// number of bytes that were actually written
						NULL);				// overlapped I/O not supported
		}


	}

	CloseHandle(hFSH);
	CloseHandle(hFFX);

	return TRUE;
}

