parser.c 9.1 KB

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