mms?pdu?2

来源:百度文库 编辑:神马文学网 时间:2024/05/02 21:20:46
mms pdu 2(2009-06-25 16:52:59) 标签:杂谈 分类:it

BYTE ChangeAsciiToHexByte(char src_char)
{
        BYTE src_byte = ( BYTE ) src_char;

        BYTE result_byte = src_byte;

        if(  src_byte >= 0x30
          && src_byte <= 0x39 )
        {
          result_byte = src_byte - 0x30;
          return result_byte;
        }
   
        if(  src_byte >= 0x41
          && src_byte <= 0x46 )
        {
          result_byte = src_byte - 0x41 + 0x0a;
        }

        if(  src_byte >= 0x61
          && src_byte >= 0x66 )
        {
          result_byte = src_byte - 0x61 + 0x0a;
        }

        return result_byte;
}

char *ChangeStringToHexBuf(char *pSrc, int len, char *pResultBuf)
{

        int     nLength = len/2 ;
    BYTE        first_byte;
        BYTE        second_byte;
        BYTE        result_byte;
        for( int nIndex = 0; nIndex < nLength; nIndex ++ )
                     
                first_byte  = ChangeAsciiToHexByte(pSrc[ nIndex * 2 ] );
                second_byte = ChangeAsciiToHexByte(pSrc[ nIndex * 2 + 1]);
                result_byte = ( BYTE ) ( first_byte * 16 + second_byte );
                pResultBuf[ nIndex ] = result_byte;
        }
        pResultBuf[ nIndex ] = '\0';
        return pResultBuf;
}

