charsets: use new bytearray_from_utf8() instead of byte_array_append()
This commit is contained in:
@@ -4784,13 +4784,10 @@ ussd_encode (const gchar *command,
|
||||
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;
|
||||
|
||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_UCS2;
|
||||
array = g_byte_array_sized_new (strlen (command) * 2);
|
||||
if (!mm_modem_charset_byte_array_append (array, command, MM_MODEM_CHARSET_UCS2, &inner_error)) {
|
||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
||||
"Failed to encode USSD command in UCS2 charset: %s", inner_error->message);
|
||||
array = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_UCS2, FALSE, error);
|
||||
if (!array) {
|
||||
g_prefix_error (error, "Failed to encode USSD command in UCS2 charset: ");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@@ -7256,8 +7256,8 @@ ussd_encode (const gchar *command,
|
||||
return (GArray *) g_steal_pointer (&barray);
|
||||
}
|
||||
|
||||
barray = g_byte_array_sized_new (command_len * 2);
|
||||
if (!mm_modem_charset_byte_array_append (barray, command, MM_MODEM_CHARSET_UCS2, &inner_error)) {
|
||||
barray = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_UCS2, FALSE, &inner_error);
|
||||
if (!barray) {
|
||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
||||
"Failed to encode USSD command in UCS2 charset: %s", inner_error->message);
|
||||
return NULL;
|
||||
|
@@ -5877,31 +5877,31 @@ modem_3gpp_ussd_send (MMIfaceModem3gppUssd *_self,
|
||||
/* USSD Encode/Decode (3GPP/USSD interface) */
|
||||
|
||||
static gchar *
|
||||
modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *self,
|
||||
modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *_self,
|
||||
const gchar *command,
|
||||
guint *scheme,
|
||||
GError **error)
|
||||
{
|
||||
MMBroadbandModem *broadband = MM_BROADBAND_MODEM (self);
|
||||
gchar *hex = NULL;
|
||||
MMBroadbandModem *self = MM_BROADBAND_MODEM (_self);
|
||||
g_autoptr(GByteArray) ussd_command = NULL;
|
||||
|
||||
ussd_command = g_byte_array_new ();
|
||||
|
||||
/* Encode to the current charset (as per AT+CSCS, which is what most modems
|
||||
* (except for Huawei it seems) will ask for. */
|
||||
if (mm_modem_charset_byte_array_append (ussd_command, command, broadband->priv->modem_current_charset, NULL)) {
|
||||
/* The scheme value does NOT represent the encoding used to encode the string
|
||||
* we're giving. This scheme reflects the encoding that the modem should use when
|
||||
* sending the data out to the network. We're hardcoding this to GSM-7 because
|
||||
* USSD commands fit well in GSM-7, unlike USSD responses that may contain code
|
||||
* points that may only be encoded in UCS-2. */
|
||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
||||
/* convert to hex representation */
|
||||
hex = mm_utils_bin2hexstr (ussd_command->data, ussd_command->len);
|
||||
ussd_command = mm_modem_charset_bytearray_from_utf8 (command, self->priv->modem_current_charset, FALSE, error);
|
||||
if (!ussd_command) {
|
||||
g_prefix_error (error, "Failed to encode USSD command: ");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return hex;
|
||||
/* The scheme value does NOT represent the encoding used to encode the string
|
||||
* we're giving. This scheme reflects the encoding that the modem should use when
|
||||
* sending the data out to the network. We're hardcoding this to GSM-7 because
|
||||
* USSD commands fit well in GSM-7, unlike USSD responses that may contain code
|
||||
* points that may only be encoded in UCS-2. */
|
||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_7BIT;
|
||||
|
||||
/* convert to hex representation */
|
||||
return (gchar *) mm_utils_bin2hexstr (ussd_command->data, ussd_command->len);
|
||||
}
|
||||
|
||||
static gchar *
|
||||
|
@@ -96,40 +96,6 @@ charset_iconv_from (MMModemCharset charset)
|
||||
return settings ? settings->iconv_name : NULL;
|
||||
}
|
||||
|
||||
static const gchar *
|
||||
charset_iconv_to (MMModemCharset charset)
|
||||
{
|
||||
return charset_iconv_from (charset);
|
||||
}
|
||||
|
||||
gboolean
|
||||
mm_modem_charset_byte_array_append (GByteArray *array,
|
||||
const gchar *utf8,
|
||||
MMModemCharset charset,
|
||||
GError **error)
|
||||
{
|
||||
g_autofree gchar *converted = NULL;
|
||||
const gchar *iconv_to;
|
||||
gsize written = 0;
|
||||
|
||||
g_return_val_if_fail (array != NULL, FALSE);
|
||||
g_return_val_if_fail (utf8 != NULL, FALSE);
|
||||
|
||||
iconv_to = charset_iconv_to (charset);
|
||||
g_assert (iconv_to);
|
||||
|
||||
converted = g_convert (utf8, -1, iconv_to, "UTF-8", NULL, &written, error);
|
||||
if (!converted) {
|
||||
g_prefix_error (error, "Failed to convert '%s' to %s character set",
|
||||
utf8, iconv_to);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_byte_array_append (array, (const guint8 *) converted, written);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gchar *
|
||||
mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
|
||||
MMModemCharset charset)
|
||||
|
@@ -37,15 +37,6 @@ MMModemCharset mm_modem_charset_from_string (const gchar *string);
|
||||
|
||||
/*****************************************************************************************/
|
||||
|
||||
/* Append the given string to the given byte array but re-encode it
|
||||
* into the given charset first. The original string is assumed to be
|
||||
* UTF-8 encoded.
|
||||
*/
|
||||
gboolean mm_modem_charset_byte_array_append (GByteArray *array,
|
||||
const gchar *utf8,
|
||||
MMModemCharset charset,
|
||||
GError **error);
|
||||
|
||||
/* Take a string encoded in the given charset in binary form, and
|
||||
* convert it to UTF-8. */
|
||||
gchar *mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
|
||||
|
@@ -1023,10 +1023,9 @@ mm_sms_part_3gpp_get_submit_pdu (MMSmsPart *part,
|
||||
g_autoptr(GByteArray) array = NULL;
|
||||
g_autoptr(GError) inner_error = NULL;
|
||||
|
||||
/* Try to guess a good value for the array */
|
||||
array = g_byte_array_sized_new (strlen (mm_sms_part_get_text (part)) * 2);
|
||||
/* Always assume UTF-16 instead of UCS-2! */
|
||||
if (!mm_modem_charset_byte_array_append (array, mm_sms_part_get_text (part), MM_MODEM_CHARSET_UTF16, &inner_error)) {
|
||||
array = mm_modem_charset_bytearray_from_utf8 (mm_sms_part_get_text (part), MM_MODEM_CHARSET_UTF16, FALSE, &inner_error);
|
||||
if (!array) {
|
||||
g_set_error (error,
|
||||
MM_MESSAGE_ERROR,
|
||||
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
|
||||
|
@@ -1368,57 +1368,49 @@ write_bearer_data_message_identifier (MMSmsPart *part,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
static GByteArray *
|
||||
decide_best_encoding (const gchar *text,
|
||||
gpointer log_object,
|
||||
GByteArray **out,
|
||||
guint *num_fields,
|
||||
guint *num_bits_per_field,
|
||||
Encoding *encoding)
|
||||
Encoding *encoding,
|
||||
GError **error)
|
||||
{
|
||||
guint ascii_unsupported = 0;
|
||||
guint i;
|
||||
guint len;
|
||||
g_autoptr(GError) error = NULL;
|
||||
g_autoptr(GByteArray) barray = NULL;
|
||||
MMModemCharset target_charset = MM_MODEM_CHARSET_UNKNOWN;
|
||||
guint len;
|
||||
|
||||
len = strlen (text);
|
||||
|
||||
/* Check if we can do ASCII-7 */
|
||||
for (i = 0; i < len; i++) {
|
||||
if (text[i] & 0x80) {
|
||||
ascii_unsupported++;
|
||||
break;
|
||||
}
|
||||
if (mm_charset_can_convert_to (text, MM_MODEM_CHARSET_IRA))
|
||||
target_charset = MM_MODEM_CHARSET_IRA;
|
||||
else if (mm_charset_can_convert_to (text, MM_MODEM_CHARSET_8859_1))
|
||||
target_charset = MM_MODEM_CHARSET_8859_1;
|
||||
else
|
||||
target_charset = MM_MODEM_CHARSET_UCS2;
|
||||
|
||||
barray = mm_modem_charset_bytearray_from_utf8 (text, target_charset, FALSE, error);
|
||||
if (!barray) {
|
||||
g_prefix_error (error, "Couldn't decide best encoding: ");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* If ASCII-7 already supported, done we are */
|
||||
if (!ascii_unsupported) {
|
||||
*out = g_byte_array_sized_new (len);
|
||||
g_byte_array_append (*out, (const guint8 *)text, len);
|
||||
if (target_charset == MM_MODEM_CHARSET_IRA) {
|
||||
*num_fields = len;
|
||||
*num_bits_per_field = 7;
|
||||
*encoding = ENCODING_ASCII_7BIT;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check if we can do Latin encoding */
|
||||
if (mm_charset_can_convert_to (text, MM_MODEM_CHARSET_8859_1)) {
|
||||
*out = g_byte_array_sized_new (len);
|
||||
if (!mm_modem_charset_byte_array_append (*out, text, MM_MODEM_CHARSET_8859_1, &error))
|
||||
mm_obj_warn (log_object, "failed to convert to latin encoding: %s", error->message);
|
||||
*num_fields = (*out)->len;
|
||||
} else if (target_charset == MM_MODEM_CHARSET_8859_1) {
|
||||
*num_fields = barray->len;
|
||||
*num_bits_per_field = 8;
|
||||
*encoding = ENCODING_LATIN;
|
||||
return;
|
||||
}
|
||||
} else if (target_charset == MM_MODEM_CHARSET_UCS2) {
|
||||
*num_fields = barray->len / 2;
|
||||
*num_bits_per_field = 16;
|
||||
*encoding = ENCODING_UNICODE;
|
||||
} else
|
||||
g_assert_not_reached ();
|
||||
|
||||
/* If no Latin and no ASCII, default to UTF-16 */
|
||||
*out = g_byte_array_sized_new (len * 2);
|
||||
if (!mm_modem_charset_byte_array_append (*out, text, MM_MODEM_CHARSET_UCS2, &error))
|
||||
mm_obj_warn (log_object, "failed to convert to UTF-16 encoding: %s", error->message);
|
||||
*num_fields = (*out)->len / 2;
|
||||
*num_bits_per_field = 16;
|
||||
*encoding = ENCODING_UNICODE;
|
||||
return g_steal_pointer (&barray);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
@@ -1462,12 +1454,14 @@ write_bearer_data_user_data (MMSmsPart *part,
|
||||
|
||||
/* Text or Data */
|
||||
if (text) {
|
||||
decide_best_encoding (text,
|
||||
log_object,
|
||||
&converted,
|
||||
&num_fields,
|
||||
&num_bits_per_field,
|
||||
&encoding);
|
||||
converted = decide_best_encoding (text,
|
||||
log_object,
|
||||
&num_fields,
|
||||
&num_bits_per_field,
|
||||
&encoding,
|
||||
error);
|
||||
if (!converted)
|
||||
return FALSE;
|
||||
aux = (const GByteArray *)converted;
|
||||
} else {
|
||||
aux = data;
|
||||
|
Reference in New Issue
Block a user