libmm-glib,common-helpers: make hexstr2bin() return a guint8 array
It makes much more sense than returning a gchar array, as gchar is signed.
This commit is contained in:
@@ -1686,18 +1686,18 @@ mm_utils_hex2byte (const gchar *hex)
|
|||||||
return (a << 4) | b;
|
return (a << 4) | b;
|
||||||
}
|
}
|
||||||
|
|
||||||
gchar *
|
guint8 *
|
||||||
mm_utils_hexstr2bin (const gchar *hex,
|
mm_utils_hexstr2bin (const gchar *hex,
|
||||||
gssize len,
|
gssize len,
|
||||||
gsize *out_len,
|
gsize *out_len,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
const gchar *ipos = hex;
|
const gchar *ipos = hex;
|
||||||
g_autofree gchar *buf = NULL;
|
g_autofree guint8 *buf = NULL;
|
||||||
gsize i;
|
gsize i;
|
||||||
gint a;
|
gint a;
|
||||||
gchar *opos
|
guint8 *opos;
|
||||||
;
|
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
len = strlen (hex);
|
len = strlen (hex);
|
||||||
|
|
||||||
@@ -1723,7 +1723,7 @@ mm_utils_hexstr2bin (const gchar *hex,
|
|||||||
ipos[0], ipos[1]);
|
ipos[0], ipos[1]);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*opos++ = a;
|
*opos++ = (guint8)a;
|
||||||
ipos += 2;
|
ipos += 2;
|
||||||
}
|
}
|
||||||
*out_len = len / 2;
|
*out_len = len / 2;
|
||||||
|
@@ -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);
|
const gchar *mm_sms_delivery_state_get_string_extended (guint delivery_state);
|
||||||
|
|
||||||
gint mm_utils_hex2byte (const gchar *hex);
|
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);
|
gchar *mm_utils_bin2hexstr (const guint8 *bin, gsize len);
|
||||||
gboolean mm_utils_ishexstr (const gchar *hex);
|
gboolean mm_utils_ishexstr (const gchar *hex);
|
||||||
|
|
||||||
|
@@ -170,14 +170,14 @@ mm_altair_parse_vendor_pco_info (const gchar *pco_info, GError **error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (g_match_info_matches (match_info)) {
|
while (g_match_info_matches (match_info)) {
|
||||||
guint pco_cid;
|
guint pco_cid;
|
||||||
g_autofree gchar *pco_id = NULL;
|
g_autofree gchar *pco_id = NULL;
|
||||||
g_autofree gchar *pco_payload = NULL;
|
g_autofree gchar *pco_payload = NULL;
|
||||||
g_autofree gchar *pco_payload_bytes = NULL;
|
g_autofree guint8 *pco_payload_bytes = NULL;
|
||||||
gsize pco_payload_bytes_len;
|
gsize pco_payload_bytes_len;
|
||||||
guint8 pco_prefix[6];
|
guint8 pco_prefix[6];
|
||||||
GByteArray *pco_raw;
|
GByteArray *pco_raw;
|
||||||
gsize pco_raw_len;
|
gsize pco_raw_len;
|
||||||
|
|
||||||
if (!mm_get_uint_from_match_info (match_info, 1, &pco_cid)) {
|
if (!mm_get_uint_from_match_info (match_info, 1, &pco_cid)) {
|
||||||
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
|
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);
|
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, 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 ();
|
pco = mm_pco_new ();
|
||||||
mm_pco_set_session_id (pco, pco_cid);
|
mm_pco_set_session_id (pco, pco_cid);
|
||||||
|
@@ -2330,7 +2330,7 @@ decode (MMIfaceModem3gppUssd *self,
|
|||||||
const gchar *reply,
|
const gchar *reply,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
g_autofree gchar *bin = NULL;
|
g_autofree guint8 *bin = NULL;
|
||||||
g_autofree guint8 *unpacked = NULL;
|
g_autofree guint8 *unpacked = NULL;
|
||||||
gsize bin_len = 0;
|
gsize bin_len = 0;
|
||||||
guint32 unpacked_len;
|
guint32 unpacked_len;
|
||||||
@@ -2339,7 +2339,7 @@ decode (MMIfaceModem3gppUssd *self,
|
|||||||
if (!bin)
|
if (!bin)
|
||||||
return NULL;
|
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 the last character in a 7-byte block is padding, then drop it */
|
||||||
if ((bin_len % 7 == 0) && (unpacked[unpacked_len - 1] == 0x0d))
|
if ((bin_len % 7 == 0) && (unpacked[unpacked_len - 1] == 0x0d))
|
||||||
unpacked_len--;
|
unpacked_len--;
|
||||||
|
@@ -157,14 +157,15 @@ mm_huawei_parse_ndisstatqry_response (const gchar *response,
|
|||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
match_info_to_ip4_addr (GMatchInfo *match_info,
|
match_info_to_ip4_addr (GMatchInfo *match_info,
|
||||||
guint match_index,
|
guint match_index,
|
||||||
guint *out_addr)
|
guint *out_addr)
|
||||||
{
|
{
|
||||||
gchar *s, *bin = NULL;
|
g_autofree gchar *s = NULL;
|
||||||
gchar buf[9];
|
g_autofree guint8 *bin = NULL;
|
||||||
gsize len, bin_len;
|
gchar buf[9];
|
||||||
gboolean success = FALSE;
|
gsize len;
|
||||||
guint32 aux;
|
gsize bin_len;
|
||||||
|
guint32 aux;
|
||||||
|
|
||||||
s = g_match_info_fetch (match_info, match_index);
|
s = g_match_info_fetch (match_info, match_index);
|
||||||
g_return_val_if_fail (s != NULL, FALSE);
|
g_return_val_if_fail (s != NULL, FALSE);
|
||||||
@@ -172,11 +173,11 @@ match_info_to_ip4_addr (GMatchInfo *match_info,
|
|||||||
len = strlen (s);
|
len = strlen (s);
|
||||||
if (len == 1 && s[0] == '0') {
|
if (len == 1 && s[0] == '0') {
|
||||||
*out_addr = 0;
|
*out_addr = 0;
|
||||||
success = TRUE;
|
return TRUE;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len < 7 || len > 8)
|
if (len < 7 || len > 8)
|
||||||
goto done;
|
return FALSE;
|
||||||
|
|
||||||
/* Handle possibly missing leading zero */
|
/* Handle possibly missing leading zero */
|
||||||
memset (buf, 0, sizeof (buf));
|
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);
|
bin = mm_utils_hexstr2bin (buf, -1, &bin_len, NULL);
|
||||||
if (!bin || bin_len != 4)
|
if (!bin || bin_len != 4)
|
||||||
goto done;
|
return FALSE;
|
||||||
|
|
||||||
memcpy (&aux, bin, 4);
|
memcpy (&aux, bin, 4);
|
||||||
*out_addr = GUINT32_SWAP_LE_BE (aux);
|
*out_addr = GUINT32_SWAP_LE_BE (aux);
|
||||||
success = TRUE;
|
return TRUE;
|
||||||
|
|
||||||
done:
|
|
||||||
g_free (s);
|
|
||||||
g_free (bin);
|
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
|
@@ -1306,9 +1306,9 @@ parse_mnc_length (const gchar *response,
|
|||||||
(sw1 == 0x91) ||
|
(sw1 == 0x91) ||
|
||||||
(sw1 == 0x92) ||
|
(sw1 == 0x92) ||
|
||||||
(sw1 == 0x9f)) {
|
(sw1 == 0x9f)) {
|
||||||
gsize buflen = 0;
|
gsize buflen = 0;
|
||||||
guint32 mnc_len;
|
guint32 mnc_len;
|
||||||
g_autofree gchar *bin = NULL;
|
g_autofree guint8 *bin = NULL;
|
||||||
|
|
||||||
/* Convert hex string to binary */
|
/* Convert hex string to binary */
|
||||||
bin = mm_utils_hexstr2bin (hex, -1, &buflen, error);
|
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 length is byte 4 of this SIM file */
|
||||||
mnc_len = bin[3] & 0xFF;
|
mnc_len = bin[3];
|
||||||
if (mnc_len == 2 || mnc_len == 3)
|
if (mnc_len == 2 || mnc_len == 3)
|
||||||
return mnc_len;
|
return mnc_len;
|
||||||
|
|
||||||
@@ -1412,8 +1412,8 @@ parse_spn (const gchar *response,
|
|||||||
(sw1 == 0x91) ||
|
(sw1 == 0x91) ||
|
||||||
(sw1 == 0x92) ||
|
(sw1 == 0x92) ||
|
||||||
(sw1 == 0x9f)) {
|
(sw1 == 0x9f)) {
|
||||||
gsize buflen = 0;
|
gsize buflen = 0;
|
||||||
g_autofree gchar *bin = NULL;
|
g_autofree guint8 *bin = NULL;
|
||||||
|
|
||||||
/* Convert hex string to binary */
|
/* Convert hex string to binary */
|
||||||
bin = mm_utils_hexstr2bin (hex, -1, &buflen, error);
|
bin = mm_utils_hexstr2bin (hex, -1, &buflen, error);
|
||||||
@@ -1423,11 +1423,11 @@ parse_spn (const gchar *response,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Remove the FF filler at the end */
|
/* Remove the FF filler at the end */
|
||||||
while (buflen > 1 && bin[buflen - 1] == (char)0xff)
|
while (buflen > 1 && bin[buflen - 1] == 0xff)
|
||||||
buflen--;
|
buflen--;
|
||||||
|
|
||||||
/* First byte is metadata; remainder is GSM-7 unpacked into octets; convert to UTF8 */
|
/* 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,
|
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
|
||||||
|
@@ -157,11 +157,11 @@ gchar *
|
|||||||
mm_modem_charset_hex_to_utf8 (const gchar *src,
|
mm_modem_charset_hex_to_utf8 (const gchar *src,
|
||||||
MMModemCharset charset)
|
MMModemCharset charset)
|
||||||
{
|
{
|
||||||
const gchar *iconv_from;
|
const gchar *iconv_from;
|
||||||
g_autofree gchar *unconverted = NULL;
|
g_autofree guint8 *unconverted = NULL;
|
||||||
g_autofree gchar *converted = NULL;
|
g_autofree gchar *converted = NULL;
|
||||||
g_autoptr(GError) error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
gsize unconverted_len = 0;
|
gsize unconverted_len = 0;
|
||||||
|
|
||||||
g_return_val_if_fail (src != NULL, NULL);
|
g_return_val_if_fail (src != NULL, NULL);
|
||||||
g_return_val_if_fail (charset != MM_MODEM_CHARSET_UNKNOWN, 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)
|
if (charset == MM_MODEM_CHARSET_UTF8 || charset == MM_MODEM_CHARSET_IRA)
|
||||||
return g_steal_pointer (&unconverted);
|
return g_steal_pointer (&unconverted);
|
||||||
|
|
||||||
converted = g_convert (unconverted, unconverted_len,
|
converted = g_convert ((const gchar *)unconverted, unconverted_len,
|
||||||
"UTF-8//TRANSLIT", iconv_from,
|
"UTF-8//TRANSLIT", iconv_from,
|
||||||
NULL, NULL, &error);
|
NULL, NULL, &error);
|
||||||
if (!converted || error)
|
if (!converted || error)
|
||||||
|
@@ -1815,7 +1815,7 @@ mm_firmware_unique_id_to_qmi_unique_id (const gchar *unique_id,
|
|||||||
gsize tmp_len;
|
gsize tmp_len;
|
||||||
|
|
||||||
tmp_len = 0;
|
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) {
|
if (!tmp) {
|
||||||
g_prefix_error (error, "Unexpected character found in unique id: ");
|
g_prefix_error (error, "Unexpected character found in unique id: ");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -4291,7 +4291,7 @@ mm_3gpp_parse_emergency_numbers (const char *raw, GError **error)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bin = (guint8 *) mm_utils_hexstr2bin (raw, -1, &binlen, error);
|
bin = mm_utils_hexstr2bin (raw, -1, &binlen, error);
|
||||||
if (!bin) {
|
if (!bin) {
|
||||||
g_prefix_error (error, "invalid raw emergency numbers list contents: ");
|
g_prefix_error (error, "invalid raw emergency numbers list contents: ");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -343,7 +343,7 @@ mm_sms_part_3gpp_new_from_pdu (guint index,
|
|||||||
gsize pdu_len;
|
gsize pdu_len;
|
||||||
|
|
||||||
/* Convert PDU from hex to binary */
|
/* 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) {
|
if (!pdu) {
|
||||||
g_prefix_error (error, "Couldn't convert 3GPP PDU from hex to binary: ");
|
g_prefix_error (error, "Couldn't convert 3GPP PDU from hex to binary: ");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -321,7 +321,7 @@ mm_sms_part_cdma_new_from_pdu (guint index,
|
|||||||
gsize pdu_len;
|
gsize pdu_len;
|
||||||
|
|
||||||
/* Convert PDU from hex to binary */
|
/* 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) {
|
if (!pdu) {
|
||||||
g_prefix_error (error, "Couldn't convert CDMA PDU from hex to binary: ");
|
g_prefix_error (error, "Couldn't convert CDMA PDU from hex to binary: ");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
Reference in New Issue
Block a user