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:
Aleksander Morgado
2020-11-26 23:22:57 +01:00
parent 5ce97abd73
commit bc449cbe87
6 changed files with 41 additions and 20 deletions

View File

@@ -2305,8 +2305,11 @@ encode (MMIfaceModem3gppUssd *self,
guint8 *gsm, *packed;
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;
gsm = mm_charset_utf8_to_unpacked_gsm (command, &len);
/* If command is a multiple of 7 characters long, Huawei firmwares
* apparently want that padded. Maybe all modems?

View File

@@ -4777,10 +4777,9 @@ ussd_encode (const gchar *command,
guint32 packed_len = 0;
*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) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Failed to encode USSD command in GSM7 charset");
g_prefix_error (error, "Failed to encode USSD command in GSM7 charset: ");
return NULL;
}
packed = mm_charset_gsm_pack (gsm, len, 0, &packed_len);

View File

@@ -428,15 +428,20 @@ mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
guint8 *
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 *next;
static const guint8 gesc = GSM_ESCAPE_CHAR;
g_return_val_if_fail (utf8 != NULL, NULL);
g_return_val_if_fail (g_utf8_validate (utf8, -1, NULL), NULL);
if (!utf8 || !g_utf8_validate (utf8, -1, 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 */
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);
if (out_len)
*out_len = 0;
return g_byte_array_free (gsm, FALSE);
return g_byte_array_free (g_steal_pointer (&gsm), FALSE);
}
next = utf8;
@@ -461,8 +466,16 @@ mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
/* Add the escape char */
g_byte_array_append (gsm, &gesc, 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);
} 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;
}
@@ -473,7 +486,7 @@ mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
/* Always make sure returned string is NUL terminated */
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
@@ -872,7 +885,7 @@ 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);
encoded = (gchar *) mm_charset_utf8_to_unpacked_gsm (str, FALSE, NULL, NULL);
g_free (str);
break;

View File

@@ -54,7 +54,9 @@ gchar *mm_modem_charset_hex_to_utf8 (const gchar *src,
MMModemCharset charset);
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,
guint32 len,
gboolean translit,

View File

@@ -987,8 +987,11 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
guint8 *unpacked, *packed;
guint32 unlen = 0, packlen = 0;
unpacked = mm_charset_utf8_to_unpacked_gsm (mm_sms_part_get_text (part), &unlen);
if (!unpacked || unlen == 0) {
unpacked = mm_charset_utf8_to_unpacked_gsm (mm_sms_part_get_text (part), FALSE, &unlen, error);
if (!unpacked)
goto error;
if (unlen == 0) {
g_free (unpacked);
g_set_error_literal (error,
MM_MESSAGE_ERROR,

View File

@@ -33,8 +33,9 @@ common_test_gsm7 (const gchar *in_utf8)
g_autoptr(GError) error = NULL;
/* 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_no_error (error);
g_assert_cmpuint (unpacked_gsm_len, >, 0);
/* Pack */