string.c 8.5 KB

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