static char HexCode[] =
{
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
      'A', 'B', 'C', 'D', 'E', 'F',
};
char * ByteToHexString(BYTE src_byte,char *pHexCode)
{
        int n = src_byte;
    if  (n < 0)
      n = 256 + n;
    pHexCode[0] = HexCode[n / 16];
    pHexCode[1] = HexCode[n % 16];
        return pHexCode;
}
char * ByteArrayToHexString(char *pSrc,int nLen ,char *pDesc)
{
        for (int nIndex = 0 ; nIndex         {
                ByteToHexString( (BYTE) pSrc[nIndex] , pDesc );
                pDesc    = pDesc + 2;
        }
    return pDesc;
}

char * UTF_8ToUnicode(char *pText,WCHAR *pOut )
{
        unsigned char* uchar = (unsigned char *)pOut;
        uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
        uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
        return (char* )uchar ;
}

char *UnicodeToUTF_8( WCHAR *pText,char *pOut)
{
        // 注意 WCHAR高低字的顺序,低字节在前,高字节在后
        unsigned char * pchar = (unsigned char *)pText;
        unsigned char * pdest = (unsigned char *)pOut;
        pdest[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
        pdest[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
        pdest[2] = (0x80 | (pchar[0] & 0x3F));
        return (char * )pdest;
}

char * Gb2312ToUnicode(char *pBuffer,WCHAR *pOut)
{
        ::MultiByteToWideChar(936,MB_PRECOMPOSED,pBuffer,2,pOut,1);
     return (char *)pOut;
}
char * UnicodeToGB2312(WCHAR *pBuffer,char  *pOut)
{
        WideCharToMultiByte(936,NULL,pBuffer,1,pOut,sizeof(WCHAR),NULL,NULL);
    return (char *)pOut;
}
char * GB2312ToUTF_8(char *pText, int pLen,char * pOut)
{
        //UTF_8 must be three bytes
        char buf[4]={0};
        char* rst = pOut;  //new char[pLen * 3 + 2];//char* rst = new char[pLen + (pLen >> 2) + 2];
        int i = 0;
        int j = 0;      
    WCHAR pbuffer;
        while(i < pLen)
        {
            //如果是英文直接复制就可以
            if( *(pText + i) >= 0)
                {
                        rst[j++] = pText[i++];
                }
            else
                {
                    Gb2312ToUnicode(pText+i,&pbuffer);
                    UnicodeToUTF_8(&pbuffer,buf);               
                    rst[j]   = buf[0];
                    rst[j+1] = buf[1];
                    rst[j+2] = buf[2];
                    j += 3;
                    i += 2;
                }
        }
        rst[j] = '\0';                          
        return pOut ;
}

char * UTF_8ToGB2312( char *pText, int pLen,char * pOut)
{
        char * newBuf = pOut ;
        char Ctemp[4]={0};
    int i = 0;
    int j = 0;
    WCHAR Wtemp;
    while(i < pLen)
    {
        if(pText > 0)
        {
             newBuf[j++] = pText[i++];                       
        }
        else                 
        {
            UTF_8ToUnicode(pText + i,&Wtemp);
            UnicodeToGB2312(&Wtemp,Ctemp);
            newBuf[j]     = Ctemp[0];
            newBuf[j + 1] = Ctemp[1];
            i += 3;   
            j += 2;   
        }
    }
    newBuf[j] = '\0';   
    return pOut;
}

 

 

char * CMMSEncoder::encodeUintvar( int data, char *pUintVar ,int *iUintVarLen)
{
        char reversed[8]={0};
    int i = 0;
    reversed = data & 0x7f;   // The lowest
    data = data >> 7;
    i++;
    while ( data > 0 )
    {
        reversed = 0x80 | (data & 0x7f);
        i++;
        data = data >> 7;
    }
    // Reverse it because it is in reverse order
    for ( int j = 0; j < i; j++ )
    {
        pUintVar[j] = reversed[i - j - 1];
    }
        *iUintVarLen=i;
        return pUintVar;
}
int  CMMSEncoder::decodeUintvar(char *pUintVar ,int iUintVarLen)
{
    int uintvar = 0 , i = 0;
    while ( pUintVar & 0x80 )
    {
        uintvar  = uintvar << 7;
        uintvar |=pUintVar & 0x7f;
        i++;
    }
    uintvar  = uintvar << 7;
    uintvar |= pUintVar & 0x7f;
    return uintvar;
}
int CMMSEncoder::encodeContentType(char *pContentType)
{
    if(stricmp(pContentType,"*/*"==0)
        return 0;
    if(stricmp(pContentType,"text/*"==0)
        return 1;
    if(stricmp(pContentType,"text/html"==0)
        return 2;
    if(stricmp(pContentType,"text/plain")==0)
        return 3;
    if(stricmp(pContentType,"text/x-hdml")==0)
        return 4;
    if(stricmp(pContentType,"text/x-ttml")==0)
        return 5;
    if(stricmp(pContentType,"text/x-vCalendar")==0)
        return 6;
    if(stricmp(pContentType,"text/x-vCard")==0)
        return 7;
    if(stricmp(pContentType,"text/vnd.wap.wml")==0)
        return 8;
    if(stricmp(pContentType,"text/vnd.wap.wmlscript")==0)
        return 9;
    if(stricmp(pContentType,"text/vnd.wap.channel")==0)
        return 10;
    if(stricmp(pContentType,"multipart/*")==0)
        return 11;
    if(stricmp(pContentType,"multipart/mixed")==0)
        return 12;
    if(stricmp(pContentType,"multipart/form-data")==0)
        return 13;
    if(stricmp(pContentType,"multipart/byteranges")==0)
        return 14;
    if(stricmp(pContentType,"multipart/alternative")==0)
        return 15;
    if(stricmp(pContentType,"application/*")==0)
        return 16;
    if(stricmp(pContentType,"application/java-vm")==0)
        return 17;
    if(stricmp(pContentType,"application/x-www-form-urlencoded")==0)
        return 18;
    if(stricmp(pContentType,"application/x-hdmlc")==0)
        return 19;
    if(stricmp(pContentType,"application/vnd.wap.wmlc")==0)
        return 20;
    if(stricmp(pContentType,"application/vnd.wap.wmlscriptc")==0)
        return 21;
    if(stricmp(pContentType,"application/vnd.wap.channelc")==0)
        return 22;
    if(stricmp(pContentType,"application/vnd.wap.uaprof")==0)
        return 23;
    if(stricmp(pContentType,"application/vnd.wap.wtls-ca-certificate")==0)
        return 24;
    if(stricmp(pContentType,"application/vnd.wap.wtls-user-certificate")==0)
        return 25;
    if(stricmp(pContentType,"application/x-x509-ca-cert")==0)
        return 26;
    if(stricmp(pContentType,"application/x-x509-user-cert")==0)
        return 27;
    if(stricmp(pContentType,"image/*")==0)
        return 28;
    if(stricmp(pContentType,"image/gif")==0)
        return 29;
    if(stricmp(pContentType,"image/jpeg")==0)
        return 30;
    if(stricmp(pContentType,"image/tiff")==0)
        return 31;
    if(stricmp(pContentType,"image/png")==0)
        return 32;
    if(stricmp(pContentType,"image/vnd.wap.wbmp")==0)
        return 33;
    if(stricmp(pContentType,"application/vnd.wap.multipart.*")==0)
        return 34;
    if(stricmp(pContentType,"application/vnd.wap.multipart.mixed")==0)
        return 35;
    if(stricmp(pContentType,"application/vnd.wap.multipart.form-data")==0)
        return 36;
    if(stricmp(pContentType,"application/vnd.wap.multipart.byteranges")==0)
        return 37;
    if(stricmp(pContentType,"application/vnd.wap.multipart.alternative")==0)
        return 38;
    if(stricmp(pContentType,"application/xml")==0)
        return 39;
    if(stricmp(pContentType,"text/xml")==0)
        return 40;
    if(stricmp(pContentType,"application/vnd.wap.wbxml")==0)
        return 41;
    if(stricmp(pContentType,"application/x-x968-cross-cert")==0)
        return 42;
    if(stricmp(pContentType,"application/x-x968-ca-cert")==0)
        return 43;
    if(stricmp(pContentType,"application/x-x968-user-cert")==0)
        return 44;
    if(stricmp(pContentType,"text/vnd.wap.si")==0)
        return 45;
    if(stricmp(pContentType,"application/vnd.wap.sic")==0)
        return 46;
    if(stricmp(pContentType,"text/vnd.wap.sl")==0)
        return 47;
    if(stricmp(pContentType,"application/vnd.wap.slc")==0)
        return 48;
    if(stricmp(pContentType,"text/vnd.wap.co")==0)
        return 49;
    if(stricmp(pContentType,"application/vnd.wap.coc")==0)
        return 50;
    if(stricmp(pContentType,"application/vnd.wap.multipart.related")==0)
        return 51;
    if(stricmp(pContentType,"application/vnd.wap.sia")==0)
        return 52;
    if(stricmp(pContentType,"text/vnd.wap.connectivity-xml")==0)
        return 53;
    if(stricmp(pContentType,"application/vnd.wap.connectivity-wbxml")==0)
                return 54;
        else
                return -1;
}