blob: 5874077c43e71dc5b6de2e65c287c9df17d73c1a [file] [log] [blame]
Harald Welted284cd92010-03-01 21:58:31 +01001
2#include <string.h>
3#include <stdint.h>
4#include <errno.h>
Harald Welteb59f9352010-03-25 11:37:04 +08005#include <stdio.h>
Harald Welted284cd92010-03-01 21:58:31 +01006
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01007#include <osmocom/core/utils.h>
Harald Welted284cd92010-03-01 21:58:31 +01008
Harald Welte8598f182011-08-17 14:19:27 +02009/*! \addtogroup utils
10 * @{
11 */
12
13/*! \file utils.c */
14
Harald Welteb59f9352010-03-25 11:37:04 +080015static char namebuf[255];
Harald Welte8598f182011-08-17 14:19:27 +020016
17/*! \brief get human-readable string for given value
18 * \param[in] vs Array of value_string tuples
19 * \param[in] val Value to be converted
20 * \returns pointer to human-readable string
21 */
Harald Welted284cd92010-03-01 21:58:31 +010022const char *get_value_string(const struct value_string *vs, uint32_t val)
23{
24 int i;
25
26 for (i = 0;; i++) {
27 if (vs[i].value == 0 && vs[i].str == NULL)
28 break;
29 if (vs[i].value == val)
30 return vs[i].str;
31 }
Harald Welteb59f9352010-03-25 11:37:04 +080032
33 snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
Holger Hans Peter Freyther8d506002013-07-04 20:14:10 +020034 namebuf[sizeof(namebuf) - 1] = '\0';
Harald Welteb59f9352010-03-25 11:37:04 +080035 return namebuf;
Harald Welted284cd92010-03-01 21:58:31 +010036}
37
Harald Welte8598f182011-08-17 14:19:27 +020038/*! \brief get numeric value for given human-readable string
39 * \param[in] vs Array of value_string tuples
40 * \param[in] str human-readable string
41 * \returns numeric value (>0) or negative numer in case of error
42 */
Harald Welted284cd92010-03-01 21:58:31 +010043int get_string_value(const struct value_string *vs, const char *str)
44{
45 int i;
46
47 for (i = 0;; i++) {
48 if (vs[i].value == 0 && vs[i].str == NULL)
49 break;
50 if (!strcasecmp(vs[i].str, str))
51 return vs[i].value;
52 }
53 return -EINVAL;
54}
Harald Weltea73e2f92010-03-04 10:50:32 +010055
Harald Welte8598f182011-08-17 14:19:27 +020056/*! \brief Convert BCD-encoded digit into printable character
57 * \param[in] bcd A single BCD-encoded digit
58 * \returns single printable character
59 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020060char osmo_bcd2char(uint8_t bcd)
Harald Weltea73e2f92010-03-04 10:50:32 +010061{
62 if (bcd < 0xa)
63 return '0' + bcd;
64 else
65 return 'A' + (bcd - 0xa);
66}
67
Harald Weltede6e4982012-12-06 21:25:27 +010068/*! \brief Convert number in ASCII to BCD value
69 * \param[in] c ASCII character
70 * \returns BCD encoded value of character
71 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020072uint8_t osmo_char2bcd(char c)
Harald Weltea73e2f92010-03-04 10:50:32 +010073{
74 return c - 0x30;
75}
Harald Welte3eba9912010-07-30 10:37:29 +020076
Harald Weltede6e4982012-12-06 21:25:27 +010077/*! \brief Parse a string ocntaining hexadecimal digits
78 * \param[in] str string containing ASCII encoded hexadecimal digits
79 * \param[out] b output buffer
80 * \param[in] max_len maximum space in output buffer
81 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020082int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +020083
84{
85 int i, l, v;
86
87 l = strlen(str);
88 if ((l&1) || ((l>>1) > max_len))
89 return -1;
90
91 memset(b, 0x00, max_len);
92
93 for (i=0; i<l; i++) {
94 char c = str[i];
95 if (c >= '0' && c <= '9')
96 v = c - '0';
97 else if (c >= 'a' && c <= 'f')
98 v = 10 + (c - 'a');
99 else if (c >= 'A' && c <= 'F')
100 v = 10 + (c - 'A');
101 else
102 return -1;
103 b[i>>1] |= v << (i&1 ? 0 : 4);
104 }
105
106 return i>>1;
107}
Harald Welte40481e82010-07-30 11:40:32 +0200108
109static char hexd_buff[4096];
110
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200111static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200112{
113 int i;
114 char *cur = hexd_buff;
115
116 hexd_buff[0] = 0;
117 for (i = 0; i < len; i++) {
118 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200119 if (len_remain <= 0)
120 break;
Harald Weltedee47cd2010-07-30 11:43:30 +0200121 int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim);
Harald Welte40481e82010-07-30 11:40:32 +0200122 if (rc <= 0)
123 break;
124 cur += rc;
125 }
126 hexd_buff[sizeof(hexd_buff)-1] = 0;
127 return hexd_buff;
128}
Harald Weltedee47cd2010-07-30 11:43:30 +0200129
Harald Welte8598f182011-08-17 14:19:27 +0200130/*! \brief Convert a sequence of unpacked bits to ASCII string
131 * \param[in] bits A sequence of unpacked bits
132 * \param[in] len Length of bits
133 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200134char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100135{
136 int i;
137
138 if (len > sizeof(hexd_buff)-1)
139 len = sizeof(hexd_buff)-1;
140 memset(hexd_buff, 0, sizeof(hexd_buff));
141
142 for (i = 0; i < len; i++) {
143 char outch;
144 switch (bits[i]) {
145 case 0:
146 outch = '0';
147 break;
148 case 0xff:
149 outch = '?';
150 break;
151 case 1:
152 outch = '1';
153 break;
154 default:
155 outch = 'E';
156 break;
157 }
158 hexd_buff[i] = outch;
159 }
160 hexd_buff[sizeof(hexd_buff)-1] = 0;
161 return hexd_buff;
162}
163
Harald Welte8598f182011-08-17 14:19:27 +0200164/*! \brief Convert binary sequence to hexadecimal ASCII string
165 * \param[in] buf pointer to sequence of bytes
166 * \param[in] len length of buf in number of bytes
167 * \returns pointer to zero-terminated string
168 *
169 * This function will print a sequence of bytes as hexadecimal numbers,
170 * adding one space character between each byte (e.g. "1a ef d9")
171 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200172char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200173{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200174 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200175}
176
Harald Welte8598f182011-08-17 14:19:27 +0200177/*! \brief Convert binary sequence to hexadecimal ASCII string
178 * \param[in] buf pointer to sequence of bytes
179 * \param[in] len length of buf in number of bytes
180 * \returns pointer to zero-terminated string
181 *
182 * This function will print a sequence of bytes as hexadecimal numbers,
183 * without any space character between each byte (e.g. "1aefd9")
184 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100185char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200186{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200187 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200188}
Harald Welte28222962011-02-18 20:37:04 +0100189
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100190 /* Compat with previous typo to preserve abi */
191char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100192 __attribute__((weak, alias("osmo_hexdump_nospc")));
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100193
Harald Welte28222962011-02-18 20:37:04 +0100194#include "../config.h"
195#ifdef HAVE_CTYPE_H
196#include <ctype.h>
Harald Welte8598f182011-08-17 14:19:27 +0200197/*! \brief Convert an entire string to lower case
198 * \param[out] out output string, caller-allocated
199 * \param[in] in input string
200 */
Harald Welte28222962011-02-18 20:37:04 +0100201void osmo_str2lower(char *out, const char *in)
202{
203 unsigned int i;
204
205 for (i = 0; i < strlen(in); i++)
206 out[i] = tolower(in[i]);
207 out[strlen(in)] = '\0';
208}
209
Harald Welte8598f182011-08-17 14:19:27 +0200210/*! \brief Convert an entire string to upper case
211 * \param[out] out output string, caller-allocated
212 * \param[in] in input string
213 */
Harald Welte28222962011-02-18 20:37:04 +0100214void osmo_str2upper(char *out, const char *in)
215{
216 unsigned int i;
217
218 for (i = 0; i < strlen(in); i++)
219 out[i] = toupper(in[i]);
220 out[strlen(in)] = '\0';
221}
222#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200223
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200224/*! @} */