charsets: make translit optional in utf8_to_unpacked_gsm()
If the conversion is not fully compatible, the user of the method needs to request transliteration enabled explicitly in order to avoid returning errors in this method.
This commit is contained in:
@@ -2305,8 +2305,11 @@ encode (MMIfaceModem3gppUssd *self,
|
|||||||
guint8 *gsm, *packed;
|
guint8 *gsm, *packed;
|
||||||
guint32 len = 0, packed_len = 0;
|
guint32 len = 0, packed_len = 0;
|
||||||
|
|
||||||
|
gsm = mm_charset_utf8_to_unpacked_gsm (command, FALSE, &len, error);
|
||||||
|
if (!gsm)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
||||||
gsm = mm_charset_utf8_to_unpacked_gsm (command, &len);
|
|
||||||
|
|
||||||
/* If command is a multiple of 7 characters long, Huawei firmwares
|
/* If command is a multiple of 7 characters long, Huawei firmwares
|
||||||
* apparently want that padded. Maybe all modems?
|
* apparently want that padded. Maybe all modems?
|
||||||
|
@@ -4777,10 +4777,9 @@ ussd_encode (const gchar *command,
|
|||||||
guint32 packed_len = 0;
|
guint32 packed_len = 0;
|
||||||
|
|
||||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
||||||
gsm = mm_charset_utf8_to_unpacked_gsm (command, &len);
|
gsm = mm_charset_utf8_to_unpacked_gsm (command, FALSE, &len, error);
|
||||||
if (!gsm) {
|
if (!gsm) {
|
||||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
g_prefix_error (error, "Failed to encode USSD command in GSM7 charset: ");
|
||||||
"Failed to encode USSD command in GSM7 charset");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
packed = mm_charset_gsm_pack (gsm, len, 0, &packed_len);
|
packed = mm_charset_gsm_pack (gsm, len, 0, &packed_len);
|
||||||
|
@@ -427,16 +427,21 @@ mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
|
|||||||
}
|
}
|
||||||
|
|
||||||
guint8 *
|
guint8 *
|
||||||
mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
||||||
guint32 *out_len)
|
gboolean translit,
|
||||||
|
guint32 *out_len,
|
||||||
|
GError **error)
|
||||||
{
|
{
|
||||||
GByteArray *gsm;
|
g_autoptr(GByteArray) gsm = NULL;
|
||||||
const gchar *c;
|
const gchar *c;
|
||||||
const gchar *next;
|
const gchar *next;
|
||||||
static const guint8 gesc = GSM_ESCAPE_CHAR;
|
static const guint8 gesc = GSM_ESCAPE_CHAR;
|
||||||
|
|
||||||
g_return_val_if_fail (utf8 != NULL, NULL);
|
if (!utf8 || !g_utf8_validate (utf8, -1, NULL)) {
|
||||||
g_return_val_if_fail (g_utf8_validate (utf8, -1, NULL), NULL);
|
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
|
||||||
|
"Couldn't convert UTF-8 to GSM: input UTF-8 validation failed");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* worst case initial length */
|
/* worst case initial length */
|
||||||
gsm = g_byte_array_sized_new (g_utf8_strlen (utf8, -1) * 2 + 1);
|
gsm = g_byte_array_sized_new (g_utf8_strlen (utf8, -1) * 2 + 1);
|
||||||
@@ -446,7 +451,7 @@ mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
|||||||
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
||||||
if (out_len)
|
if (out_len)
|
||||||
*out_len = 0;
|
*out_len = 0;
|
||||||
return g_byte_array_free (gsm, FALSE);
|
return g_byte_array_free (g_steal_pointer (&gsm), FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
next = utf8;
|
next = utf8;
|
||||||
@@ -461,8 +466,16 @@ mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
|||||||
/* Add the escape char */
|
/* Add the escape char */
|
||||||
g_byte_array_append (gsm, &gesc, 1);
|
g_byte_array_append (gsm, &gesc, 1);
|
||||||
g_byte_array_append (gsm, &gch, 1);
|
g_byte_array_append (gsm, &gch, 1);
|
||||||
} else if (utf8_to_gsm_def_char (c, next - c, &gch))
|
} else if (utf8_to_gsm_def_char (c, next - c, &gch)) {
|
||||||
g_byte_array_append (gsm, &gch, 1);
|
g_byte_array_append (gsm, &gch, 1);
|
||||||
|
} else if (translit) {
|
||||||
|
/* add ? */
|
||||||
|
g_byte_array_append (gsm, &gch, 1);
|
||||||
|
} else {
|
||||||
|
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
|
||||||
|
"Couldn't convert UTF-8 char to GSM");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
c = next;
|
c = next;
|
||||||
}
|
}
|
||||||
@@ -473,7 +486,7 @@ mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
|||||||
|
|
||||||
/* Always make sure returned string is NUL terminated */
|
/* Always make sure returned string is NUL terminated */
|
||||||
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
g_byte_array_append (gsm, (guint8 *) "\0", 1);
|
||||||
return g_byte_array_free (gsm, FALSE);
|
return g_byte_array_free (g_steal_pointer (&gsm), FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -872,7 +885,7 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case MM_MODEM_CHARSET_GSM:
|
case MM_MODEM_CHARSET_GSM:
|
||||||
encoded = (gchar *) mm_charset_utf8_to_unpacked_gsm (str, NULL);
|
encoded = (gchar *) mm_charset_utf8_to_unpacked_gsm (str, FALSE, NULL, NULL);
|
||||||
g_free (str);
|
g_free (str);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@@ -54,7 +54,9 @@ gchar *mm_modem_charset_hex_to_utf8 (const gchar *src,
|
|||||||
MMModemCharset charset);
|
MMModemCharset charset);
|
||||||
|
|
||||||
guint8 *mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
guint8 *mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
|
||||||
guint32 *out_len);
|
gboolean translit,
|
||||||
|
guint32 *out_len,
|
||||||
|
GError **error);
|
||||||
guint8 *mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
|
guint8 *mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
|
||||||
guint32 len,
|
guint32 len,
|
||||||
gboolean translit,
|
gboolean translit,
|
||||||
|
@@ -987,8 +987,11 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
|
|||||||
guint8 *unpacked, *packed;
|
guint8 *unpacked, *packed;
|
||||||
guint32 unlen = 0, packlen = 0;
|
guint32 unlen = 0, packlen = 0;
|
||||||
|
|
||||||
unpacked = mm_charset_utf8_to_unpacked_gsm (mm_sms_part_get_text (part), &unlen);
|
unpacked = mm_charset_utf8_to_unpacked_gsm (mm_sms_part_get_text (part), FALSE, &unlen, error);
|
||||||
if (!unpacked || unlen == 0) {
|
if (!unpacked)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
if (unlen == 0) {
|
||||||
g_free (unpacked);
|
g_free (unpacked);
|
||||||
g_set_error_literal (error,
|
g_set_error_literal (error,
|
||||||
MM_MESSAGE_ERROR,
|
MM_MESSAGE_ERROR,
|
||||||
|
@@ -33,8 +33,9 @@ common_test_gsm7 (const gchar *in_utf8)
|
|||||||
g_autoptr(GError) error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
|
|
||||||
/* Convert to GSM */
|
/* Convert to GSM */
|
||||||
unpacked_gsm = mm_charset_utf8_to_unpacked_gsm (in_utf8, &unpacked_gsm_len);
|
unpacked_gsm = mm_charset_utf8_to_unpacked_gsm (in_utf8, FALSE, &unpacked_gsm_len, &error);
|
||||||
g_assert_nonnull (unpacked_gsm);
|
g_assert_nonnull (unpacked_gsm);
|
||||||
|
g_assert_no_error (error);
|
||||||
g_assert_cmpuint (unpacked_gsm_len, >, 0);
|
g_assert_cmpuint (unpacked_gsm_len, >, 0);
|
||||||
|
|
||||||
/* Pack */
|
/* Pack */
|
||||||
|
Reference in New Issue
Block a user