914 lines
21 KiB
Diff
914 lines
21 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Sun, 15 Nov 2026 22:39:52 +0000
|
|
Subject: [PATCH] refactor(lib): move the simple_strto family and vsscanf
|
|
unchanged from vsprintf.c into vsscanf.c, with the field-width reader both
|
|
share in a header, so a build that formats its output elsewhere can take
|
|
Linux's input parsing alone
|
|
|
|
---
|
|
lib/Makefile | 2 +-
|
|
lib/vsprintf.c | 408 +-----------------------------------------
|
|
lib/vsprintf_atoi.h | 23 +++
|
|
lib/vsscanf.c | 418 ++++++++++++++++++++++++++++++++++++++++++++
|
|
4 files changed, 443 insertions(+), 408 deletions(-)
|
|
create mode 100644 lib/vsprintf_atoi.h
|
|
create mode 100644 lib/vsscanf.c
|
|
|
|
diff --git a/lib/Makefile b/lib/Makefile
|
|
index 8366c14790a594b2337e91abaa0ba0007e745e51..a7e4d8074eae817cf183b247e6b3291cccdc7e07 100644
|
|
--- a/lib/Makefile
|
|
+++ b/lib/Makefile
|
|
@@ -34,7 +34,7 @@ KASAN_SANITIZE_string.o := n
|
|
CFLAGS_string.o += -fno-stack-protector
|
|
endif
|
|
|
|
-lib-y := ctype.o string.o vsprintf.o cmdline.o \
|
|
+lib-y := ctype.o string.o vsprintf.o vsscanf.o cmdline.o \
|
|
rbtree.o radix-tree.o timerqueue.o xarray.o \
|
|
maple_tree.o idr.o extable.o irq_regs.o argv_split.o \
|
|
flex_proportions.o ratelimit.o \
|
|
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
|
|
index 2bc6ef483576c84ef84eeb43f13fd4fa311f54a1..d211118282bc65b91263fce4722a45131c43fe20 100644
|
|
--- a/lib/vsprintf.c
|
|
+++ b/lib/vsprintf.c
|
|
@@ -56,6 +56,7 @@
|
|
|
|
#include <linux/string_helpers.h>
|
|
#include "kstrtox.h"
|
|
+#include "vsprintf_atoi.h"
|
|
|
|
/* Disable pointer hashing if requested */
|
|
bool no_hash_pointers __ro_after_init;
|
|
@@ -75,117 +76,6 @@ enum hash_pointers_policy {
|
|
};
|
|
static enum hash_pointers_policy hash_pointers_mode __initdata;
|
|
|
|
-noinline
|
|
-static unsigned long long simple_strntoull(const char *startp, char **endp, unsigned int base, size_t max_chars)
|
|
-{
|
|
- const char *cp;
|
|
- unsigned long long result = 0ULL;
|
|
- size_t prefix_chars;
|
|
- unsigned int rv;
|
|
-
|
|
- cp = _parse_integer_fixup_radix(startp, &base);
|
|
- prefix_chars = cp - startp;
|
|
- if (prefix_chars < max_chars) {
|
|
- rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
|
|
- /* FIXME */
|
|
- cp += (rv & ~KSTRTOX_OVERFLOW);
|
|
- } else {
|
|
- /* Field too short for prefix + digit, skip over without converting */
|
|
- cp = startp + max_chars;
|
|
- }
|
|
-
|
|
- if (endp)
|
|
- *endp = (char *)cp;
|
|
-
|
|
- return result;
|
|
-}
|
|
-
|
|
-/**
|
|
- * simple_strtoull - convert a string to an unsigned long long
|
|
- * @cp: The start of the string
|
|
- * @endp: A pointer to the end of the parsed string will be placed here
|
|
- * @base: The number base to use
|
|
- *
|
|
- * This function has caveats. Please use kstrtoull instead.
|
|
- */
|
|
-noinline
|
|
-unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
|
|
-{
|
|
- return simple_strntoull(cp, endp, base, INT_MAX);
|
|
-}
|
|
-EXPORT_SYMBOL(simple_strtoull);
|
|
-
|
|
-/**
|
|
- * simple_strtoul - convert a string to an unsigned long
|
|
- * @cp: The start of the string
|
|
- * @endp: A pointer to the end of the parsed string will be placed here
|
|
- * @base: The number base to use
|
|
- *
|
|
- * This function has caveats. Please use kstrtoul instead.
|
|
- */
|
|
-unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
|
|
-{
|
|
- return simple_strtoull(cp, endp, base);
|
|
-}
|
|
-EXPORT_SYMBOL(simple_strtoul);
|
|
-
|
|
-/**
|
|
- * simple_strtol - convert a string to a signed long
|
|
- * @cp: The start of the string
|
|
- * @endp: A pointer to the end of the parsed string will be placed here
|
|
- * @base: The number base to use
|
|
- *
|
|
- * This function has caveats. Please use kstrtol instead.
|
|
- */
|
|
-long simple_strtol(const char *cp, char **endp, unsigned int base)
|
|
-{
|
|
- if (*cp == '-')
|
|
- return -simple_strtoul(cp + 1, endp, base);
|
|
-
|
|
- return simple_strtoul(cp, endp, base);
|
|
-}
|
|
-EXPORT_SYMBOL(simple_strtol);
|
|
-
|
|
-noinline
|
|
-static long long simple_strntoll(const char *cp, char **endp, unsigned int base, size_t max_chars)
|
|
-{
|
|
- /*
|
|
- * simple_strntoull() safely handles receiving max_chars==0 in the
|
|
- * case cp[0] == '-' && max_chars == 1.
|
|
- * If max_chars == 0 we can drop through and pass it to simple_strntoull()
|
|
- * and the content of *cp is irrelevant.
|
|
- */
|
|
- if (*cp == '-' && max_chars > 0)
|
|
- return -simple_strntoull(cp + 1, endp, base, max_chars - 1);
|
|
-
|
|
- return simple_strntoull(cp, endp, base, max_chars);
|
|
-}
|
|
-
|
|
-/**
|
|
- * simple_strtoll - convert a string to a signed long long
|
|
- * @cp: The start of the string
|
|
- * @endp: A pointer to the end of the parsed string will be placed here
|
|
- * @base: The number base to use
|
|
- *
|
|
- * This function has caveats. Please use kstrtoll instead.
|
|
- */
|
|
-long long simple_strtoll(const char *cp, char **endp, unsigned int base)
|
|
-{
|
|
- return simple_strntoll(cp, endp, base, INT_MAX);
|
|
-}
|
|
-EXPORT_SYMBOL(simple_strtoll);
|
|
-
|
|
-static inline int skip_atoi(const char **s)
|
|
-{
|
|
- int i = 0;
|
|
-
|
|
- do {
|
|
- i = i*10 + *((*s)++) - '0';
|
|
- } while (isdigit(**s));
|
|
-
|
|
- return i;
|
|
-}
|
|
-
|
|
/*
|
|
* Decimal conversion is by far the most typical, and is used for
|
|
* /proc and /sys data. This directly impacts e.g. top performance
|
|
@@ -3448,299 +3338,3 @@ out:
|
|
EXPORT_SYMBOL_GPL(bstr_printf);
|
|
|
|
#endif /* CONFIG_BINARY_PRINTF */
|
|
-
|
|
-/**
|
|
- * vsscanf - Unformat a buffer into a list of arguments
|
|
- * @buf: input buffer
|
|
- * @fmt: format of buffer
|
|
- * @args: arguments
|
|
- */
|
|
-int vsscanf(const char *buf, const char *fmt, va_list args)
|
|
-{
|
|
- const char *str = buf;
|
|
- char *next;
|
|
- char digit;
|
|
- int num = 0;
|
|
- u8 qualifier;
|
|
- unsigned int base;
|
|
- union {
|
|
- long long s;
|
|
- unsigned long long u;
|
|
- } val;
|
|
- s16 field_width;
|
|
- bool is_sign;
|
|
-
|
|
- while (*fmt) {
|
|
- /* skip any white space in format */
|
|
- /* white space in format matches any amount of
|
|
- * white space, including none, in the input.
|
|
- */
|
|
- if (isspace(*fmt)) {
|
|
- fmt = skip_spaces(++fmt);
|
|
- str = skip_spaces(str);
|
|
- }
|
|
-
|
|
- /* anything that is not a conversion must match exactly */
|
|
- if (*fmt != '%' && *fmt) {
|
|
- if (*fmt++ != *str++)
|
|
- break;
|
|
- continue;
|
|
- }
|
|
-
|
|
- if (!*fmt)
|
|
- break;
|
|
- ++fmt;
|
|
-
|
|
- /* skip this conversion.
|
|
- * advance both strings to next white space
|
|
- */
|
|
- if (*fmt == '*') {
|
|
- if (!*str)
|
|
- break;
|
|
- while (!isspace(*fmt) && *fmt != '%' && *fmt) {
|
|
- /* '%*[' not yet supported, invalid format */
|
|
- if (*fmt == '[')
|
|
- return num;
|
|
- fmt++;
|
|
- }
|
|
- while (!isspace(*str) && *str)
|
|
- str++;
|
|
- continue;
|
|
- }
|
|
-
|
|
- /* get field width */
|
|
- field_width = -1;
|
|
- if (isdigit(*fmt)) {
|
|
- field_width = skip_atoi(&fmt);
|
|
- if (field_width <= 0)
|
|
- break;
|
|
- }
|
|
-
|
|
- /* get conversion qualifier */
|
|
- qualifier = -1;
|
|
- if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
|
|
- *fmt == 'z') {
|
|
- qualifier = *fmt++;
|
|
- if (unlikely(qualifier == *fmt)) {
|
|
- if (qualifier == 'h') {
|
|
- qualifier = 'H';
|
|
- fmt++;
|
|
- } else if (qualifier == 'l') {
|
|
- qualifier = 'L';
|
|
- fmt++;
|
|
- }
|
|
- }
|
|
- }
|
|
-
|
|
- if (!*fmt)
|
|
- break;
|
|
-
|
|
- if (*fmt == 'n') {
|
|
- /* return number of characters read so far */
|
|
- *va_arg(args, int *) = str - buf;
|
|
- ++fmt;
|
|
- continue;
|
|
- }
|
|
-
|
|
- if (!*str)
|
|
- break;
|
|
-
|
|
- base = 10;
|
|
- is_sign = false;
|
|
-
|
|
- switch (*fmt++) {
|
|
- case 'c':
|
|
- {
|
|
- char *s = (char *)va_arg(args, char*);
|
|
- if (field_width == -1)
|
|
- field_width = 1;
|
|
- do {
|
|
- *s++ = *str++;
|
|
- } while (--field_width > 0 && *str);
|
|
- num++;
|
|
- }
|
|
- continue;
|
|
- case 's':
|
|
- {
|
|
- char *s = (char *)va_arg(args, char *);
|
|
- if (field_width == -1)
|
|
- field_width = SHRT_MAX;
|
|
- /* first, skip leading white space in buffer */
|
|
- str = skip_spaces(str);
|
|
-
|
|
- /* now copy until next white space */
|
|
- while (*str && !isspace(*str) && field_width--)
|
|
- *s++ = *str++;
|
|
- *s = '\0';
|
|
- num++;
|
|
- }
|
|
- continue;
|
|
- /*
|
|
- * Warning: This implementation of the '[' conversion specifier
|
|
- * deviates from its glibc counterpart in the following ways:
|
|
- * (1) It does NOT support ranges i.e. '-' is NOT a special
|
|
- * character
|
|
- * (2) It cannot match the closing bracket ']' itself
|
|
- * (3) A field width is required
|
|
- * (4) '%*[' (discard matching input) is currently not supported
|
|
- *
|
|
- * Example usage:
|
|
- * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
|
|
- * buf1, buf2, buf3);
|
|
- * if (ret < 3)
|
|
- * // etc..
|
|
- */
|
|
- case '[':
|
|
- {
|
|
- char *s = (char *)va_arg(args, char *);
|
|
- DECLARE_BITMAP(set, 256) = {0};
|
|
- unsigned int len = 0;
|
|
- bool negate = (*fmt == '^');
|
|
-
|
|
- /* field width is required */
|
|
- if (field_width == -1)
|
|
- return num;
|
|
-
|
|
- if (negate)
|
|
- ++fmt;
|
|
-
|
|
- for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
|
|
- __set_bit((u8)*fmt, set);
|
|
-
|
|
- /* no ']' or no character set found */
|
|
- if (!*fmt || !len)
|
|
- return num;
|
|
- ++fmt;
|
|
-
|
|
- if (negate) {
|
|
- bitmap_complement(set, set, 256);
|
|
- /* exclude null '\0' byte */
|
|
- __clear_bit(0, set);
|
|
- }
|
|
-
|
|
- /* match must be non-empty */
|
|
- if (!test_bit((u8)*str, set))
|
|
- return num;
|
|
-
|
|
- while (test_bit((u8)*str, set) && field_width--)
|
|
- *s++ = *str++;
|
|
- *s = '\0';
|
|
- ++num;
|
|
- }
|
|
- continue;
|
|
- case 'o':
|
|
- base = 8;
|
|
- break;
|
|
- case 'x':
|
|
- case 'X':
|
|
- base = 16;
|
|
- break;
|
|
- case 'i':
|
|
- base = 0;
|
|
- fallthrough;
|
|
- case 'd':
|
|
- is_sign = true;
|
|
- fallthrough;
|
|
- case 'u':
|
|
- break;
|
|
- case '%':
|
|
- /* looking for '%' in str */
|
|
- if (*str++ != '%')
|
|
- return num;
|
|
- continue;
|
|
- default:
|
|
- /* invalid format; stop here */
|
|
- return num;
|
|
- }
|
|
-
|
|
- /* have some sort of integer conversion.
|
|
- * first, skip white space in buffer.
|
|
- */
|
|
- str = skip_spaces(str);
|
|
-
|
|
- digit = *str;
|
|
- if (is_sign && digit == '-') {
|
|
- if (field_width == 1)
|
|
- break;
|
|
-
|
|
- digit = *(str + 1);
|
|
- }
|
|
-
|
|
- if (!digit
|
|
- || (base == 16 && !isxdigit(digit))
|
|
- || (base == 10 && !isdigit(digit))
|
|
- || (base == 8 && !isodigit(digit))
|
|
- || (base == 0 && !isdigit(digit)))
|
|
- break;
|
|
-
|
|
- if (is_sign)
|
|
- val.s = simple_strntoll(str, &next, base,
|
|
- field_width >= 0 ? field_width : INT_MAX);
|
|
- else
|
|
- val.u = simple_strntoull(str, &next, base,
|
|
- field_width >= 0 ? field_width : INT_MAX);
|
|
-
|
|
- switch (qualifier) {
|
|
- case 'H': /* that's 'hh' in format */
|
|
- if (is_sign)
|
|
- *va_arg(args, signed char *) = val.s;
|
|
- else
|
|
- *va_arg(args, unsigned char *) = val.u;
|
|
- break;
|
|
- case 'h':
|
|
- if (is_sign)
|
|
- *va_arg(args, short *) = val.s;
|
|
- else
|
|
- *va_arg(args, unsigned short *) = val.u;
|
|
- break;
|
|
- case 'l':
|
|
- if (is_sign)
|
|
- *va_arg(args, long *) = val.s;
|
|
- else
|
|
- *va_arg(args, unsigned long *) = val.u;
|
|
- break;
|
|
- case 'L':
|
|
- if (is_sign)
|
|
- *va_arg(args, long long *) = val.s;
|
|
- else
|
|
- *va_arg(args, unsigned long long *) = val.u;
|
|
- break;
|
|
- case 'z':
|
|
- *va_arg(args, size_t *) = val.u;
|
|
- break;
|
|
- default:
|
|
- if (is_sign)
|
|
- *va_arg(args, int *) = val.s;
|
|
- else
|
|
- *va_arg(args, unsigned int *) = val.u;
|
|
- break;
|
|
- }
|
|
- num++;
|
|
-
|
|
- if (!next)
|
|
- break;
|
|
- str = next;
|
|
- }
|
|
-
|
|
- return num;
|
|
-}
|
|
-EXPORT_SYMBOL(vsscanf);
|
|
-
|
|
-/**
|
|
- * sscanf - Unformat a buffer into a list of arguments
|
|
- * @buf: input buffer
|
|
- * @fmt: formatting of buffer
|
|
- * @...: resulting arguments
|
|
- */
|
|
-int sscanf(const char *buf, const char *fmt, ...)
|
|
-{
|
|
- va_list args;
|
|
- int i;
|
|
-
|
|
- va_start(args, fmt);
|
|
- i = vsscanf(buf, fmt, args);
|
|
- va_end(args);
|
|
-
|
|
- return i;
|
|
-}
|
|
-EXPORT_SYMBOL(sscanf);
|
|
diff --git a/lib/vsprintf_atoi.h b/lib/vsprintf_atoi.h
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..6f37c33330ea389630983bf058a561baa2491ddc
|
|
--- /dev/null
|
|
+++ b/lib/vsprintf_atoi.h
|
|
@@ -0,0 +1,23 @@
|
|
+/* SPDX-License-Identifier: GPL-2.0-only */
|
|
+/*
|
|
+ * Copyright (C) 1991, 1992 Linus Torvalds
|
|
+ *
|
|
+ * The field-width reader vsprintf.c and vsscanf.c share.
|
|
+ */
|
|
+#ifndef _LIB_VSPRINTF_ATOI_H
|
|
+#define _LIB_VSPRINTF_ATOI_H
|
|
+
|
|
+#include <linux/ctype.h>
|
|
+
|
|
+static inline int skip_atoi(const char **s)
|
|
+{
|
|
+ int i = 0;
|
|
+
|
|
+ do {
|
|
+ i = i*10 + *((*s)++) - '0';
|
|
+ } while (isdigit(**s));
|
|
+
|
|
+ return i;
|
|
+}
|
|
+
|
|
+#endif
|
|
diff --git a/lib/vsscanf.c b/lib/vsscanf.c
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..b98e306ec4b0fd2f2a231fa16f18100c4ca16a4c
|
|
--- /dev/null
|
|
+++ b/lib/vsscanf.c
|
|
@@ -0,0 +1,418 @@
|
|
+// SPDX-License-Identifier: GPL-2.0-only
|
|
+/*
|
|
+ * Copyright (C) 1991, 1992 Linus Torvalds
|
|
+ *
|
|
+ * The simple_strto*() family and vsscanf(), moved unchanged from vsprintf.c
|
|
+ * so that a build that formats its output elsewhere can take Linux's input
|
|
+ * parsing alone.
|
|
+ */
|
|
+
|
|
+#include <linux/stdarg.h>
|
|
+#include <linux/bitmap.h>
|
|
+#include <linux/ctype.h>
|
|
+#include <linux/export.h>
|
|
+#include <linux/kernel.h>
|
|
+#include <linux/kstrtox.h>
|
|
+#include <linux/limits.h>
|
|
+#include <linux/sprintf.h>
|
|
+#include <linux/string.h>
|
|
+#include <linux/types.h>
|
|
+
|
|
+#include "kstrtox.h"
|
|
+#include "vsprintf_atoi.h"
|
|
+
|
|
+noinline
|
|
+static unsigned long long simple_strntoull(const char *startp, char **endp, unsigned int base, size_t max_chars)
|
|
+{
|
|
+ const char *cp;
|
|
+ unsigned long long result = 0ULL;
|
|
+ size_t prefix_chars;
|
|
+ unsigned int rv;
|
|
+
|
|
+ cp = _parse_integer_fixup_radix(startp, &base);
|
|
+ prefix_chars = cp - startp;
|
|
+ if (prefix_chars < max_chars) {
|
|
+ rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
|
|
+ /* FIXME */
|
|
+ cp += (rv & ~KSTRTOX_OVERFLOW);
|
|
+ } else {
|
|
+ /* Field too short for prefix + digit, skip over without converting */
|
|
+ cp = startp + max_chars;
|
|
+ }
|
|
+
|
|
+ if (endp)
|
|
+ *endp = (char *)cp;
|
|
+
|
|
+ return result;
|
|
+}
|
|
+
|
|
+/**
|
|
+ * simple_strtoull - convert a string to an unsigned long long
|
|
+ * @cp: The start of the string
|
|
+ * @endp: A pointer to the end of the parsed string will be placed here
|
|
+ * @base: The number base to use
|
|
+ *
|
|
+ * This function has caveats. Please use kstrtoull instead.
|
|
+ */
|
|
+noinline
|
|
+unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
|
|
+{
|
|
+ return simple_strntoull(cp, endp, base, INT_MAX);
|
|
+}
|
|
+EXPORT_SYMBOL(simple_strtoull);
|
|
+
|
|
+/**
|
|
+ * simple_strtoul - convert a string to an unsigned long
|
|
+ * @cp: The start of the string
|
|
+ * @endp: A pointer to the end of the parsed string will be placed here
|
|
+ * @base: The number base to use
|
|
+ *
|
|
+ * This function has caveats. Please use kstrtoul instead.
|
|
+ */
|
|
+unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
|
|
+{
|
|
+ return simple_strtoull(cp, endp, base);
|
|
+}
|
|
+EXPORT_SYMBOL(simple_strtoul);
|
|
+
|
|
+/**
|
|
+ * simple_strtol - convert a string to a signed long
|
|
+ * @cp: The start of the string
|
|
+ * @endp: A pointer to the end of the parsed string will be placed here
|
|
+ * @base: The number base to use
|
|
+ *
|
|
+ * This function has caveats. Please use kstrtol instead.
|
|
+ */
|
|
+long simple_strtol(const char *cp, char **endp, unsigned int base)
|
|
+{
|
|
+ if (*cp == '-')
|
|
+ return -simple_strtoul(cp + 1, endp, base);
|
|
+
|
|
+ return simple_strtoul(cp, endp, base);
|
|
+}
|
|
+EXPORT_SYMBOL(simple_strtol);
|
|
+
|
|
+noinline
|
|
+static long long simple_strntoll(const char *cp, char **endp, unsigned int base, size_t max_chars)
|
|
+{
|
|
+ /*
|
|
+ * simple_strntoull() safely handles receiving max_chars==0 in the
|
|
+ * case cp[0] == '-' && max_chars == 1.
|
|
+ * If max_chars == 0 we can drop through and pass it to simple_strntoull()
|
|
+ * and the content of *cp is irrelevant.
|
|
+ */
|
|
+ if (*cp == '-' && max_chars > 0)
|
|
+ return -simple_strntoull(cp + 1, endp, base, max_chars - 1);
|
|
+
|
|
+ return simple_strntoull(cp, endp, base, max_chars);
|
|
+}
|
|
+
|
|
+/**
|
|
+ * simple_strtoll - convert a string to a signed long long
|
|
+ * @cp: The start of the string
|
|
+ * @endp: A pointer to the end of the parsed string will be placed here
|
|
+ * @base: The number base to use
|
|
+ *
|
|
+ * This function has caveats. Please use kstrtoll instead.
|
|
+ */
|
|
+long long simple_strtoll(const char *cp, char **endp, unsigned int base)
|
|
+{
|
|
+ return simple_strntoll(cp, endp, base, INT_MAX);
|
|
+}
|
|
+EXPORT_SYMBOL(simple_strtoll);
|
|
+
|
|
+/**
|
|
+ * vsscanf - Unformat a buffer into a list of arguments
|
|
+ * @buf: input buffer
|
|
+ * @fmt: format of buffer
|
|
+ * @args: arguments
|
|
+ */
|
|
+int vsscanf(const char *buf, const char *fmt, va_list args)
|
|
+{
|
|
+ const char *str = buf;
|
|
+ char *next;
|
|
+ char digit;
|
|
+ int num = 0;
|
|
+ u8 qualifier;
|
|
+ unsigned int base;
|
|
+ union {
|
|
+ long long s;
|
|
+ unsigned long long u;
|
|
+ } val;
|
|
+ s16 field_width;
|
|
+ bool is_sign;
|
|
+
|
|
+ while (*fmt) {
|
|
+ /* skip any white space in format */
|
|
+ /* white space in format matches any amount of
|
|
+ * white space, including none, in the input.
|
|
+ */
|
|
+ if (isspace(*fmt)) {
|
|
+ fmt = skip_spaces(++fmt);
|
|
+ str = skip_spaces(str);
|
|
+ }
|
|
+
|
|
+ /* anything that is not a conversion must match exactly */
|
|
+ if (*fmt != '%' && *fmt) {
|
|
+ if (*fmt++ != *str++)
|
|
+ break;
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (!*fmt)
|
|
+ break;
|
|
+ ++fmt;
|
|
+
|
|
+ /* skip this conversion.
|
|
+ * advance both strings to next white space
|
|
+ */
|
|
+ if (*fmt == '*') {
|
|
+ if (!*str)
|
|
+ break;
|
|
+ while (!isspace(*fmt) && *fmt != '%' && *fmt) {
|
|
+ /* '%*[' not yet supported, invalid format */
|
|
+ if (*fmt == '[')
|
|
+ return num;
|
|
+ fmt++;
|
|
+ }
|
|
+ while (!isspace(*str) && *str)
|
|
+ str++;
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ /* get field width */
|
|
+ field_width = -1;
|
|
+ if (isdigit(*fmt)) {
|
|
+ field_width = skip_atoi(&fmt);
|
|
+ if (field_width <= 0)
|
|
+ break;
|
|
+ }
|
|
+
|
|
+ /* get conversion qualifier */
|
|
+ qualifier = -1;
|
|
+ if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
|
|
+ *fmt == 'z') {
|
|
+ qualifier = *fmt++;
|
|
+ if (unlikely(qualifier == *fmt)) {
|
|
+ if (qualifier == 'h') {
|
|
+ qualifier = 'H';
|
|
+ fmt++;
|
|
+ } else if (qualifier == 'l') {
|
|
+ qualifier = 'L';
|
|
+ fmt++;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (!*fmt)
|
|
+ break;
|
|
+
|
|
+ if (*fmt == 'n') {
|
|
+ /* return number of characters read so far */
|
|
+ *va_arg(args, int *) = str - buf;
|
|
+ ++fmt;
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (!*str)
|
|
+ break;
|
|
+
|
|
+ base = 10;
|
|
+ is_sign = false;
|
|
+
|
|
+ switch (*fmt++) {
|
|
+ case 'c':
|
|
+ {
|
|
+ char *s = (char *)va_arg(args, char*);
|
|
+ if (field_width == -1)
|
|
+ field_width = 1;
|
|
+ do {
|
|
+ *s++ = *str++;
|
|
+ } while (--field_width > 0 && *str);
|
|
+ num++;
|
|
+ }
|
|
+ continue;
|
|
+ case 's':
|
|
+ {
|
|
+ char *s = (char *)va_arg(args, char *);
|
|
+ if (field_width == -1)
|
|
+ field_width = SHRT_MAX;
|
|
+ /* first, skip leading white space in buffer */
|
|
+ str = skip_spaces(str);
|
|
+
|
|
+ /* now copy until next white space */
|
|
+ while (*str && !isspace(*str) && field_width--)
|
|
+ *s++ = *str++;
|
|
+ *s = '\0';
|
|
+ num++;
|
|
+ }
|
|
+ continue;
|
|
+ /*
|
|
+ * Warning: This implementation of the '[' conversion specifier
|
|
+ * deviates from its glibc counterpart in the following ways:
|
|
+ * (1) It does NOT support ranges i.e. '-' is NOT a special
|
|
+ * character
|
|
+ * (2) It cannot match the closing bracket ']' itself
|
|
+ * (3) A field width is required
|
|
+ * (4) '%*[' (discard matching input) is currently not supported
|
|
+ *
|
|
+ * Example usage:
|
|
+ * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
|
|
+ * buf1, buf2, buf3);
|
|
+ * if (ret < 3)
|
|
+ * // etc..
|
|
+ */
|
|
+ case '[':
|
|
+ {
|
|
+ char *s = (char *)va_arg(args, char *);
|
|
+ DECLARE_BITMAP(set, 256) = {0};
|
|
+ unsigned int len = 0;
|
|
+ bool negate = (*fmt == '^');
|
|
+
|
|
+ /* field width is required */
|
|
+ if (field_width == -1)
|
|
+ return num;
|
|
+
|
|
+ if (negate)
|
|
+ ++fmt;
|
|
+
|
|
+ for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
|
|
+ __set_bit((u8)*fmt, set);
|
|
+
|
|
+ /* no ']' or no character set found */
|
|
+ if (!*fmt || !len)
|
|
+ return num;
|
|
+ ++fmt;
|
|
+
|
|
+ if (negate) {
|
|
+ bitmap_complement(set, set, 256);
|
|
+ /* exclude null '\0' byte */
|
|
+ __clear_bit(0, set);
|
|
+ }
|
|
+
|
|
+ /* match must be non-empty */
|
|
+ if (!test_bit((u8)*str, set))
|
|
+ return num;
|
|
+
|
|
+ while (test_bit((u8)*str, set) && field_width--)
|
|
+ *s++ = *str++;
|
|
+ *s = '\0';
|
|
+ ++num;
|
|
+ }
|
|
+ continue;
|
|
+ case 'o':
|
|
+ base = 8;
|
|
+ break;
|
|
+ case 'x':
|
|
+ case 'X':
|
|
+ base = 16;
|
|
+ break;
|
|
+ case 'i':
|
|
+ base = 0;
|
|
+ fallthrough;
|
|
+ case 'd':
|
|
+ is_sign = true;
|
|
+ fallthrough;
|
|
+ case 'u':
|
|
+ break;
|
|
+ case '%':
|
|
+ /* looking for '%' in str */
|
|
+ if (*str++ != '%')
|
|
+ return num;
|
|
+ continue;
|
|
+ default:
|
|
+ /* invalid format; stop here */
|
|
+ return num;
|
|
+ }
|
|
+
|
|
+ /* have some sort of integer conversion.
|
|
+ * first, skip white space in buffer.
|
|
+ */
|
|
+ str = skip_spaces(str);
|
|
+
|
|
+ digit = *str;
|
|
+ if (is_sign && digit == '-') {
|
|
+ if (field_width == 1)
|
|
+ break;
|
|
+
|
|
+ digit = *(str + 1);
|
|
+ }
|
|
+
|
|
+ if (!digit
|
|
+ || (base == 16 && !isxdigit(digit))
|
|
+ || (base == 10 && !isdigit(digit))
|
|
+ || (base == 8 && !isodigit(digit))
|
|
+ || (base == 0 && !isdigit(digit)))
|
|
+ break;
|
|
+
|
|
+ if (is_sign)
|
|
+ val.s = simple_strntoll(str, &next, base,
|
|
+ field_width >= 0 ? field_width : INT_MAX);
|
|
+ else
|
|
+ val.u = simple_strntoull(str, &next, base,
|
|
+ field_width >= 0 ? field_width : INT_MAX);
|
|
+
|
|
+ switch (qualifier) {
|
|
+ case 'H': /* that's 'hh' in format */
|
|
+ if (is_sign)
|
|
+ *va_arg(args, signed char *) = val.s;
|
|
+ else
|
|
+ *va_arg(args, unsigned char *) = val.u;
|
|
+ break;
|
|
+ case 'h':
|
|
+ if (is_sign)
|
|
+ *va_arg(args, short *) = val.s;
|
|
+ else
|
|
+ *va_arg(args, unsigned short *) = val.u;
|
|
+ break;
|
|
+ case 'l':
|
|
+ if (is_sign)
|
|
+ *va_arg(args, long *) = val.s;
|
|
+ else
|
|
+ *va_arg(args, unsigned long *) = val.u;
|
|
+ break;
|
|
+ case 'L':
|
|
+ if (is_sign)
|
|
+ *va_arg(args, long long *) = val.s;
|
|
+ else
|
|
+ *va_arg(args, unsigned long long *) = val.u;
|
|
+ break;
|
|
+ case 'z':
|
|
+ *va_arg(args, size_t *) = val.u;
|
|
+ break;
|
|
+ default:
|
|
+ if (is_sign)
|
|
+ *va_arg(args, int *) = val.s;
|
|
+ else
|
|
+ *va_arg(args, unsigned int *) = val.u;
|
|
+ break;
|
|
+ }
|
|
+ num++;
|
|
+
|
|
+ if (!next)
|
|
+ break;
|
|
+ str = next;
|
|
+ }
|
|
+
|
|
+ return num;
|
|
+}
|
|
+EXPORT_SYMBOL(vsscanf);
|
|
+
|
|
+/**
|
|
+ * sscanf - Unformat a buffer into a list of arguments
|
|
+ * @buf: input buffer
|
|
+ * @fmt: formatting of buffer
|
|
+ * @...: resulting arguments
|
|
+ */
|
|
+int sscanf(const char *buf, const char *fmt, ...)
|
|
+{
|
|
+ va_list args;
|
|
+ int i;
|
|
+
|
|
+ va_start(args, fmt);
|
|
+ i = vsscanf(buf, fmt, args);
|
|
+ va_end(args);
|
|
+
|
|
+ return i;
|
|
+}
|
|
+EXPORT_SYMBOL(sscanf);
|