www.gusucode.com > 一些VC++加密算法+实例源码源码程序 > 一些VC++加密算法+实例源码/优化后的加密注册模块/优化后的加密注册模块/inventory(syn_thread)/am_lv1.cpp

    
#include	"stdafx.h"

#include	"am_lv1.h"

// Download by http://www.codefans.net
#include	<atlbase.h>

#include	<winsock2.h>

#include	<io.h>
#include	<stdio.h>
#include	<string.h>
#include	<malloc.h>
#include	<crtdbg.h>
#include	<fstream.h>
#include	<stdarg.h>



/*
	it is hard to judge whether it is the valid '\0' from the pointer
	in 'string',so it is determined by caller.
*/


int
c_AssertIp4(const char *ipbuf)
{

	int		i=0,
			val=0,
			dot[3],
			len=0,
			c=0;
	char	*ptr,
			*ptr2,
			buf[15+1];

	if(ipbuf==NULL)
		return E_E;

	len=strlen(ipbuf);
	if(len>15)
		return E_E;
	strcpy(buf,ipbuf);

//	assert they are either numbers or '.'
	for(i=0;i<len;i++){
		if(buf[i]!='.'&&!(buf[i]>='0'&&buf[i]<='9'))
			return E_E;
//	count the '.' and its location,then set it to 0
		if(buf[i]=='.'){
			if(c==3)
				return E_E;
			dot[c++]=i;
			buf[i]=0;
		}
	}
	if(c!=3)
		return E_E;

	ptr=ptr2=(char *)buf;

//	assert the case "012" and the scope of fields	
	if(*ptr2=='0'&&strlen(ptr2)!=1)
		return E_E;
	val=atoi(ptr2);
	if(val<0||val>240)
		return E_E;

	for(i=0;i<3;i++){
		ptr2=ptr+dot[i]+1;
		val=atoi(ptr2);
		if(*ptr2=='0'&&strlen(ptr2)!=1)
			return E_E;
		if(val<0||val>255)
			return E_E;
	}
	
	return 0;	

}

int
am_GetConfIp(const char *pathfile,char *ipbuf,unsigned int *size)
{
	char	ch,
			buf[15+1];
	int		i=0;
	unsigned int	it=0;
	fstream fp;

	if(pathfile==NULL)
		return E_INVALIDSTR;

//	assert the parameters
	if(IsBadWritePtr(LPVOID(ipbuf),*size))
		return	E_INVALIDMEM;

	if((_access(pathfile,0))==-1)
		return	E_NOSUCHFILE;

	fp.open(pathfile,ios::in|ios::nocreate);
	if(!fp)
		return E_OPENFILE;

	while(!isdigit(ch=fp.peek()))
		fp.get();

	fp.getline(buf,15);

	if(c_AssertIp4(buf)!=0)
		return	E_INVALIDIP;

	it=strlen(buf)+1;
	if(it>*size){
		*size=it;
		return	E_LACKMEM;
	}
	strcpy(ipbuf,buf);
	fp.close();
		
	return	0;
}

int
am_LocaConfFile(const char *subkey,const char *valuename,
				char *buf,	unsigned int *size)
{
	char	value[REG_STRLEN];
	long	hRes;
	DWORD	type,
			vsize=REG_STRLEN;
	HKEY	hk;


//	assert the parameters

	if(subkey==NULL)
		return E_INVALIDSTR;

	if(valuename==NULL)
		return E_INVALIDSTR;
	
	if(IsBadWritePtr(LPVOID(buf),*size))
		return	E_INVALIDMEM;
	
	
	hRes=RegOpenKeyEx(HKEY_LOCAL_MACHINE, subkey, NULL, KEY_READ, &hk);
	if(hRes)
		return	E_REG;

	hRes=RegQueryValueEx(hk, valuename, NULL, &type, (LPBYTE)value, &vsize);
	if(hRes)
		return	E_REG;

	if(*size<vsize){
		*size=vsize;
		return	E_LACKMEM;
	}
//	*size=vsize;
	strcpy(buf,value);
	
	return 0;
}

int
c_GetOsVer()
{
	OSVERSIONINFO	osvi;

	memset(&osvi,0,sizeof(OSVERSIONINFO));
	osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);

	if(!GetVersionEx (&osvi))
		return	E_E;
	if((osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
		(osvi.dwMinorVersion==0)) 
		return	C_W95;
	else if((osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
		(osvi.dwMinorVersion>0)) 
		return	C_W98;
	else if((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)&&(osvi.dwMajorVersion==3))
		return	C_WNT3;
	else if((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)&&(osvi.dwMajorVersion==4))
		return	C_WNT4;
	else
		return	E_SUPP;
}


int
c_GetIpAddr(char *ipbuf,unsigned int *size)
{

#define		REG_SEAR_SCOPE		10


	long			hRes=0;
	unsigned int	len=0;
	char			subkey[128],
					value[15+1];
	DWORD			vsize=15+1,
					type;

	HKEY			hk;

	if(IsBadWritePtr(LPVOID(ipbuf),*size))
		return	E_INVALIDMEM;

	if(c_GetOsVer()==C_W95) 
	{
		for (int i = 0;i<REG_SEAR_SCOPE;i++)
		{
			sprintf(subkey,"%s%d","System\\CurrentControlSet\\Services\\Class\\NetTrans\\000",i);

			hRes=RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey, NULL, KEY_READ, &hk);
			if(hRes)
				continue;

			hRes=RegQueryValueEx(hk,"IPAddress", NULL, &type, (LPBYTE)value, &vsize);
			if(hRes==0)
				break;
		}

		if(i==REG_SEAR_SCOPE)
			return	E_SUPP;

	}

	else
	{ 
		struct in_addr	inaddr;
		WSAData			wsaData;
		struct hostent *hptr;
		char			hostname[128];


		if( WSAStartup(MAKEWORD(1,1), &wsaData ) ) 
			return E_E;

		if(gethostname(hostname,128))
			return E_E;

		if(!(hptr=gethostbyname(hostname)))
			return E_E;
		
		memcpy((char *)&inaddr,hptr->h_addr_list[0],hptr->h_length);
		WSACleanup();
		strcpy(value,inet_ntoa(inaddr));
	}

	len=strlen(value)+1;
	if(len>*size){
		*size=len;
		return	E_LACKMEM;
	}

//	*size=len;
	strcpy(ipbuf,value);

	return 0;
}

