#include "stdafx.h"

#include "CovertStr.h"
#include "..\\inc\\p_strs.h"
#include "..\\inc\\p_apps.h"


CString _WritePort(unsigned char * p, CString strName)
{
	char cTemp[6];
	CString cWriteStr;

	itoa((int)((p[0] << 8) + p[1]), cTemp, 10);
	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += cTemp;
	cWriteStr += "\n";
	return cWriteStr;
}

CString _WriteString(char * p, CString strName, int iMaxlen)
{
	char cTemp[256];
	CString cWriteStr;

	memcpy (cTemp, (char *)p, iMaxlen);
	cTemp[iMaxlen] = 0;

	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += cTemp;
	cWriteStr += "\n";
	return cWriteStr;
}

CString _WriteVal(char * p, CString strName)
{
	char cTemp[41];
	CString cWriteStr;

	itoa((int)p[0], cTemp, 10);
	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += cTemp;
	cWriteStr += "\n";
	return cWriteStr;
}

CString _WriteIPAddr(unsigned char * p, CString strName)
{
	CString cWriteStr;
	int i, iLen;
	char pBuf[20];

	pBuf[0] = 0;
	for (i = 0; i < IP_ALEN; i ++)
	{
		iLen = strlen(pBuf);
		itoa(p[i], pBuf+iLen, 10);
		if (i < IP_ALEN-1)
		{
			strcat(pBuf, ".");
		}
	}
	iLen = strlen(pBuf);
	pBuf[iLen+1] = 0;

	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += pBuf;
	cWriteStr += "\n";
	return cWriteStr;
}

CString _WriteMacAddr(char * pMac, CString strName)
{
	int i, iPos;
	unsigned char iVal;
	char pBuf[40];
	CString cWriteStr;

	iPos = 0;
	for (i = 0; i < HW_ALEN; i ++)
	{
		iVal = pMac[i];
		if (iVal < 0x10)
		{
			memcpy (pBuf+iPos, "0x", 2);
			iPos += 2;
			pBuf[iPos] = '0';
			iPos ++;
			itoa (iVal, pBuf+iPos, 16);
			iPos ++;
		}
		else
		{
			memcpy (pBuf+iPos, "0x", 2);
			iPos += 2;
			itoa(iVal, pBuf+iPos, 16);
			iPos += 2;
		}
		pBuf[iPos] = ',';
		iPos ++;
	}
	iPos --;
	pBuf[iPos] = 0;

	strName = strName + " = ";
	cWriteStr += strName;
	cWriteStr += pBuf;
	cWriteStr += "\n";
	return cWriteStr;
}

void DisplayError(CString strName)
{
	CString cError;

	cError = strName + " is invalid! Continue loading?";
	AfxMessageBox(cError);
}

CString _GetName(CString cSrc)
{
	int iPos, iSpace, iLen;
	CString cName;

	iPos = cSrc.Find('=');
	cName = cSrc.Left(iPos);

	while (cName.Find(' ') != -1)
	{
		iSpace = cName.Find(' ');
		iLen = cName.Delete(iSpace, 1);
	}

	return cName;
}

CString _GetStr(CString cSrc)
{
	CString cTemp;
	int iPos, iSpace, iLen;

	iPos = cSrc.Find('=');
	cTemp = cSrc.Right(cSrc.GetLength()-(iPos+1));

	while (cTemp.Find(' ') != -1)
	{
		iSpace = cTemp.Find(' ');
		iLen = cTemp.Delete(iSpace, 1);
	}

	while (cTemp.Find('\t') != -1)
	{
		iPos = cTemp.Find('\t');
		iLen = cTemp.Delete(iPos, 1);
	}

	while (cTemp.Find('"') != -1)
	{
		iPos = cTemp.Find('"');
		iLen = cTemp.Delete(iPos, 1);
	}

	iPos = cTemp.Find("//");
	if (iPos != -1)
		cTemp = cTemp.Left(iPos);

	return cTemp;
}

