charsets: take_and_convert() methods should support GSM encoding
The iconv() operation would fail because we wouldn't give any proper charset string to convert to/from. Use our custom GSM encoding support instead.
This commit is contained in:
@@ -458,7 +458,8 @@ mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm, guint32 len)
|
||||
g_byte_array_append (utf8, (guint8 *) "?", 1);
|
||||
}
|
||||
|
||||
g_byte_array_append (utf8, (guint8 *) "\0", 1); /* NULL terminator */
|
||||
/* Always make sure returned string is NUL terminated */
|
||||
g_byte_array_append (utf8, (guint8 *) "\0", 1);
|
||||
return g_byte_array_free (utf8, FALSE);
|
||||
}
|
||||
|
||||
@@ -471,7 +472,6 @@ mm_charset_utf8_to_unpacked_gsm (const char *utf8, guint32 *out_len)
|
||||
int i = 0;
|
||||
|
||||
g_return_val_if_fail (utf8 != NULL, NULL);
|
||||
g_return_val_if_fail (out_len != NULL, NULL);
|
||||
g_return_val_if_fail (g_utf8_validate (utf8, -1, NULL), NULL);
|
||||
|
||||
/* worst case initial length */
|
||||
@@ -480,7 +480,8 @@ mm_charset_utf8_to_unpacked_gsm (const char *utf8, guint32 *out_len)
|
||||
if (*utf8 == 0x00) {
|
||||
/* Zero-length string */
|
||||
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
||||
*out_len = 0;
|
||||
if (out_len)
|
||||
*out_len = 0;
|
||||
return g_byte_array_free (gsm, FALSE);
|
||||
}
|
||||
|
||||
@@ -501,7 +502,12 @@ mm_charset_utf8_to_unpacked_gsm (const char *utf8, guint32 *out_len)
|
||||
i++;
|
||||
}
|
||||
|
||||
*out_len = gsm->len;
|
||||
/* Output length doesn't consider terminating NUL byte */
|
||||
if (out_len)
|
||||
*out_len = gsm->len;
|
||||
|
||||
/* Always make sure returned string is NUL terminated */
|
||||
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
||||
return g_byte_array_free (gsm, FALSE);
|
||||
}
|
||||
|
||||
@@ -757,6 +763,10 @@ mm_charset_take_and_convert_to_utf8 (gchar *str, MMModemCharset charset)
|
||||
break;
|
||||
|
||||
case MM_MODEM_CHARSET_GSM:
|
||||
utf8 = (gchar *) mm_charset_gsm_unpacked_to_utf8 ((const guint8 *) str, strlen (str));
|
||||
g_free (str);
|
||||
break;
|
||||
|
||||
case MM_MODEM_CHARSET_8859_1:
|
||||
case MM_MODEM_CHARSET_PCCP437:
|
||||
case MM_MODEM_CHARSET_PCDN: {
|
||||
@@ -881,6 +891,10 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
|
||||
break;
|
||||
|
||||
case MM_MODEM_CHARSET_GSM:
|
||||
encoded = (gchar *) mm_charset_utf8_to_unpacked_gsm (str, NULL);
|
||||
g_free (str);
|
||||
break;
|
||||
|
||||
case MM_MODEM_CHARSET_8859_1:
|
||||
case MM_MODEM_CHARSET_PCCP437:
|
||||
case MM_MODEM_CHARSET_PCDN: {
|
||||
|
@@ -350,6 +350,19 @@ test_take_convert_ucs2_bad_ascii2 (void)
|
||||
g_assert (converted == NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
test_take_convert_gsm_utf8 (void)
|
||||
{
|
||||
gchar *src, *converted, *utf8;
|
||||
|
||||
src = g_strdup ("T-Mobile");
|
||||
converted = mm_charset_take_and_convert_to_utf8 (src, MM_MODEM_CHARSET_GSM);
|
||||
g_assert_cmpstr (converted, ==, "T-Mobile");
|
||||
utf8 = mm_utf8_take_and_convert_to_charset (converted, MM_MODEM_CHARSET_GSM);
|
||||
g_assert_cmpstr (utf8, ==, "T-Mobile");
|
||||
g_free (utf8);
|
||||
}
|
||||
|
||||
struct charset_can_convert_to_test_s {
|
||||
const char *utf8;
|
||||
gboolean to_gsm;
|
||||
@@ -432,6 +445,7 @@ int main (int argc, char **argv)
|
||||
g_test_add_func ("/MM/charsets/take-convert/ucs2/hex", test_take_convert_ucs2_hex_utf8);
|
||||
g_test_add_func ("/MM/charsets/take-convert/ucs2/bad-ascii", test_take_convert_ucs2_bad_ascii);
|
||||
g_test_add_func ("/MM/charsets/take-convert/ucs2/bad-ascii-2", test_take_convert_ucs2_bad_ascii2);
|
||||
g_test_add_func ("/MM/charsets/take-convert/gsm", test_take_convert_gsm_utf8);
|
||||
|
||||
g_test_add_func ("/MM/charsets/can-convert-to", test_charset_can_covert_to);
|
||||
|
||||
|
Reference in New Issue
Block a user