blob: e76297ff40baf16a1a1c252b31e72aab0bb836da [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
Neels Hofmeyr8a3c83e2016-06-13 13:16:58 +020046 *
47 * If val is found in vs, the array's string entry is returned. Otherwise, an
48 * "unknown" string containing the actual value is composed in a static buffer
49 * that is reused across invocations.
Harald Welte8598f182011-08-17 14:19:27 +020050 */
Harald Welted284cd92010-03-01 21:58:31 +010051const char *get_value_string(const struct value_string *vs, uint32_t val)
52{
53 int i;
54
55 for (i = 0;; i++) {
56 if (vs[i].value == 0 && vs[i].str == NULL)
57 break;
58 if (vs[i].value == val)
59 return vs[i].str;
60 }
Harald Welteb59f9352010-03-25 11:37:04 +080061
62 snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
Holger Hans Peter Freyther8d506002013-07-04 20:14:10 +020063 namebuf[sizeof(namebuf) - 1] = '\0';
Harald Welteb59f9352010-03-25 11:37:04 +080064 return namebuf;
Harald Welted284cd92010-03-01 21:58:31 +010065}
66
Harald Welte8598f182011-08-17 14:19:27 +020067/*! \brief get numeric value for given human-readable string
68 * \param[in] vs Array of value_string tuples
69 * \param[in] str human-readable string
70 * \returns numeric value (>0) or negative numer in case of error
71 */
Harald Welted284cd92010-03-01 21:58:31 +010072int get_string_value(const struct value_string *vs, const char *str)
73{
74 int i;
75
76 for (i = 0;; i++) {
77 if (vs[i].value == 0 && vs[i].str == NULL)
78 break;
79 if (!strcasecmp(vs[i].str, str))
80 return vs[i].value;
81 }
82 return -EINVAL;
83}
Harald Weltea73e2f92010-03-04 10:50:32 +010084
Harald Welte8598f182011-08-17 14:19:27 +020085/*! \brief Convert BCD-encoded digit into printable character
86 * \param[in] bcd A single BCD-encoded digit
87 * \returns single printable character
88 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +020089char osmo_bcd2char(uint8_t bcd)
Harald Weltea73e2f92010-03-04 10:50:32 +010090{
91 if (bcd < 0xa)
92 return '0' + bcd;
93 else
94 return 'A' + (bcd - 0xa);
95}
96
Harald Weltede6e4982012-12-06 21:25:27 +010097/*! \brief Convert number in ASCII to BCD value
98 * \param[in] c ASCII character
99 * \returns BCD encoded value of character
100 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200101uint8_t osmo_char2bcd(char c)
Harald Weltea73e2f92010-03-04 10:50:32 +0100102{
103 return c - 0x30;
104}
Harald Welte3eba9912010-07-30 10:37:29 +0200105
Harald Weltede6e4982012-12-06 21:25:27 +0100106/*! \brief Parse a string ocntaining hexadecimal digits
107 * \param[in] str string containing ASCII encoded hexadecimal digits
108 * \param[out] b output buffer
109 * \param[in] max_len maximum space in output buffer
Neels Hofmeyr3de7b052015-09-23 23:16:53 +0200110 * \returns number of parsed octets, or -1 on error
Harald Weltede6e4982012-12-06 21:25:27 +0100111 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200112int osmo_hexparse(const char *str, uint8_t *b, int max_len)
Harald Welte3eba9912010-07-30 10:37:29 +0200113
114{
115 int i, l, v;
116
117 l = strlen(str);
118 if ((l&1) || ((l>>1) > max_len))
119 return -1;
120
121 memset(b, 0x00, max_len);
122
123 for (i=0; i<l; i++) {
124 char c = str[i];
125 if (c >= '0' && c <= '9')
126 v = c - '0';
127 else if (c >= 'a' && c <= 'f')
128 v = 10 + (c - 'a');
129 else if (c >= 'A' && c <= 'F')
130 v = 10 + (c - 'A');
131 else
132 return -1;
133 b[i>>1] |= v << (i&1 ? 0 : 4);
134 }
135
136 return i>>1;
137}
Harald Welte40481e82010-07-30 11:40:32 +0200138
139static char hexd_buff[4096];
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100140static const char hex_chars[] = "0123456789abcdef";
Harald Welte40481e82010-07-30 11:40:32 +0200141
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200142static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
Harald Welte40481e82010-07-30 11:40:32 +0200143{
144 int i;
145 char *cur = hexd_buff;
146
147 hexd_buff[0] = 0;
148 for (i = 0; i < len; i++) {
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100149 const char *delimp = delim;
Harald Welte40481e82010-07-30 11:40:32 +0200150 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100151 if (len_remain < 3)
Holger Hans Peter Freyther128d9e22011-07-15 16:07:23 +0200152 break;
Nils O. SelÄsdal32447022014-01-02 14:04:43 +0100153
154 *cur++ = hex_chars[buf[i] >> 4];
155 *cur++ = hex_chars[buf[i] & 0xf];
156
157 while (len_remain > 1 && *delimp) {
158 *cur++ = *delimp++;
159 len_remain--;
160 }
161
162 *cur = 0;
Harald Welte40481e82010-07-30 11:40:32 +0200163 }
164 hexd_buff[sizeof(hexd_buff)-1] = 0;
165 return hexd_buff;
166}
Harald Weltedee47cd2010-07-30 11:43:30 +0200167
Harald Welte8598f182011-08-17 14:19:27 +0200168/*! \brief Convert a sequence of unpacked bits to ASCII string
169 * \param[in] bits A sequence of unpacked bits
170 * \param[in] len Length of bits
171 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200172char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
Harald Welte3d0ac5e2011-02-08 16:55:03 +0100173{
174 int i;
175
176 if (len > sizeof(hexd_buff)-1)
177 len = sizeof(hexd_buff)-1;
178 memset(hexd_buff, 0, sizeof(hexd_buff));
179
180 for (i = 0; i < len; i++) {
181 char outch;
182 switch (bits[i]) {
183 case 0:
184 outch = '0';
185 break;
186 case 0xff:
187 outch = '?';
188 break;
189 case 1:
190 outch = '1';
191 break;
192 default:
193 outch = 'E';
194 break;
195 }
196 hexd_buff[i] = outch;
197 }
198 hexd_buff[sizeof(hexd_buff)-1] = 0;
199 return hexd_buff;
200}
201
Harald Welte8598f182011-08-17 14:19:27 +0200202/*! \brief Convert binary sequence to hexadecimal ASCII string
203 * \param[in] buf pointer to sequence of bytes
204 * \param[in] len length of buf in number of bytes
205 * \returns pointer to zero-terminated string
206 *
207 * This function will print a sequence of bytes as hexadecimal numbers,
208 * adding one space character between each byte (e.g. "1a ef d9")
209 */
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200210char *osmo_hexdump(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200211{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200212 return _osmo_hexdump(buf, len, " ");
Harald Weltedee47cd2010-07-30 11:43:30 +0200213}
214
Harald Welte8598f182011-08-17 14:19:27 +0200215/*! \brief Convert binary sequence to hexadecimal ASCII string
216 * \param[in] buf pointer to sequence of bytes
217 * \param[in] len length of buf in number of bytes
218 * \returns pointer to zero-terminated string
219 *
220 * This function will print a sequence of bytes as hexadecimal numbers,
221 * without any space character between each byte (e.g. "1aefd9")
222 */
Sylvain Munautff23d242011-11-10 23:03:18 +0100223char *osmo_hexdump_nospc(const unsigned char *buf, int len)
Harald Weltedee47cd2010-07-30 11:43:30 +0200224{
Pablo Neira Ayuso87f7b252011-05-07 12:43:08 +0200225 return _osmo_hexdump(buf, len, "");
Harald Weltedee47cd2010-07-30 11:43:30 +0200226}
Harald Welte28222962011-02-18 20:37:04 +0100227
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200228/* Compat with previous typo to preserve abi */
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100229char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200230#if defined(__MACH__) && defined(__APPLE__)
231 ;
232#else
Sylvain Munaut17af41d2011-11-19 22:30:39 +0100233 __attribute__((weak, alias("osmo_hexdump_nospc")));
Holger Hans Peter Freyther9a1a5a12015-04-11 19:26:55 +0200234#endif
Sylvain Munaute55ae3a2011-11-11 23:06:55 +0100235
Harald Welte28222962011-02-18 20:37:04 +0100236#include "../config.h"
237#ifdef HAVE_CTYPE_H
238#include <ctype.h>
Harald Welte8598f182011-08-17 14:19:27 +0200239/*! \brief Convert an entire string to lower case
240 * \param[out] out output string, caller-allocated
241 * \param[in] in input string
242 */
Harald Welte28222962011-02-18 20:37:04 +0100243void osmo_str2lower(char *out, const char *in)
244{
245 unsigned int i;
246
247 for (i = 0; i < strlen(in); i++)
248 out[i] = tolower(in[i]);
249 out[strlen(in)] = '\0';
250}
251
Harald Welte8598f182011-08-17 14:19:27 +0200252/*! \brief Convert an entire string to upper case
253 * \param[out] out output string, caller-allocated
254 * \param[in] in input string
255 */
Harald Welte28222962011-02-18 20:37:04 +0100256void osmo_str2upper(char *out, const char *in)
257{
258 unsigned int i;
259
260 for (i = 0; i < strlen(in); i++)
261 out[i] = toupper(in[i]);
262 out[strlen(in)] = '\0';
263}
264#endif /* HAVE_CTYPE_H */
Harald Welte8598f182011-08-17 14:19:27 +0200265
Harald Welte9709b2e2016-04-25 18:47:53 +0200266/*! \brief Wishful thinking to generate a constant time compare
267 * \param[in] exp Expected data
268 * \param[in] rel Comparison value
269 * \param[in] count Number of bytes to compare
270 * \returns 1 in case \a exp equals \a rel; zero otherwise
271 *
272 * Compare count bytes of exp to rel. Return 0 if they are identical, 1
273 * otherwise. Do not return a mismatch on the first mismatching byte,
274 * but always compare all bytes, regardless. The idea is that the amount of
275 * matching bytes cannot be inferred from the time the comparison took. */
276int osmo_constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
277{
278 int x = 0, i;
279
280 for (i = 0; i < count; ++i)
281 x |= exp[i] ^ rel[i];
282
283 /* if x is zero, all data was identical */
284 return x? 1 : 0;
285}
286
287/*! \brief Generic retrieval of 1..8 bytes as big-endian uint64_t
288 * \param[in] data Input data as byte-array
289 * \param[in] data_len Length of \a data in octets
290 * \returns uint64_t of \a data interpreted as big-endian
291 *
292 * This is like osmo_load64be_ext, except that if data_len is less than
293 * sizeof(uint64_t), the data is interpreted as the least significant bytes
294 * (osmo_load64be_ext loads them as the most significant bytes into the
295 * returned uint64_t). In this way, any integer size up to 64 bits can be
296 * decoded conveniently by using sizeof(), without the need to call specific
297 * numbered functions (osmo_load16, 32, ...). */
298uint64_t osmo_decode_big_endian(const uint8_t *data, size_t data_len)
299{
300 uint64_t value = 0;
301
302 while (data_len > 0) {
303 value = (value << 8) + *data;
304 data += 1;
305 data_len -= 1;
306 }
307
308 return value;
309}
310
311/*! \brief Generic big-endian encoding of big endian number up to 64bit
312 * \param[in] value unsigned integer value to be stored
313 * \param[in] data_len number of octets
314 * \returns static buffer containing big-endian stored value
315 *
316 * This is like osmo_store64be_ext, except that this returns a static buffer of
317 * the result (for convenience, but not threadsafe). If data_len is less than
318 * sizeof(uint64_t), only the least significant bytes of value are encoded. */
319uint8_t *osmo_encode_big_endian(uint64_t value, size_t data_len)
320{
321 static uint8_t buf[sizeof(uint64_t)];
322 OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
323 osmo_store64be_ext(value, buf, data_len);
324 return buf;
325}
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200326/*! @} */