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

📄 mfw_cnvt.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    if(wstrlen((char*)ss)<=0)
    {
        uni_txt[0]=0x80;
        uni_txt[1]=0;
        uni_txt[2]=0;
        return;//ascEx_txt is NULL
    }

    if(*ss==0x80)
    {//if the txt is unicode,copy it to uni_txt
        memcpy(uni_txt,ss,wstrlen((char*)ss)+2);
        return;
    }
    else
    {
        i=0;
        uni_txt[0]=0x80;//set the first code to 0x80
        while(*ss!=0)
        {
            code =ConvertToUni((char)(*ss));
            uni_txt[2*i+1]=(char)((code>>8)&0xff);
            uni_txt[2*i+2]=(char)(code&0xff);
            i++;
            ss++;
        }
        uni_txt[2*i+1]=0x00;
        uni_txt[2*i+2]=0x00;
    }
    return;//convert success
}


/* glowing,2004-06-10, the following is imported from M188A,
   in TCS2112 ATBCommon.c define some code convert function,
   they can replace the following function.
 */

typedef  struct {
	unsigned char sms_code;
	unsigned char ascEx_code;
} _Sms_AscEx_;

//It's only 13 codes need to change form Sms to UniEx
static const _Sms_AscEx_ SmsToAscEx[]={
{0x24,0xa4},{0x40,0xa1},{0x5b,0xc4},{0x5c,0xd6},
{0x5d,0xd1},{0x5e,0xdc},{0x5f,0xa7},{0x60,0xbf},
{0x7b,0xe4},{0x7c,0xf6},{0x7d,0xf1},{0x7e,0xfc},
{0x7f,0xe0},{0x00,0x40}
};

/*for sms_to_asc, it defined in mfw_mfw.h*/
char ConvertToAscEx(char code)
{
    int i=0;
    while(i<14)//14 is SmsToAscEx[] lenth
    {
        if(SmsToAscEx[i].sms_code==(unsigned char)code)
            return(char)(SmsToAscEx[i].ascEx_code);
        i++;
    }
    return code;
}

char ConvertToSms(char code)
{
    int i=0;
    while(i<14)//14 if SmsToAscEx[] lenth
    {
        if(SmsToAscEx[i].ascEx_code==(unsigned char)code)
            return(char)(SmsToAscEx[i].sms_code);
        i++;
    }
    return code;
}


/*First we must check the code if can convert to asciiEx,if not return 0x00(blank).*/
/*Note:the return val is char*/
char ConvertToAscllEx(unsigned short code)
{
    int i=0,found=0;
    if(code<=0x7f) //if the code <=0x7f, direct return code as char
        return(char)code;

    while(i<0x20&&found!=0)
    {
        if(code==ascExToUni[i])
            found=i;
        else
            found=0;
        i++;
    }
    if(found)
        return(char)found;
    else
        return 0x00;
}

/*This function judge the txt if all is ascex code,
if not or txt is none, return 0;
else return 1;
*/
int All_Is_AscEx(char * txt)
{
    int i=0,is_ascex = 1;
    unsigned char * ss=(unsigned char*)txt;

    if((wstrlen((char*)ss)==0)||(*ss!=0x80))//not unicode txt or lenth is 0;
        return 1;

    ss++;//the first code is 0x80, so ss++;
    while(((*ss<<8)+*(ss+1)!=0) && (is_ascex!=0))
    {
        if(*ss!=0)
            is_ascex=0;//it isn't ascii
        else
            is_ascex=1;
        ss+=2;
    }

    return is_ascex;
}
/*
This function convert the sms code to ascEx code;
if sms_txt is null, or sms_txt is unicode (sms_txt[0]==0x80), or convert fault return 0;
else return success, and convert to ascex.
*/
void Sms_To_AscEx(char* sms_txt,char* ascEx_txt,int len)
{
    int i=0;
    unsigned char *ss=(unsigned char*)sms_txt;
    //if(*ss==0x80||len==0) return;//cannot use wstrlen(ss),because sms char maybe 0x00,zhonghz,2002/9/3

    while(i<len)
    {
        ascEx_txt[i++]=ConvertToAscEx((char)(*ss++));
    }
    ascEx_txt[i]=0x00;
    return;//convert success
}

