blob: d4a29248228749de56de575013a120b8b9fecf16 [file] [log] [blame]
Harald Welte468b6432014-09-11 13:05:51 +08001/*
2 * (C) 2011 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2011 by Sylvain Munaut <tnt@246tNt.com>
4 * (C) 2014 by Nils O. Selåsdal <noselasd@fiane.dyndns.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Harald Welted284cd92010-03-01 21:58:31 +010024
25#include <string.h>
26#include <stdint.h>
27#include <errno.h>
Harald Welteb59f9352010-03-25 11:37:04 +080028#include <stdio.h>
Harald Welted284cd92010-03-01 21:58:31 +010029
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/utils.h>
Harald Welted284cd92010-03-01 21:58:31 +010031
Harald Welte8598f182011-08-17 14:19:27 +020032/*! \addtogroup utils
33 * @{
34 */
35
36/*! \file utils.c */
37
Harald Welteb59f9352010-03-25 11:37:04 +080038static char namebuf[255];
Harald Welte8598f182011-08-17 14:19:27 +020039
40/*! \brief get human-readable string for given value
41 * \param[in] vs Array of value_string tuples
42 * \param[in] val Value to be converted
43 * \returns pointer to human-readable string
44 */
Harald Welted284cd92010-03-01 21:58:31 +010045const char *get_value_string(const struct value_string *vs, uint32_t val)
46{
47 int i;
48
49 for (i = 0;; i++) {
50 if (vs[i].value == 0 && vs[i].str == NULL)
51 break;
52 if (vs[i].value == val)
53 return vs[i].str;
54 }
Harald Welteb59f9352010-03-25 11:37:04 +080055
56 snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
Holger Hans Peter Freyther8d506002013-07-04 20:14:10 +020057 namebuf[sizeof(namebuf) - 1] = '\0';
Harald Welteb59f9352010-03-25 11:37:04 +080058 return namebuf;
Harald Welted284cd92010-03-01 21:58:31 +010059}
60
Harald Welte8598f182011-08-17 14:19:27 +020061/*! \brief get numeric value for given human-readable string
62 * \param[in] vs Array of value_string tuples
63 * \param[in] str human-readable string
64 * \returns numeric value (>0) or negative numer in case of error
65 */
Harald Welted284cd92010-03-01 21:58:31 +010066int get_string_value(const struct value_string *vs, const char *str)
67{
68 int i;
69
70 for (i = 0;; i++) {
71 if (vs[i].value == 0 && vs[i].str == NULL)
72 break;
73 if (!strcasecmp(vs[i].str, str))
74 return vs[i].value;
75 }
76 return -EINVAL;
77}
Harald Weltea73e2f92010-03-04 10:50:32 +010078
Harald Welte8598f182011-08-17 14:19:27 +020079/*! \brief Convert BCD-encoded digit into printable character
80 * \param[in] bcd A single BCD-encoded digit
81 * \returns single printable character
82 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020083char osmo_bcd2char(uint8_t bcd)
Harald Weltea73e2f92010-03-04 10:50:32 +010084{
85 if (bcd < 0xa)
86 return '0' + bcd;
87 else
88 return 'A' + (bcd - 0xa);
89}
90
Harald Weltede6e4982012-12-06 21:25:27 +010091/*! \brief Convert number in ASCII to BCD value
92 * \param[in] c ASCII character
93 * \returns BCD encoded value of character
94 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020095uint8_t osmo_char2bcd(char c)
Harald Weltea73e2f92010-03-04 10:50:32 +010096{
97 return c - 0x30;
98}
Harald Welte3eba9912010-07-30 10:37:29 +020099
Harald Weltede6e4982012-12-06 21:25:27 +0100100/*! \brief Parse a string ocntaining hexadecimal digits
101 * \param[in] str string containing ASCII encoded hexadecimal digits
102 * \param[out] b output buffer
103 * \param[in] max_len maximum space in output buffer
104 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200105int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +0200106
107{
108 int i, l, v;
109
110 l = strlen(str);
111 if ((l&1) || ((l>>1) > max_len))
112 return -1;
113
114 memset(b, 0x00, max_len);
115
116 for (i=0; i<l; i++) {
117 char c = str[i];
118 if (c >= '0' && c <= '9')
119 v = c - '0';
120 else if (c >= 'a' && c <= 'f')
121 v = 10 + (c - 'a');
122 else if (c >= 'A' && c <= 'F')
123 v = 10 + (c - 'A');
124 else
125 return -1;
126 b[i>>1] |= v << (i&1 ? 0 : 4);
127 }
128
129 return i>>1;
130}
Harald Welte40481e82010-07-30 11:40:32 +0200131
132static char hexd_buff[4096];
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100133static const char hex_chars[] = "0123456789abcdef";
Harald Welte40481e82010-07-30 11:40:32 +0200134
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200135static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200136{
137 int i;
138 char *cur = hexd_buff;
139
140 hexd_buff[0] = 0;
141 for (i = 0; i < len; i++) {
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100142 const char *delimp = delim;
Harald Welte40481e82010-07-30 11:40:32 +0200143 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100144 if (len_remain < 3)
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200145 break;
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100146
147 *cur++ = hex_chars[buf[i] >> 4];
148 *cur++ = hex_chars[buf[i] & 0xf];
149
150 while (len_remain > 1 && *delimp) {
151 *cur++ = *delimp++;
152 len_remain--;
153 }
154
155 *cur = 0;
Harald Welte40481e82010-07-30 11:40:32 +0200156 }
157 hexd_buff[sizeof(hexd_buff)-1] = 0;
158 return hexd_buff;
159}
Harald Weltedee47cd2010-07-30 11:43:30 +0200160
Harald Welte8598f182011-08-17 14:19:27 +0200161/*! \brief Convert a sequence of unpacked bits to ASCII string
162 * \param[in] bits A sequence of unpacked bits
163 * \param[in] len Length of bits
164 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200165char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100166{
167 int i;
168
169 if (len > sizeof(hexd_buff)-1)
170 len = sizeof(hexd_buff)-1;
171 memset(hexd_buff, 0, sizeof(hexd_buff));
172
173 for (i = 0; i < len; i++) {
174 char outch;
175 switch (bits[i]) {
176 case 0:
177 outch = '0';
178 break;
179 case 0xff:
180 outch = '?';
181 break;
182 case 1:
183 outch = '1';
184 break;
185 default:
186 outch = 'E';
187 break;
188 }
189 hexd_buff[i] = outch;
190 }
191 hexd_buff[sizeof(hexd_buff)-1] = 0;
192 return hexd_buff;
193}
194
Harald Welte8598f182011-08-17 14:19:27 +0200195/*! \brief Convert binary sequence to hexadecimal ASCII string
196 * \param[in] buf pointer to sequence of bytes
197 * \param[in] len length of buf in number of bytes
198 * \returns pointer to zero-terminated string
199 *
200 * This function will print a sequence of bytes as hexadecimal numbers,
201 * adding one space character between each byte (e.g. "1a ef d9")
202 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200203char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200204{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200205 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200206}
207
Harald Welte8598f182011-08-17 14:19:27 +0200208/*! \brief Convert binary sequence to hexadecimal ASCII string
209 * \param[in] buf pointer to sequence of bytes
210 * \param[in] len length of buf in number of bytes
211 * \returns pointer to zero-terminated string
212 *
213 * This function will print a sequence of bytes as hexadecimal numbers,
214 * without any space character between each byte (e.g. "1aefd9")
215 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100216char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200217{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200218 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200219}
Harald Welte28222962011-02-18 20:37:04 +0100220
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200221/* Compat with previous typo to preserve abi */
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100222char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200223#if defined(__MACH__) && defined(__APPLE__)
224 ;
225#else
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100226 __attribute__((weak, alias("osmo_hexdump_nospc")));
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200227#endif
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100228
Harald Welte28222962011-02-18 20:37:04 +0100229#include "../config.h"
230#ifdef HAVE_CTYPE_H
231#include <ctype.h>
Harald Welte8598f182011-08-17 14:19:27 +0200232/*! \brief Convert an entire string to lower case
233 * \param[out] out output string, caller-allocated
234 * \param[in] in input string
235 */
Harald Welte28222962011-02-18 20:37:04 +0100236void osmo_str2lower(char *out, const char *in)
237{
238 unsigned int i;
239
240 for (i = 0; i < strlen(in); i++)
241 out[i] = tolower(in[i]);
242 out[strlen(in)] = '\0';
243}
244
Harald Welte8598f182011-08-17 14:19:27 +0200245/*! \brief Convert an entire string to upper case
246 * \param[out] out output string, caller-allocated
247 * \param[in] in input string
248 */
Harald Welte28222962011-02-18 20:37:04 +0100249void osmo_str2upper(char *out, const char *in)
250{
251 unsigned int i;
252
253 for (i = 0; i < strlen(in); i++)
254 out[i] = toupper(in[i]);
255 out[strlen(in)] = '\0';
256}
257#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200258
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200259/*! @} */