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

📄 phb_utils.c

📁 最新MTK手机软件源码
💻 C
📖 第 1 页 / 共 4 页
字号:
 *  decoded_str         [OUT]       Caller allocated istring_type structure for storing decoded data. Note that caller shall prevent buffer overflow problem.
 * RETURNS
 *  Length of bytes actually decoded. Caller should set this value to the istring's `length' field.
 *  Returns 0 if raw data is not encoded with supported ASCII, 80, 81, 82 encoding.
 *****************************************************************************/
kal_uint8 istring_decode_from_sim(kal_uint8 raw_data_len, kal_uint8 *raw_data, istring_type *decoded_str)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint8 count, length;
    kal_uint8 offset = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT((raw_data != NULL) && (decoded_str != NULL));

    /* First byte is less than 128, coding is default 7-bits */
    if (raw_data[offset] < 0x80)
    {
        kal_uint8 ch;

        decoded_str->charset = CHARSET_ASCII;

        for (count = 0; offset < raw_data_len; ++offset, ++count)
        {
            /* Error, this character is not conformed to default 7-bits encoding. */
            if ((ch = raw_data[offset]) >= 0x80)
            {
                break;
            }

         /**
          * Should we assume a ASCII charset string is terminated by '\0' ?
          * Or, even by '\xFF' ??

         if ((ch == 0) || (ch == 0xFF))
            break; */

            decoded_str->data[count] = ch;

            /* Exceeds capacity, truncate */
            if (count > (decoded_str->length - 1))
            {
                break;
            }
        }
    }
    else if (raw_data[offset] == CODING_UCS2_80)
    {
        kal_uint8 ch1;
        kal_uint8 ch2;

        decoded_str->charset = CHARSET_UCS2;

        if (raw_data_len % 2 == 0)
        {
            raw_data_len--;
        }
        for (count = 0, ++offset; offset < raw_data_len;)
        {
            /* Exceeds capacity, truncate */
            if ((count + 1) >= decoded_str->length)
            {
                break;
            }

            ch1 = raw_data[offset++];
            ch2 = raw_data[offset++];

            if (ch1 == 0xFF && ch2 == 0xFF)
            {
                break;
            }

            decoded_str->data[count++] = ch1;
            decoded_str->data[count++] = ch2;
        }
    }
#if defined(__PHB_0x81_SUPPORT__)       /* Support maximum length of 0x81 and 0x82 format UCS2 */
    /*
     * For 0x81 & 0x82, Keep Original Data, Not Convert, Leave MMI to handle this part
     */
    else if (raw_data[offset] == CODING_UCS2_81)
    {
        /* kal_uint16 ch; */
        kal_uint8 specified_decoded_str_len;

        decoded_str->charset = CHARSET_UCS2_81;

        decoded_str->data[0] = raw_data[0];
        decoded_str->data[1] = raw_data[1];
        specified_decoded_str_len = raw_data[1];
        decoded_str->data[2] = raw_data[2];

        for (count = 3; count < (specified_decoded_str_len + 3); count++)
        {
            /* Exceeds capacity, truncate */
            if ((count + 1) >= decoded_str->length)
            {
                break;
            }

            decoded_str->data[count] = raw_data[count];

        }
    }
    else if (raw_data[offset] == CODING_UCS2_82)
    {
        /* kal_uint16 ch; */
        kal_uint8 specified_decoded_str_len;

        decoded_str->charset = CHARSET_UCS2_82;

        decoded_str->data[0] = raw_data[0];
        decoded_str->data[1] = raw_data[1];
        specified_decoded_str_len = raw_data[1];
        decoded_str->data[2] = raw_data[2];
        decoded_str->data[3] = raw_data[3];

        for (count = 4; count < (specified_decoded_str_len + 4); count++)
        {
            /* Exceeds capacity, truncate */
            if ((count + 1) >= decoded_str->length)
            {
                break;
            }

            decoded_str->data[count] = raw_data[count];
        }
    }
