blob: a1881f0676da4903243073e8cf7c221e331eeeb1 [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>
Pau Espin Pedrol45735022017-06-18 14:05:24 +020029#include <inttypes.h>
Harald Welted284cd92010-03-01 21:58:31 +010030
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/utils.h>
Harald Welte9709b2e2016-04-25 18:47:53 +020032#include <osmocom/core/bit64gen.h>
33
Harald Welted284cd92010-03-01 21:58:31 +010034
Harald Welte8598f182011-08-17 14:19:27 +020035/*! \addtogroup utils
36 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037 * various utility routines
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020038 *
39 * \file utils.c */
Harald Welte8598f182011-08-17 14:19:27 +020040
Harald Welteb59f9352010-03-25 11:37:04 +080041static char namebuf[255];
Harald Welte8598f182011-08-17 14:19:27 +020042
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043/*! get human-readable string for given value
Harald Welte8598f182011-08-17 14:19:27 +020044 * \param[in] vs Array of value_string tuples
45 * \param[in] val Value to be converted
46 * \returns pointer to human-readable string
Neels Hofmeyr8a3c83e2016-06-13 13:16:58 +020047 *
48 * If val is found in vs, the array's string entry is returned. Otherwise, an
49 * "unknown" string containing the actual value is composed in a static buffer
50 * that is reused across invocations.
Harald Welte8598f182011-08-17 14:19:27 +020051 */
Harald Welted284cd92010-03-01 21:58:31 +010052const char *get_value_string(const struct value_string *vs, uint32_t val)
53{
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020054 const char *str = get_value_string_or_null(vs, val);
55 if (str)
56 return str;
57
Pau Espin Pedrol45735022017-06-18 14:05:24 +020058 snprintf(namebuf, sizeof(namebuf), "unknown 0x%"PRIx32, val);
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020059 namebuf[sizeof(namebuf) - 1] = '\0';
60 return namebuf;
61}
62
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! get human-readable string or NULL for given value
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020064 * \param[in] vs Array of value_string tuples
65 * \param[in] val Value to be converted
66 * \returns pointer to human-readable string or NULL if val is not found
67 */
68const char *get_value_string_or_null(const struct value_string *vs,
69 uint32_t val)
70{
Harald Welted284cd92010-03-01 21:58:31 +010071 int i;
72
73 for (i = 0;; i++) {
74 if (vs[i].value == 0 && vs[i].str == NULL)
75 break;
76 if (vs[i].value == val)
77 return vs[i].str;
78 }
Harald Welteb59f9352010-03-25 11:37:04 +080079
Neels Hofmeyr8d6dcd92016-06-06 18:05:23 +020080 return NULL;
Harald Welted284cd92010-03-01 21:58:31 +010081}
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083/*! get numeric value for given human-readable string
Harald Welte8598f182011-08-17 14:19:27 +020084 * \param[in] vs Array of value_string tuples
85 * \param[in] str human-readable string
86 * \returns numeric value (>0) or negative numer in case of error
87 */
Harald Welted284cd92010-03-01 21:58:31 +010088int get_string_value(const struct value_string *vs, const char *str)
89{
90 int i;
91
92 for (i = 0;; i++) {
93 if (vs[i].value == 0 && vs[i].str == NULL)
94 break;
95 if (!strcasecmp(vs[i].str, str))
96 return vs[i].value;
97 }
98 return -EINVAL;
99}
Harald Weltea73e2f92010-03-04 10:50:32 +0100100
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200101/*! Convert BCD-encoded digit into printable character
Harald Welte8598f182011-08-17 14:19:27 +0200102 * \param[in] bcd A single BCD-encoded digit
103 * \returns single printable character
104 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200105char osmo_bcd2char(uint8_t bcd)
Harald Weltea73e2f92010-03-04 10:50:32 +0100106{
107 if (bcd < 0xa)
108 return '0' + bcd;
109 else
110 return 'A' + (bcd - 0xa);
111}
112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113/*! Convert number in ASCII to BCD value
Harald Weltede6e4982012-12-06 21:25:27 +0100114 * \param[in] c ASCII character
115 * \returns BCD encoded value of character
116 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200117uint8_t osmo_char2bcd(char c)
Harald Weltea73e2f92010-03-04 10:50:32 +0100118{
119 return c - 0x30;
120}
Harald Welte3eba9912010-07-30 10:37:29 +0200121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122/*! Parse a string containing hexadecimal digits
Harald Weltede6e4982012-12-06 21:25:27 +0100123 * \param[in] str string containing ASCII encoded hexadecimal digits
124 * \param[out] b output buffer
125 * \param[in] max_len maximum space in output buffer
Neels Hofmeyr3de7b052015-09-23 23:16:53 +0200126 * \returns number of parsed octets, or -1 on error
Harald Weltede6e4982012-12-06 21:25:27 +0100127 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200128int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +0200129
130{
Neels Hofmeyr437ed4a2017-02-14 15:54:31 +0100131 char c;
132 uint8_t v;
133 const char *strpos;
134 unsigned int nibblepos = 0;
Harald Welte3eba9912010-07-30 10:37:29 +0200135
136 memset(b, 0x00, max_len);
137
Neels Hofmeyr437ed4a2017-02-14 15:54:31 +0100138 for (strpos = str; (c = *strpos); strpos++) {
139 /* skip whitespace */
140 if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
141 continue;
142
143 /* If the buffer is too small, error out */
144 if (nibblepos >= (max_len << 1))
145 return -1;
146
Harald Welte3eba9912010-07-30 10:37:29 +0200147 if (c >= '0' && c <= '9')
148 v = c - '0';
149 else if (c >= 'a' && c <= 'f')
150 v = 10 + (c - 'a');
151 else if (c >= 'A' && c <= 'F')
152 v = 10 + (c - 'A');
153 else
154 return -1;
Neels Hofmeyr437ed4a2017-02-14 15:54:31 +0100155
156 b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4);
157 nibblepos ++;
Harald Welte3eba9912010-07-30 10:37:29 +0200158 }
159
Neels Hofmeyr437ed4a2017-02-14 15:54:31 +0100160 /* In case of uneven amount of digits, the last byte is not complete
161 * and that's an error. */
162 if (nibblepos & 1)
163 return -1;
164
165 return nibblepos >> 1;
Harald Welte3eba9912010-07-30 10:37:29 +0200166}
Harald Welte40481e82010-07-30 11:40:32 +0200167
168static char hexd_buff[4096];
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100169static const char hex_chars[] = "0123456789abcdef";
Harald Welte40481e82010-07-30 11:40:32 +0200170
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200171static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200172{
173 int i;
174 char *cur = hexd_buff;
175
176 hexd_buff[0] = 0;
177 for (i = 0; i < len; i++) {
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100178 const char *delimp = delim;
Harald Welte40481e82010-07-30 11:40:32 +0200179 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100180 if (len_remain < 3)
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200181 break;
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100182
183 *cur++ = hex_chars[buf[i] >> 4];
184 *cur++ = hex_chars[buf[i] & 0xf];
185
186 while (len_remain > 1 && *delimp) {
187 *cur++ = *delimp++;
188 len_remain--;
189 }
190
191 *cur = 0;
Harald Welte40481e82010-07-30 11:40:32 +0200192 }
193 hexd_buff[sizeof(hexd_buff)-1] = 0;
194 return hexd_buff;
195}
Harald Weltedee47cd2010-07-30 11:43:30 +0200196
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200197/*! Convert a sequence of unpacked bits to ASCII string
Harald Welte8598f182011-08-17 14:19:27 +0200198 * \param[in] bits A sequence of unpacked bits
199 * \param[in] len Length of bits
200 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200201char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100202{
203 int i;
204
205 if (len > sizeof(hexd_buff)-1)
206 len = sizeof(hexd_buff)-1;
207 memset(hexd_buff, 0, sizeof(hexd_buff));
208
209 for (i = 0; i < len; i++) {
210 char outch;
211 switch (bits[i]) {
212 case 0:
213 outch = '0';
214 break;
215 case 0xff:
216 outch = '?';
217 break;
218 case 1:
219 outch = '1';
220 break;
221 default:
222 outch = 'E';
223 break;
224 }
225 hexd_buff[i] = outch;
226 }
227 hexd_buff[sizeof(hexd_buff)-1] = 0;
228 return hexd_buff;
229}
230
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200231/*! Convert binary sequence to hexadecimal ASCII string
Harald Welte8598f182011-08-17 14:19:27 +0200232 * \param[in] buf pointer to sequence of bytes
233 * \param[in] len length of buf in number of bytes
234 * \returns pointer to zero-terminated string
235 *
236 * This function will print a sequence of bytes as hexadecimal numbers,
237 * adding one space character between each byte (e.g. "1a ef d9")
238 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200239char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200240{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200241 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200242}
243
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200244/*! Convert binary sequence to hexadecimal ASCII string
Harald Welte8598f182011-08-17 14:19:27 +0200245 * \param[in] buf pointer to sequence of bytes
246 * \param[in] len length of buf in number of bytes
247 * \returns pointer to zero-terminated string
248 *
249 * This function will print a sequence of bytes as hexadecimal numbers,
250 * without any space character between each byte (e.g. "1aefd9")
251 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100252char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200253{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200254 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200255}
Harald Welte28222962011-02-18 20:37:04 +0100256
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200257/* Compat with previous typo to preserve abi */
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100258char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200259#if defined(__MACH__) && defined(__APPLE__)
260 ;
261#else
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100262 __attribute__((weak, alias("osmo_hexdump_nospc")));
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200263#endif
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100264
Harald Welte28222962011-02-18 20:37:04 +0100265#include "../config.h"
266#ifdef HAVE_CTYPE_H
267#include <ctype.h>
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200268/*! Convert an entire string to lower case
Harald Welte8598f182011-08-17 14:19:27 +0200269 * \param[out] out output string, caller-allocated
270 * \param[in] in input string
271 */
Harald Welte28222962011-02-18 20:37:04 +0100272void osmo_str2lower(char *out, const char *in)
273{
274 unsigned int i;
275
276 for (i = 0; i < strlen(in); i++)
277 out[i] = tolower(in[i]);
278 out[strlen(in)] = '\0';
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! Convert an entire string to upper case
Harald Welte8598f182011-08-17 14:19:27 +0200282 * \param[out] out output string, caller-allocated
283 * \param[in] in input string
284 */
Harald Welte28222962011-02-18 20:37:04 +0100285void osmo_str2upper(char *out, const char *in)
286{
287 unsigned int i;
288
289 for (i = 0; i < strlen(in); i++)
290 out[i] = toupper(in[i]);
291 out[strlen(in)] = '\0';
292}
293#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200294
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200295/*! Wishful thinking to generate a constant time compare
Harald Welte9709b2e2016-04-25 18:47:53 +0200296 * \param[in] exp Expected data
297 * \param[in] rel Comparison value
298 * \param[in] count Number of bytes to compare
299 * \returns 1 in case \a exp equals \a rel; zero otherwise
300 *
301 * Compare count bytes of exp to rel. Return 0 if they are identical, 1
302 * otherwise. Do not return a mismatch on the first mismatching byte,
303 * but always compare all bytes, regardless. The idea is that the amount of
304 * matching bytes cannot be inferred from the time the comparison took. */
305int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
306{
307 int x = 0, i;
308
309 for (i = 0; i < count; ++i)
310 x |= exp[i] ^ rel[i];
311
312 /* if x is zero, all data was identical */
313 return x? 1 : 0;
314}
315
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200316/*! Generic retrieval of 1..8 bytes as big-endian uint64_t
Harald Welte9709b2e2016-04-25 18:47:53 +0200317 * \param[in] data Input data as byte-array
318 * \param[in] data_len Length of \a data in octets
319 * \returns uint64_t of \a data interpreted as big-endian
320 *
321 * This is like osmo_load64be_ext, except that if data_len is less than
322 * sizeof(uint64_t), the data is interpreted as the least significant bytes
323 * (osmo_load64be_ext loads them as the most significant bytes into the
324 * returned uint64_t). In this way, any integer size up to 64 bits can be
325 * decoded conveniently by using sizeof(), without the need to call specific
326 * numbered functions (osmo_load16, 32, ...). */
327uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len)
328{
329 uint64_t value = 0;
330
331 while (data_len > 0) {
332 value = (value << 8) + *data;
333 data += 1;
334 data_len -= 1;
335 }
336
337 return value;
338}
339
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200340/*! Generic big-endian encoding of big endian number up to 64bit
Harald Welte9709b2e2016-04-25 18:47:53 +0200341 * \param[in] value unsigned integer value to be stored
342 * \param[in] data_len number of octets
343 * \returns static buffer containing big-endian stored value
344 *
345 * This is like osmo_store64be_ext, except that this returns a static buffer of
346 * the result (for convenience, but not threadsafe). If data_len is less than
347 * sizeof(uint64_t), only the least significant bytes of value are encoded. */
348uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len)
349{
350 static uint8_t buf[sizeof(uint64_t)];
351 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
352 osmo_store64be_ext(value, buf, data_len);
353 return buf;
354}
Harald Welteaeecc482016-11-26 10:41:40 +0100355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! Copy a C-string into a sized buffer
Harald Welteaeecc482016-11-26 10:41:40 +0100357 * \param[in] src source string
358 * \param[out] dst destination string
Neels Hofmeyrdf83ece2017-01-13 13:55:43 +0100359 * \param[in] siz size of the \a dst buffer
360 * \returns length of \a src
Harald Welteaeecc482016-11-26 10:41:40 +0100361 *
Neels Hofmeyrdf83ece2017-01-13 13:55:43 +0100362 * Copy at most \a siz bytes from \a src to \a dst, ensuring that the result is
363 * NUL terminated. The NUL character is included in \a siz, i.e. passing the
364 * actual sizeof(*dst) is correct.
Harald Welteaeecc482016-11-26 10:41:40 +0100365 */
366size_t osmo_strlcpy(char *dst, const char *src, size_t siz)
367{
368 size_t ret = strlen(src);
369
370 if (siz) {
371 size_t len = (ret >= siz) ? siz - 1 : ret;
372 memcpy(dst, src, len);
373 dst[len] = '\0';
374 }
375 return ret;
376}
Neels Hofmeyr0aeda1b2017-01-13 14:16:02 +0100377
378/*! @} */