BOOL _SetPort(char * p, CString cStr, char cLabel[])
{
	int iPort;
	CString strName, strName1, strVal;
	CString cError;

	strName = _GetName(cStr);
	strName1 = strName + ' ';
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel) || !strcmp((char *)(LPCTSTR)strName1, (char *)cLabel))
	{
		iPort = atoi(strVal);

		if(iPort > 0 || iPort < 65535)
		{
			p[0] = (BYTE)((iPort>>8)&0xff);
			p[1] = (BYTE)(iPort&0xff);
			return TRUE;
		}
		else
		{
			DisplayError(strName);
			return TRUE;
		}
	}
	return FALSE;
}

BOOL _SetString(char * p, CString cStr, char cLabel[], int iMaxlen)
{
	int iLen;
	CString cError;
	CString strName, strName1, strVal;

	strName = _GetName(cStr);
	strName1 = strName + ' ';
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel) || !strcmp((char *)(LPCTSTR)strName1, (char *)cLabel))
	{
		iLen = strVal.GetLength();
		if (iLen <=	iMaxlen)
		{
			memcpy((char *)p, strVal, iLen);
			memset((char *)p+iLen, 0, iMaxlen-iLen);
			return TRUE;
		}
		else
		{
			DisplayError(strName);
			return TRUE;
		}
	}
	return FALSE;
}

BOOL _CompareVal(CString strName, CString strVal, int iMaxlen)
{
	UCHAR iChar;
	char * pTemp;
	int iLen, iCount;

	iLen = strVal.GetLength();
	pTemp = (char *)malloc(iLen);
	memset(pTemp, 0, iLen);
	memcpy(pTemp, strVal, iLen);

	if (atoi(strVal) < iMaxlen)
	{
		iCount = 0;
		do
		{
			iChar = (UCHAR)(*pTemp);
			pTemp ++;
			iCount ++;
			if (iChar < '0' || iChar > '9')
			{
				DisplayError(strName);
				return FALSE;
			}
		}while (iCount<strVal.GetLength());
	}
	else
	{
		DisplayError(strName);
		return FALSE;
	}
	return TRUE;
}

CString _GetProtocolVal (CString strVal)
{
	if(strVal == "h323")
		strVal = '0';
	else if(strVal == "sip")
		strVal = '1';
	else if(strVal == "mgcp")
		strVal = '2';
	else if(strVal == "n2p")
		strVal = '3';
	else if(strVal == "iax2")
		strVal = '4';
	else if(strVal == "epn")
		strVal = '5';
	else if(strVal == "wp")
		strVal = '6';
	
	return strVal;
}

BOOL _SetVal(char * p, CString cStr, char cLabel[], int iMaxlen)
{
	CString strName, strName1, strVal;
	char _cProtocolLabel[] = "protocol ";


	strName = _GetName(cStr);
	strName1 = strName + ' ';
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName1, (char *)_cProtocolLabel))
	{
		strVal = _GetProtocolVal(strVal);
	}

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel) || !strcmp((char *)(LPCTSTR)strName1, (char *)cLabel))
	{
		if (_CompareVal(strName, strVal, iMaxlen))
		{
			p[0] = (BYTE)atoi(strVal);
			return TRUE;
		}
		else
			return TRUE;
	}
	return FALSE;
}

int _GetFlag(CString cStr, char cLabel[], int iMaxlen)
{
	CString strName, strName1, strVal;

	strName = _GetName(cStr);
	strName1 = strName + ' ';
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel) || !strcmp((char *)(LPCTSTR)strName1, (char *)cLabel))
	{
		if (_CompareVal(strName, strVal, iMaxlen))
			return atoi(strVal);
		else
			return -1;
	}
	return -1;
}

