diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2021-02-14 11:06:50 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2021-02-23 11:35:11 +0000 |
commit | 34de613deab1c8728118fce389963ecb29ca51e7 (patch) | |
tree | a86e30325ec4e4e48601d432e5312293a28a0010 | |
parent | 6d8610d63ecb8e53e14486533a580ea4f37c644c (diff) |
libmm-glib,common-helpers: make hexstr2bin() return a guint8 array
It makes much more sense than returning a gchar array, as gchar is
signed.
-rw-r--r-- | libmm-glib/mm-common-helpers.c | 10 | ||||
-rw-r--r-- | libmm-glib/mm-common-helpers.h | 2 | ||||
-rw-r--r-- | plugins/altair/mm-modem-helpers-altair-lte.c | 18 | ||||
-rw-r--r-- | plugins/huawei/mm-broadband-modem-huawei.c | 4 | ||||
-rw-r--r-- | plugins/huawei/mm-modem-helpers-huawei.c | 30 | ||||
-rw-r--r-- | src/mm-base-sim.c | 16 | ||||
-rw-r--r-- | src/mm-charsets.c | 12 | ||||
-rw-r--r-- | src/mm-modem-helpers-qmi.c | 2 | ||||
-rw-r--r-- | src/mm-modem-helpers.c | 2 | ||||
-rw-r--r-- | src/mm-sms-part-3gpp.c | 2 | ||||
-rw-r--r-- | src/mm-sms-part-cdma.c | 2 |
11 files changed, 48 insertions, 52 deletions
diff --git a/libmm-glib/mm-common-helpers.c b/libmm-glib/mm-common-helpers.c index 0245fd52..43fff922 100644 --- a/libmm-glib/mm-common-helpers.c +++ b/libmm-glib/mm-common-helpers.c @@ -1686,18 +1686,18 @@ mm_utils_hex2byte (const gchar *hex) return (a << 4) | b; } -gchar * +guint8 * mm_utils_hexstr2bin (const gchar *hex, gssize len, gsize *out_len, GError **error) { const gchar *ipos = hex; - g_autofree gchar *buf = NULL; + g_autofree guint8 *buf = NULL; gsize i; gint a; - gchar *opos -; + guint8 *opos; + if (len < 0) len = strlen (hex); @@ -1723,7 +1723,7 @@ mm_utils_hexstr2bin (const gchar *hex, ipos[0], ipos[1]); return NULL; } - *opos++ = a; + *opos++ = (guint8)a; ipos += 2; } *out_len = len / 2; diff --git a/libmm-glib/mm-common-helpers.h b/libmm-glib/mm-common-helpers.h index 50d58134..4e6d9784 100644 --- a/libmm-glib/mm-common-helpers.h +++ b/libmm-glib/mm-common-helpers.h @@ -181,7 +181,7 @@ gchar *mm_get_string_unquoted_from_match_info (GMatchInfo *match_info, const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state); gint mm_utils_hex2byte (const gchar *hex); -gchar *mm_utils_hexstr2bin (const gchar *hex, gssize len, gsize *out_len, GError **error); +guint8 *mm_utils_hexstr2bin (const gchar *hex, gssize len, gsize *out_len, GError **error); gchar *mm_utils_bin2hexstr (const guint8 *bin, gsize len); gboolean mm_utils_ishexstr (const gchar *hex); diff --git a/plugins/altair/mm-modem-helpers-altair-lte.c b/plugins/altair/mm-modem-helpers-altair-lte.c index b71b458a..d2fd9af7 100644 --- a/plugins/altair/mm-modem-helpers-altair-lte.c +++ b/plugins/altair/mm-modem-helpers-altair-lte.c @@ -170,14 +170,14 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error) } while (g_match_info_matches (match_info)) { - 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; + guint pco_cid; + g_autofree gchar *pco_id = NULL; + g_autofree gchar *pco_payload = NULL; + g_autofree guint8 *pco_payload_bytes = NULL; + gsize pco_payload_bytes_len; + guint8 pco_prefix[6]; + GByteArray *pco_raw; + 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, @@ -246,7 +246,7 @@ 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_byte_array_append (pco_raw, pco_payload_bytes, pco_payload_bytes_len); pco = mm_pco_new (); mm_pco_set_session_id (pco, pco_cid); diff --git a/plugins/huawei/mm-broadband-modem-huawei.c b/plugins/huawei/mm-broadband-modem-huawei.c index feaabd46..e2c90d14 100644 --- a/plugins/huawei/mm-broadband-modem-huawei.c +++ b/plugins/huawei/mm-broadband-modem-huawei.c @@ -2330,7 +2330,7 @@ decode (MMIfaceModem3gppUssd *self, const gchar *reply, GError **error) { - g_autofree gchar *bin = NULL; + g_autofree guint8 *bin = NULL; g_autofree guint8 *unpacked = NULL; gsize bin_len = 0; guint32 unpacked_len; @@ -2339,7 +2339,7 @@ decode (MMIfaceModem3gppUssd *self, if (!bin) return NULL; - unpacked = mm_charset_gsm_unpack ((guint8*) bin, (bin_len * 8) / 7, 0, &unpacked_len); + unpacked = mm_charset_gsm_unpack (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--; diff --git a/plugins/huawei/mm-modem-helpers-huawei.c b/plugins/huawei/mm-modem-helpers-huawei.c index 49f5a6ea..1b443052 100644 --- a/plugins/huawei/mm-modem-helpers-huawei.c +++ b/plugins/huawei/mm-modem-helpers-huawei.c @@ -157,14 +157,15 @@ mm_huawei_parse_ndisstatqry_response (const gchar *response, static gboolean match_info_to_ip4_addr (GMatchInfo *match_info, - guint match_index, - guint *out_addr) + guint match_index, + guint *out_addr) { - gchar *s, *bin = NULL; - gchar buf[9]; - gsize len, bin_len; - gboolean success = FALSE; - guint32 aux; + g_autofree gchar *s = NULL; + g_autofree guint8 *bin = NULL; + gchar buf[9]; + gsize len; + gsize bin_len; + guint32 aux; s = g_match_info_fetch (match_info, match_index); g_return_val_if_fail (s != NULL, FALSE); @@ -172,11 +173,11 @@ match_info_to_ip4_addr (GMatchInfo *match_info, len = strlen (s); if (len == 1 && s[0] == '0') { *out_addr = 0; - success = TRUE; - goto done; + return TRUE; } + if (len < 7 || len > 8) - goto done; + return FALSE; /* Handle possibly missing leading zero */ memset (buf, 0, sizeof (buf)); @@ -190,16 +191,11 @@ match_info_to_ip4_addr (GMatchInfo *match_info, bin = mm_utils_hexstr2bin (buf, -1, &bin_len, NULL); if (!bin || bin_len != 4) - goto done; + return FALSE; memcpy (&aux, bin, 4); *out_addr = GUINT32_SWAP_LE_BE (aux); - success = TRUE; - -done: - g_free (s); - g_free (bin); - return success; + return TRUE; } gboolean diff --git a/src/mm-base-sim.c b/src/mm-base-sim.c index 78ffdc62..0e00342c 100644 --- a/src/mm-base-sim.c +++ b/src/mm-base-sim.c @@ -1306,9 +1306,9 @@ parse_mnc_length (const gchar *response, (sw1 == 0x91) || (sw1 == 0x92) || (sw1 == 0x9f)) { - gsize buflen = 0; - guint32 mnc_len; - g_autofree gchar *bin = NULL; + gsize buflen = 0; + guint32 mnc_len; + g_autofree guint8 *bin = NULL; /* Convert hex string to binary */ bin = mm_utils_hexstr2bin (hex, -1, &buflen, error); @@ -1323,7 +1323,7 @@ parse_mnc_length (const gchar *response, } /* MNC length is byte 4 of this SIM file */ - mnc_len = bin[3] & 0xFF; + mnc_len = bin[3]; if (mnc_len == 2 || mnc_len == 3) return mnc_len; @@ -1412,8 +1412,8 @@ parse_spn (const gchar *response, (sw1 == 0x91) || (sw1 == 0x92) || (sw1 == 0x9f)) { - gsize buflen = 0; - g_autofree gchar *bin = NULL; + gsize buflen = 0; + g_autofree guint8 *bin = NULL; /* Convert hex string to binary */ bin = mm_utils_hexstr2bin (hex, -1, &buflen, error); @@ -1423,11 +1423,11 @@ parse_spn (const gchar *response, } /* Remove the FF filler at the end */ - while (buflen > 1 && bin[buflen - 1] == (char)0xff) + while (buflen > 1 && bin[buflen - 1] == 0xff) buflen--; /* First byte is metadata; remainder is GSM-7 unpacked into octets; convert to UTF8 */ - return (gchar *)mm_charset_gsm_unpacked_to_utf8 ((guint8 *)bin + 1, buflen - 1); + return (gchar *)mm_charset_gsm_unpacked_to_utf8 (bin + 1, buflen - 1); } g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, diff --git a/src/mm-charsets.c b/src/mm-charsets.c index adc80332..bc33b2ae 100644 --- a/src/mm-charsets.c +++ b/src/mm-charsets.c @@ -157,11 +157,11 @@ gchar * mm_modem_charset_hex_to_utf8 (const gchar *src, MMModemCharset charset) { - const gchar *iconv_from; - g_autofree gchar *unconverted = NULL; - g_autofree gchar *converted = NULL; - g_autoptr(GError) error = NULL; - gsize unconverted_len = 0; + const gchar *iconv_from; + g_autofree guint8 *unconverted = NULL; + g_autofree gchar *converted = NULL; + g_autoptr(GError) error = NULL; + gsize unconverted_len = 0; g_return_val_if_fail (src != NULL, NULL); g_return_val_if_fail (charset != MM_MODEM_CHARSET_UNKNOWN, NULL); @@ -176,7 +176,7 @@ mm_modem_charset_hex_to_utf8 (const gchar *src, if (charset == MM_MODEM_CHARSET_UTF8 || charset == MM_MODEM_CHARSET_IRA) return g_steal_pointer (&unconverted); - converted = g_convert (unconverted, unconverted_len, + converted = g_convert ((const gchar *)unconverted, unconverted_len, "UTF-8//TRANSLIT", iconv_from, NULL, NULL, &error); if (!converted || error) diff --git a/src/mm-modem-helpers-qmi.c b/src/mm-modem-helpers-qmi.c index 6820419e..e4bc7e29 100644 --- a/src/mm-modem-helpers-qmi.c +++ b/src/mm-modem-helpers-qmi.c @@ -1815,7 +1815,7 @@ mm_firmware_unique_id_to_qmi_unique_id (const gchar *unique_id, gsize tmp_len; tmp_len = 0; - tmp = (guint8 *) mm_utils_hexstr2bin (unique_id, -1, &tmp_len, error); + tmp = mm_utils_hexstr2bin (unique_id, -1, &tmp_len, error); if (!tmp) { g_prefix_error (error, "Unexpected character found in unique id: "); return NULL; diff --git a/src/mm-modem-helpers.c b/src/mm-modem-helpers.c index 5baaef09..b4b5515a 100644 --- a/src/mm-modem-helpers.c +++ b/src/mm-modem-helpers.c @@ -4291,7 +4291,7 @@ mm_3gpp_parse_emergency_numbers (const char *raw, GError **error) return NULL; } - bin = (guint8 *) mm_utils_hexstr2bin (raw, -1, &binlen, error); + bin = mm_utils_hexstr2bin (raw, -1, &binlen, error); if (!bin) { g_prefix_error (error, "invalid raw emergency numbers list contents: "); return NULL; diff --git a/src/mm-sms-part-3gpp.c b/src/mm-sms-part-3gpp.c index cdf9a1a0..83181d3b 100644 --- a/src/mm-sms-part-3gpp.c +++ b/src/mm-sms-part-3gpp.c @@ -343,7 +343,7 @@ mm_sms_part_3gpp_new_from_pdu (guint index, gsize pdu_len; /* Convert PDU from hex to binary */ - pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, -1, &pdu_len, error); + pdu = mm_utils_hexstr2bin (hexpdu, -1, &pdu_len, error); if (!pdu) { g_prefix_error (error, "Couldn't convert 3GPP PDU from hex to binary: "); return NULL; diff --git a/src/mm-sms-part-cdma.c b/src/mm-sms-part-cdma.c index 4275cb8d..befc557c 100644 --- a/src/mm-sms-part-cdma.c +++ b/src/mm-sms-part-cdma.c @@ -321,7 +321,7 @@ mm_sms_part_cdma_new_from_pdu (guint index, gsize pdu_len; /* Convert PDU from hex to binary */ - pdu = (guint8 *) mm_utils_hexstr2bin (hexpdu, -1, &pdu_len, error); + pdu = mm_utils_hexstr2bin (hexpdu, -1, &pdu_len, error); if (!pdu) { g_prefix_error (error, "Couldn't convert CDMA PDU from hex to binary: "); return NULL; |