charsets: make charset_utf8_to_unpacked_gsm() private

Use the generic mm_modem_charset_bytearray_from_utf8() instead.
This commit is contained in:
Aleksander Morgado
2021-02-14 15:23:43 +01:00
parent 9c613d33e1
commit 75b37e16b1
6 changed files with 55 additions and 67 deletions

View File

@@ -2301,11 +2301,11 @@ encode (MMIfaceModem3gppUssd *self,
guint *scheme, guint *scheme,
GError **error) GError **error)
{ {
gchar *hex; g_autoptr(GByteArray) gsm = NULL;
guint8 *gsm, *packed; g_autofree guint8 *packed = NULL;
guint32 len = 0, packed_len = 0; guint32 packed_len = 0;
gsm = mm_charset_utf8_to_unpacked_gsm (command, FALSE, &len, error); gsm = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_GSM, FALSE, error);
if (!gsm) if (!gsm)
return NULL; return NULL;
@@ -2314,18 +2314,14 @@ encode (MMIfaceModem3gppUssd *self,
/* 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?
*/ */
if (len % 7 == 0) { if (gsm->len % 7 == 0) {
gsm = g_realloc (gsm, len + 1); static const guint8 padding = 0x0d;
gsm[len] = 0x0d;
len++; g_byte_array_append (gsm, &padding, 1);
} }
packed = mm_charset_gsm_pack (gsm, len, 0, &packed_len); packed = mm_charset_gsm_pack (gsm->data, gsm->len, 0, &packed_len);
hex = mm_utils_bin2hexstr (packed, packed_len); return mm_utils_bin2hexstr (packed, packed_len);
g_free (packed);
g_free (gsm);
return hex;
} }
static gchar * static gchar *

View File

