libmm-glib,common-helpers: make hexstr2bin() return a GError

This util method checks whether the input string is a valid hex
string, so make sure we return a GError on failure.
This commit is contained in:
Aleksander Morgado
2021-02-23 11:35:11 +00:00
parent dbdf67e9f7
commit 657cabcfce
11 changed files with 90 additions and 153 deletions
+17 -39
View File
@@ -155,9 +155,9 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW,
0, NULL);
g_assert (regex);
if (!g_regex_match_full (regex, pco_info, strlen (pco_info), 0, 0, &match_info, error)) {
if (!g_regex_match_full (regex, pco_info, strlen (pco_info), 0, 0, &match_info, error))
return NULL;
}
num_matches = g_match_info_get_match_count (match_info);
if (num_matches != 5) {
@@ -170,22 +170,18 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
}
while (g_match_info_matches (match_info)) {
guint pco_cid;
gchar *pco_id;
gchar *pco_payload;
gsize pco_payload_len;
gchar *pco_payload_bytes = NULL;
gsize pco_payload_bytes_len;
guint8 pco_prefix[6];
guint pco_cid;
g_autofree gchar *pco_id = NULL;
g_autofree gchar *pco_payload = NULL;
g_autofree gchar *pco_payload_bytes = NULL;
gsize pco_payload_bytes_len;
guint8 pco_prefix[6];
GByteArray *pco_raw;
gsize pco_raw_len;
gsize pco_raw_len;
if (!mm_get_uint_from_match_info (match_info, 1, &pco_cid)) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse CID from PCO info: '%s'",
pco_info);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse CID from PCO info: '%s'", pco_info);
break;
}
@@ -197,42 +193,26 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
pco_id = mm_get_string_unquoted_from_match_info (match_info, 3);
if (!pco_id) {
g_set_error (error,
MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse PCO ID from PCO info: '%s'",
pco_info);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse PCO ID from PCO info: '%s'", pco_info);
break;
}
if (g_strcmp0 (pco_id, "FF00")) {
g_free (pco_id);
g_match_info_next (match_info, error);
continue;
}
g_free (pco_id);
pco_payload = mm_get_string_unquoted_from_match_info (match_info, 4);
if (!pco_payload) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't parse PCO payload from PCO info: '%s'",
pco_info);
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't parse PCO payload from PCO info: '%s'", pco_info);
break;
}
pco_payload_len = strlen (pco_payload);
if (pco_payload_len % 2 == 0)
pco_payload_bytes = mm_utils_hexstr2bin (pco_payload, &pco_payload_bytes_len);
g_free (pco_payload);
pco_payload_bytes = mm_utils_hexstr2bin (pco_payload, &pco_payload_bytes_len, error);
if (!pco_payload_bytes) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Invalid PCO payload from PCO info: '%s'",
pco_info);
g_prefix_error (error, "Invalid PCO payload from PCO info '%s': ", pco_info);
break;
}
@@ -267,13 +247,11 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
pco_raw = g_byte_array_sized_new (pco_raw_len);
g_byte_array_append (pco_raw, pco_prefix, sizeof (pco_prefix));
g_byte_array_append (pco_raw, (guint8 *)pco_payload_bytes, pco_payload_bytes_len);
g_free (pco_payload_bytes);
pco = mm_pco_new ();
mm_pco_set_session_id (pco, pco_cid);
mm_pco_set_complete (pco, TRUE);
mm_pco_set_data (pco, pco_raw->data, pco_raw->len);
g_byte_array_unref (pco_raw);
break;
}
+9 -10
View File
@@ -2330,21 +2330,20 @@ decode (MMIfaceModem3gppUssd *self,
const gchar *reply,
GError **error)
{
gchar *bin, *utf8;
guint8 *unpacked;
gsize bin_len;
guint32 unpacked_len;
g_autofree gchar *bin = NULL;
g_autofree guint8 *unpacked = NULL;
gsize bin_len = 0;
guint32 unpacked_len;
bin = mm_utils_hexstr2bin (reply, &bin_len, error);
if (!bin)
return NULL;
bin = mm_utils_hexstr2bin (reply, &bin_len);
unpacked = mm_charset_gsm_unpack ((guint8*) bin, (bin_len * 8) / 7, 0, &unpacked_len);
/* if the last character in a 7-byte block is padding, then drop it */
if ((bin_len % 7 == 0) && (unpacked[unpacked_len - 1] == 0x0d))
unpacked_len--;
utf8 = (char*) mm_charset_gsm_unpacked_to_utf8 (unpacked, unpacked_len);
g_free (bin);
g_free (unpacked);
return utf8;
return (gchar*) mm_charset_gsm_unpacked_to_utf8 (unpacked, unpacked_len);
}
/*****************************************************************************/
+1 -1
View File
@@ -188,7 +188,7 @@ match_info_to_ip4_addr (GMatchInfo *match_info,
else
g_assert_not_reached ();
bin = mm_utils_hexstr2bin (buf, &bin_len);
bin = mm_utils_hexstr2bin (buf, &bin_len, NULL);
if (!bin || bin_len != 4)
goto done;