/*
This function convert the ascEx code to sms code;
if ascEx_txt is null, or ascEx_txt is unicode (ascEx_txt[0]==0x80), or convert fault return 0;
else return success, and convert to sms code.
*/
int AscEx_To_Sms(char* ascEx_txt, char* sms_txt)
{
    int i=0;
    unsigned char *ss=(unsigned char*)ascEx_txt;
    if(wstrlen((char*)ss)<=0||*ss==0x80)
    {
        sms_txt="\0";
        return 0;//ascEx_txt is NULL
    }

    while(*ss!=0)
    {
        sms_txt[i++]=ConvertToSms((char)(*ss++));
    }
    sms_txt[i]=0x00;
    return i;//convert success
}

//len is the quantity of 7bits-char in txt_7bits,
//lenth is the lenth of txt_7bits
void Sim_7bits_8bits(char* txt_7bits, int len,char* txt_8bits)
{
    int i=1,j=1,n=0,lenth;

    if(len<=0) return;//txt_7bits is NULL
    lenth=(len+1)*7/8;
    txt_8bits[0]=(txt_7bits[0]>>1)&0x7f;
    while(i<lenth&&j<len)
    {
        n=i%7;
        if(n==0)
        {
            txt_8bits[j++]=(txt_7bits[i++]>>1)&0x7f;
            continue;
        }
        if(j==len-1)
        {
            txt_8bits[j++]=(txt_7bits[i-1])&(0x7f>>(7-n));
            continue;
        }
        txt_8bits[j++]=((txt_7bits[i-1]<<(7-n))+((txt_7bits[i]>>(n+1))&(0x7f>>n)))&0x7f;
        if(n==6)
            txt_8bits[j++]=txt_7bits[i]&0x7f;
        i++;
    }
    txt_8bits[j]=0x00;
    return;//convert success
}


//lenth is the lenth of txt_7bits
int Sim_8bits_7bits(char* txt_8bits, int lenth,char* txt_7bits)
{
    int i=0,n=0;
    unsigned char *ss=(unsigned char*)txt_8bits;
    if(lenth<=0) return 0;
    while(*ss!=0&&n<lenth)
    {
        txt_7bits[i]=((*ss&0x7f)<<(i%7+1))+((*(ss+1)&0x7f)>>(6-i%7));
        i++;
        ss++;
        n++;
        if(i%7==0)
        {
            ss++;
            n++;
        }
    }
    return lenth;
}
/*
This function convert the unicode to ascEx code;
the parameter "lenth" is lenth of unicode_txt.
Note:the lenth must count by every 8bits.
*/
void Unicode_To_AscEx(char* uni_txt, char* ascEx_txt)
{
    int i=0;
    unsigned char ascchar;
    unsigned short tchar;
    unsigned char * ss=(unsigned char*)uni_txt;

    if(wstrlen((char *)ss)==0||*ss!=0x80)
    {//uni_txt is NULL, or it's not unicode
        ascEx_txt="\0";
        return;
    }

    ss++;
    tchar=(*ss<<8)+*(ss+1);
    i=0;
    while(tchar!=0)
    {
        if(tchar>0xff&&(tchar>0x03a9||tchar<0x0393))
            ascEx_txt[i]=0x20;//it's out off ascEx range,set it 0x00;
        else
        {
            if((ascchar=(unsigned char)ConvertToAscllEx(tchar))!=0x00)//if find the code in ascExToUni table
                ascEx_txt[i]=(char)ascchar;
            else if(tchar>0xff)//if tchar >0xff, set it 0x00;
                ascEx_txt[i]=0x20;
            else
                ascEx_txt[i]=(char)tchar&0xff;
        }
        ss+=2;
        i++;
        tchar=(*ss<<8)+*(ss+1);
    }
    ascEx_txt[i]=0x00;
    return;//convert success
}
/*
*/
char * mmi_Sim_Sys(char* intxt,int lenth,T_MFW_DCS in_dcs, int outlen)
{
    int off_set=0;
    int inlen=lenth;
    T_MFW_DCS indcs=in_dcs;
    unsigned char * ss=(unsigned char*)intxt;
    char * tmptr=(char*)malloc(inlen+3);
    static char * tr;
    free(tr);//free it before allot,zhonghz
    tr=(char *)malloc(inlen+3);

    if(indcs==MFW_MAX_DCS)
    {//for phonebook
        if(*ss==0x80)
        {
            indcs=MFW_DCS_UCS2;
            ss++;
        }
        else
            indcs=MFW_DCS_8bits;
    }

    if(inlen==0||(indcs>MFW_ASCII))
    {//if txt is null,return 0;
        memset(tr,0,3);
        free(tmptr);
        return tr;
    }

    if(indcs==MFW_DCS_UCS2)
    {
        //*if(*ss!=0x80){//if the first code is not 0x80,set it to 0x80;
        tr[0]=0x80;
        off_set=1;
        //}
        memcpy(&tr[off_set],ss,inlen);
        if(inlen>outlen)
            inlen=outlen;
        tr[inlen+off_set]=0;
        tr[inlen+off_set+1]=0;
        tr[inlen+off_set+2]=0;//to avoid crash
    }
    else if(indcs==MFW_DCS_7bits)
    {
        memcpy(tr,ss,inlen);
        Sim_7bits_8bits(tr, inlen, tmptr);
        Sms_To_AscEx(tmptr, tr, inlen);
        if(inlen>outlen)
            inlen=outlen;
        tr[inlen]=0;
        tr[inlen+1]=0;//to avoid crash
    }
    else
    {
        memcpy(tmptr,ss,inlen);
        Sms_To_AscEx(tmptr, tr, inlen);
        if(inlen>outlen)
            inlen=outlen;
        tr[inlen]=0;
        tr[inlen+1]=0;//to avoid crash
    }
    free(tmptr);
    return(tr);
}

