⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataformat.c

📁 在GPRS或者CDMA modem上实现发送/接收短信的C代码;支持Siemens
💻 C
📖 第 1 页 / 共 2 页
字号:
        ch = ch + 0x37;
    }

    return ch;
}

char ChrToHex(char ch)
{
    if(ch >= '0' && ch <= '9')
    {
        ch -= 0x30;
    }
    else if(ch >= 'A' && ch <= 'F')
    {
        ch -= 0x37;
    }
    else if(ch >= 'a' && ch <= 'f')
    {
        ch -= 0x57;
    }
    else
    {
	    return -1;
    }

    return ch;
}

/* 
* Reverse odd byte and even byte
*/
int  reversedata(const char* pSrc, char* pDst, int nSrcLen)
{
	int i, ret;
   
	if (nSrcLen%2 != 0)
	{
		WMMP_TRACE(debugType, "\r\nError: source data length");
		ret = -SMS_ERR_DATA_LEN;
		goto reversedata_fail;
	}

    for (i = 0; i < nSrcLen/2; i++)
    {
		pDst[2*i + 1] = pSrc[2*i];
        pDst[2*i]     = pSrc[2*i + 1];
    }

	return 0;

reversedata_fail:
	return ret;
}

/*
* 7-bit encoding
* pSrc: source string pointer
* pDst: destination string pointer
* nSrcLength: source string length
* return value: destination string length
*/
int gsmEncode7bit(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
    int nSrc;    //source string counter
    int nDst;    // destination string counter
    int nChar;  //sequence number of current dealing character in this group, 0-7
	int i;
    unsigned char nLeft;    //left data of previous character
	//temp uchar
	uchar ucMedCode[512];
	uchar* pTempDst;
	uchar temp;

	memset(ucMedCode, 0, sizeof(ucMedCode));
	pTempDst = ucMedCode;
    
    // initializing counters
    nSrc = 0;
    nDst = 0;
    //devide source string into groups with 8-characters each group
    // compress each group into 7 bytes
    //loop actions above until source string is handled completely
    //if there is less than 8 bytes in a group, above actions do work as well 
    while(nSrc <= nSrcLength)
    {
        // get the lowest 3 bits of source string counter
        nChar = nSrc & 7;    
        // process each byte of source string
        if(nChar == 0)
        {
            //the first byte in group, just kept used in processing next byte
            nLeft = *pSrc;
        }
        else
        {
            //other byte in group
            //add the left data of previous data TO the right part of this byte 
            //then get a new target coding byte
            *pTempDst = (*pSrc << (8-nChar)) | nLeft;
    
            //keep the left part of this byte, 
            //and stored as the left data
            nLeft = *pSrc >> nChar;
            //modify the pointer of destination string,
            //and the counter of destination string
            pTempDst++;
            nDst++; 
        }     
        //modify the pointer and the counter of source string
        pSrc++; nSrc++;
    }

	for (i = 0; i < nDst; i++)
	{
        temp = ( *(ucMedCode + i) & 0xF0 ) >> 4;
        *(pDst + i*2) = HexToChr(temp);
		
        temp = *(ucMedCode + i) & 0x0F;
		*(pDst + i*2 + 1) = HexToChr(temp);
	}

    //return the length of destination string
    return nDst*2; 
}