/*void _SetFlag(char * p, int iFlag)
{
	p[0] |= iFlag;
}

void _ClearFlag(char * p, int iFlag)
{
	p[0] &= iFlag;
}
*/
BOOL _SetIPAddr(char * p, CString cStr, char cLabel[])
{
	UCHAR iChar;
	char * pTemp;
	int iLen, iCount;
	unsigned short iTotal;
	CString strName, strVal, strName1;

	strName = _GetName(cStr);
	strName1 = strName + ' ';
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel) || !strcmp((char *)(LPCTSTR)strName1, (char *)cLabel))
	{
		if (strVal.Find(',') != -1)
		{
			strVal.Replace(',' , '.');
		}
		iLen = strVal.GetLength();
		pTemp = (char *)malloc(iLen);
		memset(pTemp, 0, iLen);
		memcpy(pTemp, strVal, iLen);

		iTotal = 0;
		iCount = 0;
		do
		{
			iChar = (UCHAR)(*pTemp);
			pTemp ++;
			if (iChar >= '0' && iChar <= '9')
			{
				iTotal *= 10;
				iTotal += iChar - '0';
				if (iTotal >= 256)
				{
					DisplayError(strName);
					return TRUE;
				}
			}
			else
			{
				p[iCount] = (UCHAR)iTotal;
				iTotal = 0;
				iCount ++;
				if (iCount < IP_ALEN)
				{
					if (iChar != '.')
					{
						DisplayError(strName);
						return TRUE;
					}
				}
				else
				{
					 return TRUE;
				}
			}
		} while (iChar);
	}

  return FALSE;
}

int _atoi_16(char * pBuf, int iRadix)
{
	int iPos;
	unsigned short iTotal;
	UCHAR iVal;

	iTotal = 0;
	iPos = 0;
	do
	{
		iVal = pBuf[iPos];
		if (iVal >= '0' && iVal <= '9')
		{
			iVal -= '0';
		}
		else 
		{
			if (iRadix == 16)
			{
				if (iVal >= 'a' && iVal <= 'f')
				{
					iVal -= 'a' - 10;
				}
				else if (iVal >= 'A' && iVal <= 'F')
				{
					iVal -= 'A' - 10;
				}
				else
				{
					break;
				}
			}
			else
			{
				break;
			}
		}
		iTotal *= iRadix;
		iTotal += iVal;
		iPos ++;
	} while (1);
	return iTotal;
}

BOOL _IsHexChar(CString cString, int iLen)
{
	char * p;
	int i;
	UCHAR iChar;

	p = (char *)malloc(iLen);
	memset(p, 0, iLen);
	memcpy(p, cString, iLen);
	for (i = 0; i < iLen; i ++)
	{
		iChar = p[i];
		if ((iChar >= '0' && iChar <= '9') 
			|| (iChar >= 'a' && iChar <= 'f')
			|| (iChar >= 'A' && iChar <= 'F'))
		{
			return TRUE;
		}
		else
			return FALSE;
	}
	return TRUE;
}

BOOL _SetMacAddr(char * p, CString cStr, char cLabel[])
{
	int i, iLen;
	char pMac[3];
	int iPos;
	char * pCur;
	CString cTemp, cError;
	CString strName, strVal;

	strName = _GetName(cStr);
	strVal = _GetStr(cStr);

	if (!strcmp((char *)(LPCTSTR)strName, (char *)cLabel))
	{
		while (strVal.Find(",") != -1)
		{
			iPos = strVal.Find(",");
			iLen = strVal.Delete(iPos, 1);
		}
		while (strVal.Find("0x") != -1)
		{
			iPos = strVal.Find("0x");
			iLen = strVal.Delete(iPos, 2);
		}

		iLen = strVal.GetLength();
		if (iLen <= HW_ALEN * 2)
		{
			if (_IsHexChar(strVal, HW_ALEN * 2))
			{
				pCur = (char *)malloc(iLen+1);
				memset(pCur, 0, iLen+1);
				memcpy((char *)pCur, strVal, iLen);
				pMac[2] = 0;
				for (i = 0; i < HW_ALEN; i ++)
				{
					pMac[0] = *pCur ++;
					pMac[1] = *pCur ++;
					p[i] = (UCHAR)_atoi_16(pMac, 16);
				}
				return TRUE;
			}
			else
			{
				DisplayError(strName);
				return TRUE;
			}
		}
		else
		{
			DisplayError(strName);
			return TRUE;
		}
	}
	return FALSE;
}

