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);
|
packed = mm_charset_gsm_pack (gsm->data, gsm->len, 0, &packed_len);
|
||||||
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;
|
|
||||||
|
|
||||||
*scheme = MM_MODEM_GSM_USSD_SCHEME_UCS2;
|
*scheme = MM_MODEM_GSM_USSD_SCHEME_UCS2;
|
||||||
array = g_byte_array_sized_new (strlen (command) * 2);
|
array = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_UCS2, FALSE, error);
|
||||||
if (!mm_modem_charset_byte_array_append (array, command, MM_MODEM_CHARSET_UCS2, &inner_error)) {
|
if (!array) {
|
||||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
g_prefix_error (error, "Failed to encode USSD command in UCS2 charset: ");
|
||||||
"Failed to encode USSD command in UCS2 charset: %s", inner_error->message);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7256,8 +7256,8 @@ ussd_encode (const gchar *command,
|
|||||||
return (GArray *) g_steal_pointer (&barray);
|
return (GArray *) g_steal_pointer (&barray);
|
||||||
}
|
}
|
||||||
|
|
||||||
barray = g_byte_array_sized_new (command_len * 2);
|
barray = mm_modem_charset_bytearray_from_utf8 (command, MM_MODEM_CHARSET_UCS2, FALSE, &inner_error);
|
||||||
if (!mm_modem_charset_byte_array_append (barray, command, MM_MODEM_CHARSET_UCS2, &inner_error)) {
|
if (!barray) {
|
||||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
|
||||||
"Failed to encode USSD command in UCS2 charset: %s", inner_error->message);
|
"Failed to encode USSD command in UCS2 charset: %s", inner_error->message);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -5877,31 +5877,31 @@ modem_3gpp_ussd_send (MMIfaceModem3gppUssd *_self,
|
|||||||
/* USSD Encode/Decode (3GPP/USSD interface) */
|
/* USSD Encode/Decode (3GPP/USSD interface) */
|
||||||
|
|
||||||
static gchar *
|
static gchar *
|
||||||
modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *self,
|
modem_3gpp_ussd_encode (MMIfaceModem3gppUssd *_self,
|
||||||
const gchar *command,
|
const gchar *command,
|
||||||
guint *scheme,
|
guint *scheme,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
MMBroadbandModem *broadband = MM_BROADBAND_MODEM (self);
|
MMBroadbandModem *self = MM_BROADBAND_MODEM (_self);
|
||||||
gchar *hex = NULL;
|
|
||||||
g_autoptr(GByteArray) ussd_command = NULL;
|
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
|
/* Encode to the current charset (as per AT+CSCS, which is what most modems
|
||||||
* (except for Huawei it seems) will ask for. */
|
* (except for Huawei it seems) will ask for. */
|
||||||
if (mm_modem_charset_byte_array_append (ussd_command, command, broadband->priv->modem_current_charset, NULL)) {
|
ussd_command = mm_modem_charset_bytearray_from_utf8 (command, self->priv->modem_current_charset, FALSE, error);
|
||||||
/* The scheme value does NOT represent the encoding used to encode the string
|
if (!ussd_command) {
|
||||||
* we're giving. This scheme reflects the encoding that the modem should use when
|
g_prefix_error (error, "Failed to encode USSD command: ");
|
||||||
* sending the data out to the network. We're hardcoding this to GSM-7 because
|
return NULL;
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 *
|
static gchar *
|
||||||
|
@@ -96,40 +96,6 @@ charset_iconv_from (MMModemCharset charset)
|
|||||||
return settings ? settings->iconv_name : NULL;
|
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 *
|
gchar *
|
||||||
mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
|
mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
|
||||||
MMModemCharset charset)
|
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
|
/* Take a string encoded in the given charset in binary form, and
|
||||||
* convert it to UTF-8. */
|
* convert it to UTF-8. */
|
||||||
gchar *mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
|
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(GByteArray) array = NULL;
|
||||||
g_autoptr(GError) inner_error = 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! */
|
/* 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,
|
g_set_error (error,
|
||||||
MM_MESSAGE_ERROR,
|
MM_MESSAGE_ERROR,
|
||||||
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
|
MM_MESSAGE_ERROR_INVALID_PDU_PARAMETER,
|
||||||
|
@@ -1368,57 +1368,49 @@ write_bearer_data_message_identifier (MMSmsPart *part,
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static GByteArray *
|
||||||
decide_best_encoding (const gchar *text,
|
decide_best_encoding (const gchar *text,
|
||||||
gpointer log_object,
|
gpointer log_object,
|
||||||
GByteArray **out,
|
|
||||||
guint *num_fields,
|
guint *num_fields,
|
||||||
guint *num_bits_per_field,
|
guint *num_bits_per_field,
|
||||||
Encoding *encoding)
|
Encoding *encoding,
|
||||||
|
GError **error)
|
||||||
{
|
{
|
||||||
guint ascii_unsupported = 0;
|
g_autoptr(GByteArray) barray = NULL;
|
||||||
guint i;
|
MMModemCharset target_charset = MM_MODEM_CHARSET_UNKNOWN;
|
||||||
guint len;
|
guint len;
|
||||||
g_autoptr(GError) error = NULL;
|
|
||||||
|
|
||||||
len = strlen (text);
|
len = strlen (text);
|
||||||
|
|
||||||
/* Check if we can do ASCII-7 */
|
if (mm_charset_can_convert_to (text, MM_MODEM_CHARSET_IRA))
|
||||||
for (i = 0; i < len; i++) {
|
target_charset = MM_MODEM_CHARSET_IRA;
|
||||||
if (text[i] & 0x80) {
|
else if (mm_charset_can_convert_to (text, MM_MODEM_CHARSET_8859_1))
|
||||||
ascii_unsupported++;
|
target_charset = MM_MODEM_CHARSET_8859_1;
|
||||||
break;
|
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 (target_charset == MM_MODEM_CHARSET_IRA) {
|
||||||
if (!ascii_unsupported) {
|
|
||||||
*out = g_byte_array_sized_new (len);
|
|
||||||
g_byte_array_append (*out, (const guint8 *)text, len);
|
|
||||||
*num_fields = len;
|
*num_fields = len;
|
||||||
*num_bits_per_field = 7;
|
*num_bits_per_field = 7;
|
||||||
*encoding = ENCODING_ASCII_7BIT;
|
*encoding = ENCODING_ASCII_7BIT;
|
||||||
return;
|
} else if (target_charset == MM_MODEM_CHARSET_8859_1) {
|
||||||
}
|
*num_fields = barray->len;
|
||||||
|
|
||||||
/* 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;
|
|
||||||
*num_bits_per_field = 8;
|
*num_bits_per_field = 8;
|
||||||
*encoding = ENCODING_LATIN;
|
*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 */
|
return g_steal_pointer (&barray);
|
||||||
*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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -1462,12 +1454,14 @@ write_bearer_data_user_data (MMSmsPart *part,
|
|||||||
|
|
||||||
/* Text or Data */
|
/* Text or Data */
|
||||||
if (text) {
|
if (text) {
|
||||||
decide_best_encoding (text,
|
converted = decide_best_encoding (text,
|
||||||
log_object,
|
log_object,
|
||||||
&converted,
|
&num_fields,
|
||||||
&num_fields,
|
&num_bits_per_field,
|
||||||
&num_bits_per_field,
|
&encoding,
|
||||||
&encoding);
|
error);
|
||||||
|
if (!converted)
|
||||||
|
return FALSE;
|
||||||
aux = (const GByteArray *)converted;
|
aux = (const GByteArray *)converted;
|
||||||
} else {
|
} else {
|
||||||
aux = data;
|
aux = data;
|
||||||
|
Reference in New Issue
Block a user