parser.c 8.9 KB

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