/*
	c_AnsiToUnicode
		convert Ansi-string to Unicode-string
	parameters
		<in>		pszA		pointer to Ansi-string
		<out>		ppszW		pointer to Unicode-string
	return
		0 success,otherwize error.
	comments
		memory of Unicode-string was allocated by library.after calling this function,
		you should call 'c_FreeA' to free the memory.
	example
		codes maybe like these:
				BSTR	bstr;
				c_AnsiToUnicode("abcde",&bstr);
				...
				c_FreeU(bstr);
*/
int
c_AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{ 
    ULONG cCharacters;

	if(pszA==NULL){
		*ppszW = NULL;
		return E_INVALIDSTR;
	}

    cCharacters =  strlen(pszA)+1;

    // Use of the OLE allocator is required if the resultant Unicode
    // string will be passed to another COM component and if that
    // component will free it. Otherwise you can use your own allocator.
    *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);

	if(*ppszW==NULL)
        return E_ALLOCMEM;

    // Covert to Unicode.
    if (MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
                  *ppszW, cCharacters)==0){

//		dwError=GetLastError();
        CoTaskMemFree(*ppszW);
        *ppszW = NULL;
        return E_E;
//      return HRESULT_FROM_WIN32(dwError);
    }

    return 0;
}
 
void
c_FreeU(BSTR pszW)
{
	if(pszW!=NULL)
		CoTaskMemFree(pszW);
	return;
}


/*
	c_UnicodeToAnsi
		convert Unicode-string to Ansi-string
	parameters
		<out>		ppszA		pointer to Ansi-string
		<in>		pszW		pointer to Unicode-string
	return
		0 success,otherwize error.
	comments
		memory of ansi-string was allocated by library.after calling this function,
		you should call 'c_FreeU' to free the memory.
	example
		codes maybe like these:
				BSTR	bstr=SysAllocString(L"abcde");
				char	*str;
				c_UnicodeToAnsi(bstr,&str);
				...
				c_FreeA(str);
*/

int
c_UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
{
	ULONG	cbAnsi,cCharacters;

	if(pszW==NULL){
		*ppszA = NULL;
		return E_INVALIDSTR;
	}

	cCharacters = wcslen(pszW)+1;

	cbAnsi = cCharacters*2;
	
	*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
	
	if (*ppszA==NULL)
      return E_ALLOCMEM;

	if (WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
                                cbAnsi, NULL, NULL)==0){
		CoTaskMemFree(*ppszA);
		*ppszA = NULL;
		return	E_E;
	}
	return 0;
}

void
c_FreeA(LPSTR pszA)
{
	if(pszA!=NULL)
		CoTaskMemFree(pszA);
	return;
}



LPWSTR
c_MakeWideStrFromAnsi(LPCSTR psz) 
{
    int		i; 
    LPWSTR	pwsz; 
 
    if(!psz) 
        return NULL; 
 
    i =  MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0); 
    if (i <= 0)
		return NULL; 
 
    pwsz = (LPWSTR) SysAllocStringLen(NULL, i - 1); 
    
	if (!pwsz) 
		return NULL; 
    
	if(!MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i))
		return NULL;

    pwsz[i - 1] = 0; 
    
	return pwsz; 
} 

void
c_FreeMake(LPWSTR pwsz)
{
	if(pwsz==0)
		return;
	SysFreeString(pwsz);
}

LPSTR 
c_MakeAnsiStrFromWide(LPCWSTR bstr) 
{ 
	int			i; 
 
	LPSTR		psz; 
 
	if(bstr == NULL)
		return NULL;
 
	i =  WideCharToMultiByte(CP_ACP, 0, bstr, -1, NULL, 0, NULL, NULL); 
	if (i <= 0) 
		return NULL;

	psz = (LPSTR)LocalAlloc(LMEM_FIXED, i*sizeof(char)); 
 
	if (psz == NULL)
		return NULL;

	if(!WideCharToMultiByte(CP_ACP, 0,bstr, -1, psz, i, NULL, NULL))
		return NULL;

	return psz;
 
} 
void
c_FreeMake2(LPSTR pwsz)
{
	if(pwsz==0)
		return;
	LocalFree(pwsz);
}

/*
static	int
s_parse_from(

int
c_LogFile(const char *file,const char *form,...)
{
	char		*ptr=file;
	va_list		marker;
	va_start(marker,first );
	
	while( i != -1 ){      
      i = va_arg( marker, int);   
	}
	
	va_end( marker );
*/