1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * lib/parser.c - simple parser for mount, etc. options.
4 */
5
6#include <linux/ctype.h>
7#include <linux/types.h>
8#include <linux/export.h>
9#include <linux/kstrtox.h>
10#include <linux/parser.h>
11#include <linux/slab.h>
12#include <linux/string.h>
13
14/*
15 * max size needed by different bases to express U64
16 * HEX: "0xFFFFFFFFFFFFFFFF" --> 18
17 * DEC: "18446744073709551615" --> 20
18 * OCT: "01777777777777777777777" --> 23
19 * pick the max one to define NUMBER_BUF_LEN
20 */
21#define NUMBER_BUF_LEN 24
22
23/**
24 * match_one - Determines if a string matches a simple pattern
25 * @s: the string to examine for presence of the pattern
26 * @p: the string containing the pattern
27 * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
28 * locations.
29 *
30 * Description: Determines if the pattern @p is present in string @s. Can only
31 * match extremely simple token=arg style patterns. If the pattern is found,
32 * the location(s) of the arguments will be returned in the @args array.
33 */
34static int match_one(char *s, const char *p, substring_t args[])
35{
36	char *meta;
37	int argc = 0;
38
39	if (!p)
40		return 1;
41
42	while(1) {
43		int len = -1;
44		meta = strchr(p, '%');
45		if (!meta)
46			return strcmp(p, s) == 0;
47
48		if (strncmp(p, s, meta-p))
49			return 0;
50
51		s += meta - p;
52		p = meta + 1;
53
54		if (isdigit(*p))
55			len = simple_strtoul(p, (char **) &p, 10);
56		else if (*p == '%') {
57			if (*s++ != '%')
58				return 0;
59			p++;
60			continue;
61		}
62
63		if (argc >= MAX_OPT_ARGS)
64			return 0;
65
66		args[argc].from = s;
67		switch (*p++) {
68		case 's': {
69			size_t str_len = strlen(s);
70
71			if (str_len == 0)
72				return 0;
73			if (len == -1 || len > str_len)
74				len = str_len;
75			args[argc].to = s + len;
76			break;
77		}
78		case 'd':
79			simple_strtol(s, &args[argc].to, 0);
80			goto num;
81		case 'u':
82			simple_strtoul(s, &args[argc].to, 0);
83			goto num;
84		case 'o':
85			simple_strtoul(s, &args[argc].to, 8);
86			goto num;
87		case 'x':
88			simple_strtoul(s, &args[argc].to, 16);
89		num:
90			if (args[argc].to == args[argc].from)
91				return 0;
92			break;
93		default:
94			return 0;
95		}
96		s = args[argc].to;
97		argc++;
98	}
99}
100
101/**
102 * match_token - Find a token (and optional args) in a string
103 * @s: the string to examine for token/argument pairs
104 * @table: match_table_t describing the set of allowed option tokens and the
105 * arguments that may be associated with them. Must be terminated with a
106 * &struct match_token whose pattern is set to the NULL pointer.
107 * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match
108 * locations.
109 *
110 * Description: Detects which if any of a set of token strings has been passed
111 * to it. Tokens can include up to %MAX_OPT_ARGS instances of basic c-style
112 * format identifiers which will be taken into account when matching the
113 * tokens, and whose locations will be returned in the @args array.
114 */
115int match_token(char *s, const match_table_t table, substring_t args[])
116{
117	const struct match_token *p;
118
119	for (p = table; !match_one(s, p->pattern, args) ; p++)
120		;
121
122	return p->token;
123}
124EXPORT_SYMBOL(match_token);
125
126/**
127 * match_number - scan a number in the given base from a substring_t
128 * @s: substring to be scanned
129 * @result: resulting integer on success
130 * @base: base to use when converting string
131 *
132 * Description: Given a &substring_t and a base, attempts to parse the substring
133 * as a number in that base.
134 *
135 * Return: On success, sets @result to the integer represented by the
136 * string and returns 0. Returns -EINVAL or -ERANGE on failure.
137 */
138static int match_number(substring_t *s, int *result, int base)
139{
140	char *endp;
141	char buf[NUMBER_BUF_LEN];
142	int ret;
143	long val;
144
145	if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
146		return -ERANGE;
147	ret = 0;
148	val = simple_strtol(buf, &endp, base);
149	if (endp == buf)
150		ret = -EINVAL;
151	else if (val < (long)INT_MIN || val > (long)INT_MAX)
152		ret = -ERANGE;
153	else
154		*result = (int) val;
155	return ret;
156}
157
158/**
159 * match_u64int - scan a number in the given base from a substring_t
160 * @s: substring to be scanned
161 * @result: resulting u64 on success
162 * @base: base to use when converting string
163 *
164 * Description: Given a &substring_t and a base, attempts to parse the substring
165 * as a number in that base.
166 *
167 * Return: On success, sets @result to the integer represented by the
168 * string and returns 0. Returns -EINVAL or -ERANGE on failure.
169 */
170static int match_u64int(substring_t *s, u64 *result, int base)
171{
172	char buf[NUMBER_BUF_LEN];
173	int ret;
174	u64 val;
175
176	if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
177		return -ERANGE;
178	ret = kstrtoull(buf, base, &val);
179	if (!ret)
180		*result = val;
181	return ret;
182}
183
184/**
185 * match_int - scan a decimal representation of an integer from a substring_t
186 * @s: substring_t to be scanned
187 * @result: resulting integer on success
188 *
189 * Description: Attempts to parse the &substring_t @s as a decimal integer.
190 *
191 * Return: On success, sets @result to the integer represented by the string
192 * and returns 0. Returns -EINVAL or -ERANGE on failure.
193 */
194int match_int(substring_t *s, int *result)
195{
196	return match_number(s, result, 0);
197}
198EXPORT_SYMBOL(match_int);
199
200/**
201 * match_uint - scan a decimal representation of an integer from a substring_t
202 * @s: substring_t to be scanned
203 * @result: resulting integer on success
204 *
205 * Description: Attempts to parse the &substring_t @s as a decimal integer.
206 *
207 * Return: On success, sets @result to the integer represented by the string
208 * and returns 0. Returns -EINVAL or -ERANGE on failure.
209 */
210int match_uint(substring_t *s, unsigned int *result)
211{
212	char buf[NUMBER_BUF_LEN];
213
214	if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >= NUMBER_BUF_LEN)
215		return -ERANGE;
216
217	return kstrtouint(buf, 10, result);
218}
219EXPORT_SYMBOL(match_uint);
220
221/**
222 * match_u64 - scan a decimal representation of a u64 from
223 *                  a substring_t
224 * @s: substring_t to be scanned
225 * @result: resulting unsigned long long on success
226 *
227 * Description: Attempts to parse the &substring_t @s as a long decimal
228 * integer.
229 *
230 * Return: On success, sets @result to the integer represented by the string
231 * and returns 0. Returns -EINVAL or -ERANGE on failure.
232 */
233int match_u64(substring_t *s, u64 *result)
234{
235	return match_u64int(s, result, 0);
236}
237EXPORT_SYMBOL(match_u64);
238
239/**
240 * match_octal - scan an octal representation of an integer from a substring_t
241 * @s: substring_t to be scanned
242 * @result: resulting integer on success
243 *
244 * Description: Attempts to parse the &substring_t @s as an octal integer.
245 *
246 * Return: On success, sets @result to the integer represented by the string
247 * and returns 0. Returns -EINVAL or -ERANGE on failure.
248 */
249int match_octal(substring_t *s, int *result)
250{
251	return match_number(s, result, 8);
252}
253EXPORT_SYMBOL(match_octal);
254
255/**
256 * match_hex - scan a hex representation of an integer from a substring_t
257 * @s: substring_t to be scanned
258 * @result: resulting integer on success
259 *
260 * Description: Attempts to parse the &substring_t @s as a hexadecimal integer.
261 *
262 * Return: On success, sets @result to the integer represented by the string
263 * and returns 0. Returns -EINVAL or -ERANGE on failure.
264 */
265int match_hex(substring_t *s, int *result)
266{
267	return match_number(s, result, 16);
268}
269EXPORT_SYMBOL(match_hex);
270
271/**
272 * match_wildcard - parse if a string matches given wildcard pattern
273 * @pattern: wildcard pattern
274 * @str: the string to be parsed
275 *
276 * Description: Parse the string @str to check if matches wildcard
277 * pattern @pattern. The pattern may contain two types of wildcards:
278 *   '*' - matches zero or more characters
279 *   '?' - matches one character
280 *
281 * Return: If the @str matches the @pattern, return true, else return false.
282 */
283bool match_wildcard(const char *pattern, const char *str)
284{
285	const char *s = str;
286	const char *p = pattern;
287	bool star = false;
288
289	while (*s) {
290		switch (*p) {
291		case '?':
292			s++;
293			p++;
294			break;
295		case '*':
296			star = true;
297			str = s;
298			if (!*++p)
299				return true;
300			pattern = p;
301			break;
302		default:
303			if (*s == *p) {
304				s++;
305				p++;
306			} else {
307				if (!star)
308					return false;
309				str++;
310				s = str;
311				p = pattern;
312			}
313			break;
314		}
315	}
316
317	if (*p == '*')
318		++p;
319	return !*p;
320}
321EXPORT_SYMBOL(match_wildcard);
322
323/**
324 * match_strlcpy - Copy the characters from a substring_t to a sized buffer
325 * @dest: where to copy to
326 * @src: &substring_t to copy
327 * @size: size of destination buffer
328 *
329 * Description: Copy the characters in &substring_t @src to the
330 * c-style string @dest.  Copy no more than @size - 1 characters, plus
331 * the terminating NUL.
332 *
333 * Return: length of @src.
334 */
335size_t match_strlcpy(char *dest, const substring_t *src, size_t size)
336{
337	size_t ret = src->to - src->from;
338
339	if (size) {
340		size_t len = ret >= size ? size - 1 : ret;
341		memcpy(dest, src->from, len);
342		dest[len] = '\0';
343	}
344	return ret;
345}
346EXPORT_SYMBOL(match_strlcpy);
347
348/**
349 * match_strdup - allocate a new string with the contents of a substring_t
350 * @s: &substring_t to copy
351 *
352 * Description: Allocates and returns a string filled with the contents of
353 * the &substring_t @s. The caller is responsible for freeing the returned
354 * string with kfree().
355 *
356 * Return: the address of the newly allocated NUL-terminated string or
357 * %NULL on error.
358 */
359char *match_strdup(const substring_t *s)
360{
361	return kmemdup_nul(s->from, s->to - s->from, GFP_KERNEL);
362}
363EXPORT_SYMBOL(match_strdup);
364