diff src/common/CheatSearch.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/common/CheatSearch.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,371 @@
     1.4 +#include <cstdlib>
     1.5 +#include <cstring>
     1.6 +
     1.7 +#include "CheatSearch.h"
     1.8 +
     1.9 +CheatSearchBlock cheatSearchBlocks[4];
    1.10 +
    1.11 +CheatSearchData cheatSearchData = {
    1.12 +	0,
    1.13 +	cheatSearchBlocks
    1.14 +};
    1.15 +
    1.16 +static bool cheatSearchEQ(u32 a, u32 b)
    1.17 +{
    1.18 +	return a == b;
    1.19 +}
    1.20 +
    1.21 +static bool cheatSearchNE(u32 a, u32 b)
    1.22 +{
    1.23 +	return a != b;
    1.24 +}
    1.25 +
    1.26 +static bool cheatSearchLT(u32 a, u32 b)
    1.27 +{
    1.28 +	return a < b;
    1.29 +}
    1.30 +
    1.31 +static bool cheatSearchLE(u32 a, u32 b)
    1.32 +{
    1.33 +	return a <= b;
    1.34 +}
    1.35 +
    1.36 +static bool cheatSearchGT(u32 a, u32 b)
    1.37 +{
    1.38 +	return a > b;
    1.39 +}
    1.40 +
    1.41 +static bool cheatSearchGE(u32 a, u32 b)
    1.42 +{
    1.43 +	return a >= b;
    1.44 +}
    1.45 +
    1.46 +static bool cheatSearchSignedEQ(s32 a, s32 b)
    1.47 +{
    1.48 +	return a == b;
    1.49 +}
    1.50 +
    1.51 +static bool cheatSearchSignedNE(s32 a, s32 b)
    1.52 +{
    1.53 +	return a != b;
    1.54 +}
    1.55 +
    1.56 +static bool cheatSearchSignedLT(s32 a, s32 b)
    1.57 +{
    1.58 +	return a < b;
    1.59 +}
    1.60 +
    1.61 +static bool cheatSearchSignedLE(s32 a, s32 b)
    1.62 +{
    1.63 +	return a <= b;
    1.64 +}
    1.65 +
    1.66 +static bool cheatSearchSignedGT(s32 a, s32 b)
    1.67 +{
    1.68 +	return a > b;
    1.69 +}
    1.70 +
    1.71 +static bool cheatSearchSignedGE(s32 a, s32 b)
    1.72 +{
    1.73 +	return a >= b;
    1.74 +}
    1.75 +
    1.76 +static bool (*cheatSearchFunc[])(u32, u32) = {
    1.77 +	cheatSearchEQ,
    1.78 +	cheatSearchNE,
    1.79 +	cheatSearchLT,
    1.80 +	cheatSearchLE,
    1.81 +	cheatSearchGT,
    1.82 +	cheatSearchGE
    1.83 +};
    1.84 +
    1.85 +static bool (*cheatSearchSignedFunc[])(s32, s32) = {
    1.86 +	cheatSearchSignedEQ,
    1.87 +	cheatSearchSignedNE,
    1.88 +	cheatSearchSignedLT,
    1.89 +	cheatSearchSignedLE,
    1.90 +	cheatSearchSignedGT,
    1.91 +	cheatSearchSignedGE
    1.92 +};
    1.93 +
    1.94 +void cheatSearchSetSavedAndBits(CheatSearchBlock *block)
    1.95 +{
    1.96 +	if (!block->saved)
    1.97 +	{
    1.98 +		block->saved = (u8 *)malloc(block->size);
    1.99 +		memcpy(block->saved, block->data, block->size);
   1.100 +	}
   1.101 +	if (!block->bits)
   1.102 +	{
   1.103 +		block->bits  = (u8 *)malloc(block->size >> 3);
   1.104 +		memset(block->bits, 0xff, block->size >> 3);
   1.105 +	}
   1.106 +}
   1.107 +
   1.108 +void cheatSearchZeroBlock(CheatSearchBlock *block)
   1.109 +{
   1.110 +	block->data	  = 0;
   1.111 +	block->offset = 0;
   1.112 +	block->size	  = 0;
   1.113 +	free(block->saved);
   1.114 +	free(block->bits);
   1.115 +	block->saved  = 0;
   1.116 +	block->bits	  = 0;
   1.117 +}
   1.118 +
   1.119 +void cheatSearchCleanup(CheatSearchData *cs)
   1.120 +{
   1.121 +	int count = cs->count;
   1.122 +
   1.123 +	for (int i = 0; i < count; i++)
   1.124 +	{
   1.125 +		CheatSearchBlock &block = cs->blocks[i];
   1.126 +		free(block.saved);
   1.127 +		free(block.bits);
   1.128 +		block.saved = 0;
   1.129 +		block.bits  = 0;
   1.130 +	}
   1.131 +	cs->count = 0;
   1.132 +}
   1.133 +
   1.134 +void cheatSearchStart(const CheatSearchData *cs)
   1.135 +{
   1.136 +	int count = cs->count;
   1.137 +
   1.138 +	for (int i = 0; i < count; i++)
   1.139 +	{
   1.140 +		CheatSearchBlock *block = &cs->blocks[i];
   1.141 +
   1.142 +		memset(block->bits, 0xff, block->size >> 3);
   1.143 +		memcpy(block->saved, block->data, block->size);
   1.144 +	}
   1.145 +}
   1.146 +
   1.147 +s32 cheatSearchSignedRead(u8 *data, int off, int size)
   1.148 +{
   1.149 +	u32 res = data[off++];
   1.150 +
   1.151 +	switch (size)
   1.152 +	{
   1.153 +	case BITS_8:
   1.154 +		res <<= 24;
   1.155 +		return ((s32)res) >> 24;
   1.156 +	case BITS_16:
   1.157 +		res  |= ((u32)data[off++])<<8;
   1.158 +		res <<= 16;
   1.159 +		return ((s32)res) >> 16;
   1.160 +	case BITS_32:
   1.161 +		res |= ((u32)data[off++])<<8;
   1.162 +		res |= ((u32)data[off++])<<16;
   1.163 +		res |= ((u32)data[off++])<<24;
   1.164 +		return (s32)res;
   1.165 +	}
   1.166 +	return (s32)res;
   1.167 +}
   1.168 +
   1.169 +u32 cheatSearchRead(u8 *data, int off, int size)
   1.170 +{
   1.171 +	u32 res = data[off++];
   1.172 +	if (size == BITS_16)
   1.173 +		res |= ((u32)data[off++])<<8;
   1.174 +	else if (size == BITS_32)
   1.175 +	{
   1.176 +		res |= ((u32)data[off++])<<8;
   1.177 +		res |= ((u32)data[off++])<<16;
   1.178 +		res |= ((u32)data[off++])<<24;
   1.179 +	}
   1.180 +	return res;
   1.181 +}
   1.182 +
   1.183 +void cheatSearch(const CheatSearchData *cs, int compare, int size,
   1.184 +                 bool isSigned)
   1.185 +{
   1.186 +	if (compare < 0 || compare > SEARCH_GE)
   1.187 +		return;
   1.188 +	int inc = 1;
   1.189 +	if (size == BITS_16)
   1.190 +		inc = 2;
   1.191 +	else if (size == BITS_32)
   1.192 +		inc = 4;
   1.193 +
   1.194 +	if (isSigned)
   1.195 +	{
   1.196 +		bool (*func)(s32, s32) = cheatSearchSignedFunc[compare];
   1.197 +
   1.198 +		for (int i = 0; i < cs->count; i++)
   1.199 +		{
   1.200 +			CheatSearchBlock *block = &cs->blocks[i];
   1.201 +			int size2 = block->size;
   1.202 +			u8 *bits  = block->bits;
   1.203 +			u8 *data  = block->data;
   1.204 +			u8 *saved = block->saved;
   1.205 +
   1.206 +			for (int j = 0; j < size2; j += inc)
   1.207 +			{
   1.208 +				if (IS_BIT_SET(bits, j))
   1.209 +				{
   1.210 +					s32 a = cheatSearchSignedRead(data, j, size);
   1.211 +					s32 b = cheatSearchSignedRead(saved, j, size);
   1.212 +
   1.213 +					if (!func(a, b))
   1.214 +					{
   1.215 +						CLEAR_BIT(bits, j);
   1.216 +						if (size == BITS_16)
   1.217 +							CLEAR_BIT(bits, j+1);
   1.218 +						if (size == BITS_32)
   1.219 +						{
   1.220 +							CLEAR_BIT(bits, j+2);
   1.221 +							CLEAR_BIT(bits, j+3);
   1.222 +						}
   1.223 +					}
   1.224 +				}
   1.225 +			}
   1.226 +		}
   1.227 +	}
   1.228 +	else
   1.229 +	{
   1.230 +		bool (*func)(u32, u32) = cheatSearchFunc[compare];
   1.231 +
   1.232 +		for (int i = 0; i < cs->count; i++)
   1.233 +		{
   1.234 +			CheatSearchBlock *block = &cs->blocks[i];
   1.235 +			int size2 = block->size;
   1.236 +			u8 *bits  = block->bits;
   1.237 +			u8 *data  = block->data;
   1.238 +			u8 *saved = block->saved;
   1.239 +
   1.240 +			for (int j = 0; j < size2; j += inc)
   1.241 +			{
   1.242 +				if (IS_BIT_SET(bits, j))
   1.243 +				{
   1.244 +					u32 a = cheatSearchRead(data, j, size);
   1.245 +					u32 b = cheatSearchRead(saved, j, size);
   1.246 +
   1.247 +					if (!func(a, b))
   1.248 +					{
   1.249 +						CLEAR_BIT(bits, j);
   1.250 +						if (size == BITS_16)
   1.251 +							CLEAR_BIT(bits, j+1);
   1.252 +						if (size == BITS_32)
   1.253 +						{
   1.254 +							CLEAR_BIT(bits, j+2);
   1.255 +							CLEAR_BIT(bits, j+3);
   1.256 +						}
   1.257 +					}
   1.258 +				}
   1.259 +			}
   1.260 +		}
   1.261 +	}
   1.262 +}
   1.263 +
   1.264 +void cheatSearchValue(const CheatSearchData *cs, int compare, int size,
   1.265 +                      bool isSigned, u32 value)
   1.266 +{
   1.267 +	if (compare < 0 || compare > SEARCH_GE)
   1.268 +		return;
   1.269 +	int inc = 1;
   1.270 +	if (size == BITS_16)
   1.271 +		inc = 2;
   1.272 +	else if (size == BITS_32)
   1.273 +		inc = 4;
   1.274 +
   1.275 +	if (isSigned)
   1.276 +	{
   1.277 +		bool (*func)(s32, s32) = cheatSearchSignedFunc[compare];
   1.278 +
   1.279 +		for (int i = 0; i < cs->count; i++)
   1.280 +		{
   1.281 +			CheatSearchBlock *block = &cs->blocks[i];
   1.282 +			int size2 = block->size;
   1.283 +			u8 *bits  = block->bits;
   1.284 +			u8 *data  = block->data;
   1.285 +
   1.286 +			for (int j = 0; j < size2; j += inc)
   1.287 +			{
   1.288 +				if (IS_BIT_SET(bits, j))
   1.289 +				{
   1.290 +					s32 a = cheatSearchSignedRead(data, j, size);
   1.291 +					s32 b = (s32)value;
   1.292 +
   1.293 +					if (!func(a, b))
   1.294 +					{
   1.295 +						CLEAR_BIT(bits, j);
   1.296 +						if (size == BITS_16)
   1.297 +							CLEAR_BIT(bits, j+1);
   1.298 +						if (size == BITS_32)
   1.299 +						{
   1.300 +							CLEAR_BIT(bits, j+2);
   1.301 +							CLEAR_BIT(bits, j+3);
   1.302 +						}
   1.303 +					}
   1.304 +				}
   1.305 +			}
   1.306 +		}
   1.307 +	}
   1.308 +	else
   1.309 +	{
   1.310 +		bool (*func)(u32, u32) = cheatSearchFunc[compare];
   1.311 +
   1.312 +		for (int i = 0; i < cs->count; i++)
   1.313 +		{
   1.314 +			CheatSearchBlock *block = &cs->blocks[i];
   1.315 +			int size2 = block->size;
   1.316 +			u8 *bits  = block->bits;
   1.317 +			u8 *data  = block->data;
   1.318 +
   1.319 +			for (int j = 0; j < size2; j += inc)
   1.320 +			{
   1.321 +				if (IS_BIT_SET(bits, j))
   1.322 +				{
   1.323 +					u32 a = cheatSearchRead(data, j, size);
   1.324 +
   1.325 +					if (!func(a, value))
   1.326 +					{
   1.327 +						CLEAR_BIT(bits, j);
   1.328 +						if (size == BITS_16)
   1.329 +							CLEAR_BIT(bits, j+1);
   1.330 +						if (size == BITS_32)
   1.331 +						{
   1.332 +							CLEAR_BIT(bits, j+2);
   1.333 +							CLEAR_BIT(bits, j+3);
   1.334 +						}
   1.335 +					}
   1.336 +				}
   1.337 +			}
   1.338 +		}
   1.339 +	}
   1.340 +}
   1.341 +
   1.342 +int cheatSearchGetCount(const CheatSearchData *cs, int size)
   1.343 +{
   1.344 +	int res = 0;
   1.345 +	int inc = 1;
   1.346 +	if (size == BITS_16)
   1.347 +		inc = 2;
   1.348 +	else if (size == BITS_32)
   1.349 +		inc = 4;
   1.350 +
   1.351 +	for (int i = 0; i < cs->count; i++)
   1.352 +	{
   1.353 +		CheatSearchBlock *block = &cs->blocks[i];
   1.354 +
   1.355 +		int size2 = block->size;
   1.356 +		u8 *bits  = block->bits;
   1.357 +		for (int j = 0; j < size2; j += inc)
   1.358 +		{
   1.359 +			if (IS_BIT_SET(bits, j))
   1.360 +				res++;
   1.361 +		}
   1.362 +	}
   1.363 +	return res;
   1.364 +}
   1.365 +
   1.366 +void cheatSearchUpdateValues(const CheatSearchData *cs)
   1.367 +{
   1.368 +	for (int i = 0; i < cs->count; i++)
   1.369 +	{
   1.370 +		CheatSearchBlock *block = &cs->blocks[i];
   1.371 +
   1.372 +		memcpy(block->saved, block->data, block->size);
   1.373 +	}
   1.374 +}