@@ -4771,20 +4771,17 @@ ussd_encode (const gchar *command,
g_autoptr(GByteArray) array = NULL; g_autoptr(GByteArray) array = NULL;
if (mm_charset_can_convert_to (command, MM_MODEM_CHARSET_GSM)) { if (mm_charset_can_convert_to (command, MM_MODEM_CHARSET_GSM)) {
guint8 *gsm; g_autoptr(GByteArray) gsm = NULL;
guint8 *packed; guint8 *packed;
guint32 len = 0; 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, FALSE, &len, error); gsm = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_GSM, FALSE, error);
if (!gsm) { if (!gsm) {
g_prefix_error (error, "Failed to encode USSD command in GSM7 charset: "); g_prefix_error (error, "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->data, gsm->len, 0, &packed_len);
g_free (gsm);
array = g_byte_array_new_take (packed, packed_len); array = g_byte_array_new_take (packed, packed_len);
} else { } else {
g_autoptr(GError) inner_error = NULL; g_autoptr(GError) inner_error = NULL;

View File

@@ -444,11 +444,11 @@ mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
return g_byte_array_free (g_steal_pointer (&utf8), FALSE); return g_byte_array_free (g_steal_pointer (&utf8), FALSE);
} }
guint8 * static guint8 *
mm_charset_utf8_to_unpacked_gsm (const gchar *utf8, charset_utf8_to_unpacked_gsm (const gchar *utf8,
gboolean translit, gboolean translit,
guint32 *out_len, guint32 *out_len,
GError **error) GError **error)
{ {
g_autoptr(GByteArray) gsm = NULL; g_autoptr(GByteArray) gsm = NULL;
const gchar *c; const gchar *c;
@@ -903,7 +903,8 @@ 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, FALSE, NULL, NULL); /* This is WRONG! GSM may have embedded NULs (character @)! */
encoded = mm_modem_charset_str_from_utf8 (str, MM_MODEM_CHARSET_GSM, FALSE, NULL);
g_free (str); g_free (str);
break; break;
@@ -968,19 +969,16 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
/* Main conversion functions */ /* Main conversion functions */
static guint8 * static guint8 *
charset_iconv_from_utf8 (const gchar *utf8, charset_iconv_from_utf8 (const gchar *utf8,
MMModemCharset charset, const CharsetSettings *settings,
gboolean translit, gboolean translit,
guint *out_size, guint *out_size,
GError **error) GError **error)
{ {
g_autoptr(GError) inner_error = NULL; g_autoptr(GError) inner_error = NULL;
const CharsetSettings *settings;
gsize bytes_written = 0; gsize bytes_written = 0;
g_autofree guint8 *encoded = NULL; g_autofree guint8 *encoded = NULL;
settings = lookup_charset_settings (charset);
encoded = (guint8 *) g_convert (utf8, -1, encoded = (guint8 *) g_convert (utf8, -1,
settings->iconv_name, "UTF-8", settings->iconv_name, "UTF-8",
NULL, &bytes_written, &inner_error); NULL, &bytes_written, &inner_error);
@@ -1015,8 +1013,11 @@ mm_modem_charset_bytearray_from_utf8 (const gchar *utf8,
gboolean translit, gboolean translit,
GError **error) GError **error)
{ {
guint8 *encoded = NULL; const CharsetSettings *settings;
guint encoded_size = 0; guint8 *encoded = NULL;
guint encoded_size = 0;
settings = lookup_charset_settings (charset);
if (charset == MM_MODEM_CHARSET_UNKNOWN) { if (charset == MM_MODEM_CHARSET_UNKNOWN) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
@@ -1026,7 +1027,7 @@ mm_modem_charset_bytearray_from_utf8 (const gchar *utf8,
switch (charset) { switch (charset) {
case MM_MODEM_CHARSET_GSM: case MM_MODEM_CHARSET_GSM:
encoded = mm_charset_utf8_to_unpacked_gsm (utf8, translit, &encoded_size, error); encoded = charset_utf8_to_unpacked_gsm (utf8, translit, &encoded_size, error);
break; break;
case MM_MODEM_CHARSET_IRA: case MM_MODEM_CHARSET_IRA:
case MM_MODEM_CHARSET_8859_1: case MM_MODEM_CHARSET_8859_1:
@@ -1035,7 +1036,7 @@ mm_modem_charset_bytearray_from_utf8 (const gchar *utf8,
case MM_MODEM_CHARSET_PCCP437: case MM_MODEM_CHARSET_PCCP437:
case MM_MODEM_CHARSET_PCDN: case MM_MODEM_CHARSET_PCDN:
case MM_MODEM_CHARSET_UTF16: case MM_MODEM_CHARSET_UTF16:
encoded = charset_iconv_from_utf8 (utf8, charset, translit, &encoded_size, error); encoded = charset_iconv_from_utf8 (utf8, settings, translit, &encoded_size, error);
break; break;
case MM_MODEM_CHARSET_UNKNOWN: case MM_MODEM_CHARSET_UNKNOWN:
default: default:
@@ -1095,15 +1096,12 @@ mm_modem_charset_str_from_utf8 (const gchar *utf8,
static gchar * static gchar *
charset_iconv_to_utf8 (const guint8 *data, charset_iconv_to_utf8 (const guint8 *data,
guint32 len, guint32 len,
MMModemCharset charset, const CharsetSettings *settings,
gboolean translit, gboolean translit,
GError **error) GError **error)
{ {
g_autoptr(GError) inner_error = NULL; g_autoptr(GError) inner_error = NULL;
g_autofree gchar *utf8 = NULL; g_autofree gchar *utf8 = NULL;
const CharsetSettings *settings;
settings = lookup_charset_settings (charset);
utf8 = g_convert ((const gchar *) data, len, utf8 = g_convert ((const gchar *) data, len,
"UTF-8", "UTF-8",
@@ -1144,6 +1142,7 @@ mm_modem_charset_bytearray_to_utf8 (GByteArray *bytearray,
} }
settings = lookup_charset_settings (charset); settings = lookup_charset_settings (charset);
switch (charset) { switch (charset) {
case MM_MODEM_CHARSET_GSM: case MM_MODEM_CHARSET_GSM:
utf8 = (gchar *) mm_charset_gsm_unpacked_to_utf8 (bytearray->data, utf8 = (gchar *) mm_charset_gsm_unpacked_to_utf8 (bytearray->data,
@@ -1160,7 +1159,7 @@ mm_modem_charset_bytearray_to_utf8 (GByteArray *bytearray,
case MM_MODEM_CHARSET_UTF16: case MM_MODEM_CHARSET_UTF16:
utf8 = charset_iconv_to_utf8 (bytearray->data, utf8 = charset_iconv_to_utf8 (bytearray->data,
bytearray->len, bytearray->len,
charset, settings,
translit, translit,
error); error);
break; break;

View File

@@ -57,10 +57,6 @@ gchar *mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
gchar *mm_modem_charset_hex_to_utf8 (const gchar *src, gchar *mm_modem_charset_hex_to_utf8 (const gchar *src,
MMModemCharset charset); MMModemCharset charset);
guint8 *mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
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,

View File

@@ -984,15 +984,15 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
} }
if (encoding == MM_SMS_ENCODING_GSM7) { if (encoding == MM_SMS_ENCODING_GSM7) {
guint8 *unpacked, *packed; g_autoptr(GByteArray) unpacked = NULL;
guint32 unlen = 0, packlen = 0; g_autofree guint8 *packed = NULL;
guint32 packlen = 0;
unpacked = mm_charset_utf8_to_unpacked_gsm (mm_sms_part_get_text (part), FALSE, &unlen, error); unpacked = mm_modem_charset_bytearray_from_utf8 (mm_sms_part_get_text (part), MM_MODEM_CHARSET_GSM, FALSE, error);
if (!unpacked) if (!unpacked)
goto error; goto error;
if (unlen == 0) { if (unpacked->len == 0) {
g_free (unpacked);
g_set_error_literal (error, g_set_error_literal (error,
MM_MESSAGE_ERROR, MM_MESSAGE_ERROR,
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER, MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
@@ -1003,15 +1003,13 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
/* Set real data length, in septets /* Set real data length, in septets
* If we had UDH, add 7 septets * If we had UDH, add 7 septets
*/ */
*udl_ptr = mm_sms_part_get_concat_sequence (part) ? (7 + unlen) : unlen; *udl_ptr = mm_sms_part_get_concat_sequence (part) ? (7 + unpacked->len) : unpacked->len;
mm_obj_dbg (log_object, " user data length is %u septets (%s UDH)", mm_obj_dbg (log_object, " user data length is %u septets (%s UDH)",
*udl_ptr, *udl_ptr,
mm_sms_part_get_concat_sequence (part) ? "with" : "without"); mm_sms_part_get_concat_sequence (part) ? "with" : "without");
packed = mm_charset_gsm_pack (unpacked, unlen, shift, &packlen); packed = mm_charset_gsm_pack (unpacked->data, unpacked->len, shift, &packlen);
g_free (unpacked);
if (!packed || packlen == 0) { if (!packed || packlen == 0) {
g_free (packed);
g_set_error_literal (error, g_set_error_literal (error,
MM_MESSAGE_ERROR, MM_MESSAGE_ERROR,
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER, MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
@@ -1020,7 +1018,6 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
} }
memcpy (&pdu[offset], packed, packlen); memcpy (&pdu[offset], packed, packlen);
g_free (packed);
offset += packlen; offset += packlen;
} else if (encoding == MM_SMS_ENCODING_UCS2) { } else if (encoding == MM_SMS_ENCODING_UCS2) {
g_autoptr(GByteArray) array = NULL; g_autoptr(GByteArray) array = NULL;

View File

@@ -23,25 +23,23 @@
static void static void
common_test_gsm7 (const gchar *in_utf8) common_test_gsm7 (const gchar *in_utf8)
{ {
guint32 unpacked_gsm_len = 0;
guint32 packed_gsm_len = 0; guint32 packed_gsm_len = 0;
guint32 unpacked_gsm_len_2 = 0; guint32 unpacked_gsm_len_2 = 0;
g_autofree guint8 *unpacked_gsm = NULL; g_autoptr(GByteArray) unpacked_gsm = NULL;
g_autofree guint8 *packed_gsm = NULL; g_autofree guint8 *packed_gsm = NULL;
g_autofree guint8 *unpacked_gsm_2 = NULL; g_autofree guint8 *unpacked_gsm_2 = NULL;
g_autofree gchar *built_utf8 = NULL; g_autofree gchar *built_utf8 = NULL;
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, FALSE, &unpacked_gsm_len, &error); unpacked_gsm = mm_modem_charset_bytearray_from_utf8 (in_utf8, MM_MODEM_CHARSET_GSM, FALSE, &error);
g_assert_nonnull (unpacked_gsm); g_assert_nonnull (unpacked_gsm);
g_assert_no_error (error); g_assert_no_error (error);
g_assert_cmpuint (unpacked_gsm_len, >, 0);
/* Pack */ /* Pack */
packed_gsm = mm_charset_gsm_pack (unpacked_gsm, unpacked_gsm_len, 0, &packed_gsm_len); packed_gsm = mm_charset_gsm_pack (unpacked_gsm->data, unpacked_gsm->len, 0, &packed_gsm_len);
g_assert_nonnull (packed_gsm); g_assert_nonnull (packed_gsm);
g_assert_cmpuint (packed_gsm_len, <=, unpacked_gsm_len); g_assert_cmpuint (packed_gsm_len, <=, unpacked_gsm->len);
#if 0 #if 0
{ {
@@ -356,6 +354,10 @@ test_take_convert_ucs2_bad_ascii2 (void)
static void static void
test_take_convert_gsm_utf8 (void) test_take_convert_gsm_utf8 (void)
{ {
/* NOTE: this is wrong, charset GSM may contain embedded NULs so we cannot convert
* from a plain UTF-8 string to a NUL-terminated string in GSM, as there is no
* such, thing. */
#if 0
gchar *src, *converted, *utf8; gchar *src, *converted, *utf8;
src = g_strdup ("T-Mobile"); src = g_strdup ("T-Mobile");
@@ -364,6 +366,7 @@ test_take_convert_gsm_utf8 (void)
utf8 = mm_utf8_take_and_convert_to_charset (converted, MM_MODEM_CHARSET_GSM); utf8 = mm_utf8_take_and_convert_to_charset (converted, MM_MODEM_CHARSET_GSM);
g_assert_cmpstr (utf8, ==, "T-Mobile"); g_assert_cmpstr (utf8, ==, "T-Mobile");
g_free (utf8); g_free (utf8);
#endif
} }
struct charset_can_convert_to_test_s { struct charset_can_convert_to_test_s {