/*
* 7-bit decoding
* pSrc: source string pointer
* pDst: destination string pointer
* nSrcLength: source string length
* return value: destination string length
*/
int gsmDecode7bit(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
    int nSrc;        //source string counter
    int nDst;        //destination string counter
    int nByte;       //sequence number of current byte in processing, 0 - 6
	int i, nTempSrcLen;
    unsigned char nLeft;    //left data of previous byte
	uchar ucMedDecode[512];
	const uchar* pTempSrc;
	uchar temp[2];
	int   iDataInx;

	if (nSrcLength%2 != 0)
	{
		return -1;
	}

	//first, convert data from BCD to real binary
    memset(ucMedDecode, 0, sizeof(ucMedDecode));
	pTempSrc = pSrc;
	iDataInx = 0;
	for (i = 0; i < nSrcLength; i += 2)
	{
		//get 4 bytes from source string
		temp[0] = ChrToHex(*pTempSrc++);
		temp[1] = ChrToHex(*pTempSrc++);
		
        //2-bytes represents 1-byte unicode
		ucMedDecode[iDataInx++] = ( (temp[0]&0x0f) << 4 ) | ( temp[1] & 0x0f );
	}

	pTempSrc = ucMedDecode;
    nTempSrcLen = nSrcLength/2;
    //initializing counters
    nSrc = 0;
    nDst = 0; 
    //initializing the in-group sequence number of byte,
    //and the left data
    nByte = 0;
    nLeft = 0;
    //devide the source string into groups with 7 bytes each group
    //decompress them into 8 bytes
    //loop for above actions until all the dat in source string are handled
    //if there are less than 7 bytes in a group, these actions do work as well
    while(nSrc<nTempSrcLen)
    {
        // add the left data to the rigth part of source byte
        // then delete the highest bit 
        // and get a target decoding byte
        *pDst = ((*pTempSrc << nByte) | nLeft) & 0x7f;
        //keep the left bit of this byte as the left data
        nLeft = *pTempSrc >> (7-nByte); 
        //modify the pointer and the counter of destination string
        pDst++;
        nDst++;
        //modify the counter of byte that have processed
        nByte++;   
        //the last byte in group
        if(nByte == 7)
        {
            //get an extra target decoding byte
            *pDst = nLeft;
            //modify the pointer and counter of destination string
            pDst++;
            nDst++;
            //initialize sequence number of group and 
            //redundant data
            nByte = 0;
            nLeft = 0;
        }
        // modify the pointer and counter of source string
        pTempSrc++;
        nSrc++;
    }
    
    *pDst = 0;
    //return the length of destination string
    return nDst;
}


int gsmEncode8bit(const uchar * pSrc, uchar * pDst, int nSrcLen)
{
	int i = 0;
	char temp;
	
	for(i = 0; i < nSrcLen; i++)
	{
		temp = (pSrc[i]&0xF0)>>4;
		//hex to char 
		pDst[i*2] = HexToChr(temp);
	
		temp = pSrc[i]&0x0F;
	       pDst[i*2 + 1] = HexToChr(temp);
	}
	//return the length of destination string
    return nSrcLen*2;
}

int gsmDecode8bit(const uchar * pSrc, uchar* pDst, int nSrcLen)
{
	int i = 0, ret = 0;
	char leftbyte = 0x00, rightbyte = 0x00;

	if((nSrcLen%2) != 0)
	{
		WMMP_TRACE(debugType, "\r\nError: source data length");
		//return source string length error
        ret = -SMS_ERR_DATA_LEN;
		goto tofdbin_fail;
	}
	
	for(i = 0; i < nSrcLen/2; i++)
	{
		leftbyte = pSrc[i*2];
		rightbyte = pSrc[i*2 + 1];

		//char to hex
		leftbyte = ChrToHex(leftbyte);
		if(leftbyte == -1)
		{
 		    WMMP_TRACE(debugType, "Error: source data value");
			ret = -SMS_ERR_DATA_VALUE;
			goto tofdbin_fail;
		}

             //char to hex
        rightbyte = ChrToHex(rightbyte);
	    if(rightbyte == -1)
		{
 		    WMMP_TRACE(debugType, "Error: source data value");
			ret = -SMS_ERR_DATA_VALUE;
			goto tofdbin_fail;
		}

		leftbyte &= 0x0F;
		leftbyte <<= 4;
		rightbyte &= 0x0F;
		
		pDst[i] = leftbyte | rightbyte;
	}

	//return the length of destination string
	return nSrcLen/2;

tofdbin_fail:
	return ret;
}

//13328657218 ->
//00310033003300320038003600350037003200310038
int gsmEncodeUniNum(uchar *dest, const uchar *src, int len)
{
	int i = 0, ret = 0;
	char temp;
	
	for(i = 0; i < len; i++)
	{
		temp = src[i];
		if (temp <= 0x39 && temp >= 0x30)
		{
			dest[i*4] = 0x30;
			dest[i*4 + 1] = 0x30;

			temp = (src[i]&0xF0)>>4;
			if (temp <= 0x09 && temp >= 0x00)
			{
				dest[i*4 + 2] = temp + 0x30;
			}
			else
			{
				WMMP_TRACE(debugType, "\r\nError: phone number");
				ret = -1;
				goto toucsnum_fail;
			}
		
			temp = src[i]&0x0F;
			if (temp <= 0x09 && temp >= 0x00)
			{
				dest[i*4 + 3] = temp + 0x30;
			}
			else
			{
				WMMP_TRACE(debugType, "\r\nError: phone number");
				ret = -1;
				goto toucsnum_fail;
			}
		}
		else
		{
			WMMP_TRACE(debugType, "\r\nError: phone number");
			ret = -1;
			goto toucsnum_fail;
		}
		

	}
	
	return len*4;

toucsnum_fail:
	return ret;
}

