/* Protect game plugin against evil memory clear hacks. */
#include "plugin.h"
#include "pluginprotector.h"
#include "debugprintf.h"

#ifdef HIGH_PLUGIN
#define GAME_PLUGIN_BASE HIGH_PLUGIN_BASE
#define GAME_PLUGIN_SIZE HIGH_PLUGIN_SIZE
#endif
#ifdef LOW_PLUGIN
#define GAME_PLUGIN_BASE LOW_PLUGIN_BASE
#define GAME_PLUGIN_SIZE LOW_PLUGIN_SIZE
#endif

void memset_replacement1(void *addr, int c, unsigned long size)
{
	unsigned long val;

#ifdef RELOAD_SUPPORT
	jmpNr = 0;
#endif

	//debug_printf("memset addr %x, %x, %x, caller %x\n", addr, c, size, __builtin_return_address(0));

	val = (unsigned long) addr;
	// Check if plugin would be overwritten.
	if (!((val >= ((unsigned long) GAME_PLUGIN_BASE))
		&& (val < (((unsigned long) GAME_PLUGIN_BASE) + GAME_PLUGIN_SIZE)))) {
		memset_orig1(addr, c, size);
	}
}

#ifdef RELOAD_SUPPORT
void memset_replacement2(void *addr, int c, unsigned long size)
{
	unsigned long val;

	jmpNr = 1;

	//debug_printf("memset addr %x, %x, %x, caller %x\n", addr, c, size, __builtin_return_address(0));

	val = (unsigned long) addr;
	// Check if plugin would be overwritten.
	if (!((val >= ((unsigned long) GAME_PLUGIN_BASE))
		&& (val < (((unsigned long) GAME_PLUGIN_BASE) + GAME_PLUGIN_SIZE)))) {
		memset_orig2(addr, c, size);
	}
}
#endif