#else /* defined(__PHB_0x81_SUPPORT__) */ 
    else if (raw_data[offset] == CODING_UCS2_81)
    {
        kal_uint16 ch;

        kal_uint8 specified_decoded_str_len;
        kal_uint16 base_ptr;

        decoded_str->charset = CHARSET_UCS2;

        //if (raw_data_len % 2 == 0)
        //   raw_data_len--;

        ++offset;
        specified_decoded_str_len = raw_data[offset++];
        base_ptr = raw_data[offset++] << 7;
        for (count = 0; offset < raw_data_len;)
        {
            ch = raw_data[offset++];

            /* Exceeds capacity, truncate */
            if ((count + 1) >= decoded_str->length)
            {
                break;
            }

            /* All bytes are docoed */
            if ((count + 1) >= (specified_decoded_str_len * 2))
            {
                break;
            }

            if (ch < 0x80)
            {
                decoded_str->data[count++] = 0x0;
                decoded_str->data[count++] = (kal_uint8) ch;
            }
            else
            {
                ch &= 0x7F;
                ch += base_ptr;

                decoded_str->data[count++] = phb_more_significant_char(ch);
                decoded_str->data[count++] = phb_less_significant_char(ch);
            }
        }
    }
    else if (raw_data[offset] == CODING_UCS2_82)
    {
        kal_uint16 ch;

        kal_uint8 specified_decoded_str_len;
        kal_uint16 base_ptr;

        decoded_str->charset = CHARSET_UCS2;

        //if (raw_data_len % 2 == 0)
        //   raw_data_len--;

        ++offset;
        specified_decoded_str_len = raw_data[offset++];
        base_ptr = raw_data[offset++] << 8;
        base_ptr |= raw_data[offset++];
        for (count = 0; offset < raw_data_len;)
        {
            ch = raw_data[offset++];

            /* Exceeds capacity, truncate */
            if ((count + 1) >= (decoded_str->length - 1))
            {
                break;
            }

            /* All bytes are docoed */
            if ((count + 1) >= (specified_decoded_str_len * 2))
            {
                break;
            }

            if (ch < 0x80)
            {
                decoded_str->data[count++] = 0x0;
                decoded_str->data[count++] = (kal_uint8) ch;
            }
            else
            {
                ch &= 0x7F;
                ch += base_ptr;

                decoded_str->data[count++] = phb_more_significant_char(ch);
                decoded_str->data[count++] = phb_less_significant_char(ch);
            }
        }
    }
#endif /* defined(__PHB_0x81_SUPPORT__) */ 
    /* Error */
    else
    {
        return 0;
    }

    length = count;

    /* Complement all rest of bytes to 0xFF */
    for (; count < decoded_str->length; ++count)
    {
        decoded_str->data[count] = 0;
    }

    return length;
}


/*****************************************************************************
 * FUNCTION
 *  istring_decode_0x81_to_0x80
 * DESCRIPTION
 *  Decode raw data of alpha id stored in SIM in 0x81 format
 *  into a `istring'.
 * PARAMETERS
 *  raw_data_len        [IN]        Length of raw data
 *  raw_data            [IN]        Raw data
 *  decoded_str         [OUT]       Caller allocated istring_type structure for storing decoded data. Note that caller shall prevent buffer overflow problem.
 * RETURNS
 *  Length of bytes actually decoded. Caller should set this value to the istring's `length' field.
 *  Returns 0 if raw data is not encoded with supported ASCII, 80, 81, 82 encoding.
 *****************************************************************************/
