aboutsummaryrefslogtreecommitdiffstats
path: root/lib/parser.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parser.c')
-rw-r--r--lib/parser.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/parser.c b/lib/parser.c
index b6d11631231b..3278958b472a 100644
--- a/lib/parser.c
+++ b/lib/parser.c
@@ -152,6 +152,36 @@ static int match_number(substring_t *s, int *result, int base)
152} 152}
153 153
154/** 154/**
155 * match_u64int: scan a number in the given base from a substring_t
156 * @s: substring to be scanned
157 * @result: resulting u64 on success
158 * @base: base to use when converting string
159 *
160 * Description: Given a &substring_t and a base, attempts to parse the substring
161 * as a number in that base. On success, sets @result to the integer represented
162 * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
163 */
164static int match_u64int(substring_t *s, u64 *result, int base)
165{
166 char *buf;
167 int ret;
168 u64 val;
169 size_t len = s->to - s->from;
170
171 buf = kmalloc(len + 1, GFP_KERNEL);
172 if (!buf)
173 return -ENOMEM;
174 memcpy(buf, s->from, len);
175 buf[len] = '\0';
176
177 ret = kstrtoull(buf, base, &val);
178 if (!ret)
179 *result = val;
180 kfree(buf);
181 return ret;
182}
183
184/**
155 * match_int: - scan a decimal representation of an integer from a substring_t 185 * match_int: - scan a decimal representation of an integer from a substring_t
156 * @s: substring_t to be scanned 186 * @s: substring_t to be scanned
157 * @result: resulting integer on success 187 * @result: resulting integer on success
@@ -167,6 +197,23 @@ int match_int(substring_t *s, int *result)
167EXPORT_SYMBOL(match_int); 197EXPORT_SYMBOL(match_int);
168 198
169/** 199/**
200 * match_u64: - scan a decimal representation of a u64 from
201 * a substring_t
202 * @s: substring_t to be scanned
203 * @result: resulting unsigned long long on success
204 *
205 * Description: Attempts to parse the &substring_t @s as a long decimal
206 * integer. On success, sets @result to the integer represented by the
207 * string and returns 0.
208 * Returns -ENOMEM, -EINVAL, or -ERANGE on failure.
209 */
210int match_u64(substring_t *s, u64 *result)
211{
212 return match_u64int(s, result, 0);
213}
214EXPORT_SYMBOL(match_u64);
215
216/**
170 * match_octal: - scan an octal representation of an integer from a substring_t 217 * match_octal: - scan an octal representation of an integer from a substring_t
171 * @s: substring_t to be scanned 218 * @s: substring_t to be scanned
172 * @result: resulting integer on success 219 * @result: resulting integer on success