blob: 1c2fc32662e08aee24b8b3b48611451744b9b28a [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 Welte9709b2e2016-04-25 18:47:53 +020031#include <osmocom/core/bit64gen.h>
32
Harald Welted284cd92010-03-01 21:58:31 +010033
Harald Welte8598f182011-08-17 14:19:27 +020034/*! \addtogroup utils
35 * @{
36 */
37
38/*! \file utils.c */
39
Harald Welteb59f9352010-03-25 11:37:04 +080040static char namebuf[255];
Harald Welte8598f182011-08-17 14:19:27 +020041
42/*! \brief get human-readable string for given value
43 * \param[in] vs Array of value_string tuples
44 * \param[in] val Value to be converted
45 * \returns pointer to human-readable string
46 */
Harald Welted284cd92010-03-01 21:58:31 +010047const char *get_value_string(const struct value_string *vs, uint32_t val)
48{
49 int i;
50
51 for (i = 0;; i++) {
52 if (vs[i].value == 0 && vs[i].str == NULL)
53 break;
54 if (vs[i].value == val)
55 return vs[i].str;
56 }
Harald Welteb59f9352010-03-25 11:37:04 +080057
58 snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
Holger Hans Peter Freyther8d506002013-07-04 20:14:10 +020059 namebuf[sizeof(namebuf) - 1] = '\0';
Harald Welteb59f9352010-03-25 11:37:04 +080060 return namebuf;
Harald Welted284cd92010-03-01 21:58:31 +010061}
62
Harald Welte8598f182011-08-17 14:19:27 +020063/*! \brief get numeric value for given human-readable string
64 * \param[in] vs Array of value_string tuples
65 * \param[in] str human-readable string
66 * \returns numeric value (>0) or negative numer in case of error
67 */
Harald Welted284cd92010-03-01 21:58:31 +010068int get_string_value(const struct value_string *vs, const char *str)
69{
70 int i;
71
72 for (i = 0;; i++) {
73 if (vs[i].value == 0 && vs[i].str == NULL)
74 break;
75 if (!strcasecmp(vs[i].str, str))
76 return vs[i].value;
77 }
78 return -EINVAL;
79}
Harald Weltea73e2f92010-03-04 10:50:32 +010080
Harald Welte8598f182011-08-17 14:19:27 +020081/*! \brief Convert BCD-encoded digit into printable character
82 * \param[in] bcd A single BCD-encoded digit
83 * \returns single printable character
84 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020085char osmo_bcd2char(uint8_t bcd)
Harald Weltea73e2f92010-03-04 10:50:32 +010086{
87 if (bcd < 0xa)
88 return '0' + bcd;
89 else
90 return 'A' + (bcd - 0xa);
91}
92
Harald Weltede6e4982012-12-06 21:25:27 +010093/*! \brief Convert number in ASCII to BCD value
94 * \param[in] c ASCII character
95 * \returns BCD encoded value of character
96 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020097uint8_t osmo_char2bcd(char c)
Harald Weltea73e2f92010-03-04 10:50:32 +010098{
99 return c - 0x30;
100}
Harald Welte3eba9912010-07-30 10:37:29 +0200101
Harald Weltede6e4982012-12-06 21:25:27 +0100102/*! \brief Parse a string ocntaining hexadecimal digits
103 * \param[in] str string containing ASCII encoded hexadecimal digits
104 * \param[out] b output buffer
105 * \param[in] max_len maximum space in output buffer
Neels Hofmeyr3de7b052015-09-23 23:16:53 +0200106 * \returns number of parsed octets, or -1 on error
Harald Weltede6e4982012-12-06 21:25:27 +0100107 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200108int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +0200109
110{
111 int i, l, v;
112
113 l = strlen(str);
114 if ((l&1) || ((l>>1) > max_len))
115 return -1;
116
117 memset(b, 0x00, max_len);
118
119 for (i=0; i<l; i++) {
120 char c = str[i];
121 if (c >= '0' && c <= '9')
122 v = c - '0';
123 else if (c >= 'a' && c <= 'f')
124 v = 10 + (c - 'a');
125 else if (c >= 'A' && c <= 'F')
126 v = 10 + (c - 'A');
127 else
128 return -1;
129 b[i>>1] |= v << (i&1 ? 0 : 4);
130 }
131
132 return i>>1;
133}
Harald Welte40481e82010-07-30 11:40:32 +0200134
135static char hexd_buff[4096];
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100136static const char hex_chars[] = "0123456789abcdef";
Harald Welte40481e82010-07-30 11:40:32 +0200137
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200138static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200139{
140 int i;
141 char *cur = hexd_buff;
142
143 hexd_buff[0] = 0;
144 for (i = 0; i < len; i++) {
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100145 const char *delimp = delim;
Harald Welte40481e82010-07-30 11:40:32 +0200146 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100147 if (len_remain < 3)
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200148 break;
Nils O. Selåsdal32447022014-01-02 14:04:43 +0100149
150 *cur++ = hex_chars[buf[i] >> 4];
151 *cur++ = hex_chars[buf[i] & 0xf];
152
153 while (len_remain > 1 && *delimp) {
154 *cur++ = *delimp++;
155 len_remain--;
156 }
157
158 *cur = 0;
Harald Welte40481e82010-07-30 11:40:32 +0200159 }
160 hexd_buff[sizeof(hexd_buff)-1] = 0;
161 return hexd_buff;
162}
Harald Weltedee47cd2010-07-30 11:43:30 +0200163
Harald Welte8598f182011-08-17 14:19:27 +0200164/*! \brief Convert a sequence of unpacked bits to ASCII string
165 * \param[in] bits A sequence of unpacked bits
166 * \param[in] len Length of bits
167 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200168char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100169{
170 int i;
171
172 if (len > sizeof(hexd_buff)-1)
173 len = sizeof(hexd_buff)-1;
174 memset(hexd_buff, 0, sizeof(hexd_buff));
175
176 for (i = 0; i < len; i++) {
177 char outch;
178 switch (bits[i]) {
179 case 0:
180 outch = '0';
181 break;
182 case 0xff:
183 outch = '?';
184 break;
185 case 1:
186 outch = '1';
187 break;
188 default:
189 outch = 'E';
190 break;
191 }
192 hexd_buff[i] = outch;
193 }
194 hexd_buff[sizeof(hexd_buff)-1] = 0;
195 return hexd_buff;
196}
197
Harald Welte8598f182011-08-17 14:19:27 +0200198/*! \brief Convert binary sequence to hexadecimal ASCII string
199 * \param[in] buf pointer to sequence of bytes
200 * \param[in] len length of buf in number of bytes
201 * \returns pointer to zero-terminated string
202 *
203 * This function will print a sequence of bytes as hexadecimal numbers,
204 * adding one space character between each byte (e.g. "1a ef d9")
205 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200206char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200207{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200208 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200209}
210
Harald Welte8598f182011-08-17 14:19:27 +0200211/*! \brief Convert binary sequence to hexadecimal ASCII string
212 * \param[in] buf pointer to sequence of bytes
213 * \param[in] len length of buf in number of bytes
214 * \returns pointer to zero-terminated string
215 *
216 * This function will print a sequence of bytes as hexadecimal numbers,
217 * without any space character between each byte (e.g. "1aefd9")
218 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100219char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200220{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200221 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200222}
Harald Welte28222962011-02-18 20:37:04 +0100223
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200224/* Compat with previous typo to preserve abi */
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100225char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200226#if defined(__MACH__) && defined(__APPLE__)
227 ;
228#else
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100229 __attribute__((weak, alias("osmo_hexdump_nospc")));
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200230#endif
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100231
Harald Welte28222962011-02-18 20:37:04 +0100232#include "../config.h"
233#ifdef HAVE_CTYPE_H
234#include <ctype.h>
Harald Welte8598f182011-08-17 14:19:27 +0200235/*! \brief Convert an entire string to lower case
236 * \param[out] out output string, caller-allocated
237 * \param[in] in input string
238 */
Harald Welte28222962011-02-18 20:37:04 +0100239void osmo_str2lower(char *out, const char *in)
240{
241 unsigned int i;
242
243 for (i = 0; i < strlen(in); i++)
244 out[i] = tolower(in[i]);
245 out[strlen(in)] = '\0';
246}
247
Harald Welte8598f182011-08-17 14:19:27 +0200248/*! \brief Convert an entire string to upper case
249 * \param[out] out output string, caller-allocated
250 * \param[in] in input string
251 */
Harald Welte28222962011-02-18 20:37:04 +0100252void osmo_str2upper(char *out, const char *in)
253{
254 unsigned int i;
255
256 for (i = 0; i < strlen(in); i++)
257 out[i] = toupper(in[i]);
258 out[strlen(in)] = '\0';
259}
260#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200261
Harald Welte9709b2e2016-04-25 18:47:53 +0200262/*! \brief Wishful thinking to generate a constant time compare
263 * \param[in] exp Expected data
264 * \param[in] rel Comparison value
265 * \param[in] count Number of bytes to compare
266 * \returns 1 in case \a exp equals \a rel; zero otherwise
267 *
268 * Compare count bytes of exp to rel. Return 0 if they are identical, 1
269 * otherwise. Do not return a mismatch on the first mismatching byte,
270 * but always compare all bytes, regardless. The idea is that the amount of
271 * matching bytes cannot be inferred from the time the comparison took. */
272int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
273{
274 int x = 0, i;
275
276 for (i = 0; i < count; ++i)
277 x |= exp[i] ^ rel[i];
278
279 /* if x is zero, all data was identical */
280 return x? 1 : 0;
281}
282
283/*! \brief Generic retrieval of 1..8 bytes as big-endian uint64_t
284 * \param[in] data Input data as byte-array
285 * \param[in] data_len Length of \a data in octets
286 * \returns uint64_t of \a data interpreted as big-endian
287 *
288 * This is like osmo_load64be_ext, except that if data_len is less than
289 * sizeof(uint64_t), the data is interpreted as the least significant bytes
290 * (osmo_load64be_ext loads them as the most significant bytes into the
291 * returned uint64_t). In this way, any integer size up to 64 bits can be
292 * decoded conveniently by using sizeof(), without the need to call specific
293 * numbered functions (osmo_load16, 32, ...). */
294uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len)
295{
296 uint64_t value = 0;
297
298 while (data_len > 0) {
299 value = (value << 8) + *data;
300 data += 1;
301 data_len -= 1;
302 }
303
304 return value;
305}
306
307/*! \brief Generic big-endian encoding of big endian number up to 64bit
308 * \param[in] value unsigned integer value to be stored
309 * \param[in] data_len number of octets
310 * \returns static buffer containing big-endian stored value
311 *
312 * This is like osmo_store64be_ext, except that this returns a static buffer of
313 * the result (for convenience, but not threadsafe). If data_len is less than
314 * sizeof(uint64_t), only the least significant bytes of value are encoded. */
315uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len)
316{
317 static uint8_t buf[sizeof(uint64_t)];
318 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
319 osmo_store64be_ext(value, buf, data_len);
320 return buf;
321}
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200322/*! @} */