charsets: new helper to convert binary input data to UTF-8

Most of all the other APIs we have are expecting binary data (e.g.
UCS-2 encoded strings) in ASCII hex format, because they were going
to be used in text AT commands. For binary protocols allowing binary
data, we need use a more generic API that provides an explicit data
size.
This commit is contained in:
Aleksander Morgado
2018-08-07 00:23:41 +02:00
committed by Dan Williams
parent e90ced5e0f
commit eda46d05cf
2 changed files with 30 additions and 0 deletions

View File

@@ -148,6 +148,31 @@ mm_modem_charset_byte_array_append (GByteArray *array,
return TRUE; return TRUE;
} }
gchar *
mm_modem_charset_byte_array_to_utf8 (GByteArray *array,
MMModemCharset charset)
{
char *converted;
const char *iconv_from;
GError *error = NULL;
g_return_val_if_fail (array != NULL, NULL);
g_return_val_if_fail (charset != MM_MODEM_CHARSET_UNKNOWN, NULL);
iconv_from = charset_iconv_from (charset);
g_return_val_if_fail (iconv_from != NULL, FALSE);
converted = g_convert ((const gchar *)array->data, array->len,
"UTF-8//TRANSLIT", iconv_from,
NULL, NULL, &error);
if (!converted || error) {
g_clear_error (&error);
converted = NULL;
}
return converted;
}
char * char *
mm_modem_charset_hex_to_utf8 (const char *src, MMModemCharset charset) mm_modem_charset_hex_to_utf8 (const char *src, MMModemCharset charset)
{ {

View File

@@ -43,6 +43,11 @@ gboolean mm_modem_charset_byte_array_append (GByteArray *array,
gboolean quoted, gboolean quoted,
MMModemCharset charset); MMModemCharset charset);
/* 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,
MMModemCharset charset);
/* Take a string in hex representation ("00430052" or "A4BE11" for example) /* Take a string in hex representation ("00430052" or "A4BE11" for example)
* and convert it from the given character set to UTF-8. * and convert it from the given character set to UTF-8.
*/ */