//00310033003300320038003600350037003200310038 ->
//13328657218
int gsmDecodeUniNum(uchar *dest, const uchar *src, int len)
{
	int i = 0, ret = 0;
	uchar leftbyte = 0x00, rightbyte = 0x00;
       uchar temp = 0x00;

	if((len%4) != 0)
	{
		WMMP_TRACE(debugType, "\r\nError: source data length");
		ret = -1;
		goto toasciinum_fail;
	}
	
	for(i = 0; i < len/4; i++)
	{
		leftbyte = src[i*4 + 2];
		rightbyte = src[i*4 + 3];
		
		if(leftbyte >= '0' && leftbyte <= '9')
		{
			leftbyte -= 0x30;
		}
		else
		{
			WMMP_TRACE(debugType, "Error: usc phone number");
			ret = -2;
			goto toasciinum_fail;
		}

		if(rightbyte >= '0' && rightbyte <= '9')
		{
			rightbyte -= 0x30;
		}
		else
		{
			WMMP_TRACE(debugType, "Error: usc phone number");
			ret = -2;
			goto toasciinum_fail;
		}

		leftbyte &= 0x0F;
		leftbyte <<= 4;
		rightbyte &= 0x0F;
		
		dest[i] = leftbyte | rightbyte;
	}
	
	return len/4;

toasciinum_fail:
	return ret;
}

int gsmEncodeGb2Uni(const uchar* pSrc,  uchar* pDst, int nSrcLen)
{
    int i, j;
    const uchar* pData;
    uchar   unicode[512];
    char    temp;

    pData = pSrc;
    j = 0;
    memset(unicode, 0, sizeof(unicode));

    //GB code to UNICODE code
    for(i = 0; i < nSrcLen; i++)
    {
        if(*pData >= 0xA1 )
		{//chinese, get 2 bytes
	        fast_gb2uni((uchar*)pData, unicode+j);
	        i++;
	        j += 2; //unicode moves two bytes forward
     	    pData += 2; //move two bytes forward
		}
	    else
		{//character code, get one byte
   	        unicode[j++] = 0;
	        unicode[j++] = *pData;
	        pData += 1; //move one byte forward
		}
    }
	
    WMMP_TRACE(debugType, "\r\nUnicode length: %d", j);
    
    //here, unicode length is the value of number j.
    //hex to char, to
    pData = unicode;
    for(i = 0; i < j; i++)
    {
        temp = ( *(pData + i) & 0xF0 ) >> 4;
        *(pDst + i*2) = HexToChr(temp);

        temp = *(pData + i) & 0x0F;
	    *(pDst + i*2 + 1) = HexToChr(temp);
    }
    
    WMMP_TRACE(debugType, "\r\nPDU message: %s", pDst);

    //return the length of destination string
    return j*2;
}

int gsmDecodeUni2Gb(const uchar* pSrc, uchar* pDst, int nSrcLen)
{
    int i;
    int ret;
    uint uiDataInx;
    const uchar* pData;
    char temp[4];
    uchar unicode[3];
    uchar acDataBuf[400];
    
    if(nSrcLen%4 != 0)
    {
        WMMP_TRACE(debugType, "\r\nError: source unicode length.");
        ret = -SMS_ERR_DATA_LEN;
        goto gsmEncodeUni2Gb_fail;
    }
    //char to hex, then transform to UNICODE
    pData = pSrc;
    uiDataInx = 0;
    memset(acDataBuf, 0, sizeof(acDataBuf));
    for( i = 0; i < nSrcLen; i += 4 )
    {
    	//get 4 bytes from source string
	    temp[0] = ChrToHex(*pData++);
	    temp[1] = ChrToHex(*pData++);
	    temp[2] = ChrToHex(*pData++);
	    temp[3] = ChrToHex(*pData++);

        //2-bytes represents 1-byte unicode
	    unicode[0] = ( (temp[0]&0x0f) << 4 ) | ( temp[1] & 0x0f );
    	unicode[1] = ( (temp[2]&0x0f) << 4 ) | ( temp[3] & 0x0f );
    	unicode[3] = '\0';
    	if(unicode[0] == 0x00)
		{//character code
	        acDataBuf[uiDataInx] = unicode[1];
	        uiDataInx++;
		}
	    else
		{//chinese GB code
	        uni2gb(unicode, acDataBuf + uiDataInx);
	        uiDataInx += 2;
		}
    }    
    //copy GB coding data to pDst buffer	
	memcpy(pDst, acDataBuf, uiDataInx);
    //return the length of destination string
    return uiDataInx;
    
gsmEncodeUni2Gb_fail:
    return ret;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -