有关字符串的算法集合

/*****************************************************************************
 * DESCRIPTION:
 *     Conversion unsinged int to string.
 *
 * RETURN VALUE:
 *     uVal[In]: unsinged int value.
 *     str[Out]: Point of the out string buffer.
 *
 * PARAMETER:
 *     void
 *
 * REMARK:
 *
 *****************************************************************************/
void unsingedTostring(unsigned int uVal, CHAR *str)
{
    unsigned int i,j;
    S8 itoaArray[20];

    i = uVal;
    j = 0;
    do
    {
        itoaArray[j] = (CHAR) (i % 10) + 0x30;
        j++;
        i = i / 10;
    } while (i!=0);

    str[j] = 0;
    while (j!=0)
    {
        j--;
        *str = itoaArray[j];
        str++;
    }
}


/****************************************************************************
 *  Function:           ustr_Strlen( U16* ucs2Str )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
ustr_Strlen( const U16* str )
{
        const U16* ustr = str;
 
        while( *ustr++ );
 
        return( (U16)(ustr - str - 1) );
}
 
/****************************************************************************
 *  Function:           ustr_Strcpy( U16* dst, U16* src )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strcpy( U16* dst, const U16* src )
{
    U16* cp = dst;
 
    while( *cp++ = *src++ )
        ;               /* Copy src over dst */
 
    return( dst );
}
 
/****************************************************************************
 *  Function:           ustr_Strncpy( U16* dest, const U16* src, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strncpy( U16* dst, const U16* src, U16 count )
{
    U16* start = dst;
 
    while( count && (*dst++ = *src++) )    /* copy string */
        count--;
 
    if( count )                              /* pad out with zeroes */
        while(--count)
            *dst++ = '/0';
 
    return(start);
}
 
/****************************************************************************
 *  Function:           ustr_Strcat ( U16* dst, const U16* src )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strcat ( U16* dst, const U16* src )
{
    U16 * cp = dst;
 
    while( *cp )
        cp++;                   /* find end of dst */
 
    while( *cp++ = *src++ ) ;   /* Copy src to end of dst */
 
    return( dst );              /* return dst */
}
 
/****************************************************************************
 *  Function:           ustr_Strncat ( U16* front, const U16* back, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_Strncat ( U16* dst, const U16* src, U16 count )
{
    U16 *start = dst;
 
    while (*dst++);
 
    dst--;
 
    while( count-- )
        if( !(*dst++ = *src++) )
            return(start);
 
    *dst = 0;
    return(start);
}
 
/****************************************************************************
 *  Function:           ustr_Strcmp( const U16 * src, const U16 * dst )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Strcmp( const U16 * string1, const U16 * string2 )
{
 S16 ret = 0 ;

 while( !( ret = ( S16 )( *string1 - *string2 ) ) && ( *string1 ) && ( *string2 ) )
 {
  ++string1;
  ++string2;
 }

 if( ret < 0 )
 {
  ret = -1 ;
 }

 if( ret > 0 )
 {
  ret = 1 ;
 }

 return( ret );
}
 
/****************************************************************************
 *  Function:           ustr_Strncmp ( const U16* first, const U16* last, U16 count )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Strncmp ( const U16* string1, const U16* string2, U16 count )
{
    if( !count )
    {
        return( 0 );
    }

    while( ( --count ) && ( *string1 ) && ( *string2 ) && ( *string1 == *string2 ) )
    {
        string1++;
        string2++;
    }

    return( ( S16 )( *string1 - *string2 ) );
}

/* BQC 2004/03/15 Tony Yang, Add for supporting UCS2 file system { */

/****************************************************************************
 *  Function:           ustr_Strstr( const U16 * string, U16 charSet )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16 *
ustr_Strstr( const U16 * string, const U16 * charSet )
{
    const U16 * pu16Temp = string;
   
    if( *string == 0x0000 || *charSet == 0x0000 )
    {
        return NULL;
    }
    else
    {
        while( *string != 0x0000 )
        {
            if( *string == *charSet )
            {
                pu16Temp = string;
               
                for( ; *string == *charSet && *charSet != 0x0000 && *string != 0x0000; string++, charSet++ )
                    ;
               
                if( *charSet == 0x0000 )
                {
                    return ( U16 * )pu16Temp;
                }
                else
                {
                    string = pu16Temp + 1;
                }
            }
            else
            {
                string++;
            }
        }
    }
   
    return NULL;
}

U16 * ustr_Strstrc( const U16 * string, U16 charSet )
{
    const U16 * pu16Temp = string;
   
    while( ( *pu16Temp != 0x0000 ) && ( *pu16Temp != charSet ) )
    {
        pu16Temp++;
    }
   
    return ( U16 * )( ( *pu16Temp != 0x0000 ) ? pu16Temp : NULL );
}

/****************************************************************************
 *  Function:           ustr_Uppercase( U16 * string )
 *--------------------------------------------------------------------------
 *  Description:
 *
 *  Return Value:
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
S16
ustr_Uppercase( U16 * string )
{
    S16 s16Ret = 1;
   
    while( s16Ret > 0 && *string != 0x0000 )
    {
        if( ( *string >= 0x0041 ) && ( *string <= 0x005A ) )
        {
            string++;
        }
        else if ( ( *string >= 0x0061 ) && ( *string <= 0x007A ) )
        {
            *string = *string - 0x0020;
           
            string++;
        }
        else
        {
            s16Ret = 0;
        }
    }
   
    return s16Ret;
}

/* BQC 2004/03/15 Tony Yang, Add for supporting UCS2 file system } */
 
/****************************************************************************
 *  Function:           uisdigit(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a digit.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisdigit( U16 ch )
{
        return (U8)(ch >= '0' && ch <= '9');
}
 
/****************************************************************************
 *  Function:           uisprint( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is undisplay char.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisprint( U16 ch )
{
        if ((ch >= 0x00 && ch <= 0x1F)
                && (ch != 0x09)         /* 0x09 is Tab */
                && (ch != 0x0A)         /* 0x0A is LF */
                && (ch != 0x0D))        /* 0x0D is CR */
        {
                return 0;
        }
        else
        {
                return 1;
        }
 
}
/****************************************************************************
 *  Function:           uisalpha( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisalpha( U16 ch )
{
        return (U8)((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
 
/****************************************************************************
 *  Function:           uislower( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uislower( U16 ch )
{
        return (U8)( ch>='a' && ch<='z' );
}
 
/****************************************************************************
 *  Function:           uisupper( U16 ch )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is an alpha.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisupper( U16 ch )
{
        return (U8)( ch>='A' && ch<='Z' );
}
 
/****************************************************************************
 *  Function:           uisspace(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a space.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisspace(U16 ch)
{
        return (U8)(ch == 0x20);
}
 
/****************************************************************************
 *  Function:           uisnbsp(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is a no breaking space.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisnbsp( U16 ch )
{
        return (U8)(ch == 0xA0);
}
 
/****************************************************************************
 *  Function:           uisbreak(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is CR or LF.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisbreak(U16 ch)
{
        return (U8)(ch == 0x0A || ch == 0x0D);
}
 
/****************************************************************************
 *  Function:           uisCR( U16 ucs2Code )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given unicode expresses CR.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisCR( U16 ch )
{
    return (U8)( ch == 0x0d );
}
 
/****************************************************************************
 *  Function:           uisLF( U16 ucs2Code )
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given unicode expresses LF.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uisLF( U16 ch )
{
    return (U8)( ch == 0x0a );
}
 
/****************************************************************************
 *  Function:           uistab(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        judge whether the given char is Tab key.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
uistab(U16 ch)
{
        return (U8)(ch == 0x09);
 
}       /* end of ustr_IsTab() */
 
/****************************************************************************
 *  Function:           utoupper(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        make the small letter to the capital letter.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
utoupper(U16 ch)
{
        if (ch >= 'a' && ch <= 'z')
                ch -= 'a'-'A';
        return ch;
}
 
/****************************************************************************
 *  Function:           utolower(U16 ch)
 *--------------------------------------------------------------------------
 *  Description:        change the capital letter to the small letter.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
utolower(U16 ch)
{
        if (ch >= 'A' && ch <= 'Z')
                ch += 'a'-'A';
        return ch;
 
}
 
/****************************************************************************
 *  Function:           ustr_AsciiToUCS2( U16* pUnicode, const U8* pAscii )
 *--------------------------------------------------------------------------
 *  Description:        convert Ascii code to unicode
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16*
ustr_AsciiToUCS2( U16* pUnicode, const U8* pAscii )
{
        U16 *pUni = pUnicode;
 
        while( *pUnicode++ = *pAscii++ );
 
        return pUni;
 
}       /* end of ustr_AsciiToUCS2() */
 
/****************************************************************************
 *  Function:           ustr_UCS2ToAscii( U8* pAscii, const U16* pUnicode )
 *--------------------------------------------------------------------------
 *  Description:        convert unicode to Ascii code
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8*
ustr_UCS2ToAscii( U8* pAscii, const U16* pUnicode )
{
        U8 *pAscCode = pAscii;
 
        while( !(*pUnicode & 0xFF00) && (*pAscii++ = (U8)(*pUnicode++)) )
                ;
 
        *pAscii = 0;
 
        return pAscCode;
 
}       /* end of ustr_AsciiToUCS2() */
 
 
/****************************************************************************
 *  Function:           ustr_GSMToUCS2(U16* pUnicode, U16 gsmCode )
 *--------------------------------------------------------------------------
 *  Description:        change the 03.38 gsm code to the standard unicode
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
ustr_GSMToUCS2( U16* pUnicode, U16 gsmCode )
{
        if( (gsmCode & 0xff00) != 0x1B00 )
        {
                /* if the gsm code is not extended, we can directly
                   get out the unicode from the normal codepage accords
                   to the gsm code. */
                *pUnicode = ustrNormGsmCodePage[gsmCode];
                return 1;
        }
        else
        {
                /* to extended gsm code, we must look for the corresponding
                   unicode from the extended codepage. */
                U8 i;
                gsmCode &= 0x00FF;
                for(i=0; i<NUM_OF_EXTENDED_GSMCODE; i++)
                {
                        if (ustrExtGsmCodePage[i].extGsmCode == gsmCode)
                        {
                                *pUnicode = ustrExtGsmCodePage[i].unicode;
                                return 1;
                        }
                }
        }
 
        return 0;
 
}       /* end of ustr_GSMToUCS2() */
 
/****************************************************************************
 *  Function:           ustr_UCS2ToGSM( U16* pGsmCode, U16 unicode )
 *--------------------------------------------------------------------------
 *  Description:        change unicode to the gsm 03.38 code.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8
ustr_UCS2ToGSM( U16* pGsmCode, U16 unicode )
{
        U8 retCode;
        U16     i;
 
        retCode = 0;
 
        if ((unicode >= 0x0020 && unicode <= 0x0023)
                || (unicode >= 0x0025 && unicode <= 0x003F)
                || (unicode >= 0x0041 && unicode <= 0x005A)
                || (unicode >= 0x0061 && unicode <= 0x007A))
        {
                /* in these condictions, the unicode and gsm
                   code is as same. */
                *pGsmCode = (U16)(unicode & 0xFF);
                retCode   = 1;
        }
        else
        {
                for(i=0; i<NUM_OF_NORMAL_GSMCODE && !retCode ; i++)
                {
                        if (ustrNormGsmCodePage[i] == unicode)
                        {
                                *pGsmCode = i;
                                retCode   = 1;
                        }
                }
        }
 
        if( !retCode )
        {
                /* to extended gsm code, we must look for the corresponding
                   gsm code for the unicode from the extended codepage. */
                for( i=0; i<NUM_OF_EXTENDED_GSMCODE && !retCode ; i++ )
                {
                        if (ustrExtGsmCodePage[i].unicode == unicode)
                        {
                                *pGsmCode = ustrExtGsmCodePage[i].extGsmCode;
                                *pGsmCode |= 0x1B00;
                                retCode   =  1;
                        }
                }
        }
 
        return retCode;
 
}       /* end of ustr_UCS2ToGSM() */
 
/****************************************************************************
 *  Function:           ustr_GSMStrToUCS2Str( U8* pUnicode, U8 *gsmCode,
 *                                                                                U16 srcLen , U16 *dstStrLen)
 *--------------------------------------------------------------------------
 *  Description:        change 03.38 gsm code string to standard unicode string
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8 ustr_GSMStrToUCS2Str( U16* pu16Unicode, U8 *gsmCode, U16 srcLen, U16 *dstStrLen)
{
        U16 i;
        U8 success = 1;
        U16 code;
    U16 dstCode;
    U16* pSrcCode = pu16Unicode;
        *dstStrLen = 0;
 
        for(i=0; i<srcLen; i++, gsmCode++)
        {
                if(*gsmCode == 0x1b)
                {
                        code = (U16)(*(gsmCode+1) + (*gsmCode << 8));
 
                        if( ustr_GSMToUCS2(&dstCode, code) )
                        {
                                i++;
                                gsmCode++;
                                *pu16Unicode++ = dstCode;
                                continue;
                        }
                        else
                        {
                                code = *gsmCode;
                        }
                }
                else
                {
                        code = *gsmCode;
                }
 
                if(ustr_GSMToUCS2(&dstCode, code))
        {
            *pu16Unicode++ = dstCode;
        }
        else
        {
            success = 0;
        }
        }
    *pu16Unicode  = 0;
    *dstStrLen = (U16)(pu16Unicode - pSrcCode);
        return success;
 
}       /* end of ustr_GSMStrToUCS2Str() */
 
/****************************************************************************
 *  Function:           ustr_UCS2StrToGSMStr( U8* pGsmCode, U8* unicode,
 *                                                                                U16 srcStrLen, U16 *dstLen )
 *--------------------------------------------------------------------------
 *  Description:        change unicode string to the gsm 03.38 string.
 *
 *  Return Value:       U8
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8 ustr_UCS2StrToGSMStr( U8* pGsmCode, U16* pu16Unicode, U16 srcStrLen, U16 *dstLen )
{
        U8 success = 1;
        U16 i;
        U16 code;
    U8* pSrcCode = pGsmCode;
 
        for(i=0; i<srcStrLen; i++, pu16Unicode++)
        {
                if(ustr_UCS2ToGSM(&code, *pu16Unicode))
                {
                        if(code & 0x1b00)
                        {
                                *pGsmCode++ = 0x1b;
                        }
                *pGsmCode++ = (U8) (code & 0xFF);
                }
                else
                {
                        success = 0;
                }
        }
    *dstLen = (U16)(pGsmCode - pSrcCode);
        *pGsmCode = 0;
        return success;
 
}       /* end of ustr_USC2StrToGSMStr() */
 
/***************************************************************************
 *  Function:           ustr_UCS2ToBcd( )
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              |   | * |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          | * |   |   |  the pointer of string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustr_UCS2ToBcd( ustr_BcdFormatE bcdFmt, U16* pUnicode, U8* pBcd )
{
    U16 strLen;
 
    strLen = ustr_Strlen( pUnicode );
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        ustrUCS2ToBcdUp( pUnicode, strLen, pBcd );
        break;
 
    case BCD_PACKED_LSB_FIRST:
        ustrUCS2ToBcdLf( pUnicode, strLen, pBcd );
        break;
 
    case BCD_PACKED_MSB_FIRST:
        default:
        ustrUCS2ToBcdMf( pUnicode, strLen, pBcd );
        break;
    }
 
}   /* End Of ustr_UCS2ToBcd() */
 
/***************************************************************************
 *  Function:           ustr_BcdToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustr_BcdToUCS2( ustr_BcdFormatE bcdFmt, U8 *pBcd, U16* pUnicode)
{
    /* NOTE: LSB first 1234 = 0x0021 0x0043, bcdLen = 4               */
    /*                 123  = 0x0021 0x00f3, bcdLen = 3               */
    /*       MSB first 1234 = 0x0012 0x0034, bcdLen = 4               */
    /*                 123  = 0x0012 0x003f, bcdLen = 3               */
    /*       unpacked  1234 = 0x0001 0x0002 0x0003 0x0004, bcdLen = 4 */
        U16 bcdLen;
 
        bcdLen = ustr_GetBcdLen( bcdFmt, pBcd );
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        ustrBcdUpToUCS2( pBcd, bcdLen, pUnicode );
        break;
 
    case BCD_PACKED_LSB_FIRST:
        ustrBcdLfToUCS2( pBcd, bcdLen, pUnicode );
        break;
 
    case BCD_PACKED_MSB_FIRST:
        default:
        ustrBcdMfToUCS2( pBcd, bcdLen, pUnicode );
        break;
    }
 
}   /* End Of ustr_BcdToUCS2() */
 
/***************************************************************************
 *  Function  :         ustr_GetBcdLen( )
 *--------------------------------------------------------------------------
 *  Description:        Get bcd number length.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *  bcdFmt            | * |   |   |  bcd code format.
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16
ustr_GetBcdLen( ustr_BcdFormatE bcdFmt, U8 *pBcd )
{
    /* NOTE: LSB first 1234 = 0x21 0x43, bcdLen = 4           */
    /*                 123  = 0x21 0xf3, bcdLen = 3           */
    /*       MSB first 1234 = 0x12 0x34, bcdLen = 4           */
    /*                 123  = 0x12 0x3f, bcdLen = 3           */
    /*       unpacked  1234 = 0x01 0x02 0x03 0x04, bcdLen = 4 */
 
        U16 i;
    U16 bcdLen;
 
    switch( bcdFmt )
    {
        case BCD_UNPACKED:
        bcdLen = 0;
        for( i = 0; pBcd[i] != 0xff && pBcd[i] != 0x0f; i++)
                        ;
        bcdLen = i;
        break;
 
    case BCD_PACKED_LSB_FIRST:
        bcdLen = 0;
        for( i = 0; 1; i++ )
        {
            if( ( (pBcd[i/2] >> (((i+1)&1)?0:4)) & 0x0f ) == 0x0f )
            {
                                /* NOTE: BCD is end by 0x0f */
                break;
            }
            bcdLen ++;
        }
        bcdLen = i;
        break;
 
        case BCD_PACKED_MSB_FIRST:
        default:
        bcdLen = 0;
        for( i = 0; 1; i++ )
        {
            if( ( (pBcd[i/2] >> ((i&1)?0:4)) & 0x0f ) == 0x0f )
            {
               break;
            }
            bcdLen ++;
        }
        bcdLen = i;
        break;
    }
 
    return bcdLen;
 
}   /* End Of ustr_GetBcdLen() */
 
/***************************************************************************
 *  Function:           ustrBcdLfToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert LSB format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdLfToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode )
{
        /*BCD format - LSB first (1234 = 0x0021, 0x0043)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0; i < bcdLen; i++ )
    {
        bcdCode = (U8)((pBcd[i/2] >> (((i+1) & 1) ? 0 : 4)) & 0x0F);
        if( bcdCode == DIALBCD_FILLER )
        {
            break;
        }
 
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*' ||
               unicode == '#' || (unicode == 'P' || unicode == 'p') ||
               unicode == 'W' || unicode == 'w' ) )
                {
                        unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdLfToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrBcdMfToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert MSB format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdMfToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode )
{
        /*BCD format - MSB first (1234 = 0x0012 0x0034)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0;i < bcdLen; i++ )
    {
        bcdCode = (U8)((pBcd[i/2] >> ((i & 1) ? 0 : 4)) & 0x0F);
        if( bcdCode == 0x0f )
                {
                        break;
                }
 
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
           unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdMfToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrBcdUpToUCS2( )
 *--------------------------------------------------------------------------
 *  Description:        Convert unpacked format bcd code to unicode.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *  pBcd              | * |   |   |  the pointer of bcd code.
 *--------------------+---+---+---+-----------------------------------------
 *  bcdLen            | * |   |   |  bcd code length.
 *--------------------+---+---+---+-----------------------------------------
 *  pUnicode          |   | * |   |  the pointer of output string.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
void
ustrBcdUpToUCS2( U8 *pBcd, U16 bcdLen, U16* pUnicode)
{
        /*BCD format - unpacked (1 digit per byte)*/
    U16 i;
    U16 unicode;
        U8  bcdCode;
 
    for( i = 0; i < bcdLen; i++ )
    {
        bcdCode = pBcd[i];
        unicode = (U16)((bcdCode == DIALBCD_STAR) ? '*':
                        (bcdCode == DIALBCD_HASH) ? '#':
                        (bcdCode == DIALBCD_PAUSE)? 'P':
                        (bcdCode == DIALBCD_WILD) ? 'w': (bcdCode + '0'));
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
           unicode = 0;
                }
 
        pUnicode[i] = unicode;
    }
 
    pUnicode[i] = 0;
 
}  /*-- End of ustrBcdUpToUCS2( ) --*/
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdMf()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to MSB format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdMf( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++)
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
            break;
                }
 
        switch( unicode )
        {
        case '*':
            bcdCode = DIALBCD_STAR;
            break;
        case '#':
            bcdCode = DIALBCD_HASH;
            break;
        case 'P':
        case 'p':
            bcdCode = DIALBCD_PAUSE;
            break;
        case 'W':
        case 'w':
            bcdCode = DIALBCD_WILD;
            break;
        default:
            bcdCode = (U8)(unicode - '0');
            break;
        }
        pBcd[i/2] = (U8)(((i & 1) ? pBcd[i/2] : 0) + (bcdCode << ((i+1 & 1) ? 4 : 0)));
    }
    if( i & 1 )
    {
       pBcd[ (i-1) / 2 ] |= 0x0f;
    }
 
}   /* End Of ustrUCS2ToBcdMf() */
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdLf()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to LSB format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdLf( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++ )
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
                        break;
                }
 
        switch( unicode )
        {
            case '*':
                bcdCode = DIALBCD_STAR;
                break;
            case '#':
                bcdCode = DIALBCD_HASH;
                break;
            case 'P':
            case 'p':
                bcdCode = DIALBCD_PAUSE;
                break;
            case 'W':
            case 'w':
                bcdCode = DIALBCD_WILD;
                break;
            default:
                bcdCode = (U8)(unicode - '0');
                break;
        }
        pBcd[i/2] = (U8)(((i & 1) ? pBcd[i/2] : 0) + (bcdCode << ((i & 1) ? 4 : 0)));
    }
    if( i & 1 )
    {
       pBcd[ (i-1) / 2 ] |= 0xf0;
    }
 
}   /* End Of ustrUCS2ToBcdLf() */
 
/***************************************************************************
 *  Function:           ustrUCS2ToBcdUp()
 *--------------------------------------------------------------------------
 *  Description:        Convert unicode to unpacked format bcd code.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
void
ustrUCS2ToBcdUp( U16* pUnicode, U16 uniLen, U8 *pBcd )
{
    U8  bcdCode;
    U16 unicode;
    U16 i;
 
    for( i = 0; i < uniLen; i++)
    {
                if( (unicode = pUnicode[i]) & 0xFF00 )
                {
                        /* invalid unicode */
                        break;
                }
 
        if (unicode == '+')
                {
                        unicode = 'W';
                }
 
        if( !( (unicode >= '0' && unicode <= '9') || unicode == '*'
               || unicode == '#' || (unicode == 'P' || unicode == 'p')
               || unicode == 'W' || unicode == 'w' ) )
                {
            break;
                }
 
        switch( unicode )
        {
            case '*':
                bcdCode = DIALBCD_STAR;
                break;
            case '#':
                bcdCode = DIALBCD_HASH;
                break;
            case 'P':
            case 'p':
                bcdCode = DIALBCD_PAUSE;
                break;
            case 'W':
            case 'w':
                bcdCode = DIALBCD_WILD;
                break;
            default:
                bcdCode = (U8)(unicode - '0');
                break;
        }
        pBcd[i] = bcdCode;
    }
 
}   /* End Of ustrUCS2ToBcdUp() */
 
/***************************************************************************
 *  Function:           ustr_StrcatUCS2StrGSMStr()
 *--------------------------------------------------------------------------
 *  Description:        Strcat one UCS2 string and one GSM string to UCS2String.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
 
U16 ustr_StrcatUCS2StrGSMStr(U16 *frontStr, U16 frontStrLen, U8 *backStr, U16 backStrLen)
{
        U16 i;
        U16 txtIndex;
        txtIndex = frontStrLen;
        for(i= 0; i < backStrLen; i++,backStr++)
        {
                U16 GSMChar, UCS2Char;
                GSMChar = *backStr;
                if (GSMChar == 0x1b)
                {
                        GSMChar = (U16)(*(backStr+1) + (*backStr << 8));
                        i++;
                        backStr++;
                }
                if(ustr_GSMToUCS2(&UCS2Char, GSMChar))
                {
                        frontStr[txtIndex++] = UCS2Char;
                }
                else
                {
                        return 0;
                }
        }
        frontStr[txtIndex] = 0;
        return txtIndex;
}
 
/***************************************************************************
 *  Function:           ustr_StrcatGSMStrUCS2Str()
 *--------------------------------------------------------------------------
 *  Description:        Strcat GSM string and UCS2 string to UCS2String.
 *
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
U16 ustr_StrcatGSMStrUCS2Str(U8 *frontStr, U16 frontStrLen, U8 *backStr, U16 backStrLen)
{
        /* assume memory is enough, copy GSM string to the end of string */
        U16 i;
        U8 *bufStr;
        U16 *pu16BufStr;
        U16 txtIndex = 0;
 
    pu16BufStr = (U16*)frontStr;
        memcpy(&frontStr[frontStrLen*2], frontStr, frontStrLen);
        bufStr = &frontStr[frontStrLen*2];
 
        for(i= 0; i < frontStrLen; i++,bufStr++)
        {
                U16 GSMChar, UCS2Char;
                GSMChar = *bufStr;
                if (GSMChar == 0x1b)
                {
                        GSMChar = (U16)(*(bufStr+1) + (*bufStr << 8));
                        i++;
                        bufStr++;
                }
                if(ustr_GSMToUCS2(&UCS2Char, GSMChar))
                {
                        pu16BufStr[txtIndex++] = UCS2Char;
                }
                else
                {
                        return 0;
                }
        }
        memcpy(&pu16BufStr[txtIndex], backStr, backStrLen*2);
        txtIndex += backStrLen;
        pu16BufStr[txtIndex] = 0;
        return txtIndex;
}
/***************************************************************************
 *  Function:           ustr_UCS2EndianCvrt()
 *--------------------------------------------------------------------------
 *  Description:        Convert the byte order when send/receive signal from network
 *                  Allow source and destination are the same
 *  Return Value:       none
 *
 *  Interface :
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Use
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 ****************************************************************************/
 
void ustr_UCS2EndianCvrt(U8 *dst, U8* src, U16 strLen)
{
        U16 i;
        U8 swap;
        for(i=0; i<strLen; i+=2)
        {
                swap = *src;
                *dst++ = *(++src);
                *dst++ = swap;
                src++;
        }
}
 
 
/****************************************************************************
 *  Function:           U8 ustr_IsUCS2Str(U8* unicode, U16 srcLen)
 *--------------------------------------------------------------------------
 *  Description:        Check whether a string is UCS2 format only
 *
 *  Return Value:       U8, 1 means cannot be represented by defualy
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U8 ustr_IsUCS2Str(U16* pu16Unicode, U16 srcLen)
{
        U8 success = 0;
        U16 i;
        U16 code;
 
        for(i=0; i<srcLen; i+=2, pu16Unicode++)
        {
                if(!ustr_UCS2ToGSM(&code, *pu16Unicode))
        {
                        success = 1;
            return success;
                }
        }
        return success;
 
}       /* end of ustr_USC2StrToGSMStr() */
 
/* *************************************************************
 *              UTF8 to UCS2
 *      this function will return the converted UCS2 string, and changes
 *      the bytelength to the length of UCS2 string.
 *      If the input code is not UTF8, just return the same code
 *  and set the isUTF8 be FALSE
 *****************************************************************/
void ustr_Utf8ToUcs2(U8 *mstr, U16 inStringLen, U16 *ucs2, U16 *byteLength)
{
        U8  *src;
        U16 *dst;
        U16 i,slen,dlen;
 
        slen = 0;
        dlen = 0;
        src = mstr;
        dst = ucs2;
        i = 0;
 
        while(i < inStringLen)
        {
                if((*(src+i) & 0x80) == 0x00)   /* 1 to 2*/
                {
                        *(dst) = (U16)((*(src+i))&0xFF);
                        slen = 1;
                }
                else if((*(src+i) & 0xe0) == 0xc0)      /*2 to 2*/
                {
                        *(dst) = (U16)((((*(src + i)) & 0x1F)<<6)|((*(src + i + 1)) & 0x3f));
                        slen = 2;
                }
                else if((*(src+i) & 0xf0) == 0xe0)      /*3 to 2*/
                {
                        *(dst) = (U16)((((*(src + i)) & 0x0F)<<12)|(((*(src + i + 1)) & 0x3f)<<6)|((*(src + i + 2)) & 0x3f));
                        slen =3;
                }
    else{
     /*Wayne CH Chen */
     /*Review by Maddux Hsu,Jojo Hsu*/
     break;
     
    }
                dst++;
                i += slen;
        }
 
        *byteLength = (U16)((dst - ucs2)*2);
 
}
 
/* *************************************************************
 *              UCS2 to UTF8
 *      this function will return the converted UTF8 string, and changes
 *      the bytelength to the length of UTF8 string.
 *
 *****************************************************************/
void ustr_Ucs2ToUtf8(U16 *mstr, U16 inStringLen, U8 *utf8, U16 *byteLength)
{
        U16 *src;
        U8 *dst;
        U16 i,slen,dlen;
 
        slen = inStringLen;
        dlen = 0;
        src = mstr;
        dst = utf8;
        i = 0;
        while(i < slen)
        {
                if(*(src + i) >= 0x0800) /*3 bytes*/
                {
                        *(dst + 2) = (U8)( 0x80 | ((*(src + i)) & 0x3f));       /*lower byte*/
                        *(dst + 1) = (U8)( 0x80 | (((*(src + i)) & 0x0fc0) >> 6 ));     /*middle byte*/
                        *(dst)     = (U8)( 0xe0 | (((*(src + i)) & 0xf000) >> 12));     /*heighter byte*/
                        dlen = 3;
                }
                else
                {
                        if(((*(src + i)) & 0xFF80) == 0x00)             /*1 bytes*/
                        {
                                *dst = (U8)(*(src + i));
                                dlen = 1;
                        }
                        else    /*2 bytes*/
                        {
                                *(dst + 1) = (U8)( 0x80 | ((*(src + i)) & 0x3f)); /*lower byte*/
                                *(dst)     = (U8)( 0xc0 | (((*(src + i)) & 0x07C0) >> 6) );/*heigher byte*/
                                dlen = 2;
                        }
 
                }
                dst += dlen;
                i++;
        }
 
        *byteLength = (U16)(dst - utf8);
 
}
 
 
/* *************************************************************
 *
 *      this function will check whether the Rapid string is default
 *      alphabet string.
 *
 *****************************************************************/
//add by Maddux
BOOL ustr_IsRapidStrDefaultAlphabet(const U16 *pSrc)
{
        BOOL    ret = TRUE;
        U16 code;
        U16 tempCode;
 
        while(*pSrc)
        {
                code = *pSrc++;
 
                if(!ustr_UCS2ToGSM(&tempCode, code))
                {
                        //non default alphabet string
                        return FALSE;
                }
        }
 
        return ret;
}
 
U8 ustr_decode_sim_alpha_id(U8* pSrc, U16 srcLen, U16* pDest, U16* pDestLen){
        U8                  ret = FALSE;
        U8*                     pSrcEnd;
        U16                     uBaseNbr=0, uUnicode, j;
 
 
        if(!(srcLen && pSrc && pDest && pDestLen))
                return FALSE;
 
        switch(*pSrc)
        {
        case 0x80:
                /* UCS2 */
                /* Byte 1 - Byte 2 - Byte 3 - Byte 4 - Byte 5 - Byte 6 - Byte 7 */
                /* '0x80'-  Ch1MSO - Ch1LSO - Ch2MSO - Ch2LSO -  'FF'  -  'FF'  */
                /* Unused octet is set to '0xFF'                                                                */
 
                /* if the lengh is even, the last octet is unused and is set to 'FF' */
                srcLen -= (U16)((srcLen+1)&1);
 
 
                pSrcEnd = pSrc+srcLen;
 
                for(j=0, pSrc++; pSrc<pSrcEnd; pSrc+=2)
                {
                        uUnicode = (U16)((((U16) *pSrc) << 8) | (U16) *(pSrc+1));
 
                        if( !(uUnicode^0xFFFF) )
                                break;
 
                        *(pDest+j) = uUnicode;
                        j++;
                }
 
                *pDestLen = j;
                ret = TRUE;
                break;
 
        case 0x81:
                /* UCS2 */
                /* Byte 1 - Byte 2 - Byte 3 - Byte 4 - Byte 5 - Byte 6 - Byte 7 - Byte 8 */
                /* '0x81'-   '04'  -  '13'  -  '53'  -  '95'  -  'A6'  -  'FF'  -  'FF'  */
                /* Byte 2: lengh, '04' means there are 4 characters in this string       */
                /* Byte 3: bit 15 - 8 of the base pointer number, 0x0013<<7 := 0x0980    */
                /*         this number will be added to every character with bit 8 set   */
                /*         in this string.                                               */
                /* Byte 4: 0x53, which is < 0x80, means 'S' in GSM Default Coding        */
                /* Byte 5: 0x95, 0x95 & 0x7F := 0x15, 0x15 + 0x0980 := 0x0995            */
                /* Byte 6: 0xA6, 0xA6 & 0x7F := 0x26, 0x26 + 0x0980 := 0x09A6            */
                /* Byte 7: 0xFF, 0xFF & 0x7F := 0x7F, 0x7F + 0x0980 := 0x09FF            */
                /* Byte 8: 0xFF, Not used, since the length is 4 and this is the 5th ch. */
 
                pSrcEnd = pSrc + *(pSrc+1) + 3;
                uBaseNbr = (U16)(*(pSrc +2) << 7);
 
                for(pSrc+=3 ,j=0; pSrc<pSrcEnd; pSrc++)
                {
                        if(!(*pSrc&0x80))
                                ustr_GSMToUCS2(pDest+j, *pSrc);
                        else
                                *(pDest+j) = (U16)(uBaseNbr | (U16) (*(pSrc)&0x7F));
 
                        j++;
                }
                *pDestLen = j;
                ret = TRUE;
                break;
 
        case 0x82:
                /* UCS2 */
                /* Byte 1 - Byte 2 - Byte 3 - Byte 4 - Byte 5 - Byte 6 - Byte 7 - Byte 8 */
                /* '0x82'-   '04'  -  '05'  -  '30'  -  '2D'  -  '82'  -  'D3'  -  '2D'  */
                /* Byte 2: lengh, '04' means there are 4 characters in this string       */
                /* Byte 3:                                                               */
                /* Bute 4: 16 bit base pointer number '0x0530'                           */
                /* Byte 5: 0x2D, which is < 0x80, means '-' in GSM Default Coding        */
                /* Byte 6: 0x82, 0x82 & 0x7F := 0x02, 0x02 + 0x0530 := 0x0532            */
                /* Byte 7: 0xD3, 0xD3 & 0x7F := 0x53, 0x53 + 0x0530 := 0x0583            */
                /* Byte 8: 0x2D, which is < 0x80, means '-' in GSM Default Coding        */
                pSrcEnd = pSrc + *(pSrc+1) + 3;
                uBaseNbr = (U16)(((U16)*(pSrc +2))<<8 | *(pSrc+3));
 
                for(pSrc+=4 ,j=0; pSrc<pSrcEnd; pSrc++)
                {
                        if(!(*pSrc&0x80))
                                ustr_GSMToUCS2(pDest+j, *pSrc);
                        else
                                *(pDest+j) = (U16)(uBaseNbr | (U16) ((*pSrc)&0x7F));
 
                        j++;
                }
                *pDestLen = j;
                ret = TRUE;
                break;
        default:
                /* GSM Default Coding (with bit 8 = 0) */
                /* Unused octet is set to '0xFF' */
                for(j=0; *pSrc != 0xFF; pSrc++)
                {
                        ustr_GSMToUCS2(&uUnicode, *pSrc);
                        *(pDest+j) = uUnicode;
                        j++;
                }
                *pDestLen = j;
                ret = TRUE;
                break;
        }
        return ret;
}
 
 
 
//  ==========================================================================================
//  Function:           utf8_getLen
//  ==========================================================================================
//
//  Prototype           U32 utf8_getLen(U8 *UTF8, U32 totalBytes)
//
//  Header file         ucs2str.h
//
//  Description:        Get the number of total UTF8 charactes for the specified UFT8 string
//
//  Return Value:       The number of total UTF8 charactes
//
//  Parameters:
//  ==========================================================================================
//  Variable Name     |IN |OUT|GLB|  Usage
//  ------------------+---+---+---+----------------------------------------------------------
//  UTF8              | * |   |   | The UTF8 string
//  totalBytes        | * |   |   | Total length in bytes of UTF8
//  ------------------+---+---+---+-----------------------------------------------------------
 
 
 
 
U32 utf8_getLen(U8 *UTF8, U32 totalBytes)
{
        U32     index;
        U32     totalLen = 0;
 
        // count UTF8 character
 
        for (index = 0; index < totalBytes; )
        {
             if ((UTF8[index] & 0x80) == 0x00)
                 index ++;
             else if ((UTF8[index] & 0xE0) == 0xC0)
                 index += 2;
             else if ((UTF8[index] & 0xF0) == 0xE0)
                 index += 3;
             else
             {
                 // illegal UTF8 character
 
                 rpd_error("Error: Wrong UTF8 String/n");
                 return totalLen;
             }
 
             totalLen ++;
        }
 
        return totalLen;
}
 
 
 
//  ==========================================================================================
//  Function:           utf8_getUCS2_Char
//  ==========================================================================================
//
//  Prototype           U32 utf8_getUCS2_Char(U8 *UTF8, U16 *UCS2_Char)
//
//  Header file         ucs2str.h
//
//  Description:        utf8_getUCS2_Char converts the first UTF8 character to a UCS2 character
//
//  Return Value:       0 - wrong UTF8 char
//                      1 - 1 byte-UTF8 char
//                      2 - 2 byte-UTF8 char
//                      3 - 3 byte-UTF8 char
//
//  Parameters:
//  ==========================================================================================
//  Variable Name     |IN |OUT|GLB|  Usage
//  ------------------+---+---+---+----------------------------------------------------------
//  UTF8              | * |   |   | The UTF8 string
//  UCS2_Char         | * |   |   | the converted UCS2 char
//  ------------------+---+---+---+-----------------------------------------------------------
 
 
 
 
U32 utf8_getUCS2_Char(U8 *UTF8, U16 *UCS2_Char)
{
        if ((UTF8[0] & 0x80) == 0x00)
        {
            *UCS2_Char = (U16) UTF8[0];
 
            return 1;
        }
 
        if ((UTF8[0] & 0xE0) == 0xC0)
        {
            *UCS2_Char = (U16) (((UTF8[0] & 0x1F) << 6) | UTF8[1] & 0x3F);
            return 2;
        }
 
        if ((UTF8[0] & 0xF0) == 0xE0)
        {
            *UCS2_Char = (U16) (((UTF8[0] & 0x0F) << 12) | ((UTF8[1] & 0x3f) << 6) | (UTF8[3] & 0x3f));
 
 
            return 3;
        }
 
        return 0;
}
 
//  ==========================================================================================
//  Function:           UCS2_getUTF8Byte
//  ==========================================================================================
//
//  Prototype           U32 UCS2_getUTF8Byte(U16 *UCS2, U32 charNum)
//
//  Header file         ucs2str.h
//
//  Description:        Get the number of total UTF8 bytes for the specified UCS2 string
//
//  Return Value:       The number of total UTF8 charactes
//
//  Parameters:
//  ==========================================================================================
//  Variable Name     |IN |OUT|GLB|  Usage
//  ------------------+---+---+---+----------------------------------------------------------
//  UTF8              | * |   |   | The UTF8 string
//  totalBytes        | * |   |   | Total length in bytes of UTF8
//  ------------------+---+---+---+-----------------------------------------------------------
 
 
 
 
U32 UCS2_getUTF8Byte(U16 *UCS2, U32 charNum)
{
        U32     index;
        U32     totalByte = 0;
 
 
        for (index = 0; index < charNum; index++)
        {
             if (UCS2[index] < 0x80)
                 totalByte ++;
             else if (UCS2[index] >= 0x80 && UCS2[index] < 0x800)
                 totalByte += 2;
             else if (UCS2[index] >= 0x800)
                 totalByte += 3;
             else
             {
                 // illegal UCS2 character
 
                 rpd_error("Error: Wrong UCS2 String/n");
                 return totalByte;
             }
        }
 
        return totalByte;
}
 
 
 
 
//  ==========================================================================================
//  Function: BCD_makeOneByteBCD
//  ==========================================================================================
//
//  Prototype:    U8 BCD_makeOneByteBCD(U8 binary)
//
//  Header file:  ucs2str.h
//
//  Description:  Convert a binary value to a BCD value 2x4bit nibbles network byte order
//
//  Return Value: The BCD value
//
//  Parameters:
//  ==========================================================================================
//  Variable Name     |IN |OUT|GLB|  Usage
//  ------------------+---+---+---+----------------------------------------------------------
//  binary            | * |   |   | input value
//  ------------------+---+---+---+-----------------------------------------------------------
 
U8 BCD_makeOneByteBCD(U8 binary)
{
        if (binary > 99)
        {
            rpd_error("Error: binaryToBCD() fails.");
            return 0;
        }
 
        return  (U8)((binary / 10) + ((binary % 10) << 4));
}
 
 
 
 
 
 
 
 
 
 
 
//  ==========================================================================================
//  Function: BCD_makeTwoByteBCD
//  ==========================================================================================
//
//  Prototype:    U16 BCD_makeTwoByteBCD(U16 binary)
//
//  Header file:  ucs2str.h
//
//  Description:  Convert a binary value to a BCD value 4x4bit nibbles network byte order
//
//  Return Value: The BCD value
//
//  Parameters:
//  ==========================================================================================
//  Variable Name     |IN |OUT|GLB|  Usage
//  ------------------+---+---+---+----------------------------------------------------------
//  binary            | * |   |   | input value
//  ------------------+---+---+---+-----------------------------------------------------------
 
static U16 div_(U16 X, U16 Y, U16 *R)
{
        *R = (U16) (X % Y);
        return (U16) (X / Y);
}
 
 
 
U16 BCD_makeTwoByteBCD(U16 binary)
{
        U16      digit[4];
 
        if (binary > 9999)
        {
            rpd_error("Error: binaryToBCD() fails.");
            return 0;
        }
 
        digit[0] = div_(binary,   1000,  digit + 1);
        digit[1] = div_(digit[1], 100,   digit + 2);
        digit[2] = div_(digit[2], 10,    digit + 3);
 
        return (U16) (digit[0] + (digit[1] << 4) + (digit[2] << 8) + (digit[3] << 12));
}

/* Terry Lu adds for UCS2 */
/****************************************************************************
 *  Function:           ustr_itow (S32 Val, U16 *buf, U8 radix)
 *--------------------------------------------------------------------------
 *  Description:  Convert a Interger to a unicode string
 *
 *  Return Value:  pointer to the converted unicode string
 * 
 * Parameters:   Val   - the Integer you want to convert
 *      *buf  - the output unicode string
 *      radix - the radix, ranged from 2 to 32
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
U16 *ustr_itow (S32 Val, U16 *buf, U8 radix)
{
 U8 astring[INT_SIZE_LENGTH];
 
 ascii_itoa (Val, astring, radix);
 
 ustr_AsciiToUCS2 (buf, astring);

    return (buf);
}

/****************************************************************************
 *  Function:           ascii_itoa (S32 Val, U8 *buf, U8 radix)
 *--------------------------------------------------------------------------
 *  Description:  Convert a Interger to a ascii string
 *
 *  Return Value:  pointer to the converted ascii string
 * 
 * Parameters:   Val   - the Integer you want to convert
 *      *buf  - the output ascii string
 *      radix - the radix, ranged from 2 to 32
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
static U8 *ascii_itoa (S32 val, U8 *buf, U8 radix)
{
   if (radix == 10 && val < 0)
     ascii_xtoa ((unsigned long)val, buf, radix, 1);
   else
        ascii_xtoa ((unsigned long)(unsigned int)val, buf, radix, 0);
   return buf;
}

/****************************************************************************
 *  Function:           ascii_xtoa (U32 val, U8 *buf, U8 radix, int is_neg)
 *--------------------------------------------------------------------------
 *  Description:  Convert a Integer to a ascii string
 *
 *  Return Value:  pointer to the converted ascii string
 * 
 * Parameters:   Val    - the Integer you want to convert
 *      *buf   - the output ascii string
 *      radix  - the radix, ranged from 2 to 32
 *      is_neg - indicate if the number is negitive
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *                    |   |   |   |
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/
static void ascii_xtoa (U32 val, U8 *buf, U8 radix, int is_neg)
{
 U8   *p;                /* pointer to traverse string */
 U8   *firstdig;         /* pointer to first digit */
 U8   temp;              /* temp char */
 U32  digval;        /* value of digit */
 
 p = buf;
 
 if (is_neg) {
  /* negative, so output '-' and negate */
  *p++ = '-';
  val = (U32)(-(S32)val);
 }
 
 firstdig = p;           /* save pointer to first digit */
 
 do {
  digval = (U32) (val % radix);
  val /= radix;       /* get next digit */
  
  /* convert to ascii and store */
  if (digval > 9)
   *p++ = (U8) (digval - 10 + 'a');  /* a letter */
  else
   *p++ = (U8) (digval + '0');       /* a digit */
 } while (val > 0);
 
 /* We now have the digit of the number in the buffer, but in reverse
 order.  Thus we reverse them now. */
 
 *p-- = '/0';            /* terminate string; p points to last digit */
 
 do {
  temp = *p;
  *p = *firstdig;
  *firstdig = temp;   /* swap *p and *firstdig */
  --p;
  ++firstdig;         /* advance to next two digits */
 } while (firstdig < p); /* repeat until halfway */
}

void Unicode2ASCII( char * buffer )
{
 char * pointer_1 = buffer;
 char * pointer_2 = buffer;
 char   count     = 0;

 while( count != 2 )
 {
  if( *pointer_2 != 0x00 )
  {
   *( pointer_1++ ) = *( pointer_2++ );
   
   count = 0;
  }
  else
  {
   pointer_2++;
   count++;
  }
 }
 
 *pointer_1 = 0x00;
}

/***************************************************************************
 *  Function  :         ustr_GetGSMStringLen( )
 *--------------------------------------------------------------------------
 *  Description:        Get gsm string length.
 *
 *  Return Value:       U16
 *
 *  Interface:
 *--------------------------------------------------------------------------
 *  Variable Name     |IN |OUT|GLB|  Usage
 *--------------------+---+---+---+-----------------------------------------
 *--------------------+---+---+---+-----------------------------------------
 *  pgsm              | * |   |   |  the pointer of gsm code.
 *--------------------+---+---+---+-----------------------------------------
 ****************************************************************************/


U16  ustr_GetGSMStringLen(const U16 *pSrc)

{

  U16 code;
        U16 NumOfNormalGSM = 0;
        U16 NumOfExtendGSM = 0;
        U16 NumOfOtherCode = 0;

  U8 i;
 
        while(*pSrc)
        {
   code = *pSrc++;
   
   for(i=0; i<NUM_OF_NORMAL_GSMCODE; i++)
   {
    if (ustrNormGsmCodePage[i] == code)
    {
     ++NumOfNormalGSM;
     break;
    }
   }
   
   
   
   if( i == NUM_OF_NORMAL_GSMCODE)
   {
                for( i=0; i<NUM_OF_EXTENDED_GSMCODE; i++ )
                {
     if (ustrExtGsmCodePage[i].unicode == code)
     {
      ++NumOfExtendGSM;
      break;
     }
                }
   }
   if( i == NUM_OF_EXTENDED_GSMCODE)
   {
      ++NumOfOtherCode;
      
   }
    
        }
   
 
 return NumOfNormalGSM + 2 * NumOfExtendGSM + 2 * NumOfOtherCode;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值