summaryrefslogtreecommitdiffstats
path: root/src/gsm
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2018-12-05 21:32:40 +0100
committerHarald Welte <laforge@gnumonks.org>2018-12-10 17:06:30 +0000
commit627e0113d142c91eaf9a750ab76b722bb776b1ce (patch)
tree643df37330095ad84bee057a8a951172de9ee577 /src/gsm
parent7079e698481705c82c7aff58ea9c63469626af80 (diff)
gsm48_mi_to_string: use osmo_bcd2str(), fix some corner cases
By using osmo_bcd2str(), ensure that the resulting string is always nul terminated, and always return strlen()+1 whether truncated or not. Still keep up the previous return value style, even if that isn't consistent at all. The difference between IMSI/IMEI and TMSI return values remains and is not part of this patch. Change-Id: I1b51b72a721e1cc9d69796b804ebda741ff0f36b
Diffstat (limited to 'src/gsm')
-rw-r--r--src/gsm/gsm48.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/gsm/gsm48.c b/src/gsm/gsm48.c
index e684a3ca..4558dfb6 100644
--- a/src/gsm/gsm48.c
+++ b/src/gsm/gsm48.c
@@ -638,7 +638,8 @@ int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
}
-/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string
+/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
+ * This function does not validate the Mobile Identity digits, i.e. digits > 9 are returned as 'A'-'F'.
* \param[out] string Caller-provided buffer for output
* \param[in] str_len Length of \a string in bytes
* \param[in] mi Mobile Identity to be stringified
@@ -650,7 +651,7 @@ int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
const int mi_len)
{
- int i;
+ int rc;
uint8_t mi_type;
char *str_cur = string;
uint32_t tmsi;
@@ -670,17 +671,15 @@ int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
case GSM_MI_TYPE_IMSI:
case GSM_MI_TYPE_IMEI:
case GSM_MI_TYPE_IMEISV:
- *str_cur++ = osmo_bcd2char(mi[0] >> 4);
-
- for (i = 1; i < mi_len; i++) {
- if (str_cur + 2 >= string + str_len)
- return str_cur - string;
- *str_cur++ = osmo_bcd2char(mi[i] & 0xf);
- /* skip last nibble in last input byte when GSM_EVEN */
- if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
- *str_cur++ = osmo_bcd2char(mi[i] >> 4);
- }
- break;
+ rc = osmo_bcd2str(string, str_len, mi,
+ 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
+ /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
+ if (rc < 0)
+ return 0;
+ else if (rc < str_len)
+ return rc + 1;
+ else
+ return strlen(string) + 1;
default:
break;
}