diff --git a/include/libtcap.h b/include/libtcap.h index da919da..1ab435b 100644 --- a/include/libtcap.h +++ b/include/libtcap.h @@ -26,6 +26,7 @@ void *inap_decode(Invoke_t *invoke, asn_TYPE_descriptor_t **); int tcap_extract(const char *buf, size_t len, const char *spec, struct output_buffer *out); int inap_extract(const char *buf, size_t len, const char *spec, struct output_buffer *out); int isup_convert_number(const char *inp, int inlen, char *out); +int isup_convert_number_hex(const char *inp, int inlen, char *out); #endif diff --git a/src/tcap.c b/src/tcap.c index 59d0f0b..5b32c5a 100644 --- a/src/tcap.c +++ b/src/tcap.c @@ -298,18 +298,9 @@ error: static inline char phone_digit(unsigned char d) { + if (d >= 0 && d <= 9) + return '0' + d; switch (d) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - case 8: - case 9: - return '0' + d; case 11: return '*'; case 12: @@ -321,8 +312,18 @@ static inline char phone_digit(unsigned char d) { } } +static inline char phone_digit_hex(unsigned char d) { + if (d >= 0 && d <= 9) + return '0' + d; + else if (d >= 10 && d <= 15) + return 'A' + d - 10; + return '?'; +} -int isup_convert_number(const char *inp, int inlen, char *out) { + +static inline int isup_convert_number_common(const char *inp, int inlen, char *out, + char (*func)(unsigned char)) +{ const char *pi; unsigned char raw[32] = "", *pr; int rawlen, odd; @@ -380,8 +381,8 @@ int isup_convert_number(const char *inp, int inlen, char *out) { pr = raw + 2; rawlen -= 2; while (rawlen) { - *(out++) = phone_digit(*pr & 0xf); - *(out++) = phone_digit(*pr >> 4); + *(out++) = func(*pr & 0xf); + *(out++) = func(*pr >> 4); rawlen--; pr++; } @@ -396,3 +397,10 @@ int isup_convert_number(const char *inp, int inlen, char *out) { error: return -1; } + +int isup_convert_number(const char *inp, int inlen, char *out) { + return isup_convert_number_common(inp, inlen, out, phone_digit); +} +int isup_convert_number_hex(const char *inp, int inlen, char *out) { + return isup_convert_number_common(inp, inlen, out, phone_digit_hex); +}