UBYTE* mmi_Sys_Sim(char* intxt, USHORT * len,SHORT *dcs)
{
    int lenth=wstrlen(intxt);
    static UBYTE * tr;
    UBYTE * tmptr;
    UBYTE * ss=(UBYTE*)intxt;
    free(tr);
    tr=(UBYTE*)malloc(lenth+3);
    tmptr=(UBYTE*)malloc(lenth+3);
    if((UBYTE)*ss==0x80)
    {
        if(All_Is_AscEx((char*)ss))
        {//if all is ascii, convert txt to asciiEx
            Unicode_To_AscEx((char*)ss, (char*)tmptr);
            AscEx_To_Sms((char*)tmptr, (char*)tr);
            *dcs=MFW_DCS_7bits;
            *len=lenth/2;
        }
        else
        {//else it is unicode
            memcpy(tr,&ss[1],lenth+1);//lenth+1 for two 0 in txt end; remove 0x80
            *dcs=MFW_DCS_UCS2;
            *len=lenth-1;
        }
    }
    else
    {
        AscEx_To_Sms((char*)ss,(char*)tr);
        *dcs=MFW_DCS_7bits;
        *len=lenth;
    }
    free(tmptr);
    return(tr);
}

/*for phonebook */
UBYTE* mmi_Sys_Sim_phb(char* intxt, int * outlen)
{
    int lenth=wstrlen(intxt);
    static UBYTE * tr;
    UBYTE * tmptr;
    UBYTE * ss=(UBYTE*)intxt;
    free(tr);
    tr=(UBYTE*)malloc(lenth+3);
    tmptr=(UBYTE*)malloc(lenth+3);
    if((UBYTE)*ss==0x80)
    {
        if(All_Is_AscEx((char*)ss))
        {//if all is ascii, convert txt to asciiEx
            Unicode_To_AscEx((char*)ss, (char*)tmptr);
            AscEx_To_Sms((char*)tmptr, (char*)tr);
            *outlen=lenth/2;
        }
        else
        {//else it is unicode
            memcpy(tr,ss,lenth+2);//lenth+1 for two 0 in txt end; remove 0x80
            *outlen=lenth;
        }
    }
    else
    {
        AscEx_To_Sms((char*)ss,(char*)tr);
        *outlen=lenth;
    }
    free(tmptr);
    return(tr);
}
/* glowing,2004-06-10, end of import */

⌨️ 快捷键说明

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