kal_uint8 istring_decode_0x81_to_0x80(kal_uint8 raw_data_len, kal_uint8 *raw_data, istring_type *decoded_str)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint16 ch;

    kal_uint8 specified_decoded_str_len;
    kal_uint16 base_ptr;

    kal_uint8 count, length;
    kal_uint8 offset = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    decoded_str->charset = CHARSET_UCS2;

    ++offset;
    specified_decoded_str_len = raw_data[offset++];
    base_ptr = raw_data[offset++] << 7;
    for (count = 0; offset < raw_data_len;)
    {
        ch = raw_data[offset++];

        /* Exceeds capacity, truncate */
        if ((count + 1) >= decoded_str->length)
        {
            break;
        }

        /* All bytes are docoed */
        if ((count + 1) >= (specified_decoded_str_len * 2))
        {
            break;
        }

        if (ch < 0x80)
        {
            decoded_str->data[count++] = 0x0;
            decoded_str->data[count++] = (kal_uint8) ch;
        }
        else
        {
            ch &= 0x7F;
            ch += base_ptr;

            decoded_str->data[count++] = phb_more_significant_char(ch);
            decoded_str->data[count++] = phb_less_significant_char(ch);
        }
    }

    length = count;

    /* Complement all rest of bytes to 0xFF */
    for (; count < decoded_str->length; ++count)
    {
        decoded_str->data[count] = 0;
    }

    return length;
}


/*****************************************************************************
 * FUNCTION
 *  istring_decode_0x82_to_0x80
 * DESCRIPTION
 *  Decode raw data of alpha id stored in SIM in 0x82 format
 *  into a `istring'.
 * PARAMETERS
 *  raw_data_len        [IN]        Length of raw data
 *  raw_data            [IN]        Raw data
 *  decoded_str         [OUT]       Caller allocated istring_type structure for storing decoded data. Note that caller shall prevent buffer overflow problem.
 * RETURNS
 *  Length of bytes actually decoded. Caller should set this value to the istring's `length' field.
 *  Returns 0 if raw data is not encoded with supported ASCII, 80, 81, 82 encoding.
 *****************************************************************************/
kal_uint8 istring_decode_0x82_to_0x80(kal_uint8 raw_data_len, kal_uint8 *raw_data, istring_type *decoded_str)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    kal_uint16 ch;

    kal_uint8 specified_decoded_str_len;
    kal_uint16 base_ptr;

    kal_uint8 count, length;
    kal_uint8 offset = 0;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    decoded_str->charset = CHARSET_UCS2;

    ++offset;
    specified_decoded_str_len = raw_data[offset++];
    base_ptr = raw_data[offset++] << 8;
    base_ptr |= raw_data[offset++];
    for (count = 0; offset < raw_data_len;)
    {
        ch = raw_data[offset++];

        /* Exceeds capacity, truncate */
        if ((count + 1) >= (decoded_str->length - 1))
        {
            break;
        }

        /* All bytes are docoed */
        if ((count + 1) >= (specified_decoded_str_len * 2))
        {
            break;
        }

        if (ch < 0x80)
        {
            decoded_str->data[count++] = 0x0;
            decoded_str->data[count++] = (kal_uint8) ch;
        }
        else
        {
            ch &= 0x7F;
            ch += base_ptr;

            decoded_str->data[count++] = phb_more_significant_char(ch);
            decoded_str->data[count++] = phb_less_significant_char(ch);
        }
    }

    length = count;

    /* Complement all rest of bytes to 0xFF */
    for (; count < decoded_str->length; ++count)
    {
        decoded_str->data[count] = 0;
    }

    return length;

}


/*****************************************************************************
 * FUNCTION
 *  istring_len
 * DESCRIPTION
 *  Returns number of characters in `istring' according to its charset.
 * PARAMETERS
 *  istr        [IN]        The istring
 * RETURNS
 *  Number of characters in `istr' according to its charset.
 *****************************************************************************/
kal_uint8 istring_len(istring_type *istr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    ASSERT(istr != NULL);

    return (get_0338_charset_bits(istr->charset) == CHARSET_ASCII) ? istr->length : istr->length / 2;
}   /* end of get_char_len */

⌨️ 快捷键说明

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