hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/firmware/efi/libstub/string.c
....@@ -6,6 +6,8 @@
66 * Copyright (C) 1991, 1992 Linus Torvalds
77 */
88
9
+#include <linux/ctype.h>
10
+#include <linux/kernel.h>
911 #include <linux/types.h>
1012 #include <linux/string.h>
1113
....@@ -56,3 +58,58 @@
5658 return 0;
5759 }
5860 #endif
61
+
62
+/* Works only for digits and letters, but small and fast */
63
+#define TOLOWER(x) ((x) | 0x20)
64
+
65
+static unsigned int simple_guess_base(const char *cp)
66
+{
67
+ if (cp[0] == '0') {
68
+ if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
69
+ return 16;
70
+ else
71
+ return 8;
72
+ } else {
73
+ return 10;
74
+ }
75
+}
76
+
77
+/**
78
+ * simple_strtoull - convert a string to an unsigned long long
79
+ * @cp: The start of the string
80
+ * @endp: A pointer to the end of the parsed string will be placed here
81
+ * @base: The number base to use
82
+ */
83
+
84
+unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
85
+{
86
+ unsigned long long result = 0;
87
+
88
+ if (!base)
89
+ base = simple_guess_base(cp);
90
+
91
+ if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
92
+ cp += 2;
93
+
94
+ while (isxdigit(*cp)) {
95
+ unsigned int value;
96
+
97
+ value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
98
+ if (value >= base)
99
+ break;
100
+ result = result * base + value;
101
+ cp++;
102
+ }
103
+ if (endp)
104
+ *endp = (char *)cp;
105
+
106
+ return result;
107
+}
108
+
109
+long simple_strtol(const char *cp, char **endp, unsigned int base)
110
+{
111
+ if (*cp == '-')
112
+ return -simple_strtoull(cp + 1, endp, base);
113
+
114
+ return simple_strtoull(cp, endp, base);
115
+}