#include "StdAfx.h"
#include "utl.h"
#include "StrArray.h"
#include <windows.h>
#include <Lmcons.h>

//**************************************************************************
Cutl::Cutl(void)
{
}
//**************************************************************************

Cutl::~Cutl(void)
{
}
//**************************************************************************
void Cutl::big_little( BYTE* b,int nb )
{
	BYTE*aux=(BYTE*)malloc(nb);
	for (int i=0;i<nb;i++)
	{
		aux[i]=b[nb-(1+i)];
	}
	memcpy(b,aux,nb);
}
//***************************************************************************
void Cutl::cifra1( BYTE*b,int nb )
{
	BYTE *aux=(BYTE*)malloc(nb);
	BYTE bit;
	char n=0;
	int n1=(nb-1)/2;
	for (int i=0;i<nb;i++)
	{
		//damos la vuelta al bit de 4 en 4
		bit=0;
		bit=bit|((b[i]& 0x01)<<3);
		bit=bit|((b[i]& 0x02)<<1);
		bit=bit|((b[i]& 0x04)>>1);
		bit=bit|((b[i]& 0x08)>>3);
		bit=bit|((b[i]& 0x10)<<3);
		bit=bit|((b[i]& 0x20)<<1);
		bit=bit|((b[i]& 0x40)>>1);
		bit=bit|((b[i]& 0x80)>>3);
		//inverso----------------
		if (i%2==0)
			bit=~bit;
		else
			bit=bit;

		//orden------------------
		if (i==0)
		{
			n=255-bit-(char)(nb%256);
			aux[0]=bit;
		}
		else
			aux[1+(n1+i)%(nb-1)]=bit-n;
	}
	memcpy(b,aux,nb);
	free(aux);
}
//***************************************************************************
void Cutl::cifra1( char*b )
{
	cifra1((BYTE*)b,(int)strlen(b));
}

