parser.c 8.1 KB

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