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,
GError **error)
{
gchar *hex;
guint8 *gsm, *packed;
guint32 len = 0, packed_len = 0;
g_autoptr(GByteArray) gsm = NULL;
g_autofree guint8 *packed = NULL;
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)
return NULL;
@@ -2314,18 +2314,14 @@ encode (MMIfaceModem3gppUssd *self,
/* If command is a multiple of 7 characters long, Huawei firmwares
* apparently want that padded. Maybe all modems?
*/
if (len % 7 == 0) {
gsm = g_realloc (gsm, len + 1);
gsm[len] = 0x0d;
len++;
if (gsm->len % 7 == 0) {
static const guint8 padding = 0x0d;
g_byte_array_append (gsm, &padding, 1);
}
packed = mm_charset_gsm_pack (gsm, len, 0, &packed_len);
hex = mm_utils_bin2hexstr (packed, packed_len);
g_free (packed);
g_free (gsm);
return hex;
packed = mm_charset_gsm_pack (gsm->data, gsm->len, 0, &packed_len);
return mm_utils_bin2hexstr (packed, packed_len);
}
static gchar *

View File

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

View File

@@ -444,8 +444,8 @@ mm_charset_gsm_unpacked_to_utf8 (const guint8 *gsm,
return g_byte_array_free (g_steal_pointer (&utf8), FALSE);
}
guint8 *
mm_charset_utf8_to_unpacked_gsm (const gchar *utf8,
static guint8 *
charset_utf8_to_unpacked_gsm (const gchar *utf8,
gboolean translit,
guint32 *out_len,
GError **error)
@@ -903,7 +903,8 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
break;
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);
break;
@@ -969,18 +970,15 @@ mm_utf8_take_and_convert_to_charset (gchar *str,
static guint8 *
charset_iconv_from_utf8 (const gchar *utf8,
MMModemCharset charset,
const CharsetSettings *settings,
gboolean translit,
guint *out_size,
GError **error)
{
g_autoptr(GError) inner_error = NULL;
const CharsetSettings *settings;
gsize bytes_written = 0;
g_autofree guint8 *encoded = NULL;
settings = lookup_charset_settings (charset);
encoded = (guint8 *) g_convert (utf8, -1,
settings->iconv_name, "UTF-8",
NULL, &bytes_written, &inner_error);
@@ -1015,9 +1013,12 @@ mm_modem_charset_bytearray_from_utf8 (const gchar *utf8,
gboolean translit,
GError **error)
{
const CharsetSettings *settings;
guint8 *encoded = NULL;
guint encoded_size = 0;
settings = lookup_charset_settings (charset);
if (charset == MM_MODEM_CHARSET_UNKNOWN) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS,
"Cannot convert from UTF-8: unknown target charset");
@@ -1026,7 +1027,7 @@ mm_modem_charset_bytearray_from_utf8 (const gchar *utf8,
switch (charset) {
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;
case MM_MODEM_CHARSET_IRA:
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_PCDN:
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;
case MM_MODEM_CHARSET_UNKNOWN:
default:
@@ -1095,15 +1096,12 @@ mm_modem_charset_str_from_utf8 (const gchar *utf8,
static gchar *
charset_iconv_to_utf8 (const guint8 *data,
guint32 len,
MMModemCharset charset,
const CharsetSettings *settings,
gboolean translit,
GError **error)
{
g_autoptr(GError) inner_error = NULL;
g_autofree gchar *utf8 = NULL;
const CharsetSettings *settings;
settings = lookup_charset_settings (charset);
utf8 = g_convert ((const gchar *) data, len,
"UTF-8",
@@ -1144,6 +1142,7 @@ mm_modem_charset_bytearray_to_utf8 (GByteArray *bytearray,
}
settings = lookup_charset_settings (charset);
switch (charset) {
case MM_MODEM_CHARSET_GSM:
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:
utf8 = charset_iconv_to_utf8 (bytearray->data,
bytearray->len,
charset,
settings,
translit,
error);
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,
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,
guint32 len,
gboolean translit,

View File

@@ -984,15 +984,15 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
}
if (encoding == MM_SMS_ENCODING_GSM7) {
guint8 *unpacked, *packed;
guint32 unlen = 0, packlen = 0;
g_autoptr(GByteArray) unpacked = NULL;
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)
goto error;
if (unlen == 0) {
g_free (unpacked);
if (unpacked->len == 0) {
g_set_error_literal (error,
MM_MESSAGE_ERROR,
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
* 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)",
*udl_ptr,
mm_sms_part_get_concat_sequence (part) ? "with" : "without");
packed = mm_charset_gsm_pack (unpacked, unlen, shift, &packlen);
g_free (unpacked);
packed = mm_charset_gsm_pack (unpacked->data, unpacked->len, shift, &packlen);
if (!packed || packlen == 0) {
g_free (packed);
g_set_error_literal (error,
MM_MESSAGE_ERROR,
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
@@ -1020,7 +1018,6 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
}
memcpy (&pdu[offset], packed, packlen);
g_free (packed);
offset += packlen;
} else if (encoding == MM_SMS_ENCODING_UCS2) {
g_autoptr(GByteArray) array = NULL;

View File

@@ -23,25 +23,23 @@
static void
common_test_gsm7 (const gchar *in_utf8)
{
guint32 unpacked_gsm_len = 0;
guint32 packed_gsm_len = 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 *unpacked_gsm_2 = NULL;
g_autofree gchar *built_utf8 = NULL;
g_autoptr(GError) error = NULL;
/* 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_no_error (error);
g_assert_cmpuint (unpacked_gsm_len, >, 0);
/* 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_cmpuint (packed_gsm_len, <=, unpacked_gsm_len);
g_assert_cmpuint (packed_gsm_len, <=, unpacked_gsm->len);
#if 0
{
@@ -356,6 +354,10 @@ test_take_convert_ucs2_bad_ascii2 (void)
static 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;
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);
g_assert_cmpstr (utf8, ==, "T-Mobile");
g_free (utf8);
#endif
}
struct charset_can_convert_to_test_s {