//***************************************************************************
void Cutl::descifra1( BYTE* b,int nb )
{
	BYTE *aux=(BYTE*)malloc(nb);
	BYTE bit;
	BYTE bit1;
	char n=0;
	int n1=(nb-1)/2;

	for (int i=0;i<nb;i++)
	{

		//orden------------------
		if (i==0)
		{
			bit1=b[0];
			n=255-bit1-(char)(nb%256);	
		}
		else
		{
			bit1=b[1+(n1+i)%(nb-1)];
			bit1=(bit1+n)%256;
		}
		//inverso-------------

		if (i%2==0)
			bit1=~bit1;
		else
			bit1=bit1;
		//damos la vuelta al bit de 4 en 4
		bit=0;
		bit=bit|((bit1& 0x01)<<3);
		bit=bit|((bit1& 0x02)<<1);
		bit=bit|((bit1& 0x04)>>1);
		bit=bit|((bit1& 0x08)>>3);
		bit=bit|((bit1& 0x10)<<3);
		bit=bit|((bit1& 0x20)<<1);
		bit=bit|((bit1& 0x40)>>1);
		bit=bit|((bit1& 0x80)>>3);
		aux[i]=bit;
	}
	memcpy(b,aux,nb);
	free(aux);
}
//***************************************************************************
void Cutl::descifra1( char*b )
{
	descifra1((BYTE*)b,(int)strlen(b));
}
//***************************************************************************
int Cutl::busca( BYTE*lista,int n,int z,BYTE *e, int (*funcion)(BYTE*,BYTE*) )
{
	int i,f,c,r;
	i=0;
	f=n-1;
	c=n/2;
	while(c<f &&c>i)
	{
		r=funcion(&lista[z*c],e);
		if (r<0)
			f=r;
		else if(r>0)
			i=r;
		else
			return c;
		c=(f-i)/2;
	}
	//quedan como mucho 2 elementos
	if (funcion(&lista[z*i],e)<=0)
		return i;
	return f;
}
//********************************************************************************************
int pon_id(char* idm, int nb, int nbAct, char *buf, int nbbuf )
{
	int i;
	for(i=0; i<nbbuf; i++)
	{
		idm[nbAct]^=buf[i];
		nbAct=(nbAct+1)%nb;
	}
	return nbAct;
}
//***************************************************************************
char* Cutl::id_pc(char* idm, int nb)
{
	//lee del registro----------------------------------------------
	char str[2048];
	HKEY  hKey1; 
	long   l2;
	int i, ii,er, v_er=0;
	DWORD dwData=0;
	nb--;
	//LARGE_INTEGER lpFrequency;
	static	char prop_bios[12][32] = {
		{"BaseBoardManufacturer" },
		{ "BaseBoardProduct" },
		{ "BaseBoardManufacturer" }, 
		{ "BaseBoardVersion" }, 
		{ "BIOSReleaseDate" }, 
		{ "BIOSVendor" }, 
		{ "BIOSVersion" }, 
		{ "SystemFamily" }, 
		{ "SystemManufacturer" }, 
		{ "SystemProductName" }, 
		{ "SystemSKU" }, 
		{ "SystemVersion" }
	};
	static	char prop_sistem[1][32] = {
		{"Identifier" },
		/*{ "SystemBiosDate" },
		{ "VideoBiosDate" }, */
	};
	static	char prop_proce[5][32] = {
		{"Identifier" },
		{ "ProcessorNameString" },
		{ "VendorIdentifier" },
		{ "~MHz" },
		{"FeatureSet"}
		
	};
	
	memset(idm,0,nb);
	ii=0;
	er = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System", 0,KEY_READ,&hKey1);
	if (er != ERROR_SUCCESS)
		return NULL;
	
	for (i=0; i<1; i++)
	{
		l2=2048;
		er=RegQueryValueEx(hKey1,prop_sistem[i],0,NULL,(LPBYTE) str,(LPDWORD) &l2);
		if(er==ERROR_FILE_NOT_FOUND)
		{
			v_er++;
			if(v_er>10)
				return NULL;
			continue;
		}
		if(ERROR_SUCCESS!=er)
			return NULL;
		ii=pon_id(idm, nb,ii,str,strlen(str));
	}
	RegCloseKey( hKey1);

	er = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\BIOS", 0,KEY_READ,&hKey1);
	if (er != ERROR_SUCCESS)
	{
		return NULL;
	}
	for (i=0; i<12; i++)
	{
		l2=2048;
		er=RegQueryValueEx(hKey1,prop_bios[i],0,NULL,(LPBYTE) str,(LPDWORD) &l2);
		if(er==ERROR_FILE_NOT_FOUND)
		{
			v_er++;
			if(v_er>10)
				return NULL;
			continue;
		}
		if(ERROR_SUCCESS!=er)
			return NULL;
		ii=pon_id(idm, nb,ii,str,strlen(str));
	}
	RegCloseKey( hKey1);

	er = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,KEY_READ,&hKey1);
	if (er != ERROR_SUCCESS)
	{
		return NULL;
	}
	for (i=0; i<3; i++)
	{
		l2=2048;
		er=RegQueryValueEx(hKey1,prop_proce[i],0,NULL,(LPBYTE) str,(LPDWORD) &l2);
		if(er==ERROR_FILE_NOT_FOUND)
		{
			v_er++;
			if(v_er>10)
				return NULL;
			continue;
		}
		if(ERROR_SUCCESS!=er)
			return NULL;
		ii=pon_id(idm, nb,ii,str,strlen(str));
	}
	
	
	for (; i<5; i++)
	{
		l2 = sizeof(DWORD);
		er=RegQueryValueEx(hKey1, prop_proce[i], NULL, NULL, (LPBYTE)&dwData, (LPDWORD)&l2);
		if(ERROR_SUCCESS!=er)
		{
			v_er++;
			if(v_er>10)
				return FALSE;
			continue;
		}
		sprintf(str,"%u",dwData);
		ii=pon_id(idm, nb,ii,str,strlen(str));
	}
	
	//QueryPerformanceFrequency( &lpFrequency);

	//RegQueryValueEx(hKey1,prop_proce[i],0,NULL,(LPBYTE) &dd,(LPDWORD) &l2);
	//	sprintf(str,"%ld",i);
	//l2=strlen(str);
	//ii=pon_id(id,ii,(char*)&lpFrequency,sizeof(LARGE_INTEGER));

	RegCloseKey( hKey1);

	//lo pasa a string
	int n1='z'-'a'+1;
	int n2='Z'-'A'+1;
	int nt=n1+n2+'9'-'0'+1;
	for (i=0; i<nb; i++)
	{
		idm[i]=(char)((BYTE)(idm[i])%nt);
		if(idm[i]<n1)
		{
			idm[i]='a'+idm[i];
			continue;
		}
		idm[i]-=(char)n1;
		if(idm[i]<n2)
		{
			idm[i]='A'+idm[i];
			continue;
		}
		idm[i]-=(char)n2;
		idm[i]='0'+idm[i];
	}
	idm[nb]=0;
	return idm;
}
//***************************************************************************
int Cutl::str_hex2int( char* st )
{
	char *s;
	int res=0;
	s=strstr(st,"0x");
	if(!s)
		strstr(st,"0X");
	if(!s)
		return res;
	s+=2;
	while(*s!=0)
	{
		if(*s>='0' && *s<='9')
			res=(res<<4)|(int)(*s-'0');
		else if(*s>='a' && *s<='f')
			res=(res<<4)|(10+(int)(*s-'a'));
		else if(*s>='A' && *s<='F')
			res=(res<<4)|(10+(int)(*s-'A'));
		else
			break;
		s++;
	}
	return res;
}
//***************************************************************************
void Cutl::int2str_hex( char* st , int n)
{
	int i,a;
	strcpy(st,"0x00000000");
	for(i=0; i<8; i++)
	{
		a=(n>>4*i)&0xf;
		if(a<10)
			st[2+7-i]='0'+(char)a;
		else
			st[2+7-i]='a'+(char)a-10;
	}

}
//***************************************************************************
DWORDLONG Cutl::getMemoDisp()
{
	MEMORYSTATUSEX status;
	status.dwLength = sizeof(status);
	GlobalMemoryStatusEx(&status);
	return status.ullAvailVirtual;
}
//***************************************************************************
bool Cutl::lanza_exe(char *appName, char *comline, DWORD *processId)
{
	// additional information
	STARTUPINFO si;     
	PROCESS_INFORMATION pi;
	BOOL is_alive=FALSE;

	// set the size of the structures
	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );

	// start the program up
	CreateProcess( appName,   // the path
		comline,        // Command line
		NULL,           // Process handle not inheritable
		NULL,           // Thread handle not inheritable
		FALSE,          // Set handle inheritance to FALSE
		0,              // No creation flags
		NULL,           // Use parent's environment block
		NULL,           // Use parent's starting directory 
		&si,            // Pointer to STARTUPINFO structure
		&pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
		);

	//check is alive
	DWORD  lpExitCode;
	GetExitCodeProcess(pi.hProcess,&lpExitCode);
	is_alive=(lpExitCode==STILL_ACTIVE);

	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );
	if(is_alive==TRUE && processId)
		*processId=pi.dwProcessId;
	return is_alive==TRUE;
}
//***************************************************************************
DWORDLONG Cutl::getMemoDisp1Malloc()
{
	void* bufprinci = NULL;
	//en un malloc-----------------------
	DWORDLONG i=0;
	DWORDLONG paso;
	//ia=paso;
	//for (int j=0; j<3; j++)
	//{
	paso=1024*1024*1024;
	while(TRUE)
	{
		bufprinci=malloc(size_t(i+paso));
		if (bufprinci)
		{
			free(bufprinci);
			i+=paso;
		}
		else
		{
			if (paso<1)
				break;
			paso=paso/2;
			if (paso<1)
				break;
		}
	}
	if (bufprinci)
		free(bufprinci);
	return i;
}
//***************************************************************************
StrArray* Cutl::stringSplit( char *orig,char* subStr, StrArray*buf )
{
	char *s = orig;
	char *s1, *s2;
	int siz =strlen(subStr);
	s1 = strstr(s, subStr);
	int n;
	while(s1)
	{
		n = (int)(s1-s);
		buf->add(NULL, n+1);
		s2 = buf->get(buf->n_i-1);
		if(!s1)
			return NULL;
		for (int i =0; i<n; i++)
		{
			s2[i] = s[i];
		}
		s2[n]=0;
		s = s1+siz;
		s1 = strstr(s, subStr);
	}

	buf->add(s);
	s2 = buf->get(buf->n_i-1);
	if(!s2)
		return NULL;
	return buf;
}
//***************************************************************************
char* Cutl::getWinUser( char* buf )
{
	DWORD username_len = UNLEN+1;
	GetUserName(buf, &username_len);
	return buf;
}

//***************************************************************************
