^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- linux-c -*- ------------------------------------------------------- *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2007 rPath, Inc. - All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * ----------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Very basic string functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ctype.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define KSTRTOX_OVERFLOW (1U << 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Undef these macros so that the functions that we provide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * here will have the correct names regardless of how string.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * may have chosen to #define them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #undef memcpy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #undef memset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #undef memcmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int memcmp(const void *s1, const void *s2, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bool diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) asm("repe; cmpsb" CC_SET(nz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Clang may lower `memcmp == 0` to `bcmp == 0`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int bcmp(const void *s1, const void *s2, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return memcmp(s1, s2, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int strcmp(const char *str1, const char *str2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const unsigned char *s1 = (const unsigned char *)str1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const unsigned char *s2 = (const unsigned char *)str2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) while (*s1 || *s2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) delta = *s1 - *s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) s2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int strncmp(const char *cs, const char *ct, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned char c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) c1 = *cs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) c2 = *ct++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (c1 != c2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return c1 < c2 ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!c1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) size_t strnlen(const char *s, size_t maxlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) const char *es = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) while (*es && maxlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) es++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) maxlen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return (es - s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned int atou(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) while (isdigit(*s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) i = i * 10 + (*s++ - '0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Works only for digits and letters, but small and fast */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define TOLOWER(x) ((x) | 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static unsigned int simple_guess_base(const char *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (cp[0] == '0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * simple_strtoull - convert a string to an unsigned long long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @cp: The start of the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @endp: A pointer to the end of the parsed string will be placed here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @base: The number base to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long long result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) base = simple_guess_base(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) while (isxdigit(*cp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (value >= base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) result = result * base + value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (endp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *endp = (char *)cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) long simple_strtol(const char *cp, char **endp, unsigned int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (*cp == '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -simple_strtoull(cp + 1, endp, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return simple_strtoull(cp, endp, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * strlen - Find the length of a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @s: The string to be sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) size_t strlen(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) const char *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) for (sc = s; *sc != '\0'; ++sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return sc - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * strstr - Find the first substring in a %NUL terminated string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * @s1: The string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * @s2: The string to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) char *strstr(const char *s1, const char *s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) size_t l1, l2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) l2 = strlen(s2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!l2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) l1 = strlen(s1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) while (l1 >= l2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) l1--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!memcmp(s1, s2, l2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return (char *)s1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) s1++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * strchr - Find the first occurrence of the character c in the string s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @s: the string to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * @c: the character to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) char *strchr(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) while (*s != (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (*s++ == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) u64 v64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) u32 v32[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) } d = { dividend };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) u32 upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) upper = d.v32[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) d.v32[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (upper >= divisor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) d.v32[1] = upper / divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) upper %= divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) "rm" (divisor), "0" (d.v32[0]), "1" (upper));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return d.v64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static inline u64 __div_u64(u64 dividend, u32 divisor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) u32 remainder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return __div_u64_rem(dividend, divisor, &remainder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static inline char _tolower(const char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return c | 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (*base == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (s[0] == '0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) *base = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *base = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *base = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) s += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Convert non-negative integer string representation in explicitly given radix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * to an integer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * Return number of characters consumed maybe or-ed with overflow bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * If overflow occurs, result integer (incorrect) is still returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * Don't you dare use this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static unsigned int _parse_integer(const char *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) unsigned int base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) unsigned long long *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned long long res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) unsigned int c = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) unsigned int lc = c | 0x20; /* don't tolower() this line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if ('0' <= c && c <= '9')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) val = c - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) else if ('a' <= lc && lc <= 'f')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) val = lc - 'a' + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (val >= base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Check for overflow only if we are within range of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * it in the max base we support (16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (unlikely(res & (~0ull << 60))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (res > __div_u64(ULLONG_MAX - val, base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) rv |= KSTRTOX_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) res = res * base + val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) rv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *p = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) unsigned long long _res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) s = _parse_integer_fixup_radix(s, &base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) rv = _parse_integer(s, base, &_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (rv & KSTRTOX_OVERFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (rv == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) s += rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (*s == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *res = _res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * kstrtoull - convert a string to an unsigned long long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * may also be a plus sign, but not a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * Used as a replacement for the obsolete simple_strtoull. Return code must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int kstrtoull(const char *s, unsigned int base, unsigned long long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (s[0] == '+')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return _kstrtoull(s, base, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int _kstrtoul(const char *s, unsigned int base, unsigned long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) unsigned long long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) rv = kstrtoull(s, base, &tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (rv < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (tmp != (unsigned long)tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *res = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * kstrtoul - convert a string to an unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @s: The start of the string. The string must be null-terminated, and may also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * include a single newline before its terminating null. The first character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * may also be a plus sign, but not a minus sign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * @base: The number base to use. The maximum supported base is 16. If base is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * given as 0, then the base of the string is automatically detected with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * conventional semantics - If it begins with 0x the number will be parsed as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * parsed as an octal number. Otherwise it will be parsed as a decimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * @res: Where to write the result of the conversion on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * Used as a replacement for the simple_strtoull.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int boot_kstrtoul(const char *s, unsigned int base, unsigned long *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * We want to shortcut function call, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (sizeof(unsigned long) == sizeof(unsigned long long) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) __alignof__(unsigned long) == __alignof__(unsigned long long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return kstrtoull(s, base, (unsigned long long *)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return _kstrtoul(s, base, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }