// BIOSChecksumGenerator.cpp : Defines the initialization routines for the DLL.
//
// created by Unwinder
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <afxdllx.h>

#include "BIOSChecksumGenerator.h"
/////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
static AFX_EXTENSION_MODULE BIOSChecksumGeneratorDLL = { NULL, NULL };
/////////////////////////////////////////////////////////////////////////////
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		if (!AfxInitExtensionModule(BIOSChecksumGeneratorDLL, hInstance))
			return 0;

		new CDynLinkLibrary(BIOSChecksumGeneratorDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		AfxTermExtensionModule(BIOSChecksumGeneratorDLL);
	}
	return 1;
}
/////////////////////////////////////////////////////////////////////////////
// This exported function is called by RivaTuner's patch script engine to
// process data pre/post-processing
/////////////////////////////////////////////////////////////////////////////
BIOSCHECKSUMGENERATOR_API BOOL PlugInEntry(LPBYTE lpData, DWORD dwSize, LPCSTR lpFilename, LPCSTR lpVersion, CString& strLog, DWORD dwUserData)
{
	char czFname[_MAX_FNAME];
	char czExt  [_MAX_EXT  ];
	_splitpath(lpFilename, NULL, NULL, czFname, czExt);

	//log plugin activity in the patch script engine's report

	CString strBuf; 
	strBuf.Format("Generating BIOS checksum for %s%s... \n", czFname, czExt);
	strLog += strBuf;

	//calculate native BIOS checksum (8-bit inverted sum of all bytes)

	BYTE checksum = 0;

	for (DWORD index=0; index < dwSize - 1; index++)
		checksum += lpData[index];

	lpData[index] = -checksum;

	return TRUE;
}
/////////////////////////////////////////////////////////////////////////////