Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2023 Marvell.
3 : : */
4 : :
5 : : #include <ctype.h>
6 : : #include <errno.h>
7 : : #include <stdio.h>
8 : : #include <stdlib.h>
9 : : #include <string.h>
10 : :
11 : : #include <rte_common.h>
12 : : #include <rte_string_fns.h>
13 : :
14 : : #include "module_api.h"
15 : :
16 : : static void
17 : 0 : hex_string_to_uint64(uint64_t *dst, const char *hexs)
18 : : {
19 : 0 : char buf[2] = {0};
20 : : uint8_t shift = 4;
21 : : int iter = 0;
22 : : char c;
23 : :
24 : 0 : while ((c = *hexs++)) {
25 : 0 : buf[0] = c;
26 : 0 : *dst |= (strtol(buf, NULL, 16) << shift);
27 : 0 : shift -= 4;
28 : 0 : iter++;
29 : 0 : if (iter == 2) {
30 : : iter = 0;
31 : : shift = 4;
32 : 0 : dst++;
33 : : }
34 : : }
35 : 0 : }
36 : :
37 : : int
38 : 0 : parser_uint64_read(uint64_t *value, const char *p)
39 : : {
40 : : char *next;
41 : : uint64_t val;
42 : :
43 : 0 : p = rte_str_skip_leading_spaces(p);
44 : 0 : if (!isdigit(*p))
45 : : return -EINVAL;
46 : :
47 : 0 : val = strtoul(p, &next, 0);
48 : 0 : if (p == next)
49 : : return -EINVAL;
50 : :
51 : : p = next;
52 : 0 : switch (*p) {
53 : 0 : case 'T':
54 : 0 : val *= 1024ULL;
55 : : /* fall through */
56 : 0 : case 'G':
57 : 0 : val *= 1024ULL;
58 : : /* fall through */
59 : 0 : case 'M':
60 : 0 : val *= 1024ULL;
61 : : /* fall through */
62 : 0 : case 'k':
63 : : case 'K':
64 : 0 : val *= 1024ULL;
65 : 0 : p++;
66 : 0 : break;
67 : : }
68 : :
69 : 0 : p = rte_str_skip_leading_spaces(p);
70 : 0 : if (*p != '\0')
71 : : return -EINVAL;
72 : :
73 : 0 : *value = val;
74 : 0 : return 0;
75 : : }
76 : :
77 : : int
78 : 0 : parser_uint32_read(uint32_t *value, const char *p)
79 : : {
80 : 0 : uint64_t val = 0;
81 : 0 : int rc = parser_uint64_read(&val, p);
82 : :
83 : 0 : if (rc < 0)
84 : : return rc;
85 : :
86 : 0 : if (val > UINT32_MAX)
87 : : return -ERANGE;
88 : :
89 : 0 : *value = val;
90 : 0 : return 0;
91 : : }
92 : :
93 : : int
94 : 0 : parser_ip4_read(uint32_t *value, char *p)
95 : : {
96 : : uint8_t shift = 24;
97 : : uint32_t ip = 0;
98 : 0 : char *token, *saveptr = NULL;
99 : :
100 : 0 : token = strtok_r(p, ".", &saveptr);
101 : 0 : while (token != NULL) {
102 : 0 : ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
103 : 0 : token = strtok_r(NULL, ".", &saveptr);
104 : 0 : shift -= 8;
105 : : }
106 : :
107 : 0 : *value = ip;
108 : :
109 : 0 : return 0;
110 : : }
111 : :
112 : : int
113 : 0 : parser_ip6_read(uint8_t *value, char *p)
114 : : {
115 : 0 : uint64_t val = 0;
116 : 0 : char *token, *saveptr = NULL;
117 : :
118 : 0 : token = strtok_r(p, ":", &saveptr);
119 : 0 : while (token != NULL) {
120 : 0 : hex_string_to_uint64(&val, token);
121 : 0 : *value = val;
122 : 0 : token = strtok_r(NULL, ":", &saveptr);
123 : 0 : value++;
124 : 0 : val = 0;
125 : : }
126 : :
127 : 0 : return 0;
128 : : }
129 : :
130 : : int
131 : 0 : parser_mac_read(uint64_t *value, char *p)
132 : : {
133 : 0 : uint64_t mac = 0, val = 0;
134 : : uint8_t shift = 40;
135 : 0 : char *token, *saveptr = NULL;
136 : :
137 : 0 : token = strtok_r(p, ":", &saveptr);
138 : 0 : while (token != NULL) {
139 : 0 : hex_string_to_uint64(&val, token);
140 : 0 : mac |= val << shift;
141 : 0 : token = strtok_r(NULL, ":", &saveptr);
142 : 0 : shift -= 8;
143 : 0 : val = 0;
144 : : }
145 : :
146 : 0 : *value = mac;
147 : :
148 : 0 : return 0;
149 : : }
|