osmo_hexdump: Fix segfault when input is too long.
[osmocom-bb.git] / src / utils.c
1
2 #include <string.h>
3 #include <stdint.h>
4 #include <errno.h>
5 #include <stdio.h>
6
7 #include <osmocom/core/utils.h>
8
9 static char namebuf[255];
10 const char *get_value_string(const struct value_string *vs, uint32_t val)
11 {
12         int i;
13
14         for (i = 0;; i++) {
15                 if (vs[i].value == 0 && vs[i].str == NULL)
16                         break;
17                 if (vs[i].value == val)
18                         return vs[i].str;
19         }
20
21         snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val);
22         return namebuf;
23 }
24
25 int get_string_value(const struct value_string *vs, const char *str)
26 {
27         int i;
28
29         for (i = 0;; i++) {
30                 if (vs[i].value == 0 && vs[i].str == NULL)
31                         break;
32                 if (!strcasecmp(vs[i].str, str))
33                         return vs[i].value;
34         }
35         return -EINVAL;
36 }
37
38 char osmo_bcd2char(uint8_t bcd)
39 {
40         if (bcd < 0xa)
41                 return '0' + bcd;
42         else
43                 return 'A' + (bcd - 0xa);
44 }
45
46 /* only works for numbers in ascii */
47 uint8_t osmo_char2bcd(char c)
48 {
49         return c - 0x30;
50 }
51
52 int osmo_hexparse(const char *str, uint8_t *b, int max_len)
53
54 {
55         int i, l, v;
56
57         l = strlen(str);
58         if ((l&1) || ((l>>1) > max_len))
59                 return -1;
60
61         memset(b, 0x00, max_len);
62
63         for (i=0; i<l; i++) {
64                 char c = str[i];
65                 if (c >= '0' && c <= '9')
66                         v = c - '0';
67                 else if (c >= 'a' && c <= 'f')
68                         v = 10 + (c - 'a');
69                 else if (c >= 'A' && c <= 'F')
70                         v = 10 + (c - 'A');
71                 else
72                         return -1;
73                 b[i>>1] |= v << (i&1 ? 0 : 4);
74         }
75
76         return i>>1;
77 }
78
79 static char hexd_buff[4096];
80
81 static char *_osmo_hexdump(const unsigned char *buf, int len, char *delim)
82 {
83         int i;
84         char *cur = hexd_buff;
85
86         hexd_buff[0] = 0;
87         for (i = 0; i < len; i++) {
88                 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
89                 if (len_remain <= 0)
90                         break;
91                 int rc = snprintf(cur, len_remain, "%02x%s", buf[i], delim);
92                 if (rc <= 0)
93                         break;
94                 cur += rc;
95         }
96         hexd_buff[sizeof(hexd_buff)-1] = 0;
97         return hexd_buff;
98 }
99
100 char *osmo_ubit_dump(const uint8_t *bits, unsigned int len)
101 {
102         int i;
103
104         if (len > sizeof(hexd_buff)-1)
105                 len = sizeof(hexd_buff)-1;
106         memset(hexd_buff, 0, sizeof(hexd_buff));
107
108         for (i = 0; i < len; i++) {
109                 char outch;
110                 switch (bits[i]) {
111                 case 0:
112                         outch = '0';
113                         break;
114                 case 0xff:
115                         outch = '?';
116                         break;
117                 case 1:
118                         outch = '1';
119                         break;
120                 default:
121                         outch = 'E';
122                         break;
123                 }
124                 hexd_buff[i] = outch;
125         }
126         hexd_buff[sizeof(hexd_buff)-1] = 0;
127         return hexd_buff;
128 }
129
130 char *osmo_hexdump(const unsigned char *buf, int len)
131 {
132         return _osmo_hexdump(buf, len, " ");
133 }
134
135 char *osmo_osmo_hexdump_nospc(const unsigned char *buf, int len)
136 {
137         return _osmo_hexdump(buf, len, "");
138 }
139
140 #include "../config.h"
141 #ifdef HAVE_CTYPE_H
142 #include <ctype.h>
143 void osmo_str2lower(char *out, const char *in)
144 {
145         unsigned int i;
146
147         for (i = 0; i < strlen(in); i++)
148                 out[i] = tolower(in[i]);
149         out[strlen(in)] = '\0';
150 }
151
152 void osmo_str2upper(char *out, const char *in)
153 {
154         unsigned int i;
155
156         for (i = 0; i < strlen(in); i++)
157                 out[i] = toupper(in[i]);
158         out[strlen(in)] = '\0';
159 }
160 #endif /* HAVE_CTYPE_H */