blob: 3dc06933ca37b4c73c2729caa98a57a89442fd44 [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
Neels Hofmeyr3de7b052015-09-23 23:16:53 +0200104 * \returns number of parsed octets, or -1 on error
Harald Weltede6e4982012-12-06 21:25:27 +0100105 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200106int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +0200107
108{
109 int i, l, v;
110
111 l = strlen(str);
112 if ((l&1) || ((l>>1) > max_len))
113 return -1;
114
115 memset(b, 0x00, max_len);
116
117 for (i=0; i<l; i++) {
118 char c = str[i];
119 if (c >= '0' && c <= '9')
120 v = c - '0';
121 else if (c >= 'a' && c <= 'f')
122 v = 10 + (c - 'a');
123 else if (c >= 'A' && c <= 'F')
124 v = 10 + (c - 'A');
125 else
126 return -1;
127 b[i>>1] |= v << (i&1 ? 0 : 4);
128 }
129
130 return i>>1;
131}
Harald Welte40481e82010-07-30 11:40:32 +0200132
133static char hexd_buff[4096];
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100134static const char hex_chars[] = "0123456789abcdef";
Harald Welte40481e82010-07-30 11:40:32 +0200135
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200136static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200137{
138 int i;
139 char *cur = hexd_buff;
140
141 hexd_buff[0] = 0;
142 for (i = 0; i < len; i++) {
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100143 const char *delimp = delim;
Harald Welte40481e82010-07-30 11:40:32 +0200144 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100145 if (len_remain < 3)
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200146 break;
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100147
148 *cur++ = hex_chars[buf[i] >> 4];
149 *cur++ = hex_chars[buf[i] & 0xf];
150
151 while (len_remain > 1 && *delimp) {
152 *cur++ = *delimp++;
153 len_remain--;
154 }
155
156 *cur = 0;
Harald Welte40481e82010-07-30 11:40:32 +0200157 }
158 hexd_buff[sizeof(hexd_buff)-1] = 0;
159 return hexd_buff;
160}
Harald Weltedee47cd2010-07-30 11:43:30 +0200161
Harald Welte8598f182011-08-17 14:19:27 +0200162/*! \brief Convert a sequence of unpacked bits to ASCII string
163 * \param[in] bits A sequence of unpacked bits
164 * \param[in] len Length of bits
165 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200166char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100167{
168 int i;
169
170 if (len > sizeof(hexd_buff)-1)
171 len = sizeof(hexd_buff)-1;
172 memset(hexd_buff, 0, sizeof(hexd_buff));
173
174 for (i = 0; i < len; i++) {
175 char outch;
176 switch (bits[i]) {
177 case 0:
178 outch = '0';
179 break;
180 case 0xff:
181 outch = '?';
182 break;
183 case 1:
184 outch = '1';
185 break;
186 default:
187 outch = 'E';
188 break;
189 }
190 hexd_buff[i] = outch;
191 }
192 hexd_buff[sizeof(hexd_buff)-1] = 0;
193 return hexd_buff;
194}
195
Harald Welte8598f182011-08-17 14:19:27 +0200196/*! \brief Convert binary sequence to hexadecimal ASCII string
197 * \param[in] buf pointer to sequence of bytes
198 * \param[in] len length of buf in number of bytes
199 * \returns pointer to zero-terminated string
200 *
201 * This function will print a sequence of bytes as hexadecimal numbers,
202 * adding one space character between each byte (e.g. "1a ef d9")
203 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200204char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200205{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200206 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200207}
208
Harald Welte8598f182011-08-17 14:19:27 +0200209/*! \brief Convert binary sequence to hexadecimal ASCII string
210 * \param[in] buf pointer to sequence of bytes
211 * \param[in] len length of buf in number of bytes
212 * \returns pointer to zero-terminated string
213 *
214 * This function will print a sequence of bytes as hexadecimal numbers,
215 * without any space character between each byte (e.g. "1aefd9")
216 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100217char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200218{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200219 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200220}
Harald Welte28222962011-02-18 20:37:04 +0100221
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200222/* Compat with previous typo to preserve abi */
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100223char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200224#if defined(__MACH__) && defined(__APPLE__)
225 ;
226#else
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100227 __attribute__((weak, alias("osmo_hexdump_nospc")));
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200228#endif
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100229
Harald Welte28222962011-02-18 20:37:04 +0100230#include "../config.h"
231#ifdef HAVE_CTYPE_H
232#include <ctype.h>
Harald Welte8598f182011-08-17 14:19:27 +0200233/*! \brief Convert an entire string to lower case
234 * \param[out] out output string, caller-allocated
235 * \param[in] in input string
236 */
Harald Welte28222962011-02-18 20:37:04 +0100237void osmo_str2lower(char *out, const char *in)
238{
239 unsigned int i;
240
241 for (i = 0; i < strlen(in); i++)
242 out[i] = tolower(in[i]);
243 out[strlen(in)] = '\0';
244}
245
Harald Welte8598f182011-08-17 14:19:27 +0200246/*! \brief Convert an entire string to upper case
247 * \param[out] out output string, caller-allocated
248 * \param[in] in input string
249 */
Harald Welte28222962011-02-18 20:37:04 +0100250void osmo_str2upper(char *out, const char *in)
251{
252 unsigned int i;
253
254 for (i = 0; i < strlen(in); i++)
255 out[i] = toupper(in[i]);
256 out[strlen(in)] = '\0';
257}
258#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200259
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200260/*! @} */